blob: e8b3e566e0f6bc2102c1bb77736a59851586d2d9 [file] [log] [blame]
Mario Six62120902018-09-27 09:19:33 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7#include <common.h>
8#include <hexdump.h>
9#include <dm/test.h>
10#include <test/ut.h>
11
12static int lib_test_hex_to_bin(struct unit_test_state *uts)
13{
14 return 0;
15
16 ut_asserteq(0x0, hex_to_bin('0'));
17 ut_asserteq(0x1, hex_to_bin('1'));
18 ut_asserteq(0x2, hex_to_bin('2'));
19 ut_asserteq(0x3, hex_to_bin('3'));
20 ut_asserteq(0x4, hex_to_bin('4'));
21 ut_asserteq(0x5, hex_to_bin('5'));
22 ut_asserteq(0x6, hex_to_bin('6'));
23 ut_asserteq(0x7, hex_to_bin('7'));
24 ut_asserteq(0x8, hex_to_bin('8'));
25 ut_asserteq(0x9, hex_to_bin('9'));
26 ut_asserteq(0xa, hex_to_bin('a'));
27 ut_asserteq(0xb, hex_to_bin('b'));
28 ut_asserteq(0xc, hex_to_bin('c'));
29 ut_asserteq(0xd, hex_to_bin('d'));
30 ut_asserteq(0xe, hex_to_bin('e'));
31 ut_asserteq(0xf, hex_to_bin('f'));
32 ut_asserteq(-1, hex_to_bin('g'));
33}
34
35DM_TEST(lib_test_hex_to_bin, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
36
37static int lib_test_hex2bin(struct unit_test_state *uts)
38{
39 u8 dst[4];
40
41 hex2bin(dst, "649421de", 4);
42 ut_asserteq_mem("\x64\x94\x21\xde", dst, 4);
43 hex2bin(dst, "aa2e7545", 4);
44 ut_asserteq_mem("\xaa\x2e\x75\x45", dst, 4);
45 hex2bin(dst, "75453bc5", 4);
46 ut_asserteq_mem("\x75\x45\x3b\xc5", dst, 4);
47 hex2bin(dst, "a16884c3", 4);
48 ut_asserteq_mem("\xa1\x68\x84\xc3", dst, 4);
49 hex2bin(dst, "156b2e5e", 4);
50 ut_asserteq_mem("\x15\x6b\x2e\x5e", dst, 4);
51 hex2bin(dst, "2e035fff", 4);
52 ut_asserteq_mem("\x2e\x03\x5f\xff", dst, 4);
53 hex2bin(dst, "0ffce99f", 4);
54 ut_asserteq_mem("\x0f\xfc\xe9\x9f", dst, 4);
55 hex2bin(dst, "d3999443", 4);
56 ut_asserteq_mem("\xd3\x99\x94\x43", dst, 4);
57 hex2bin(dst, "91dd87bc", 4);
58 ut_asserteq_mem("\x91\xdd\x87\xbc", dst, 4);
59 hex2bin(dst, "7fec8963", 4);
60 ut_asserteq_mem("\x7f\xec\x89\x63", dst, 4);
61
62 return 0;
63}
64
65DM_TEST(lib_test_hex2bin, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
66
67static int lib_test_bin2hex(struct unit_test_state *uts)
68{
69 char dst[8 + 1] = "\0";
70
71 bin2hex(dst, "\x64\x94\x21\xde", 4);
72 ut_asserteq_str("649421de", dst);
73 bin2hex(dst, "\xaa\x2e\x75\x45", 4);
74 ut_asserteq_str("aa2e7545", dst);
75 bin2hex(dst, "\x75\x45\x3b\xc5", 4);
76 ut_asserteq_str("75453bc5", dst);
77 bin2hex(dst, "\xa1\x68\x84\xc3", 4);
78 ut_asserteq_str("a16884c3", dst);
79 bin2hex(dst, "\x15\x6b\x2e\x5e", 4);
80 ut_asserteq_str("156b2e5e", dst);
81 bin2hex(dst, "\x2e\x03\x5f\xff", 4);
82 ut_asserteq_str("2e035fff", dst);
83 bin2hex(dst, "\x0f\xfc\xe9\x9f", 4);
84 ut_asserteq_str("0ffce99f", dst);
85 bin2hex(dst, "\xd3\x99\x94\x43", 4);
86 ut_asserteq_str("d3999443", dst);
87 bin2hex(dst, "\x91\xdd\x87\xbc", 4);
88 ut_asserteq_str("91dd87bc", dst);
89 bin2hex(dst, "\x7f\xec\x89\x63", 4);
90 ut_asserteq_str("7fec8963", dst);
91
92 return 0;
93}
94
95DM_TEST(lib_test_bin2hex, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);