Created
August 23, 2021 17:40
-
-
Save raytroop/8ce5495044a2846056cc601ebe4f0b95 to your computer and use it in GitHub Desktop.
Google Test (GTest) with CMake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.16) | |
set(CMAKE_CXX_STANDARD 11) | |
project(gtestTest) | |
find_package(Threads REQUIRED) | |
find_package(GTest REQUIRED) | |
include_directories(${GTEST_INCLUDE_DIRS}) | |
add_executable(hello_test hello_test.cc) | |
# Note that we also have to link to the pthread library or the application won’t compile. | |
target_link_libraries(hello_test ${GTEST_LIBRARIES} pthread) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtest/gtest.h> | |
#include <math.h> | |
double squareRoot(const double a) { | |
double b = sqrt(a); | |
if(b != b) { // nan check | |
return -1.0; | |
}else{ | |
return sqrt(a); | |
} | |
} | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment