Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2018 |
| 4 | * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc |
| 5 | */ |
| 6 | |
Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 7 | #include <hexdump.h> |
Heinrich Schuchardt | 8334431 | 2019-02-11 18:29:24 +0100 | [diff] [blame] | 8 | #include <test/lib.h> |
| 9 | #include <test/test.h> |
Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 10 | #include <test/ut.h> |
| 11 | |
| 12 | static int lib_test_hex_to_bin(struct unit_test_state *uts) |
| 13 | { |
Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 14 | 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 Goldschmidt | b0b1768 | 2018-12-04 21:30:08 +0100 | [diff] [blame] | 31 | |
| 32 | return 0; |
Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Heinrich Schuchardt | 8334431 | 2019-02-11 18:29:24 +0100 | [diff] [blame] | 35 | LIB_TEST(lib_test_hex_to_bin, 0); |
Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 36 | |
| 37 | static 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 | |
Heinrich Schuchardt | 8334431 | 2019-02-11 18:29:24 +0100 | [diff] [blame] | 65 | LIB_TEST(lib_test_hex2bin, 0); |
Mario Six | 87be2fe | 2018-09-27 09:19:33 +0200 | [diff] [blame] | 66 | |
| 67 | static 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 | |
Heinrich Schuchardt | 8334431 | 2019-02-11 18:29:24 +0100 | [diff] [blame] | 95 | LIB_TEST(lib_test_bin2hex, 0); |