Skip to content

Commit

Permalink
#1 establishing the GoogleTest framework
Browse files Browse the repository at this point in the history
  • Loading branch information
samanseifi committed Feb 10, 2024
1 parent b0d7317 commit ebb3847
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 62 deletions.
29 changes: 21 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
21 changes: 9 additions & 12 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 29 additions & 42 deletions test/tests.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/test/unit_test.hpp>

// Assuming you have GoogleTest included in your project setup

#include <gtest/gtest.h>
#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);
}

0 comments on commit ebb3847

Please sign in to comment.