Skip to content

Commit

Permalink
Improved testing framework. (cuberite#4376)
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft authored Aug 26, 2019
1 parent 02fbf16 commit 74579fb
Show file tree
Hide file tree
Showing 18 changed files with 413 additions and 388 deletions.
62 changes: 25 additions & 37 deletions src/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 5 additions & 34 deletions tests/BlockTypeRegistry/BlockStateTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
)
30 changes: 3 additions & 27 deletions tests/BlockTypeRegistry/BlockTypeRegistryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
)
51 changes: 20 additions & 31 deletions tests/ByteBuffer/ByteBufferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Implements the main app entrypoint for the cByteBuffer class test

#include "Globals.h"
#include "../TestHelpers.h"
#include "ByteBuffer.h"


Expand All @@ -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);
}


Expand All @@ -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);
}


Expand All @@ -48,41 +52,26 @@ 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
}
}





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.");
}





)
7 changes: 3 additions & 4 deletions tests/ByteBuffer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 22 additions & 17 deletions tests/ChunkData/ArraytoCoord.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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);
}

{
Expand All @@ -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);
}

{
Expand All @@ -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();
)
Loading

0 comments on commit 74579fb

Please sign in to comment.