diff --git a/.github/workflows/build.yml b/.github/workflows/build-and-test.yml similarity index 92% rename from .github/workflows/build.yml rename to .github/workflows/build-and-test.yml index ae7501e8..1f4d0dca 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build-and-test.yml @@ -7,7 +7,7 @@ on: branches: [ main ] jobs: - build: + build-and-test: runs-on: ubuntu-latest @@ -20,6 +20,7 @@ jobs: with: configurePreset: 'default' buildPreset: 'default' + testPreset: 'default' - name: artifacts uses: actions/upload-artifact@v2 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index a8395c8a..85d2316a 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,8 @@ 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) + 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 + } + } ] } 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..f1894927 --- /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; }} 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; +}