DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
testBasicMathFunctions.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: testBasicMathFunctions.hpp
5// Author: crdrisko
6// Date: 09/28/2020-11:46:58
7// Description: Provides ~100% unit test coverage over all basic mathematical functions
8
9#ifndef DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTUTILITIES_TESTBASICMATHFUNCTIONS_HPP
10#define DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTUTILITIES_TESTBASICMATHFUNCTIONS_HPP
11
12#include <cstddef>
13#include <string>
14#include <vector>
15
16#include <common-utils/math.hpp>
17#include <gtest/gtest.h>
18
19GTEST_TEST(testBasicMathFunctions, isEvenReturnCorrectBooleansForIntegralTypes)
20{
21 char cValue {'2'};
22 short sValue {4};
23 int iValue {6};
24 long lValue {8};
25 long long llValue {10};
26
27 ASSERT_TRUE(DryChem::isEven(cValue));
28 ASSERT_TRUE(DryChem::isEven(sValue));
29 ASSERT_TRUE(DryChem::isEven(iValue));
30 ASSERT_TRUE(DryChem::isEven(lValue));
31 ASSERT_TRUE(DryChem::isEven(llValue));
32
33 ASSERT_FALSE(DryChem::isEven(3));
34
35 // ASSERT_TRUE(DryChem::isEven(false)); // no instance of function template matches the argument list
36 // ASSERT_TRUE(DryChem::isEven(2.0)); // no instance of function template matches the argument list
37}
38
39GTEST_TEST(testBasicMathFunctions, isOddReturnCorrectBooleansForIntegralTypes)
40{
41 char cValue {'3'};
42 short sValue {5};
43 int iValue {7};
44 long lValue {9};
45 long long llValue {11};
46
47 ASSERT_TRUE(DryChem::isOdd(cValue));
48 ASSERT_TRUE(DryChem::isOdd(sValue));
49 ASSERT_TRUE(DryChem::isOdd(iValue));
50 ASSERT_TRUE(DryChem::isOdd(lValue));
51 ASSERT_TRUE(DryChem::isOdd(llValue));
52
53 ASSERT_FALSE(DryChem::isOdd(2));
54
55 // ASSERT_TRUE(DryChem::isOdd(true)); // no instance of function template matches the argument list
56 // ASSERT_TRUE(DryChem::isOdd(3.0)); // no instance of function template matches the argument list
57}
58
59GTEST_TEST(testBasicMathFunctions, withinRangeFunctionalityWorksForIntegers)
60{
61 int integerValue {10};
62
63 ASSERT_TRUE(DryChem::withinRange(integerValue, 5, 15));
64 ASSERT_FALSE(DryChem::withinRange(integerValue, 15, 20));
65
66 bool passingRangeTestForIntegers = DryChem::withinRange<5, 15>(integerValue);
67 bool failingRangeTestIntegers = DryChem::withinRange<15, 20>(integerValue);
68
69 ASSERT_TRUE(passingRangeTestForIntegers);
70 ASSERT_FALSE(failingRangeTestIntegers);
71}
72
73GTEST_TEST(testBasicMathFunctions, withinRangeFunctionalityWorksForDoubles)
74{
75 double doubleValue {42.67};
76
77 ASSERT_TRUE(DryChem::withinRange(doubleValue, 42.0, 42.75));
78 ASSERT_FALSE(DryChem::withinRange(doubleValue, 42.0, 42.5));
79
80 bool passingRangeTestForDoubles = DryChem::withinRange<42, 43>(doubleValue);
81 bool failingRangeTestDoubles = DryChem::withinRange<41, 42>(doubleValue);
82
83 ASSERT_TRUE(passingRangeTestForDoubles);
84 ASSERT_FALSE(failingRangeTestDoubles);
85}
86
87GTEST_TEST(testBasicMathFunctions, withinRangeFunctionalityWorksForStrings)
88{
89 std::string stringValue {"How are you?"};
90
91 ASSERT_TRUE(DryChem::withinRange<std::string>(stringValue, "Hello", "What's new?"));
92 ASSERT_FALSE(DryChem::withinRange<std::string>(stringValue, "Hello", "Goodbye"));
93}
94
95GTEST_TEST(testBasicMathFunctions, linearInterpolationOf_y_along_x_gives_x)
96{
97 std::vector<long double> x;
98 std::vector<long double> y;
99
100 for (std::size_t i {}; i <= 10; ++i)
101 x.push_back(static_cast<long double>(i));
102
103 y = DryChem::linearlyInterpolate(x, 0.0l, 10.0l);
104
105 ASSERT_EQ(x, y);
106}
107
108#endif
GTEST_TEST(testBasicMathFunctions, isEvenReturnCorrectBooleansForIntegralTypes)
Definition testBasicMathFunctions.hpp:19