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