syncfiles/lib/endian_test.c

66 lines
1.1 KiB
C
Raw Normal View History

// Copyright 2022 Dietrich Epp.
// This file is part of SyncFiles. SyncFiles is licensed under the terms of the
// Mozilla Public License, version 2.0. See LICENSE.txt for details.
2022-03-29 19:34:27 +00:00
#include "lib/endian.h"
#include "lib/test.h"
2022-03-29 19:34:27 +00:00
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
static void EndianFail(const char *name, UInt32 got, UInt32 expect)
2022-03-29 19:34:27 +00:00
{
fprintf(stderr, "%s = 0x%" PRIx32 ", expect 0x%" PRIx32 "\n", name, got,
expect);
gFailCount++;
2022-03-29 19:34:27 +00:00
}
#define CHECK(fn, expect) \
be = fn(u.i); \
if (be != expect) { \
EndianFail(#fn, be, expect); \
2022-03-29 19:34:27 +00:00
}
static void Test16(void)
{
union {
UInt16 i;
UInt8 b[2];
} u;
UInt16 be;
u.b[0] = 0x12;
u.b[1] = 0x34;
CHECK(EndianU16_BtoN, 0x1234)
CHECK(EndianU16_NtoB, 0x1234)
}
static void Test32(void)
{
union {
UInt32 i;
2022-04-12 05:52:05 +00:00
UInt8 b[4];
2022-03-29 19:34:27 +00:00
} u;
UInt32 be;
u.b[0] = 0x12;
u.b[1] = 0x34;
u.b[2] = 0x56;
u.b[3] = 0x78;
CHECK(EndianU32_BtoN, 0x12345678)
CHECK(EndianU32_NtoB, 0x12345678)
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
Test16();
Test32();
return TestsDone();
2022-03-29 19:34:27 +00:00
}