DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
testStatisticalFunctions.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: testStatisticalFunctions.hpp
5// Author: crdrisko
6// Date: 10/23/2020-15:54:44
7// Description: Provides ~100% unit test coverage over all statistics-based mathematical functions
8
9#ifndef DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTSTATISTICS_TESTSTATISTICALFUNCTIONS_HPP
10#define DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTSTATISTICS_TESTSTATISTICALFUNCTIONS_HPP
11
12#include <array>
13#include <cmath>
14#include <vector>
15
16#include <common-utils/math.hpp>
17#include <gtest/gtest.h>
18
19GTEST_TEST(testStatisticalFunctions, orderOfMagnitudeIsCalculatedCorrectly)
20{
21 ASSERT_EQ(-15, DryChem::findOrderOfMagnitude(1e-15));
22 ASSERT_EQ(0, DryChem::findOrderOfMagnitude(6.8));
23 ASSERT_EQ(1, DryChem::findOrderOfMagnitude(50));
24 ASSERT_EQ(2, DryChem::findOrderOfMagnitude(457));
25 ASSERT_EQ(3, DryChem::findOrderOfMagnitude(1000));
26 ASSERT_EQ(18, DryChem::findOrderOfMagnitude(1.7e18));
27}
28
29GTEST_TEST(testStatisticalFunctions, weCanEasilyApproximatePiToACetainDegreeOfAccurracy)
30{
31 ASSERT_NEAR(3.14159265, (355.0 / 113.0), DryChem::findAbsoluteError(3.14159265, 7));
32}
33
34GTEST_TEST(testStatisticalFunctions, averageCalculationsWorkOnARangeOfValuesInAGivenContainer)
35{
36 std::vector<long double> x {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
37 std::array<long double, 10> y {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
38
39 ASSERT_EQ(5.5, DryChem::calculateAverage(x.begin(), x.end()));
40 ASSERT_EQ(3.0, DryChem::calculateAverage(y.begin(), y.end() - 5));
41}
42
43GTEST_TEST(testStatisticalFunctions, varianceCalculationsWorkOnARangeOfValuesInAGivenContainer)
44{
45 using namespace DryChem;
46
47 std::vector<long double> x {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
48 std::array<long double, 10> y {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
49
50 ASSERT_NEAR(3.027650, std::sqrt(calculateVariance(x.begin(), x.end())), findAbsoluteError(3.027650, 7));
51 ASSERT_NEAR(2.449490, std::sqrt(calculateVariance(y.begin() + 2, y.end())), findAbsoluteError(2.449490, 7));
52}
53
54#endif
Allow for a shorter namespace name for less using statements.
Definition errors.hpp:23
GTEST_TEST(testStatisticalFunctions, orderOfMagnitudeIsCalculatedCorrectly)
Definition testStatisticalFunctions.hpp:19