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