libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/test/test_endian.c (71 lines, 2.0 KiB) -rw-r--r-- blame download

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
// test_endian.c
// Endian unit test for libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

#include "../endian.h"
#include "unit.h"

#include <stdint.h>
#include <stdio.h>

int main(void) {
	{
		ASSERT("G00T00 BSWAP16",
				BSWAP16(0xFF00u) == 0x00FFu);
		ASSERT("G00T02 BSWAP32",
				BSWAP32(0xFF00FF00u) == 0x00FF00FFu);
		ASSERT("G00T04 BSWAP64",
				BSWAP64(0xFF00FF00FF00FF00) == 0x00FF00FF00FF00FF);
	}
	
	{
		uint8_t a16[2] = { 0xFF, 0x00 };
		uint8_t a32[4] = { 0xFF, 0x00, 0x00, 0x00 };
		uint8_t a64[8] = { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		
		ASSERT("G01T00 LD16LE",
				LD16LE(a16) == 0x00FFu);
		ASSERT("G01T01 LD16BE",
				LD16BE(a16) == 0xFF00u);
		ASSERT("G01T02 LD32LE",
				LD32LE(a32) == 0x000000FFu);
		ASSERT("G01T03 LD32BE",
				LD32BE(a32) == 0xFF000000u);
		ASSERT("G01T04 LD64LE",
				LD64LE(a64) == 0x00000000000000FFu);
		ASSERT("G01T05 LD64BE",
				LD64BE(a64) == 0xFF00000000000000u);
	}
	
	{
		uint16_t a16 = 0x00FFu; uint32_t a32 = 0x000000FFu;
		uint64_t a64 = 0x00000000000000FFu;
		uint8_t b16[2]; uint8_t b32[4]; uint8_t b64[8];
		
		ST16LE(b16, a16); ASSERT("G02T00 ST16LE",
				b16[0] == 0xFFu && b16[1] == 0x00u);
		ST16BE(b16, a16); ASSERT("G02T01 ST16BE",
				b16[0] == 0x00u && b16[1] == 0xFFu);
		ST32LE(b32, a32); ASSERT("G02T02 ST32LE",
				b32[0] == 0xFFu && b32[1] == 0x00u &&
				b32[2] == 0x00u && b32[3] == 0x00u);
		ST32BE(b32, a32); ASSERT("G02T03 ST32BE",
				b32[0] == 0x00u && b32[1] == 0x00u &&
				b32[2] == 0x00u && b32[3] == 0xFFu);
		ST64LE(b64, a64); ASSERT("G02T04 ST64LE",
				b64[0] == 0xFFu && b64[1] == 0x00u &&
				b64[2] == 0x00u && b64[3] == 0x00u &&
				b64[4] == 0x00u && b64[5] == 0x00u &&
				b64[6] == 0x00u && b64[7] == 0x00u);
		ST64BE(b64, a64); ASSERT("G02T05 ST64BE",
				b64[0] == 0x00u && b64[1] == 0x00u &&
				b64[2] == 0x00u && b64[3] == 0x00u &&
				b64[4] == 0x00u && b64[5] == 0x00u &&
				b64[6] == 0x00u && b64[7] == 0xFFu);
	}
	
	printf("%d of %d tests passed\n", testspassed, testsrun);
	return testsfailed;
}