Teach uc_hex_decode to skip spaces

This commit is contained in:
Nils O. Selåsdal
2013-12-06 23:10:32 +01:00
parent 42612803a1
commit 784dc1cecb
3 changed files with 64 additions and 5 deletions
+45 -2
View File
@@ -87,7 +87,7 @@ END_TEST
START_TEST (test_uc_hex_decode_empty_string)
char hex[] = "";
uint8_t binary[4] = {0x11, 0x11, 0x11};
uint8_t binary[] = {0x11, 0x11, 0x11};
uint8_t *res;
res = uc_hex_decode(hex, 2, binary);
@@ -95,7 +95,47 @@ START_TEST (test_uc_hex_decode_empty_string)
fail_if(binary[0] != 0x11);
fail_if(binary[1] != 0x11);
fail_if(binary[2] != 0x11);
fail_if(res != binary);
fail_if(res != binary, "length %zu\n", res - binary);
END_TEST
START_TEST (test_uc_hex_decode_spaces)
char hex[] = " 11 1213";
uint8_t binary[3];
uint8_t *res;
res = uc_hex_decode(hex, strlen(hex), binary);
fail_if(binary[0] != 0x11);
fail_if(binary[1] != 0x12);
fail_if(binary[2] != 0x13);
fail_if(res != binary+3, "length %zu\n", res - binary);
END_TEST
START_TEST (test_uc_hex_decode_spaces2)
char hex[] = "1112 13\r\n";
uint8_t binary[3];
uint8_t *res;
res = uc_hex_decode(hex, strlen(hex), binary);
fail_if(binary[0] != 0x11);
fail_if(binary[1] != 0x12);
fail_if(binary[2] != 0x13);
fail_if(res != binary+3, "length %zu\n", res - binary);
END_TEST
START_TEST (test_uc_hex_decode_spaces_empty)
char hex[] = " \r\n ";
uint8_t binary[] = {0x11,0x12};
uint8_t *res;
res = uc_hex_decode(hex, strlen(hex), binary);
fail_if(binary[0] != 0x11);
fail_if(binary[1] != 0x12);
fail_if(res != binary, "length %zu\n", res - binary);
END_TEST
@@ -194,6 +234,9 @@ Suite *hex_suite(void)
tcase_add_test(tc, test_uc_hex_decode_1);
tcase_add_test(tc, test_uc_hex_decode_2);
tcase_add_test(tc, test_uc_hex_decode_zero_length);
tcase_add_test(tc, test_uc_hex_decode_spaces);
tcase_add_test(tc, test_uc_hex_decode_spaces2);
tcase_add_test(tc, test_uc_hex_decode_spaces_empty);
tcase_add_test(tc, test_uc_hex_decode_odd_length);
tcase_add_test(tc, test_uc_hex_decode_invalid);
tcase_add_test(tc, test_uc_hex_decode_empty_string);