Files
libucore/test/test_hex.c
T
2025-07-18 22:45:08 +02:00

286 lines
5.6 KiB
C

#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[sizeof binary * 2 + 1] = "";
uc_hex_encode(binary, sizeof binary, hex);
fail_if(strcmp(hex,"7F8081") != 0);
}
END_TEST
START_TEST (test_uc_hex_encode_3)
{
uint8_t binary[] = {0, 1, 9, 11};
char hex[sizeof binary * 2 + 1] = "";
uc_hex_encode(binary, sizeof binary, hex);
fail_if(strcmp(hex,"0001090B") != 0);
}
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);
}
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);
}
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[] = {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_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);
}
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);
}
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);
}
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);
*res = 0;
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);
fail_if(res != hex + 7);
}
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);
fail_if(res != hex);
}
END_TEST
START_TEST (test_uc_hex_decode_lowercase)
{
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);
}
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_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);
tcase_add_test(tc, test_uc_hex_decode_lowercase);
suite_add_tcase(s, tc);
return s;
}