/* libmisc/tests/test_endian.c - Tests for <libmisc/endian.h>
 *
 * Copyright (C) 2024-2025  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[(2+4+8)*2] = {0};
	size_t pos = 0;
	pos += uint16be_encode(&act[pos], UINT16_C(0x1234));
	pos += uint32be_encode(&act[pos], UINT32_C(0x56789ABC));
	pos += uint64be_encode(&act[pos], UINT64_C(0xAC589A93278CB30A));
	pos += uint16le_encode(&act[pos], UINT16_C(0x1234));
	pos += uint32le_encode(&act[pos], UINT32_C(0x56789ABC));
	pos += uint64le_encode(&act[pos], UINT64_C(0xAC589A93278CB30A));

	test_assert(pos == sizeof(act));
	uint8_t exp[(2+4+8)*2] = { 0x12, 0x34,
	                           0x56, 0x78, 0x9A, 0xBC,
	                           0xAC, 0x58, 0x9A, 0x93, 0x27, 0x8C, 0xB3, 0x0A,
	                           0x34, 0x12,
	                           0xBC, 0x9A, 0x78, 0x56,
	                           0x0A, 0xB3, 0x8C, 0x27, 0x93, 0x9A, 0x58, 0xAC};
	test_assert(memcmp(act, exp, sizeof(act)) == 0);

	pos = 0;
	test_assert(uint16be_decode(&act[pos]) == UINT16_C(0x1234)); pos += 2;
	test_assert(uint32be_decode(&act[pos]) == UINT32_C(0x56789ABC)); pos += 4;
	test_assert(uint64be_decode(&act[pos]) == UINT64_C(0xAC589A93278CB30A)); pos += 8;
	test_assert(uint16le_decode(&act[pos]) == UINT16_C(0x1234)); pos += 2;
	test_assert(uint32le_decode(&act[pos]) == UINT32_C(0x56789ABC)); pos += 4;
	test_assert(uint64le_decode(&act[pos]) == UINT64_C(0xAC589A93278CB30A)); pos += 8;

	return 0;
}