platformTypesTest.cpp
Engine/source/platform/test/platformTypesTest.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2014 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifdef TORQUE_TESTS_ENABLED 25#include "testing/unitTesting.h" 26#include "platform/platform.h" 27#include "core/util/endian.h" 28 29TEST(PlatformTypes, Sizes) 30{ 31 // Run through all the types and ensure they're the right size. 32#define CheckType(typeName, expectedSize) \ 33 EXPECT_EQ( sizeof(typeName), expectedSize) \ 34 << "Wrong size for a " #typeName ", expected " #expectedSize; 35 36 // One byte types. 37 CheckType(bool, 1); 38 CheckType(U8, 1); 39 CheckType(S8, 1); 40 CheckType(UTF8, 1); 41 42 // Two byte types. 43 CheckType(U16, 2); 44 CheckType(S16, 2); 45 CheckType(UTF16, 2); 46 47 // Four byte types. 48 CheckType(U32, 4); 49 CheckType(S32, 4); 50 CheckType(F32, 4); 51 CheckType(UTF32, 4); 52 53 // Eight byte types. 54 CheckType(U64, 8); 55 CheckType(S64, 8); 56 CheckType(F64, 8); 57 58 // 16 byte (128bit) types will go here, when we get some. 59#undef CheckType 60}; 61 62TEST(PlatformTypes, EndianConversion) 63{ 64 // Convenient and non-palindrome byte patterns to test with. 65 const U16 U16Test = 0xA1B2; 66 const S16 S16Test = 0x52A1; 67 68 const U32 U32Test = 0xA1B2C3D4; 69 const S32 S32Test = 0xD4C3B2A1; 70 const F32 F32Test = 1234.5678f; 71 72 //const U64 U64Test = 0xA1B2C3D4E3F2E10A; 73 //const S64 S64Test = 0x1A2B3C4D3E2F1EA0; 74 const F64 F64Test = 12345678.9101112131415; 75 76 // Run through all the conversions - bump stuff from host to little or big 77 // endian and back again. 78#define CheckEndianRoundTrip(type, b_or_l) \ 79 EXPECT_EQ( type##Test, convert##b_or_l##EndianToHost(convertHostTo##b_or_l##Endian(type##Test))) \ 80 << "Failed to convert the " #type " test value to " #b_or_l " endian and back to host endian order."; 81 82#define CheckTypeBothWays(type) \ 83 CheckEndianRoundTrip(type, B); \ 84 CheckEndianRoundTrip(type, L); 85 86#define CheckIntsForBitSize(bits) \ 87 CheckTypeBothWays( U##bits ); \ 88 CheckTypeBothWays( S##bits ); 89 90 // Don't check 8-bit types - they aren't affected by endian issues. 91 92 // Check the >1 byte int types, though. 93 CheckIntsForBitSize(16); 94 CheckIntsForBitSize(32); 95 // CheckIntsForBitSize(64); // don't have convertHostToLEndian(const U64/S64) so this doesn't work 96 97 // And check the float types. 98 CheckTypeBothWays(F32); 99 CheckTypeBothWays(F64); 100 101 // We'd check 128bit types here, if we had any. 102 103#undef CheckIntsForBitSize 104#undef CheckTypeBothWays 105#undef CheckEndianRoundTrip 106}; 107 108TEST(PlatformTypes, EndianSwap) 109{ 110 U32 swap32 = 0xABCDEF12; 111 U16 swap16 = 0xABCD; 112 113 EXPECT_EQ(endianSwap(swap32), 0x12EFCDAB) 114 << "32 bit endianSwap should reverse byte order, but didn't."; 115 EXPECT_EQ(endianSwap(swap16), 0xCDAB) 116 << "16 bit endianSwap should reverse byte order, but didn't."; 117}; 118 119#endif 120