Initial import of libucore
This commit is contained in:
+193
@@ -0,0 +1,193 @@
|
||||
#include <check.h>
|
||||
#include <string.h>
|
||||
#include "ucore_string.h"
|
||||
|
||||
|
||||
START_TEST (test_uc_hex_encode_1)
|
||||
uint8_t binary[] = {0xff};
|
||||
char hex[3] = "";
|
||||
|
||||
uc_hex_encode(binary, 1, hex);
|
||||
|
||||
fail_if(strcmp(hex,"FF") != 0);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_2)
|
||||
uint8_t binary[] = {127, 128, 129};
|
||||
char hex[4] = "";
|
||||
|
||||
uc_hex_encode(binary, sizeof binary, hex);
|
||||
|
||||
fail_if(strcmp(hex,"7F8081") != 0, "was %s", hex);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_3)
|
||||
uint8_t binary[] = {0, 1, 9, 11};
|
||||
char hex[5] = "";
|
||||
|
||||
uc_hex_encode(binary, sizeof binary, hex);
|
||||
|
||||
fail_if(strcmp(hex,"0001090B") != 0, "was %s", hex);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_zero_len)
|
||||
uint8_t binary[1] = {0xff};
|
||||
char hex[3] = "aa";
|
||||
|
||||
uc_hex_encode(binary, 0, hex);
|
||||
|
||||
fail_if(hex[0] != 0);
|
||||
fail_if(hex[1] != 'a');
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_1)
|
||||
char hex[] = "1F";
|
||||
uint8_t binary[2] = {0x11,0x22};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 2, binary);
|
||||
|
||||
fail_if(binary[0] != 0x1f);
|
||||
fail_if(binary[1] != 0x22);
|
||||
fail_if(res != binary + 1, "binary %p, res %p", &binary[0], res);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_2)
|
||||
char hex[] = "00FF80";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 6, binary);
|
||||
|
||||
fail_if(binary[0] != 0x00);
|
||||
fail_if(binary[1] != 0xFF);
|
||||
fail_if(binary[2] != 0x80);
|
||||
fail_if(res != binary + 3, "binary %p, res %p", &binary[0], res);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_zero_length)
|
||||
char hex[] = "00FF80";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 0, binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(res != binary);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_empty_string)
|
||||
char hex[] = "";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 2, binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(res != binary);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_odd_length)
|
||||
char hex[] = "33FF80";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 3, binary);
|
||||
|
||||
fail_if(binary[0] != 0x33);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(res != &binary[1]);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_invalid)
|
||||
char hex[] = "TEST ";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 3, binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(res != NULL);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_delim_1)
|
||||
uint8_t binary[4] = {0xFF, 0x00, 0x7F, 0x0A};
|
||||
char hex[17] = "";
|
||||
char *res;
|
||||
|
||||
res = uc_hex_encode_delim(binary, sizeof binary, hex, sizeof hex, " ");
|
||||
|
||||
fail_if(strcmp(hex, "FF 00 7F 0A ") != 0, "hex was '%s'", hex);
|
||||
fail_if(res != hex + 12);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_delim_2)
|
||||
uint8_t binary[] = {0xFF, 0x00};
|
||||
char hex[8] = "";
|
||||
char *res;
|
||||
|
||||
res = uc_hex_encode_delim(binary, sizeof binary, hex, sizeof hex, " \n");
|
||||
|
||||
fail_if(strcmp(hex, "FF \n00 ") != 0, "hex was '%s'", hex);
|
||||
fail_if(res != hex + 8);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_delim_zero_len)
|
||||
uint8_t binary[] = {0xFF, 0x00};
|
||||
char hex[8] = "......";
|
||||
char *res;
|
||||
|
||||
res = uc_hex_encode_delim(binary, 0, hex, sizeof hex, " \n");
|
||||
|
||||
fail_if(strcmp(hex, "") != 0, "hex was '%s'", hex);
|
||||
fail_if(res != hex);
|
||||
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *hex_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("hex");
|
||||
TCase *tc = tcase_create("hex tests");
|
||||
tcase_add_test(tc, test_uc_hex_encode_1);
|
||||
tcase_add_test(tc, test_uc_hex_encode_2);
|
||||
tcase_add_test(tc, test_uc_hex_encode_3);
|
||||
tcase_add_test(tc, test_uc_hex_encode_zero_len);
|
||||
|
||||
tcase_add_test(tc, test_uc_hex_encode_delim_1);
|
||||
tcase_add_test(tc, test_uc_hex_encode_delim_2);
|
||||
tcase_add_test(tc, test_uc_hex_encode_delim_zero_len);
|
||||
|
||||
|
||||
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_odd_length);
|
||||
tcase_add_test(tc, test_uc_hex_decode_invalid);
|
||||
tcase_add_test(tc, test_uc_hex_decode_empty_string);
|
||||
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user