/* Test-SST_Endian.cpp Author: Patrick Baggett Created: 4/18/2012 Purpose: Tests libsst-os byte swapping functions License: This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more details. */ #include "ZUnitTest.hpp" #include static const char* testHostEndian(); static const char* testBswap16(); static const char* testBswap32(); static const char* testBswap64(); static const char* testHost2BE(); static const char* testHost2LE(); static const char* testBE2Host(); static const char* testLE2Host(); #if SST_COMPILER == SST_COMPILER_MSVC #pragma warning(push) #pragma warning(disable:4127) #endif //List of unit tests ZUnitTest SST_EndianUnitTests[] = { { "SST_Endian: GetHostEndianness()", testHostEndian }, { "SST_Endian: ByteSwap16()", testBswap16 }, { "SST_Endian: ByteSwap32()", testBswap32 }, { "SST_Endian: ByteSwap64()", testBswap64 }, { "SST_Endian: HostToBE[16/32/64]()", testHost2BE }, { "SST_Endian: HostToLE[16/32/64]()", testHost2LE }, { "SST_Endian: BEToHost[16/32/64]()", testBE2Host }, { "SST_Endian: LEToHost[16/32/64]()", testLE2Host } }; DECLARE_ZTESTBLOCK(SST_Endian); /*************************************************************************/ static const char* testHostEndian() { #if defined(SST_BYTEORDER_LITTLE_ENDIAN) const SST_ByteOrder compileTimeEndianness = SST_LITTLE_ENDIAN; #else const SST_ByteOrder compileTimeEndianness = SST_BIG_ENDIAN; #endif TASSERT(compileTimeEndianness == SST_OS_GetHostEndianness(), "Compile-time byte-order does not match runtime byte-order"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testBswap16() { const uint16_t v1 = 0xAABB; const uint16_t v2 = 0xBBAA; TASSERT(SST_OS_ByteSwap16(v1) == v2, "ByteSwap16() does not swap properly"); TASSERT(SST_OS_ByteSwap16(SST_OS_ByteSwap16(v1)) == v1, "ByteSwap16() 2x doesn't return same value"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testBswap32() { const uint32_t v1 = 0xAABBCCDD; const uint32_t v2 = 0xDDCCBBAA; TASSERT(SST_OS_ByteSwap32(v1) == v2, "ByteSwap32() does not swap properly"); TASSERT(SST_OS_ByteSwap32(SST_OS_ByteSwap32(v1)) == v1, "ByteSwap32() 2x doesn't return same value"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testBswap64() { const uint64_t v1 = 0xAABBCCDD00112233ull; const uint64_t v2 = 0x33221100DDCCBBAAull; TASSERT(SST_OS_ByteSwap64(v1) == v2, "ByteSwap64() does not swap properly"); TASSERT(SST_OS_ByteSwap64(SST_OS_ByteSwap64(v1)) == v1, "ByteSwap64() 2x doesn't return same value"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testHost2BE() { const uint16_t a1 = 0xAABB; const uint32_t b1 = 0xAABBCCDD; const uint64_t c1 = 0xAABBCCDD00112233ull; #if defined(SST_BYTEORDER_LITTLE_ENDIAN) const uint16_t a2 = 0xBBAA; const uint32_t b2 = 0xDDCCBBAA; const uint64_t c2 = 0x33221100DDCCBBAAull; #else const uint16_t a2 = a1; const uint32_t b2 = b1; const uint64_t c2 = c1; #endif TASSERT(SST_OS_HostToBE16(a1) == a2, "HostToBE16() incorrect"); TASSERT(SST_OS_HostToBE32(b1) == b2, "HostToBE32() incorrect"); TASSERT(SST_OS_HostToBE64(c1) == c2, "HostToBE64() incorrect"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testHost2LE() { const uint16_t a1 = 0xAABB; const uint32_t b1 = 0xAABBCCDD; const uint64_t c1 = 0xAABBCCDD00112233ull; #if defined(SST_BYTEORDER_LITTLE_ENDIAN) const uint16_t a2 = a1; const uint32_t b2 = b1; const uint64_t c2 = c1; #else const uint16_t a2 = 0xBBAA; const uint32_t b2 = 0xDDCCBBAA; const uint64_t c2 = 0x33221100DDCCBBAAull; #endif TASSERT(SST_OS_HostToLE16(a1) == a2, "HostToLE16() incorrect"); TASSERT(SST_OS_HostToLE32(b1) == b2, "HostToLE32() incorrect"); TASSERT(SST_OS_HostToLE64(c1) == c2, "HostToLE64() incorrect"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testBE2Host() { const uint16_t a1 = 0xAABB; const uint32_t b1 = 0xAABBCCDD; const uint64_t c1 = 0xAABBCCDD00112233ull; #if defined(SST_BYTEORDER_LITTLE_ENDIAN) const uint16_t a2 = 0xBBAA; const uint32_t b2 = 0xDDCCBBAA; const uint64_t c2 = 0x33221100DDCCBBAAull; #else const uint16_t a2 = a1; const uint32_t b2 = b1; const uint64_t c2 = c1; #endif TASSERT(SST_OS_BEToHost16(a1) == a2, "BEToHost16() incorrect"); TASSERT(SST_OS_BEToHost32(b1) == b2, "BEToHost32() incorrect"); TASSERT(SST_OS_BEToHost64(c1) == c2, "BEToHost64() incorrect"); return ZTEST_SUCCESS; } /*************************************************************************/ static const char* testLE2Host() { const uint16_t a1 = 0xAABB; const uint32_t b1 = 0xAABBCCDD; const uint64_t c1 = 0xAABBCCDD00112233ull; #if defined(SST_BYTEORDER_LITTLE_ENDIAN) const uint16_t a2 = a1; const uint32_t b2 = b1; const uint64_t c2 = c1; #else const uint16_t a2 = 0xBBAA; const uint32_t b2 = 0xDDCCBBAA; const uint64_t c2 = 0x33221100DDCCBBAAull; #endif TASSERT(SST_OS_LEToHost16(a1) == a2, "LEToHost16() incorrect"); TASSERT(SST_OS_LEToHost32(b1) == b2, "LEToHost32() incorrect"); TASSERT(SST_OS_LEToHost64(c1) == c2, "LEToHost64() incorrect"); return ZTEST_SUCCESS; } #if SST_COMPILER == SST_COMPILER_MSVC #pragma warning(pop) #endif