From a591dbfe9cedbb28c4f671d7dbb5a8015c212137 Mon Sep 17 00:00:00 2001 From: Xiphoseer Date: Mon, 3 Jan 2022 16:00:21 +0100 Subject: [PATCH 1/6] Basic tests infrastructure --- CMakeLists.txt | 6 +++++- dCommon/NiPoint3.cpp | 9 ++------- tests/CMakeLists.txt | 21 +++++++++++++++++++++ tests/CommonCxxTests.cpp | 0 tests/CommonCxxTests.h | 4 ++++ tests/TestLDFFormat.cpp | 31 +++++++++++++++++++++++++++++++ tests/TestNiPoint3.cpp | 13 +++++++++++++ 7 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/CommonCxxTests.cpp create mode 100644 tests/CommonCxxTests.h create mode 100644 tests/TestLDFFormat.cpp create mode 100644 tests/TestNiPoint3.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a8395c8a..cb94a2d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.12) project(Darkflame) +include(CTest) # Read variables from file FILE(READ "${CMAKE_SOURCE_DIR}/CMakeVariables.txt" variables) @@ -492,4 +493,7 @@ if(WIN32) add_dependencies(MasterServer WorldServer) add_dependencies(MasterServer AuthServer) add_dependencies(MasterServer ChatServer) -endif() \ No newline at end of file +endif() + +# Finally, add the tests +add_subdirectory(tests) \ No newline at end of file diff --git a/dCommon/NiPoint3.cpp b/dCommon/NiPoint3.cpp index d74be90b..4baefa13 100644 --- a/dCommon/NiPoint3.cpp +++ b/dCommon/NiPoint3.cpp @@ -93,14 +93,9 @@ Vector3 NiPoint3::CrossProduct(const Vector3& vec) const { //! Unitize the vector NiPoint3 NiPoint3::Unitize(void) const { - NiPoint3 unitVec; float length = this->Length(); - - unitVec.x = length != 0 ? this->x / length : 0; - unitVec.y = length != 0 ? this->y / length : 0; - unitVec.z = length != 0 ? this->z / length : 0; - - return unitVec; + + return length != 0 ? *this / length : NiPoint3::ZERO; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..eab4bbc9 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,21 @@ +# create the testing file and list of tests +create_test_sourcelist (Tests + CommonCxxTests.cpp + TestNiPoint3.cpp + TestLDFFormat.cpp +) + +# add the executable +add_executable (CommonCxxTests ${Tests}) +target_link_libraries(CommonCxxTests dCommon raknet) + +# remove the test driver source file +set (TestsToRun ${Tests}) +remove (TestsToRun CommonCxxTests.cpp) + +# Add all the ADD_TEST for each test +foreach (test ${TestsToRun}) + get_filename_component (TName ${test} NAME_WE) + add_test (NAME ${TName} COMMAND CommonCxxTests ${TName}) + set_property(TEST ${TName} PROPERTY ENVIRONMENT CTEST_OUTPUT_ON_FAILURE=1) +endforeach () diff --git a/tests/CommonCxxTests.cpp b/tests/CommonCxxTests.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/CommonCxxTests.h b/tests/CommonCxxTests.h new file mode 100644 index 00000000..8d2dbbab --- /dev/null +++ b/tests/CommonCxxTests.h @@ -0,0 +1,4 @@ +#include + +#define ASSERT_EQ(a,b) { if (!(a == b)) { printf("Failed assertion: " #a " == " #b " \n in %s:%d\n", __FILE__, __LINE__); return 1; }} +#define ASSERT_NE(a,b) { if (!(a != b)) { printf("Failed assertion: " #a " != " #b " \n in %s:%d\n", __FILE__, __LINE__); return 1; }} \ No newline at end of file diff --git a/tests/TestLDFFormat.cpp b/tests/TestLDFFormat.cpp new file mode 100644 index 00000000..276a8175 --- /dev/null +++ b/tests/TestLDFFormat.cpp @@ -0,0 +1,31 @@ +#include "LDFFormat.h" +#include "CommonCxxTests.h" + +/** + * @brief Test parsing an LDF value + * + * @param argc Number of command line arguments for this test + * @param argv Command line arguments + * @return 0 on success, non-zero on failure + */ +int TestLDFFormat(int argc, char** argv) { + // Create + auto* data = LDFBaseData::DataFromString("KEY=0:VALUE"); + + // Check that the data type is correct + ASSERT_EQ(data->GetValueType(), eLDFType::LDF_TYPE_UTF_16); + + // Check that the key is correct + ASSERT_EQ(data->GetKey(), u"KEY"); + + // Check that the value is correct + ASSERT_EQ(((LDFData* )data)->GetValue(), u"VALUE"); + + // Check that the serialization is correct + ASSERT_EQ(data->GetString(), "KEY=0:VALUE"); + + // Cleanup the object + delete data; + + return 0; +} diff --git a/tests/TestNiPoint3.cpp b/tests/TestNiPoint3.cpp new file mode 100644 index 00000000..68da4571 --- /dev/null +++ b/tests/TestNiPoint3.cpp @@ -0,0 +1,13 @@ +#include + +#include "NiPoint3.h" +#include "CommonCxxTests.h" + +int TestNiPoint3(int argc, char** argv) { + // Check that Unitize works + ASSERT_EQ(NiPoint3(3,0,0).Unitize(), NiPoint3::UNIT_X); + // Check what unitize does to a vector of length 0 + ASSERT_EQ(NiPoint3::ZERO.Unitize(), NiPoint3::ZERO); + // If we get here, all was successful + return 0; +} From 1100c57e2db5b4c109feccda11906fd27c92a4ad Mon Sep 17 00:00:00 2001 From: Xiphoseer Date: Mon, 3 Jan 2022 16:00:32 +0100 Subject: [PATCH 2/6] Run tests with CI --- .github/workflows/{build.yml => build-and-test.yml} | 1 + 1 file changed, 1 insertion(+) rename .github/workflows/{build.yml => build-and-test.yml} (95%) diff --git a/.github/workflows/build.yml b/.github/workflows/build-and-test.yml similarity index 95% rename from .github/workflows/build.yml rename to .github/workflows/build-and-test.yml index ae7501e8..d91244a7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build-and-test.yml @@ -20,6 +20,7 @@ jobs: with: configurePreset: 'default' buildPreset: 'default' + testPreset: 'default' - name: artifacts uses: actions/upload-artifact@v2 with: From eb18115c993aa413e70d41c54431c61bfbbd7991 Mon Sep 17 00:00:00 2001 From: Xiphoseer Date: Mon, 3 Jan 2022 16:25:47 +0100 Subject: [PATCH 3/6] Update CMakePresets --- CMakePresets.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CMakePresets.json b/CMakePresets.json index 8b25452f..77d41205 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -36,5 +36,19 @@ "description": "Default Build", "jobs": 2 } + ], + "testPresets": [ + { + "name": "default", + "configurePreset": "default", + "displayName": "Default Tests", + "description": "Runs all tests", + "execution": { + "jobs": 2 + }, + "output": { + "outputOnFailure": true + } + } ] } From 03d4d84eeb0e9421149f437b1accb74e750619bc Mon Sep 17 00:00:00 2001 From: Xiphoseer Date: Mon, 3 Jan 2022 16:28:16 +0100 Subject: [PATCH 4/6] Update CI pipeline --- .github/workflows/build-and-test.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index d91244a7..cd763ca7 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -7,7 +7,7 @@ on: branches: [ main ] jobs: - build: + build-and-test: runs-on: ubuntu-latest @@ -15,11 +15,17 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - name: cmake + - name: cmake-configure uses: lukka/run-cmake@v10 with: configurePreset: 'default' + - name: cmake-build + uses: lukka/run-cmake@v10 + with: buildPreset: 'default' + - name: cmake-test + uses: lukka/run-cmake@v10 + with: testPreset: 'default' - name: artifacts uses: actions/upload-artifact@v2 From a06fa39a5d94cb6b59dbfd3ee077f1d0132dfbc8 Mon Sep 17 00:00:00 2001 From: Xiphoseer Date: Mon, 3 Jan 2022 16:30:29 +0100 Subject: [PATCH 5/6] Add newline at EOF; re-merge cmake steps --- .github/workflows/build-and-test.yml | 8 +------- tests/CommonCxxTests.h | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index cd763ca7..1f4d0dca 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -15,17 +15,11 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - name: cmake-configure + - name: cmake uses: lukka/run-cmake@v10 with: configurePreset: 'default' - - name: cmake-build - uses: lukka/run-cmake@v10 - with: buildPreset: 'default' - - name: cmake-test - uses: lukka/run-cmake@v10 - with: testPreset: 'default' - name: artifacts uses: actions/upload-artifact@v2 diff --git a/tests/CommonCxxTests.h b/tests/CommonCxxTests.h index 8d2dbbab..f1894927 100644 --- a/tests/CommonCxxTests.h +++ b/tests/CommonCxxTests.h @@ -1,4 +1,4 @@ #include #define ASSERT_EQ(a,b) { if (!(a == b)) { printf("Failed assertion: " #a " == " #b " \n in %s:%d\n", __FILE__, __LINE__); return 1; }} -#define ASSERT_NE(a,b) { if (!(a != b)) { printf("Failed assertion: " #a " != " #b " \n in %s:%d\n", __FILE__, __LINE__); return 1; }} \ No newline at end of file +#define ASSERT_NE(a,b) { if (!(a != b)) { printf("Failed assertion: " #a " != " #b " \n in %s:%d\n", __FILE__, __LINE__); return 1; }} From 227b82c446e2fe256bf3d6791a29e5ad4d39ee2c Mon Sep 17 00:00:00 2001 From: Xiphoseer Date: Tue, 4 Jan 2022 09:26:43 +0100 Subject: [PATCH 6/6] Add newline at EOF [skip ci] --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb94a2d3..85d2316a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -496,4 +496,5 @@ add_dependencies(MasterServer ChatServer) endif() # Finally, add the tests -add_subdirectory(tests) \ No newline at end of file +add_subdirectory(tests) +