From ebb3847f80b1c3a052a462bb2faf2a410ed74f67 Mon Sep 17 00:00:00 2001 From: samanseifi Date: Fri, 9 Feb 2024 20:02:09 -0500 Subject: [PATCH] #1 establishing the GoogleTest framework --- CMakeLists.txt | 29 +++++++++++++----- test/CMakeLists.txt | 21 ++++++-------- test/tests.cpp | 71 ++++++++++++++++++--------------------------- 3 files changed, 59 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f73b9a3..88b6d59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,16 @@ cmake_minimum_required(VERSION 3.1) project(SimpleCloth) +# Ensure we have at least CMake 3.14 +if(${CMAKE_VERSION} VERSION_LESS 3.14) + message(FATAL_ERROR "GoogleTest integration requires at least CMake 3.14") +endif() + set(BINARY_NAME ${CMAKE_PROJECT_NAME}) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED True) -# Add the path of the cmake files to the CMAKE_MODULE_PATH set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) if(NOT CMAKE_BUILD_TYPE) @@ -17,27 +21,36 @@ set(CMAKE_CXX_FLAGS "-Wall -Wextra") set(CMAKE_CXX_FLAGS_DEBUG "-g") set(CMAKE_CXX_FLAGS_RELEASE "-O3") -message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") - if(MSVC) add_compile_options(/W4) else() add_compile_options(-Wall -Wextra -pedantic) endif() - -#find_package(GSL REQUIRED) -#link_libraries(GSL::gsl GSL::gslcblas) - set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) add_executable(${BINARY_NAME} src/main.cpp includes/ArrayT.h includes/Environment.h includes/MultArrayT.h includes/Vec3.h) include_directories(includes) +# Include GoogleTest +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip +) +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # For Windows +FetchContent_MakeAvailable(googletest) + include(CTest) -enable_testing(test) +enable_testing() +# Only include test subdirectory if BUILD_TESTING is on if (BUILD_TESTING) add_subdirectory(test) endif() + +# Example of how to link a test executable +# add_executable(example_test test/example_test.cpp) +# target_link_libraries(example_test gtest_main) +# add_test(NAME example_test COMMAND example_test) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2d55750..d144a65 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,16 +1,13 @@ -# see https://cmake.org/cmake/help/latest/module/FindBoost.html -set(Boost_USE_STATIC_LIBS OFF) #enable dynamic linking +# Assuming GoogleTest has already been made available in the main CMakeLists.txt -# search for unit_test_framework -find_package(Boost REQUIRED COMPONENTS unit_test_framework) +# Create a test executable target from tests.cpp +add_executable(SimpleCloth_test tests.cpp) -include_directories(${Boost_INCLUDE_DIR}) +# Link GoogleTest libraries to the new target +target_link_libraries(SimpleCloth_test gtest_main) -# create a cmake_testapp_boost target from tests.cpp -add_executable(SimpleCloth_boost tests.cpp) +# Optionally, if you have additional libraries your tests need to link against, include them here +# target_link_libraries(SimpleCloth_test gtest_main your_additional_libraries) -# link Boost libraries to the new target -target_link_libraries(SimpleCloth_boost ${Boost_LIBRARIES}) - -# link Boost with code library -target_link_libraries(SimpleCloth_boost) +# Register the executable with CTest +add_test(NAME SimpleCloth_test COMMAND SimpleCloth_test) diff --git a/test/tests.cpp b/test/tests.cpp index 9df6723..4c68299 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,49 +1,36 @@ -// -// Created by saman on 10/29/20. -// - -#define BOOST_TEST_MAIN -#define BOOST_TEST_MODULE my_unit_tests -#define BOOST_TEST_DYN_LINK -#include - +// Assuming you have GoogleTest included in your project setup +#include #include "../includes/Vec3.h" #include "../includes/ArrayT.h" +// Define the test suite name with TEST() +TEST(MyTestSuite, DotProduct) { + Vec3 v1(1.0, 2.0, 0.0); + Vec3 v2(-1.0, -3.0, 1.0); + EXPECT_DOUBLE_EQ(v1.Dot(v2), -7.0); // Use EXPECT_DOUBLE_EQ for comparing floating-point numbers +} -BOOST_AUTO_TEST_SUITE(my_testsuite) +TEST(MyTestSuite, AssignToScalar) { + Vec3 v1(3, 5, 0); + double val = 3; + v1 = val; + EXPECT_EQ(v1.x, 3); + EXPECT_EQ(v1.y, 3); +} - BOOST_AUTO_TEST_CASE(dot_product) - { - Vec3 v1(1.0, 2.0, 0.0); - Vec3 v2(-1.0, -3.0, 1.0); - BOOST_TEST ( v1.Dot(v2) == -7.0); - } +TEST(MyTestSuite, MultiplyingByScalar) { + Vec3 v1(5, 4, 0); + double val = 3; + v1 *= val; + EXPECT_EQ(v1.x, 15); + EXPECT_EQ(v1.y, 12); +} - BOOST_AUTO_TEST_CASE(assign_to_scalar) - { - Vec3 v1(3, 5, 0); - double val = 3; - v1 = val; - BOOST_TEST (v1.x == 3); - BOOST_TEST (v1.y == 3); - } - BOOST_AUTO_TEST_CASE(multiplying_by_scalar) - { - Vec3 v1(5, 4, 0); - double val = 3; - v1 *= val; - BOOST_TEST (v1.x == 15); - BOOST_TEST (v1.y == 12); - } - BOOST_AUTO_TEST_CASE(multiplying_by_scalar_on_the_line) - { - Vec3 v1(2.0, 3.0, 0.0); - double val = 2.0; - Vec3 v2 = v1 * val; - BOOST_TEST (v2.x == 4.0); - BOOST_TEST (v2.y == 6.0); - } - -BOOST_AUTO_TEST_SUITE_END() +TEST(MyTestSuite, MultiplyingByScalarOnTheLine) { + Vec3 v1(2.0, 3.0, 0.0); + double val = 2.0; + Vec3 v2 = v1 * val; + EXPECT_EQ(v2.x, 4.0); + EXPECT_EQ(v2.y, 6.0); +}