summaryrefslogtreecommitdiff
path: root/libmisc/tests/test_endian.c
blob: d0b547c483f5c7fa7abc6815d7a596f9fe4766b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* libmisc/tests/test_endian.c - Tests for <libmisc/endian.h>
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <string.h> /* for memcmp() */

#include <libmisc/endian.h>

#include "test.h"

int main() {
	uint8_t act[12] = {0};
	uint16be_encode(&act[0], UINT16_C(0x1234));
	uint32be_encode(&act[2], UINT32_C(0x56789ABC));
	uint16le_encode(&act[6], UINT16_C(0x1234));
	uint32le_encode(&act[8], UINT32_C(0x56789ABC));

	uint8_t exp[12] = { 0x12, 0x34,
	                    0x56, 0x78, 0x9A, 0xBC,
	                    0x34, 0x12,
	                    0xBC, 0x9A, 0x78, 0x56 };
	test_assert(memcmp(act, exp, 12) == 0);

	test_assert(uint16be_decode(&act[0]) == UINT16_C(0x1234));
	test_assert(uint32be_decode(&act[2]) == UINT32_C(0x56789ABC));
	test_assert(uint16le_decode(&act[6]) == UINT16_C(0x1234));
	test_assert(uint32le_decode(&act[8]) == UINT32_C(0x56789ABC));

	return 0;
}