(base) ninja@luke:~/workspace/google_test$ cat main_test.cpp
// main_test.cpp
#include "whattotest.cpp"
#include <gtest/gtest.h>
TEST(SquareRootTest, PositiveNos) {
ASSERT_EQ(6, squareRoot(36.0));
ASSERT_EQ(18.0, squareRoot(324.0));
ASSERT_EQ(25.4, squareRoot(645.16));
ASSERT_EQ(0, squareRoot(0.0));
}
TEST(SquareRootTest, NegativeNos) {
ASSERT_EQ(-1.0, squareRoot(-15.0));
ASSERT_EQ(-1.0, squareRoot(-0.2));
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
---
(base) ninja@luke:~/workspace/google_test$ cat whattotest.cpp
// whattotest.cpp
#include <math.h>
double squareRoot(const double a) {
double b = sqrt(a);
if(b != b) { // nan check
return -1.0;
}else{
return sqrt(a);
}
}
---
(base) ninja@luke:~/workspace/google_test$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(test)
################################
# GTest
################################
project(googletest-git NONE)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
################################
# main test program
################################
# Add test cpp file
add_executable(test main_test.cpp)
# Link test executable against gtest & gtest_main
target_link_libraries(test gtest gtest_main)
commands:-
cd ~/workspace/google_test
mkdir build
cd build
cmake ..
make test
No comments:
Post a Comment