1 /*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 
3     This program is free software: you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation, either version 3 of the License, or
6     (at your option) any later version.
7 
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12 
13     You should have received a copy of the GNU General Public License
14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
15  */
16 
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <tap/basic.h>
21 
22 #include "libknot/endian.h"
23 
main(int argc,char * argv[])24 int main(int argc, char *argv[])
25 {
26 	plan(12);
27 
28 	typedef union {
29 		uint16_t value;
30 		uint8_t  array[2];
31 	} trafo16_t;
32 
33 	const uint16_t host16 = 0x0102;
34 	const trafo16_t be16 = { .array = { 0x01, 0x02 } };
35 	const trafo16_t le16 = { .array = { 0x02, 0x01 } };
36 	ok(htobe16(host16) == be16.value, "htobe16");
37 	ok(htole16(host16) == le16.value, "htole16");
38 	ok(be16toh(be16.value) == host16, "be16toh");
39 	ok(le16toh(le16.value) == host16, "le16toh");
40 
41 	typedef union {
42 		uint32_t value;
43 		uint8_t  array[4];
44 	} trafo32_t;
45 
46 	const uint32_t host32 = 0x01020304;
47 	const trafo32_t be32 = { .array = { 0x01, 0x02, 0x03, 0x04 } };
48 	const trafo32_t le32 = { .array = { 0x04, 0x03, 0x02, 0x01 } };
49 	ok(htobe32(host32) == be32.value, "htobe32");
50 	ok(htole32(host32) == le32.value, "htole32");
51 	ok(be32toh(be32.value) == host32, "be32toh");
52 	ok(le32toh(le32.value) == host32, "le32toh");
53 
54 	typedef union {
55 		uint64_t value;
56 		uint8_t  array[8];
57 	} trafo64_t;
58 
59 	const uint64_t host64 = 0x0102030405060708;
60 	const trafo64_t be64 = { .array = { 0x01, 0x02, 0x03, 0x04,
61 	                                    0x05, 0x06, 0x07, 0x08 } };
62 	const trafo64_t le64 = { .array = { 0x08, 0x07, 0x06, 0x05,
63 	                                    0x04, 0x03, 0x02, 0x01 } };
64 	ok(htobe64(host64) == be64.value, "htobe64");
65 	ok(htole64(host64) == le64.value, "htole64");
66 	ok(be64toh(be64.value) == host64, "be64toh");
67 	ok(le64toh(le64.value) == host64, "le64toh");
68 
69 	return 0;
70 }
71