| 1 |
|
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <lib.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <tests/test.h> |
| 8 |
|
| 9 |
struct hexstr_t { |
| 10 |
char *str; |
| 11 |
int *val; |
| 12 |
size_t res; |
| 13 |
} hexstr[] = { |
| 14 |
{.str = "A", .res = 0}, |
| 15 |
{.str = "AB", .val = (int[]){171}, .res = 1}, |
| 16 |
{.str = "277a", .val = (int[]){39, 122}, .res = 2}, |
| 17 |
{.str = "277ab", .val = (int[]){39, 122}, .res = 2}, |
| 18 |
{.str = "\n\rx1234567ijkl", .val = (int[]){18, 52, 86}, .res = 3}, |
| 19 |
{.str = "\nB*e/ef-", .val = (int[]){190, 239}, .res = 2}, |
| 20 |
}; |
| 21 |
|
| 22 |
static void test_hexstrtobin(void **state) |
| 23 |
{ |
| 24 |
uint8_t *buf; |
| 25 |
size_t res, len; |
| 26 |
|
| 27 |
for (int i = 0; i < ARRAY_SIZE(hexstr); i++) { |
| 28 |
len = strlen(hexstr[i].str) / 2 + 1; |
| 29 |
buf = malloc(len); |
| 30 |
res = hexstrtobin(hexstr[i].str, buf, len); |
| 31 |
|
| 32 |
assert_int_equal(hexstr[i].res, res); |
| 33 |
|
| 34 |
for (int j = 0; j < res; j++) |
| 35 |
assert_int_equal(hexstr[i].val[j], buf[j]); |
| 36 |
|
| 37 |
free(buf); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
int main(void) |
| 42 |
{ |
| 43 |
const struct CMUnitTest tests[] = { |
| 44 |
cmocka_unit_test(test_hexstrtobin), |
| 45 |
}; |
| 46 |
|
| 47 |
return cb_run_group_tests(tests, NULL, NULL); |
| 48 |
} |