diff options
Diffstat (limited to '')
-rw-r--r-- | src/Globals.h | 62 | ||||
-rw-r--r-- | tests/BlockTypeRegistry/BlockStateTest.cpp | 39 | ||||
-rw-r--r-- | tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp | 30 | ||||
-rw-r--r-- | tests/ByteBuffer/ByteBufferTest.cpp | 51 | ||||
-rw-r--r-- | tests/ByteBuffer/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/ChunkData/ArraytoCoord.cpp | 39 | ||||
-rw-r--r-- | tests/ChunkData/Coordinates.cpp | 111 | ||||
-rw-r--r-- | tests/ChunkData/Copies.cpp | 45 | ||||
-rw-r--r-- | tests/ChunkData/CopyBlocks.cpp | 16 | ||||
-rw-r--r-- | tests/CompositeChat/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/CompositeChat/CompositeChatTest.cpp | 86 | ||||
-rw-r--r-- | tests/FastRandom/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/FastRandom/FastRandomTest.cpp | 23 | ||||
-rw-r--r-- | tests/Generating/LoadablePieces.cpp | 5 | ||||
-rw-r--r-- | tests/HTTP/UrlClientTest.cpp | 30 | ||||
-rw-r--r-- | tests/SchematicFileSerializer/SchematicFileSerializerTest.cpp | 22 | ||||
-rw-r--r-- | tests/TestHelpers.h | 167 | ||||
-rw-r--r-- | tests/UUID/UUIDTest.cpp | 54 |
18 files changed, 413 insertions, 388 deletions
diff --git a/src/Globals.h b/src/Globals.h index e37f6dc60..890394e2e 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -296,61 +296,49 @@ template class SizeChecker<UInt8, 1>; /** Faster than (int)floorf((float)x / (float)div) */ #define FAST_FLOOR_DIV(x, div) (((x) - (((x) < 0) ? ((div) - 1) : 0)) / (div)) -// Own version of assert() that writes failed assertions to the log for review +// Own version of ASSERT() that plays nicely with the testing framework #ifdef TEST_GLOBALS class cAssertFailure { - }; - - #ifdef _WIN32 - #if (defined(_MSC_VER) && defined(_DEBUG)) - #define DBG_BREAK _CrtDbgBreak() - #else - #define DBG_BREAK - #endif - #define REPORT_ERROR(FMT, ...) \ - { \ - AString msg = Printf(FMT, __VA_ARGS__); \ - puts(msg.c_str()); \ - fflush(stdout); \ - OutputDebugStringA(msg.c_str()); \ - DBG_BREAK; \ + AString mExpression; + AString mFileName; + int mLineNumber; + + public: + cAssertFailure(const AString & aExpression, const AString & aFileName, int aLineNumber): + mExpression(aExpression), + mFileName(aFileName), + mLineNumber(aLineNumber) + { } - #else - #define REPORT_ERROR(FMT, ...) \ - { \ - AString msg = Printf(FMT, __VA_ARGS__); \ - puts(msg.c_str()); \ - fflush(stdout); \ - } - #endif + + const AString & expression() const { return mExpression; } + const AString & fileName() const { return mFileName; } + int lineNumber() const { return mLineNumber; } + }; #ifdef _DEBUG - #define ASSERT(x) do { if (!(x)) { throw cAssertFailure();} } while (0) - #define testassert(x) do { if (!(x)) { REPORT_ERROR("Test failure: %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } } while (0) - #define CheckAsserts(x) do { try {x} catch (cAssertFailure) { break; } REPORT_ERROR("Test failure: assert didn't fire for %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } while (0) + #define ASSERT(x) do { if (!(x)) { throw cAssertFailure(#x, __FILE__, __LINE__);} } while (0) #else #define ASSERT(...) - #define testassert(...) - #define CheckAsserts(...) LOG("Assert checking is disabled in Release-mode executables (file %s, line %d)", __FILE__, __LINE__) #endif -#else + // Pretty much the same as ASSERT() but stays in Release builds + #define VERIFY(x) (!!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), exit(1), 0)) + +#else // TEST_GLOBALS + #ifdef _DEBUG #define ASSERT(x) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), assert(0), 0)) #else #define ASSERT(x) #endif -#endif -// Pretty much the same as ASSERT() but stays in Release builds -#define VERIFY( x) ( !!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), exit(1), 0)) + // Pretty much the same as ASSERT() but stays in Release builds + #define VERIFY(x) (!!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), exit(1), 0)) -// Same as assert but in all Self test builds -#ifdef SELF_TEST - #define assert_test(x) ( !!(x) || (assert(!#x), exit(1), 0)) -#endif +#endif // else TEST_GLOBALS /** Use to mark code that should be impossible to reach. */ #define UNREACHABLE(x) do { FLOGERROR("Hit unreachable code: {0}, file {1}, line {2}", #x, __FILE__, __LINE__); PrintStackTrace(); std::terminate(); } while (false) diff --git a/tests/BlockTypeRegistry/BlockStateTest.cpp b/tests/BlockTypeRegistry/BlockStateTest.cpp index d3ce92455..57317cbdf 100644 --- a/tests/BlockTypeRegistry/BlockStateTest.cpp +++ b/tests/BlockTypeRegistry/BlockStateTest.cpp @@ -109,37 +109,8 @@ static void testReplacing() -int main() -{ - LOGD("BlockStateTest started"); - - try - { - testStaticCreation(); - testDynamicCreation(); - testReplacing(); - } - catch (const TestException & exc) - { - LOGERROR("BlockStateTest has failed explicitly: %s", exc.mMessage.c_str()); - return 1; - } - catch (const std::runtime_error & exc) - { - LOGERROR("BlockStateTest has failed, an unhandled exception was thrown: %s", exc.what()); - return 1; - } - catch (...) - { - LOGERROR("BlockStateTest has failed, an unknown exception was thrown."); - return 1; - } - - LOGD("BlockStateTest finished"); - - return 0; -} - - - - +IMPLEMENT_TEST_MAIN("BlockStateTest", + testStaticCreation(); + testDynamicCreation(); + testReplacing(); +) diff --git a/tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp b/tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp index 3479e55d7..6616dfdc0 100644 --- a/tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp +++ b/tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp @@ -236,30 +236,6 @@ static void testBlockTypeRegistry() -int main() -{ - LOGD("BlockTypeRegistryTest started"); - - try - { - testBlockTypeRegistry(); - } - catch (const TestException & exc) - { - LOGERROR("BlockTypeRegistryTest has failed, an unhandled exception was thrown: %s", exc.mMessage.c_str()); - return 1; - } - catch (...) - { - LOGERROR("BlockTypeRegistryTest has failed, an unhandled exception was thrown."); - return 1; - } - - LOGD("BlockTypeRegistryTest finished"); - - return 0; -} - - - - +IMPLEMENT_TEST_MAIN("BlockTypeRegistryTest", + testBlockTypeRegistry(); +) diff --git a/tests/ByteBuffer/ByteBufferTest.cpp b/tests/ByteBuffer/ByteBufferTest.cpp index ddfe9e1d2..7278c5f0f 100644 --- a/tests/ByteBuffer/ByteBufferTest.cpp +++ b/tests/ByteBuffer/ByteBufferTest.cpp @@ -4,6 +4,7 @@ // Implements the main app entrypoint for the cByteBuffer class test #include "Globals.h" +#include "../TestHelpers.h" #include "ByteBuffer.h" @@ -15,11 +16,14 @@ static void TestRead(void) cByteBuffer buf(50); buf.Write("\x05\xac\x02\x00", 4); UInt32 v1; - assert_test(buf.ReadVarInt(v1) && (v1 == 5)); + TEST_TRUE(buf.ReadVarInt(v1)); + TEST_EQUAL(v1, 5); UInt32 v2; - assert_test(buf.ReadVarInt(v2) && (v2 == 300)); + TEST_TRUE(buf.ReadVarInt(v2)); + TEST_EQUAL(v2, 300); UInt32 v3; - assert_test(buf.ReadVarInt(v3) && (v3 == 0)); + TEST_TRUE(buf.ReadVarInt(v3)); + TEST_EQUAL(v3, 0); } @@ -34,8 +38,8 @@ static void TestWrite(void) buf.WriteVarInt32(0); AString All; buf.ReadAll(All); - assert_test(All.size() == 4); - assert_test(memcmp(All.data(), "\x05\xac\x02\x00", All.size()) == 0); + TEST_EQUAL(All.size(), 4); + TEST_EQUAL(memcmp(All.data(), "\x05\xac\x02\x00", All.size()), 0); } @@ -48,17 +52,17 @@ static void TestWrap(void) for (int i = 0; i < 1000; i++) { size_t FreeSpace = buf.GetFreeSpace(); - assert_test(buf.GetReadableSpace() == 0); - assert_test(FreeSpace > 0); - assert_test(buf.Write("a", 1)); - assert_test(buf.CanReadBytes(1)); - assert_test(buf.GetReadableSpace() == 1); + TEST_EQUAL(buf.GetReadableSpace(), 0); + TEST_GREATER_THAN_OR_EQUAL(FreeSpace, 1); + TEST_TRUE(buf.Write("a", 1)); + TEST_TRUE(buf.CanReadBytes(1)); + TEST_EQUAL(buf.GetReadableSpace(), 1); UInt8 v = 0; - assert_test(buf.ReadBEUInt8(v)); - assert_test(v == 'a'); - assert_test(buf.GetReadableSpace() == 0); + TEST_TRUE(buf.ReadBEUInt8(v)); + TEST_EQUAL(v, 'a'); + TEST_EQUAL(buf.GetReadableSpace(), 0); buf.CommitRead(); - assert_test(buf.GetFreeSpace() == FreeSpace); // We're back to normal + TEST_EQUAL(buf.GetFreeSpace(), FreeSpace); // We're back to normal } } @@ -66,23 +70,8 @@ static void TestWrap(void) -int main(int argc, char * argv[]) -{ - LOGD("Test started"); - - LOGD("Testing reads"); +IMPLEMENT_TEST_MAIN("ByteBuffer", TestRead(); - - LOGD("Testing writes"); TestWrite(); - - LOGD("Testing wraps"); TestWrap(); - - LOG("ByteBuffer test finished."); -} - - - - - +) diff --git a/tests/ByteBuffer/CMakeLists.txt b/tests/ByteBuffer/CMakeLists.txt index aa053e80f..989fabc23 100644 --- a/tests/ByteBuffer/CMakeLists.txt +++ b/tests/ByteBuffer/CMakeLists.txt @@ -8,14 +8,13 @@ add_definitions(-DTEST_GLOBALS=1) set (SHARED_SRCS ${CMAKE_SOURCE_DIR}/src/ByteBuffer.cpp - ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.cpp - ${CMAKE_SOURCE_DIR}/src/OSSupport/WinStackWalker.cpp + ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp ) set (SHARED_HDRS + ../TestHelpers.h ${CMAKE_SOURCE_DIR}/src/ByteBuffer.h - ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.h - ${CMAKE_SOURCE_DIR}/src/OSSupport/WinStackWalker.h + ${CMAKE_SOURCE_DIR}/src/StringUtils.h ) set (SRCS diff --git a/tests/ChunkData/ArraytoCoord.cpp b/tests/ChunkData/ArraytoCoord.cpp index 440730e46..e4ca9ce8d 100644 --- a/tests/ChunkData/ArraytoCoord.cpp +++ b/tests/ChunkData/ArraytoCoord.cpp @@ -1,12 +1,13 @@ #include "Globals.h" +#include "../TestHelpers.h" #include "ChunkData.h" -int main(int argc, char** argv) +/** Performs the entire ArrayToCoords test. */ +static void test() { - LOGD("Test started"); class cMockAllocationPool : public cAllocationPool<cChunkData::sChunkSection> @@ -35,23 +36,23 @@ int main(int argc, char** argv) memset(SrcBlockBuffer, 0x00, sizeof(SrcBlockBuffer)); SrcBlockBuffer[7 + (4 * 16) + (5 * 16 * 16)] = 0xcd; buffer.SetBlockTypes(SrcBlockBuffer); - testassert(buffer.GetBlock({ 7, 5, 4 }) == 0xcd); + TEST_EQUAL(buffer.GetBlock({ 7, 5, 4 }), 0xcd); NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); SrcNibbleBuffer[(6 + (1 * 16) + (2 * 16 * 16)) / 2] = 0xe; buffer.SetMetas(SrcNibbleBuffer); - testassert(buffer.GetMeta({ 6, 2, 1 }) == 0xe); + TEST_EQUAL(buffer.GetMeta({ 6, 2, 1 }), 0xe); memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); SrcNibbleBuffer[(6 + (1 * 16) + (2 * 16 * 16)) / 2] = 0xe; buffer.SetBlockLight(SrcNibbleBuffer); - testassert(buffer.GetBlockLight({ 6, 2, 1 }) == 0xe); + TEST_EQUAL(buffer.GetBlockLight({ 6, 2, 1 }), 0xe); memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); SrcNibbleBuffer[(6 + (1 * 16) + (2 * 16 * 16)) / 2] = 0xe; buffer.SetSkyLight(SrcNibbleBuffer); - testassert(buffer.GetSkyLight({ 6, 2, 1 }) == 0xe); + TEST_EQUAL(buffer.GetSkyLight({ 6, 2, 1 }), 0xe); } { @@ -62,23 +63,23 @@ int main(int argc, char** argv) memset(SrcBlockBuffer, 0x00, sizeof(SrcBlockBuffer)); SrcBlockBuffer[7 + (4 * 16) + (24 * 16 * 16)] = 0xcd; buffer.SetBlockTypes(SrcBlockBuffer); - testassert(buffer.GetBlock({ 7, 24, 4 }) == 0xcd); + TEST_EQUAL(buffer.GetBlock({ 7, 24, 4 }), 0xcd); NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); SrcNibbleBuffer[(6 + (1 * 16) + (24 * 16 * 16)) / 2] = 0xe; buffer.SetMetas(SrcNibbleBuffer); - testassert(buffer.GetMeta({ 6, 24, 1 }) == 0xe); + TEST_EQUAL(buffer.GetMeta({ 6, 24, 1 }), 0xe); memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); SrcNibbleBuffer[(6 + 1 * 16 + 24 * 16 * 16) / 2] = 0xe; buffer.SetBlockLight(SrcNibbleBuffer); - testassert(buffer.GetBlockLight({ 6, 24, 1 }) == 0xe); + TEST_EQUAL(buffer.GetBlockLight({ 6, 24, 1 }), 0xe); memset(SrcNibbleBuffer, 0xff, sizeof(SrcNibbleBuffer)); SrcNibbleBuffer[(6 + (1 * 16) + (24 * 16 * 16)) / 2] = 0xe; buffer.SetSkyLight(SrcNibbleBuffer); - testassert(buffer.GetSkyLight({ 6, 24, 1 }) == 0xe); + TEST_EQUAL(buffer.GetSkyLight({ 6, 24, 1 }), 0xe); } { @@ -88,23 +89,27 @@ int main(int argc, char** argv) BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; memset(SrcBlockBuffer, 0x00, sizeof(SrcBlockBuffer)); buffer.SetBlockTypes(SrcBlockBuffer); - testassert(buffer.GetBlock({ 7, 24, 4 }) == 0x00); + TEST_EQUAL(buffer.GetBlock({ 7, 24, 4 }), 0x00); NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); buffer.SetMetas(SrcNibbleBuffer); - testassert(buffer.GetMeta({ 6, 24, 1 }) == 0x0); + TEST_EQUAL(buffer.GetMeta({ 6, 24, 1 }), 0x0); memset(SrcNibbleBuffer, 0x00, sizeof(SrcNibbleBuffer)); buffer.SetBlockLight(SrcNibbleBuffer); - testassert(buffer.GetBlockLight({ 6, 24, 1 }) == 0x0); + TEST_EQUAL(buffer.GetBlockLight({ 6, 24, 1 }), 0x0); memset(SrcNibbleBuffer, 0xff, sizeof(SrcNibbleBuffer)); buffer.SetSkyLight(SrcNibbleBuffer); - testassert(buffer.GetSkyLight({ 6, 24, 1 }) == 0xf); + TEST_EQUAL(buffer.GetSkyLight({ 6, 24, 1 }), 0xf); } - - // All tests passed: - return 0; } + + + + +IMPLEMENT_TEST_MAIN("ChunkData ArrayToCoord", + test(); +) diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp index 304df191f..5070f2cda 100644 --- a/tests/ChunkData/Coordinates.cpp +++ b/tests/ChunkData/Coordinates.cpp @@ -1,13 +1,14 @@ #include "Globals.h" +#include "../TestHelpers.h" #include "ChunkData.h" -int main(int argc, char** argv) -{ - LOGD("Test started"); +/** Performs the entire cChunkData coordinates test. */ +static void test() +{ class cMockAllocationPool : public cAllocationPool<cChunkData::sChunkSection> { @@ -31,72 +32,72 @@ int main(int argc, char** argv) // Empty chunks buffer.SetBlock({ 0, 0, 0 }, 0xAB); - testassert(buffer.GetBlock({ 0, 0, 0 }) == 0xAB); + TEST_EQUAL(buffer.GetBlock({ 0, 0, 0 }), 0xAB); buffer.SetMeta({ 0, 16, 0 }, 0xC); - testassert(buffer.GetMeta({ 0, 16, 0 }) == 0xC); + TEST_EQUAL(buffer.GetMeta({ 0, 16, 0 }), 0xC); // loaded but not written segments - testassert(buffer.GetBlock({ 1, 0, 0 }) == 0x0); - testassert(buffer.GetMeta({ 1, 16, 0 }) == 0x0); + TEST_EQUAL(buffer.GetBlock({ 1, 0, 0 }), 0x0); + TEST_EQUAL(buffer.GetMeta({ 1, 16, 0 }), 0x0); // Notloaded segments - testassert(buffer.GetBlock({ 0, 32, 0 }) == 0x0); - testassert(buffer.GetMeta({ 0, 48, 0 }) == 0x0); + TEST_EQUAL(buffer.GetBlock({ 0, 32, 0 }), 0x0); + TEST_EQUAL(buffer.GetMeta({ 0, 48, 0 }), 0x0); // Out of range SetBlock - CheckAsserts( - buffer.SetBlock({ -1, 0, 0 }, 0); + TEST_ASSERTS( + buffer.SetBlock({ -1, 0, 0 }, 0) ); - CheckAsserts( - buffer.SetBlock({ 0, -1, 0 }, 0); + TEST_ASSERTS( + buffer.SetBlock({ 0, -1, 0 }, 0) ); - CheckAsserts( - buffer.SetBlock({ 0, 0, -1 }, 0); + TEST_ASSERTS( + buffer.SetBlock({ 0, 0, -1 }, 0) ); - CheckAsserts( - buffer.SetBlock({ 256, 0, 0 }, 0); + TEST_ASSERTS( + buffer.SetBlock({ 256, 0, 0 }, 0) ); - CheckAsserts( - buffer.SetBlock({ 0, 256, 0 }, 0); + TEST_ASSERTS( + buffer.SetBlock({ 0, 256, 0 }, 0) ); - CheckAsserts( - buffer.SetBlock({ 0, 0, 256 }, 0); + TEST_ASSERTS( + buffer.SetBlock({ 0, 0, 256 }, 0) ); // Out of range SetMeta - CheckAsserts( - buffer.SetMeta({ -1, 0, 0 }, 0); + TEST_ASSERTS( + buffer.SetMeta({ -1, 0, 0 }, 0) ); - CheckAsserts( - buffer.SetMeta({ 0, -1, 0 }, 0); + TEST_ASSERTS( + buffer.SetMeta({ 0, -1, 0 }, 0) ); - CheckAsserts( - buffer.SetMeta({ 0, 0, -1 }, 0); + TEST_ASSERTS( + buffer.SetMeta({ 0, 0, -1 }, 0) ); - CheckAsserts( - buffer.SetMeta({ 256, 0, 0 }, 0); + TEST_ASSERTS( + buffer.SetMeta({ 256, 0, 0 }, 0) ); - CheckAsserts( - buffer.SetMeta({ 0, 256, 0 }, 0); + TEST_ASSERTS( + buffer.SetMeta({ 0, 256, 0 }, 0) ); - CheckAsserts( - buffer.SetMeta({ 0, 0, 256 }, 0); + TEST_ASSERTS( + buffer.SetMeta({ 0, 0, 256 }, 0) ); // Reading out of range blocks should return air - testassert(buffer.GetBlock({ -1, 0, 0 }) == 0); - testassert(buffer.GetBlock({ 0, -1, 0 }) == 0); - testassert(buffer.GetBlock({ 0, 0, -1 }) == 0); - testassert(buffer.GetBlock({ 256, 0, 0 }) == 0); - testassert(buffer.GetBlock({ 0, 256, 0 }) == 0); - testassert(buffer.GetBlock({ 0, 0, 256 }) == 0); + TEST_EQUAL(buffer.GetBlock({ -1, 0, 0 }), 0); + TEST_EQUAL(buffer.GetBlock({ 0, -1, 0 }), 0); + TEST_EQUAL(buffer.GetBlock({ 0, 0, -1 }), 0); + TEST_EQUAL(buffer.GetBlock({ 256, 0, 0 }), 0); + TEST_EQUAL(buffer.GetBlock({ 0, 256, 0 }), 0); + TEST_EQUAL(buffer.GetBlock({ 0, 0, 256 }), 0); // Reading out of range metas should return 0 - testassert(buffer.GetMeta({ -1, 0, 0 }) == 0); - testassert(buffer.GetMeta({ 0, -1, 0 }) == 0); - testassert(buffer.GetMeta({ 0, 0, -1 }) == 0); - testassert(buffer.GetMeta({ 256, 0, 0 }) == 0); - testassert(buffer.GetMeta({ 0, 256, 0 }) == 0); - testassert(buffer.GetMeta({ 0, 0, 256 }) == 0); + TEST_EQUAL(buffer.GetMeta({ -1, 0, 0 }), 0); + TEST_EQUAL(buffer.GetMeta({ 0, -1, 0 }), 0); + TEST_EQUAL(buffer.GetMeta({ 0, 0, -1 }), 0); + TEST_EQUAL(buffer.GetMeta({ 256, 0, 0 }), 0); + TEST_EQUAL(buffer.GetMeta({ 0, 256, 0 }), 0); + TEST_EQUAL(buffer.GetMeta({ 0, 0, 256 }), 0); } { @@ -105,13 +106,13 @@ int main(int argc, char** argv) // Zero's buffer.SetBlock({ 0, 0, 0 }, 0x0); buffer.SetBlock({ 0, 0, 1 }, 0xab); - testassert(buffer.GetBlock({ 0, 0, 0 }) == 0x0); - testassert(buffer.GetBlock({ 0, 0, 1 }) == 0xab); + TEST_EQUAL(buffer.GetBlock({ 0, 0, 0 }), 0x0); + TEST_EQUAL(buffer.GetBlock({ 0, 0, 1 }), 0xab); buffer.SetMeta({ 0, 16, 0 }, 0x0); buffer.SetMeta({ 0, 16, 1 }, 0xc); - testassert(buffer.GetMeta({ 0, 16, 0 }) == 0x0); - testassert(buffer.GetMeta({ 0, 16, 1 }) == 0xc); + TEST_EQUAL(buffer.GetMeta({ 0, 16, 0 }), 0x0); + TEST_EQUAL(buffer.GetMeta({ 0, 16, 1 }), 0xc); } @@ -121,8 +122,14 @@ int main(int argc, char** argv) buffer.SetBlock({ 0, 0, 0 }, 0x42); cChunkData copy(Pool); copy = std::move(buffer); - testassert(copy.GetBlock({ 0, 0, 0 }) == 0x42); + TEST_EQUAL(copy.GetBlock({ 0, 0, 0 }), 0x42); } - - return 0; } + + + + + +IMPLEMENT_TEST_MAIN("ChunkData Coordinates", + test(); +) diff --git a/tests/ChunkData/Copies.cpp b/tests/ChunkData/Copies.cpp index 7fc027ece..6cbb58387 100644 --- a/tests/ChunkData/Copies.cpp +++ b/tests/ChunkData/Copies.cpp @@ -1,10 +1,14 @@ #include "Globals.h" +#include "../TestHelpers.h" #include "ChunkData.h" -int main(int argc, char** argv) + + +/** Performs the entire Copies test. */ +static void test() { LOGD("Test started"); @@ -34,8 +38,8 @@ int main(int argc, char** argv) cChunkData copy(Pool); copy.Assign(buffer); - testassert(copy.GetBlock({ 3, 1, 4 }) == 0xDE); - testassert(copy.GetMeta({ 3, 1, 4 }) == 0xA); + TEST_EQUAL(copy.GetBlock({ 3, 1, 4 }), 0xDE); + TEST_EQUAL(copy.GetMeta({ 3, 1, 4 }), 0xA); BLOCKTYPE SrcBlockBuffer[16 * 16 * 256]; for (int i = 0; i < 16 * 16 * 256; i += 4) @@ -49,12 +53,12 @@ int main(int argc, char** argv) buffer.SetBlockTypes(SrcBlockBuffer); BLOCKTYPE DstBlockBuffer[16 * 16 * 256]; buffer.CopyBlockTypes(DstBlockBuffer); - testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1) == 0); + TEST_EQUAL(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1), 0); memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); buffer.SetBlockTypes(SrcBlockBuffer); buffer.CopyBlockTypes(DstBlockBuffer); - testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1) == 0); + TEST_EQUAL(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1), 0); } { @@ -72,12 +76,12 @@ int main(int argc, char** argv) buffer.SetMetas(SrcNibbleBuffer); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256/ 2]; buffer.CopyMetas(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 /2); buffer.SetMetas(SrcNibbleBuffer); buffer.CopyMetas(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); } { @@ -95,12 +99,12 @@ int main(int argc, char** argv) buffer.SetBlockLight(SrcNibbleBuffer); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; buffer.CopyBlockLight(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1), 0); memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 /2); buffer.SetBlockLight(SrcNibbleBuffer); buffer.CopyBlockLight(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) - 1), 0); } { @@ -118,12 +122,12 @@ int main(int argc, char** argv) buffer.SetSkyLight(SrcNibbleBuffer); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256/ 2]; buffer.CopySkyLight(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0xFF, 16 * 16 * 256 / 2); buffer.SetSkyLight(SrcNibbleBuffer); buffer.CopySkyLight(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); } { @@ -133,23 +137,28 @@ int main(int argc, char** argv) memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); BLOCKTYPE DstBlockBuffer[16 * 16 * 256]; buffer.CopyBlockTypes(DstBlockBuffer); - testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1) == 0); + TEST_EQUAL(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) - 1), 0); NIBBLETYPE SrcNibbleBuffer[16 * 16 * 256 / 2]; memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 / 2); NIBBLETYPE DstNibbleBuffer[16 * 16 * 256 / 2]; buffer.CopyMetas(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0x00, 16 * 16 * 256 / 2); buffer.CopyBlockLight(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); memset(SrcNibbleBuffer, 0xFF, 16 * 16 * 256 / 2); buffer.CopySkyLight(DstNibbleBuffer); - testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1) == 0); + TEST_EQUAL(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 / 2) - 1), 0); } - - // All tests successful: - return 0; } + + + + + +IMPLEMENT_TEST_MAIN("ChunkData Copies", + test() +) diff --git a/tests/ChunkData/CopyBlocks.cpp b/tests/ChunkData/CopyBlocks.cpp index 623ed208b..ad1524fe5 100644 --- a/tests/ChunkData/CopyBlocks.cpp +++ b/tests/ChunkData/CopyBlocks.cpp @@ -8,16 +8,16 @@ #include "Globals.h" +#include "../TestHelpers.h" #include "ChunkData.h" -int main(int argc, char ** argv) +/** Performs the entire CopyBlocks test. */ +static void test() { - LOGD("Test started"); - // Set up a cChunkData with known contents - all blocks 0x01, all metas 0x02: class cMockAllocationPool : public cAllocationPool<cChunkData::sChunkSection> @@ -67,17 +67,17 @@ int main(int argc, char ** argv) // Verify the data copied: for (size_t i = 0; i < len; i++) { - assert_test(WritePosition[i] == 0x01); + TEST_EQUAL(WritePosition[i], 0x01); } // Verify the space before the copied data hasn't been changed: for (size_t i = 0; i < WritePosIdx; i++) { - assert_test(TestBuffer[i] == 0x03); + TEST_EQUAL(TestBuffer[i], 0x03); } // Verify the space after the copied data hasn't been changed: for (size_t i = WritePosIdx + idx + len; i < ARRAYCOUNT(TestBuffer); i++) { - assert_test(TestBuffer[i] == 0x03); + TEST_EQUAL(TestBuffer[i], 0x03); } // Re-initialize the buffer for the next test: @@ -87,10 +87,12 @@ int main(int argc, char ** argv) } } // for len } // for idx - return 0; } +IMPLEMENT_TEST_MAIN("ChunkData CopyBlocks", + test() +) diff --git a/tests/CompositeChat/CMakeLists.txt b/tests/CompositeChat/CMakeLists.txt index 52eeedbe9..03a100d91 100644 --- a/tests/CompositeChat/CMakeLists.txt +++ b/tests/CompositeChat/CMakeLists.txt @@ -10,14 +10,13 @@ add_definitions(-DTEST_GLOBALS=1) set (SHARED_SRCS ${CMAKE_SOURCE_DIR}/src/CompositeChat.cpp - ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.cpp - ${CMAKE_SOURCE_DIR}/src/OSSupport/WinStackWalker.cpp + ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp ) set (SHARED_HDRS + ../TestHelpers.h ${CMAKE_SOURCE_DIR}/src/CompositeChat.h - ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.h - ${CMAKE_SOURCE_DIR}/src/OSSupport/WinStackWalker.cpp + ${CMAKE_SOURCE_DIR}/src/StringUtils.h ) set (SRCS diff --git a/tests/CompositeChat/CompositeChatTest.cpp b/tests/CompositeChat/CompositeChatTest.cpp index 65d05b6f1..a6ac86921 100644 --- a/tests/CompositeChat/CompositeChatTest.cpp +++ b/tests/CompositeChat/CompositeChatTest.cpp @@ -4,6 +4,7 @@ // Implements the main app entrypoint for the cCompositeChat class test #include "Globals.h" +#include "../TestHelpers.h" #include "CompositeChat.h" @@ -15,15 +16,15 @@ static void TestParser1(void) cCompositeChat Msg; Msg.ParseText("Testing @2color codes and http://links parser"); const cCompositeChat::cParts & Parts = Msg.GetParts(); - assert_test(Parts.size() == 4); - assert_test(Parts[0]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[1]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[2]->m_PartType == cCompositeChat::ptUrl); - assert_test(Parts[3]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[0]->m_Style == ""); - assert_test(Parts[1]->m_Style == "@2"); - assert_test(Parts[2]->m_Style == "@2"); - assert_test(Parts[3]->m_Style == "@2"); + TEST_EQUAL(Parts.size(), 4); + TEST_EQUAL(Parts[0]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[1]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[2]->m_PartType, cCompositeChat::ptUrl); + TEST_EQUAL(Parts[3]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[0]->m_Style, ""); + TEST_EQUAL(Parts[1]->m_Style, "@2"); + TEST_EQUAL(Parts[2]->m_Style, "@2"); + TEST_EQUAL(Parts[3]->m_Style, "@2"); } @@ -35,15 +36,15 @@ static void TestParser2(void) cCompositeChat Msg; Msg.ParseText("@3Advanced stuff: @5overriding color codes and http://links.with/@4color-in-them handling"); const cCompositeChat::cParts & Parts = Msg.GetParts(); - assert_test(Parts.size() == 4); - assert_test(Parts[0]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[1]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[2]->m_PartType == cCompositeChat::ptUrl); - assert_test(Parts[3]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[0]->m_Style == "@3"); - assert_test(Parts[1]->m_Style == "@5"); - assert_test(Parts[2]->m_Style == "@5"); - assert_test(Parts[3]->m_Style == "@5"); + TEST_EQUAL(Parts.size(), 4); + TEST_EQUAL(Parts[0]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[1]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[2]->m_PartType, cCompositeChat::ptUrl); + TEST_EQUAL(Parts[3]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[0]->m_Style, "@3"); + TEST_EQUAL(Parts[1]->m_Style, "@5"); + TEST_EQUAL(Parts[2]->m_Style, "@5"); + TEST_EQUAL(Parts[3]->m_Style, "@5"); } @@ -55,11 +56,11 @@ static void TestParser3(void) cCompositeChat Msg; Msg.ParseText("http://links.starting the text"); const cCompositeChat::cParts & Parts = Msg.GetParts(); - assert_test(Parts.size() == 2); - assert_test(Parts[0]->m_PartType == cCompositeChat::ptUrl); - assert_test(Parts[1]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[0]->m_Style == ""); - assert_test(Parts[1]->m_Style == ""); + TEST_EQUAL(Parts.size(), 2); + TEST_EQUAL(Parts[0]->m_PartType, cCompositeChat::ptUrl); + TEST_EQUAL(Parts[1]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[0]->m_Style, ""); + TEST_EQUAL(Parts[1]->m_Style, ""); } @@ -71,11 +72,11 @@ static void TestParser4(void) cCompositeChat Msg; Msg.ParseText("links finishing the text: http://some.server"); const cCompositeChat::cParts & Parts = Msg.GetParts(); - assert_test(Parts.size() == 2); - assert_test(Parts[0]->m_PartType == cCompositeChat::ptText); - assert_test(Parts[1]->m_PartType == cCompositeChat::ptUrl); - assert_test(Parts[0]->m_Style == ""); - assert_test(Parts[1]->m_Style == ""); + TEST_EQUAL(Parts.size(), 2); + TEST_EQUAL(Parts[0]->m_PartType, cCompositeChat::ptText); + TEST_EQUAL(Parts[1]->m_PartType, cCompositeChat::ptUrl); + TEST_EQUAL(Parts[0]->m_Style, ""); + TEST_EQUAL(Parts[1]->m_Style, ""); } @@ -87,38 +88,19 @@ static void TestParser5(void) cCompositeChat Msg; Msg.ParseText("http://only.links"); const cCompositeChat::cParts & Parts = Msg.GetParts(); - assert_test(Parts.size() == 1); - assert_test(Parts[0]->m_PartType == cCompositeChat::ptUrl); - assert_test(Parts[0]->m_Style == ""); + TEST_EQUAL(Parts.size(), 1); + TEST_EQUAL(Parts[0]->m_PartType, cCompositeChat::ptUrl); + TEST_EQUAL(Parts[0]->m_Style, ""); } -int main(int argc, char * argv[]) -{ - LOGD("Test started."); - - LOGD("Running tests: 1"); +IMPLEMENT_TEST_MAIN("CompositeChat", TestParser1(); - - LOGD("Running tests: 2"); TestParser2(); - - LOGD("Running tests: 3"); TestParser3(); - - LOGD("Running tests: 4"); TestParser4(); - - LOGD("Running tests: 5"); TestParser5(); - - LOG("CompositeChat test finished."); -} - - - - - +) diff --git a/tests/FastRandom/CMakeLists.txt b/tests/FastRandom/CMakeLists.txt index bd51f90c8..3030b78e7 100644 --- a/tests/FastRandom/CMakeLists.txt +++ b/tests/FastRandom/CMakeLists.txt @@ -8,14 +8,13 @@ add_definitions(-DTEST_GLOBALS=1) set (SHARED_SRCS ${CMAKE_SOURCE_DIR}/src/FastRandom.cpp - ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.cpp - ${CMAKE_SOURCE_DIR}/src/OSSupport/WinStackWalker.cpp + ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp ) set (SHARED_HDRS + ../TestHelpers.h ${CMAKE_SOURCE_DIR}/src/FastRandom.h - ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.h - ${CMAKE_SOURCE_DIR}/src/OSSupport/WinStackWalker.h + ${CMAKE_SOURCE_DIR}/src/StringUtils.h ) set (SRCS diff --git a/tests/FastRandom/FastRandomTest.cpp b/tests/FastRandom/FastRandomTest.cpp index 2bfa7ced6..bb55d3349 100644 --- a/tests/FastRandom/FastRandomTest.cpp +++ b/tests/FastRandom/FastRandomTest.cpp @@ -4,6 +4,7 @@ // Tests the randomness of cFastRandom #include "Globals.h" +#include "../TestHelpers.h" #include "FastRandom.h" @@ -20,8 +21,8 @@ static void TestInts(void) for (int i = 0; i < ITER; i++) { int v = rnd.RandInt(1000); - assert_test(v >= 0); - assert_test(v <= 1000); + TEST_GREATER_THAN_OR_EQUAL(v, 0); + TEST_LESS_THAN_OR_EQUAL(v, 1000); Counts[v % BUCKETS]++; sum += v; } @@ -47,8 +48,8 @@ static void TestFloats(void) for (int i = 0; i < ITER; i++) { float v = rnd.RandReal(1000.0f); - assert_test(v >= 0); - assert_test(v <= 1000); + TEST_GREATER_THAN_OR_EQUAL(v, 0); + TEST_LESS_THAN_OR_EQUAL(v, 1000); Counts[static_cast<int>(v) % BUCKETS]++; sum += v; } @@ -100,18 +101,8 @@ static void TestReCreation(void) -int main(void) -{ - LOG("FastRandom Test started"); - - LOG("Testing ints"); +IMPLEMENT_TEST_MAIN("FastRandom", TestInts(); - - LOG("Testing floats"); TestFloats(); - - LOG("Testing re-creation"); TestReCreation(); - - LOG("FastRandom test finished"); -} +) diff --git a/tests/Generating/LoadablePieces.cpp b/tests/Generating/LoadablePieces.cpp index 58b65464d..26cec8627 100644 --- a/tests/Generating/LoadablePieces.cpp +++ b/tests/Generating/LoadablePieces.cpp @@ -11,6 +11,7 @@ #include <unistd.h> #define GetCurrentFolder getcwd #endif +#include "../TestHelpers.h" #include "Generating/PrefabPiecePool.h" @@ -29,8 +30,8 @@ static int DoLoaderTest(void) LOG("Loaded %u regular pieces and %u starting pieces", static_cast<unsigned>(test.GetAllPiecesCount()), static_cast<unsigned>(test.GetStartingPiecesCount())); // Check that we loaded all the pieces: - testassert(test.GetAllPiecesCount() == 1); - testassert(test.GetStartingPiecesCount() == 1); + TEST_EQUAL(test.GetAllPiecesCount(), 1); + TEST_EQUAL(test.GetStartingPiecesCount(), 1); return 0; } diff --git a/tests/HTTP/UrlClientTest.cpp b/tests/HTTP/UrlClientTest.cpp index e86ad9156..206165dee 100644 --- a/tests/HTTP/UrlClientTest.cpp +++ b/tests/HTTP/UrlClientTest.cpp @@ -1,5 +1,6 @@ #include "Globals.h" +#include "../TestHelpers.h" #include "HTTP/UrlClient.h" #include "OSSupport/NetworkSingleton.h" @@ -228,28 +229,15 @@ int TestRequests() -int main() -{ - LOGD("Test started"); - - LOGD("Initializing cNetwork..."); +IMPLEMENT_TEST_MAIN("UrlClient", + LOG("Initializing cNetwork..."); cNetworkSingleton::Get().Initialise(); - - LOGD("Testing..."); - auto res = TestRequests(); - - LOGD("Terminating cNetwork..."); + LOG("Testing..."); + TEST_EQUAL(TestRequests(), 0); + LOG("Terminating cNetwork..."); cNetworkSingleton::Get().Terminate(); // No leaked callback instances - LOGD("cCallback instances still alive: %d", g_ActiveCallbacks.load()); - assert_test(g_ActiveCallbacks == 0); - - LOGD("cUrlClient test finished"); - - return res; -} - - - - + LOG("cCallback instances still alive: %d", g_ActiveCallbacks.load()); + TEST_EQUAL(g_ActiveCallbacks, 0); +) diff --git a/tests/SchematicFileSerializer/SchematicFileSerializerTest.cpp b/tests/SchematicFileSerializer/SchematicFileSerializerTest.cpp index fc18daf93..f4db9852c 100644 --- a/tests/SchematicFileSerializer/SchematicFileSerializerTest.cpp +++ b/tests/SchematicFileSerializer/SchematicFileSerializerTest.cpp @@ -4,6 +4,7 @@ // Implements the SchematicFileSerializer test main entrypoint #include "Globals.h" +#include "../TestHelpers.h" #include "WorldStorage/SchematicFileSerializer.h" @@ -16,28 +17,15 @@ static void DoTest(void) ba.Create(21, 256, 21); ba.RelLine(0, 0, 0, 9, 8, 7, cBlockArea::baTypes | cBlockArea::baMetas, E_BLOCK_WOODEN_STAIRS, 1); AString Schematic; - if (!cSchematicFileSerializer::SaveToSchematicString(ba, Schematic)) - { - assert_test(!"Schematic failed to save!"); - } + TEST_TRUE(cSchematicFileSerializer::SaveToSchematicString(ba, Schematic)); cBlockArea ba2; - if (!cSchematicFileSerializer::LoadFromSchematicString(ba2, Schematic)) - { - assert_test(!"Schematic failed to load!"); - } + TEST_TRUE(cSchematicFileSerializer::LoadFromSchematicString(ba2, Schematic)); } -int main(int argc, char * argv[]) -{ +IMPLEMENT_TEST_MAIN("SchematicFileSerializer", DoTest(); - LOG("SchematicFileSerializer test done."); - return 0; -} - - - - +) diff --git a/tests/TestHelpers.h b/tests/TestHelpers.h index 2e43e1e0d..7a0725f66 100644 --- a/tests/TestHelpers.h +++ b/tests/TestHelpers.h @@ -1,5 +1,20 @@ // Helper macros for writing exception-based tests +/* +The tests are supposed to be contained in small static functions that get called from a main function provided by this framework: +static void test1() +{ + // Perform the test +} + +... + +IMPLEMENT_TEST_MAIN("TestName", + test1(); + ... +) +*/ + @@ -10,11 +25,17 @@ It bears a single message that is to be displayed to stderr. */ class TestException { public: - TestException(const AString & aMessage): + TestException(const AString & aFileName, int aLineNumber, const AString & aFunctionName, const AString & aMessage): + mFileName(aFileName), + mLineNumber(aLineNumber), + mFunctionName(aFunctionName), mMessage(aMessage) { } + AString mFileName; + int mLineNumber; + AString mFunctionName; AString mMessage; }; @@ -26,33 +47,87 @@ public: #define TEST_EQUAL(VAL1, VAL2) \ if (VAL1 != VAL2) \ { \ - throw TestException(Printf("%s (line %d): Equality test failed: %s != %s", \ - __FUNCTION__, __LINE__, \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Equality test failed: %s != %s", \ #VAL1, #VAL2 \ )); \ } + + +/** Checks that the two values are equal; if not, throws a TestException, includes the specified message. */ +#define TEST_EQUAL_MSG(VAL1, VAL2, MSG) \ + if (VAL1 != VAL2) \ + { \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Equality test failed: %s != %s (%s)", \ + #VAL1, #VAL2, MSG \ + )); \ + } + + + + + /** Checks that the two values are not equal; if they are, throws a TestException. */ #define TEST_NOTEQUAL(VAL1, VAL2) \ if (VAL1 == VAL2) \ { \ - throw TestException(Printf("%s (line %d): Inequality test failed: %s == %s", \ - __FUNCTION__, __LINE__, \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Inequality test failed: %s == %s", \ #VAL1, #VAL2 \ )); \ } + + +/** Checks that the statement evaluates to true. */ +#define TEST_TRUE(X) TEST_EQUAL(X, true) + + + + + +/** Checks that the statement evaluates to false. */ +#define TEST_FALSE(X) TEST_EQUAL(X, false) + + + + + +/** Checks that the statement returns a value greater than or equal to the specified value. */ +#define TEST_GREATER_THAN_OR_EQUAL(Stmt, Val) \ + do { \ + if (Stmt < Val) \ + { \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Comparison failed: %s < %s", #Stmt, #Val)); \ + } \ + } while (false) + + + + + +/** Checks that the statement returns a value less than or equal to the specified value. */ +#define TEST_LESS_THAN_OR_EQUAL(Stmt, Val) \ + do { \ + if (Stmt > Val) \ + { \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Comparison failed: %s > %s", #Stmt, #Val)); \ + } \ + } while (false) + + + + + /** Checks that the statement throws an exception of the specified class. */ #define TEST_THROWS(Stmt, ExcClass) \ try \ { \ Stmt; \ - throw TestException(Printf("%s (line %d): Failed to throw an exception of type %s", \ - __FUNCTION__, __LINE__, \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Failed to throw an exception of type %s", \ #ExcClass \ )); \ } \ @@ -62,18 +137,88 @@ public: } \ catch (const std::exception & exc) \ { \ - throw TestException(Printf("%s (line %d): An unexpected std::exception descendant was thrown, was expecting type %s. Message is: %s", \ - __FUNCTION__, __LINE__, \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("An unexpected std::exception descendant was thrown, was expecting type %s. Message is: %s", \ #ExcClass, exc.what() \ )); \ } \ catch (...) \ { \ - throw TestException(Printf("%s (line %d): An unexpected exception object was thrown, was expecting type %s", \ - __FUNCTION__, __LINE__, \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("An unexpected unknown exception object was thrown, was expecting type %s", \ #ExcClass \ )); \ } + + +/** Checks that the statement throws an exception of any type. */ +#define TEST_THROWS_ANY(Stmt) \ + try \ + { \ + Stmt; \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, "Failed to throw an exception of any type"); \ + } \ + catch (const TestException & exc) \ + { \ + throw exc; \ + } \ + catch (...) \ + { \ + /* This is the expected case */ \ + } + + + + + +/** Checks that the statement causes an ASSERT trigger. */ +#ifdef _DEBUG + #define TEST_ASSERTS(Stmt) TEST_THROWS(Stmt, cAssertFailure) +#else + #define TEST_ASSERTS(Stmt) LOG("Skipped, cannot test in Release mode: TEST_ASSERT(%s); (%s:%d)", #Stmt, __FILE__, __LINE__); +#endif // else _DEBUG + + + + + +/** Used to implement the main() function for tests. */ +#define IMPLEMENT_TEST_MAIN(TestName, TestCode) \ +int main() \ +{ \ + LOG("Test started: %s", TestName); \ + \ + try \ + { \ + TestCode; \ + } \ + catch (const TestException & exc) \ + { \ + LOGERROR("Test has failed at file %s, line %d, function %s: %s", \ + exc.mFileName.c_str(), \ + exc.mLineNumber, \ + exc.mFunctionName.c_str(), \ + exc.mMessage.c_str() \ + ); \ + return 1; \ + } \ + catch (const std::exception & exc) \ + { \ + LOGERROR("Test has failed, an exception was thrown: %s", exc.what()); \ + return 1; \ + } \ + catch (const cAssertFailure & exc) \ + { \ + LOGERROR("Test has failed, an unexpected ASSERT was triggered at %s:%d", exc.fileName().c_str(), exc.lineNumber()); \ + return 1; \ + } \ + catch (...) \ + { \ + LOGERROR("Test has failed, an unhandled exception was thrown."); \ + return 1; \ + } \ + \ + LOG("Test finished"); \ + return 0; \ +} diff --git a/tests/UUID/UUIDTest.cpp b/tests/UUID/UUIDTest.cpp index bcd8c09cb..1912da8fa 100644 --- a/tests/UUID/UUIDTest.cpp +++ b/tests/UUID/UUIDTest.cpp @@ -2,11 +2,15 @@ // UUIDTest.cpp #include "Globals.h" +#include "../TestHelpers.h" #include "UUID.h" #include <numeric> // for std::iota + + + /** Test that FromString -> ToShortString preserves the original value for short UUIDs. */ static void UUIDFromStringToShortString() { @@ -23,12 +27,12 @@ static void UUIDFromStringToShortString() for (auto TestString : TestStrings) { cUUID UUID; - assert_test(UUID.FromString(TestString)); + TEST_TRUE(UUID.FromString(TestString)); auto ResultString = UUID.ToShortString(); // Result should be equivalent to original - assert_test(NoCaseCompare(ResultString, TestString) == 0); + TEST_EQUAL(NoCaseCompare(ResultString, TestString), 0); // And should be all lower case - assert_test(ResultString == StrToLower(ResultString)); + TEST_EQUAL(ResultString, StrToLower(ResultString)); } } @@ -52,12 +56,12 @@ static void UUIDFromStringToLongString() for (auto TestString : TestStrings) { cUUID UUID; - assert_test(UUID.FromString(TestString)); + TEST_TRUE(UUID.FromString(TestString)); auto ResultString = UUID.ToLongString(); // Result should be equivalent to original - assert_test(NoCaseCompare(ResultString, TestString) == 0); + TEST_EQUAL(NoCaseCompare(ResultString, TestString), 0); // And should be all lower case - assert_test(ResultString == StrToLower(ResultString)); + TEST_EQUAL(ResultString, StrToLower(ResultString)); } } @@ -80,7 +84,7 @@ static void UUIDFromRawToRaw() cUUID UUID; UUID.FromRaw(TestRaw); auto ResultRaw = UUID.ToRaw(); - assert_test(ResultRaw == TestRaw); + TEST_EQUAL(ResultRaw, TestRaw); } } @@ -96,13 +100,13 @@ static void UUIDNil() { cUUID UUID; - assert_test(UUID.FromString(NilString)); - assert_test(UUID.IsNil()); + TEST_TRUE(UUID.FromString(NilString)); + TEST_TRUE(UUID.IsNil()); } { cUUID UUID; - assert_test(UUID.FromString(NonNilString)); - assert_test(!UUID.IsNil()); + TEST_TRUE(UUID.FromString(NonNilString)); + TEST_TRUE(!UUID.IsNil()); } } @@ -123,9 +127,9 @@ static void UUIDType() for (const auto & String : TestStrings) { cUUID UUID; - assert_test(UUID.FromString(String)); - assert_test(UUID.Variant() == 1); - assert_test(UUID.Version() == 3); + TEST_TRUE(UUID.FromString(String)); + TEST_EQUAL(UUID.Variant(), 1); + TEST_EQUAL(UUID.Version(), 3); } } @@ -134,28 +138,10 @@ static void UUIDType() -int main(int argc, char * argv[]) -{ - LOG("UUID tests started"); - - LOG("Testing short string UUIDs"); +IMPLEMENT_TEST_MAIN("UUID", UUIDFromStringToShortString(); - - LOG("Testing long strings UUIDs"); UUIDFromStringToLongString(); - - LOG("Testing raw UUIDs"); UUIDFromRawToRaw(); - - LOG("Testing nil UUIDs"); UUIDNil(); - - LOG("Testing UUID type information"); UUIDType(); - - LOG("UUID tests finished"); -} - - - - +) |