1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

tests/unittests: add fmt hex/bytes converter tests

This commit is contained in:
Alexandre Abadie 2018-01-10 10:17:27 +01:00
parent 2d02bc0394
commit 2b6e42672c

View File

@ -34,6 +34,53 @@ static void test_fmt_byte_hex(void)
TEST_ASSERT_EQUAL_STRING("FF", (char *) out);
}
static void test_fmt_bytes_hex(void)
{
char out[15] = "--------------";
uint8_t val[7] = { 0xAA, 9, 8, 7, 6, 0xA8, 0xEF};
uint8_t bytes = 0;
bytes = fmt_bytes_hex(out, val, 0);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(0, bytes);
TEST_ASSERT_EQUAL_STRING("", (char *) out);
bytes = fmt_bytes_hex(out, val, 1);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(2, bytes);
TEST_ASSERT_EQUAL_STRING("AA", (char *) out);
bytes = fmt_bytes_hex(out, val, 2);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(4, bytes);
TEST_ASSERT_EQUAL_STRING("AA09", (char *) out);
bytes = fmt_bytes_hex(out, val, 3);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(6, bytes);
TEST_ASSERT_EQUAL_STRING("AA0908", (char *) out);
bytes = fmt_bytes_hex(out, val, 4);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(8, bytes);
TEST_ASSERT_EQUAL_STRING("AA090807", (char *) out);
bytes = fmt_bytes_hex(out, val, 5);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(10, bytes);
TEST_ASSERT_EQUAL_STRING("AA09080706", (char *) out);
bytes = fmt_bytes_hex(out, val, 6);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(12, bytes);
TEST_ASSERT_EQUAL_STRING("AA09080706A8", (char *) out);
bytes = fmt_bytes_hex(out, val, 7);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(14, bytes);
TEST_ASSERT_EQUAL_STRING("AA09080706A8EF", (char *) out);
}
static void test_fmt_bytes_hex_reverse(void)
{
char out[9] = "--------";
@ -61,6 +108,53 @@ static void test_fmt_bytes_hex_reverse(void)
TEST_ASSERT_EQUAL_STRING("06070809", (char *) out);
}
static void test_fmt_hex_bytes(void)
{
uint8_t val = 0;
uint8_t bytes = fmt_hex_bytes(&val, "");
TEST_ASSERT_EQUAL_INT(0, bytes);
TEST_ASSERT_EQUAL_INT(0, val);
bytes = fmt_hex_bytes(&val, "A");
TEST_ASSERT_EQUAL_INT(0, val);
TEST_ASSERT_EQUAL_INT(0, bytes);
char hex2[3] = "00";
uint8_t val1[1] = { 0 };
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0, val1[0]);
memcpy(hex2, "AB", 2);
hex2[2] = '\0';
val1[0] = 0;
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0xAB, val1[0]);
memcpy(hex2, "CD", 2);
hex2[2] = '\0';
val1[0] = 0;
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0xCD, val1[0]);
memcpy(hex2, "EF", 2);
hex2[2] = '\0';
val1[0] = 0;
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0xEF, val1[0]);
char hex6[] = "0102aF";
uint8_t val3[3];
bytes = fmt_hex_bytes(val3, hex6);
TEST_ASSERT_EQUAL_INT(3, bytes);
TEST_ASSERT_EQUAL_INT(1, val3[0]);
TEST_ASSERT_EQUAL_INT(2, val3[1]);
TEST_ASSERT_EQUAL_INT(0xAF, val3[2]);
}
static void test_fmt_u32_hex(void)
{
char out[9] = "--------";
@ -376,7 +470,9 @@ Test *tests_fmt_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_fmt_byte_hex),
new_TestFixture(test_fmt_bytes_hex),
new_TestFixture(test_fmt_bytes_hex_reverse),
new_TestFixture(test_fmt_hex_bytes),
new_TestFixture(test_fmt_u32_hex),
new_TestFixture(test_fmt_u64_hex),
new_TestFixture(test_fmt_u32_dec),