File: hexstrtobin-test.c

package info (click to toggle)
coreboot 25.09%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 217,084 kB
  • sloc: ansic: 1,685,313; sh: 15,803; python: 11,200; perl: 10,186; asm: 8,519; makefile: 5,179; cpp: 4,724; pascal: 2,327; ada: 1,985; yacc: 1,264; lex: 731; sed: 75; ruby: 5; lisp: 5; awk: 4
file content (48 lines) | stat: -rw-r--r-- 1,046 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* SPDX-License-Identifier: GPL-2.0-only */

#include <stdlib.h>
#include <string.h>
#include <lib.h>
#include <stdint.h>
#include <tests/test.h>

struct hexstr_t {
	char *str;
	int *val;
	size_t res;
} hexstr[] = {
	{.str = "A", .res = 0},
	{.str = "AB", .val = (int[]){171}, .res = 1},
	{.str = "277a", .val = (int[]){39, 122}, .res = 2},
	{.str = "277ab", .val = (int[]){39, 122}, .res = 2},
	{.str = "\n\rx1234567ijkl", .val = (int[]){18, 52, 86}, .res = 3},
	{.str = "\nB*e/ef-", .val = (int[]){190, 239}, .res = 2},
};

static void test_hexstrtobin(void **state)
{
	uint8_t *buf;
	size_t res, len;

	for (int i = 0; i < ARRAY_SIZE(hexstr); i++) {
		len = strlen(hexstr[i].str) / 2 + 1;
		buf = malloc(len);
		res = hexstrtobin(hexstr[i].str, buf, len);

		assert_int_equal(hexstr[i].res, res);

		for (int j = 0; j < res; j++)
			assert_int_equal(hexstr[i].val[j], buf[j]);

		free(buf);
	}
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_hexstrtobin),
	};

	return cb_run_group_tests(tests, NULL, NULL);
}