From ab6f68b42cac83e4f8fdc93e9654eb536f7f2e32 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 13 Mar 2016 18:59:42 +0100 Subject: SelfTests: Moved ByteBuffer test to a separate project. --- src/ByteBuffer.cpp | 66 ------------------------------ tests/ByteBuffer/ByteBufferTest.cpp | 80 +++++++++++++++++++++++++++++++++++++ tests/ByteBuffer/CMakeLists.txt | 46 +++++++++++++++++++++ tests/CMakeLists.txt | 5 ++- 4 files changed, 129 insertions(+), 68 deletions(-) create mode 100644 tests/ByteBuffer/ByteBufferTest.cpp create mode 100644 tests/ByteBuffer/CMakeLists.txt diff --git a/src/ByteBuffer.cpp b/src/ByteBuffer.cpp index 16620becd..230a5ffbb 100644 --- a/src/ByteBuffer.cpp +++ b/src/ByteBuffer.cpp @@ -8,7 +8,6 @@ #include "ByteBuffer.h" #include "Endianness.h" #include "OSSupport/IsThread.h" -#include "SelfTests.h" @@ -54,71 +53,6 @@ Unfortunately it is very slow, so it is disabled even for regular DEBUG builds. -#ifdef SELF_TEST - -/** Self-test of the VarInt-reading and writing code */ -static class cByteBufferSelfTest -{ -public: - cByteBufferSelfTest(void) - { - cSelfTests::Get().Register(cSelfTests::SelfTestFunction(&TestRead), "ByteBuffer read"); - cSelfTests::Get().Register(cSelfTests::SelfTestFunction(&TestWrite), "ByteBuffer write"); - cSelfTests::Get().Register(cSelfTests::SelfTestFunction(&TestWrap), "ByteBuffer wraparound"); - } - - static void TestRead(void) - { - cByteBuffer buf(50); - buf.Write("\x05\xac\x02\x00", 4); - UInt32 v1; - assert_test(buf.ReadVarInt(v1) && (v1 == 5)); - UInt32 v2; - assert_test(buf.ReadVarInt(v2) && (v2 == 300)); - UInt32 v3; - assert_test(buf.ReadVarInt(v3) && (v3 == 0)); - } - - static void TestWrite(void) - { - cByteBuffer buf(50); - buf.WriteVarInt32(5); - buf.WriteVarInt32(300); - 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); - } - - static void TestWrap(void) - { - cByteBuffer buf(3); - 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); - UInt8 v = 0; - assert_test(buf.ReadBEUInt8(v)); - assert_test(v == 'a'); - assert_test(buf.GetReadableSpace() == 0); - buf.CommitRead(); - assert_test(buf.GetFreeSpace() == FreeSpace); // We're back to normal - } - } - -} g_ByteBufferTest; - -#endif - - - - - #ifdef DEBUG_SINGLE_THREAD_ACCESS /** Simple RAII class that is used for checking that no two threads are using an object simultanously. diff --git a/tests/ByteBuffer/ByteBufferTest.cpp b/tests/ByteBuffer/ByteBufferTest.cpp new file mode 100644 index 000000000..98a802554 --- /dev/null +++ b/tests/ByteBuffer/ByteBufferTest.cpp @@ -0,0 +1,80 @@ + +// ByteBufferTest.cpp + +// Implements the main app entrypoint for the cByteBuffer class test + +#include "Globals.h" +#include "ByteBuffer.h" + + + + + +static void TestRead(void) +{ + cByteBuffer buf(50); + buf.Write("\x05\xac\x02\x00", 4); + UInt32 v1; + assert_test(buf.ReadVarInt(v1) && (v1 == 5)); + UInt32 v2; + assert_test(buf.ReadVarInt(v2) && (v2 == 300)); + UInt32 v3; + assert_test(buf.ReadVarInt(v3) && (v3 == 0)); +} + + + + + +static void TestWrite(void) +{ + cByteBuffer buf(50); + buf.WriteVarInt32(5); + buf.WriteVarInt32(300); + 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); +} + + + + + +static void TestWrap(void) +{ + cByteBuffer buf(3); + 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); + UInt8 v = 0; + assert_test(buf.ReadBEUInt8(v)); + assert_test(v == 'a'); + assert_test(buf.GetReadableSpace() == 0); + buf.CommitRead(); + assert_test(buf.GetFreeSpace() == FreeSpace); // We're back to normal + } +} + + + + + +int main(int argc, char * argv[]) +{ + TestRead(); + TestWrite(); + TestWrap(); + LOG("ByteBuffer test finished."); +} + + + + + diff --git a/tests/ByteBuffer/CMakeLists.txt b/tests/ByteBuffer/CMakeLists.txt new file mode 100644 index 000000000..5c82d25d9 --- /dev/null +++ b/tests/ByteBuffer/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required (VERSION 2.6) + +enable_testing() +add_definitions(-DTEST_GLOBALS=1) + +include_directories(${CMAKE_SOURCE_DIR}/src/) + +add_definitions(-DTEST_GLOBALS=1) + +set (SHARED_SRCS + ${CMAKE_SOURCE_DIR}/src/ByteBuffer.cpp + ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.cpp +) + +set (SHARED_HDRS + ${CMAKE_SOURCE_DIR}/src/ByteBuffer.h + ${CMAKE_SOURCE_DIR}/src/OSSupport/StackTrace.h +) + +set (SRCS + ByteBufferTest.cpp +) + +if (MSVC) + # Add the MSVC-specific LeakFinder / StackTracer sources: + list (APPEND SHARED_SRCS ${CMAKE_SOURCE_DIR}/src/LeakFinder.cpp ${CMAKE_SOURCE_DIR}/src/StackWalker.cpp) + list (APPEND SHARED_HDRS ${CMAKE_SOURCE_DIR}/src/LeakFinder.h ${CMAKE_SOURCE_DIR}/src/StackWalker.h) +endif() + +source_group("Shared" FILES ${SHARED_SRCS} ${SHARED_HDRS}) +source_group("Sources" FILES ${SRCS}) +add_executable(ByteBuffer-exe ${SRCS} ${SHARED_SRCS} ${SHARED_HDRS}) +if (WIN32) + target_link_libraries(ByteBuffer-exe ws2_32) +endif() +add_test(NAME ByteBuffer-test COMMAND ByteBuffer-exe) + + + + + +# Put the projects into solution folders (MSVC): +set_target_properties( + ByteBuffer-exe + PROPERTIES FOLDER Tests +) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 63eb8ae3a..2a85f7b3d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,9 +6,10 @@ if (CMAKE_BUILD_TYPE STREQUAL "COVERAGE") setup_target_for_coverage("${PROJECT_NAME}_coverage" "ctest" coverage) endif() -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +# include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_subdirectory(ByteBuffer) add_subdirectory(ChunkData) add_subdirectory(HTTP) -add_subdirectory(Network) add_subdirectory(LoadablePieces) +add_subdirectory(Network) -- cgit v1.2.3