From f2c2eaf36d55c984cccbb2cab350679a39981cf2 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 15 Jun 2025 15:55:15 +0200 Subject: [PATCH] replace endianness tests with more robust and architecture-agnostic tests for swapping byte order --- test/dat.h | 8 ++++++-- test/t_util.h | 50 +++++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/test/dat.h b/test/dat.h index d728094..29f2cea 100644 --- a/test/dat.h +++ b/test/dat.h @@ -1,5 +1,4 @@ #include -#include #include "../src/util/conf.h" #include "../src/util/vec/float3.h" @@ -10,7 +9,12 @@ testdat tests[] = { {"", test_float3_norm, &(float3){2.0F, 0.67F, 5.0F} }, {"", test_float3_norm, &(float3){0.2F, 0.4F, 0.1F} }, - {"", test_compat_endianess, (uint32_t[]){0x15F59267} }, + {"", test_bswap16, (uint16_t[]){0x11EE, 0xEE11} }, + {"", test_bswap32, (uint32_t[]){0x1142ADEE, 0xEEAD4211} }, + {"", test_bswap64, (uint64_t[]){0x114266897799ADEE, 0xEEAD997789664211} }, + {"", test_bswap16_impl, (uint16_t[]){0x11EE, 0xEE11} }, + {"", test_bswap32_impl, (uint32_t[]){0x1142ADEE, 0xEEAD4211} }, + {"", test_bswap64_impl, (uint64_t[]){0x114266897799ADEE, 0xEEAD997789664211} }, {"k=v", test_procbuf, &(struct test_procbuf){"key=val", "key", "val", 0} }, {"sometxt", test_procbuf, &(struct test_procbuf){"sometxt", "sometxt", "", CONF_ESYNTAX}}, {"comment", test_procbuf, &(struct test_procbuf){"# comment", "", "", CONF_ENODAT} }, diff --git a/test/t_util.h b/test/t_util.h index f3e7ba2..e63c123 100644 --- a/test/t_util.h +++ b/test/t_util.h @@ -3,7 +3,7 @@ #include #include "../src/util/colour32.h" -#include "../src/util/compat/endian.h" +#include "../src/util/compat/bswap.h" #include "../src/util/vec/float3.h" #include "test.h" @@ -23,24 +23,32 @@ int test_colour32_endianess(void* d) { return assert_true(c.ch.r == 0xFF); } -int test_compat_endianess(void* d) { - uint32_t x = *((uint32_t*)d + 0); - int res = 0; - -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - res |= assert_false(be32ton(x) == x); - res |= assert_false(ntobe32(x) == x); - res |= assert_true(be32ton(ntobe32(x)) == x); - res |= assert_true(le32ton(x) == x); - res |= assert_true(ntole32(x) == x); -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - res |= assert_false(le32ton(x) == x); - res |= assert_false(ntole32(x) == x); - res |= assert_true(le32ton(ntole32(x)) == x); - res |= assert_true(be32ton(x) == x); - res |= assert_true(ntobe32(x) == x); -#else - res |= assert_false("platform unsupported!!"); -#endif - return res; +int test_bswap16(void* d) { + uint16_t* arg = d; + return assert_true(bswap16(arg[0]) == arg[1]); +} + +int test_bswap32(void* d) { + uint32_t* arg = d; + return assert_true(bswap32(arg[0]) == arg[1]); +} + +int test_bswap64(void* d) { + uint64_t* arg = d; + return assert_true(bswap64(arg[0]) == arg[1]); +} + +int test_bswap16_impl(void* d) { + uint16_t* arg = d; + return assert_true(bswap16_impl(arg[0]) == arg[1]); +} + +int test_bswap32_impl(void* d) { + uint32_t* arg = d; + return assert_true(bswap32_impl(arg[0]) == arg[1]); +} + +int test_bswap64_impl(void* d) { + uint64_t* arg = d; + return assert_true(bswap64_impl(arg[0]) == arg[1]); }