From 3f4e18744d9e3b4d37de3a18dd3d6da6064e6a7d Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Wed, 30 Dec 2020 16:18:19 +0100 Subject: [PATCH] add test functions --- cpp_template/calc.cpp | 11 +++++++++++ cpp_template/calc_test.cpp | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 cpp_template/calc.cpp create mode 100644 cpp_template/calc_test.cpp diff --git a/cpp_template/calc.cpp b/cpp_template/calc.cpp new file mode 100644 index 0000000..065bd7a --- /dev/null +++ b/cpp_template/calc.cpp @@ -0,0 +1,11 @@ +#include + +// Get the Square root of a number. +double squareRoot(const double a) + { + double b = sqrt(a); + if(b != b) // NaN check + { return -1.0; } + else + { return sqrt(a); } + } diff --git a/cpp_template/calc_test.cpp b/cpp_template/calc_test.cpp new file mode 100644 index 0000000..f771e7d --- /dev/null +++ b/cpp_template/calc_test.cpp @@ -0,0 +1,22 @@ +#include "calc.cpp" +#include + +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(); + }