DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
testIntegrationMethods.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: testIntegrationMethods.hpp
5// Author: crdrisko
6// Date: 10/28/2020-07:58:30
7// Description: Provides ~100% unit test coverage over the approximate integration methods
8
9#ifndef DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTCALCULUS_TESTINTEGRATIONMETHODS_HPP
10#define DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTCALCULUS_TESTINTEGRATIONMETHODS_HPP
11
12#include <cstddef>
13#include <sstream>
14#include <vector>
15
16#include <common-utils/math.hpp>
17#include <gtest/gtest.h>
18
19GTEST_TEST(testIntegrationMethods, theTrapzMethodCanBeUsedInCompileTimeCalculations)
20{
21 const int x1 {0}, x2 {15}, y1 {0}, y2 {6};
22
23 static_assert(45 == DryChem::trapz(x1, x2, y1, y2), "Trapz method failed.");
24}
25
26GTEST_TEST(testIntegrationMethods, theTrapzMethodCanAcceptMultipleTypes)
27{
28 long double x1 {0.1}, x2 {22.5}, y1 {2.3}, y2 {6.0};
29
30 ASSERT_DOUBLE_EQ(92.96, DryChem::trapz(x1, x2, y1, y2));
31}
32
33GTEST_TEST(testIntegrationMethods, theCumulativeTrapzMethodHasAnOptionalParameterWhichCanBeOverriden)
34{
35 std::vector<long double> x, y, expectedResult;
36
37 for (std::size_t i {}; i <= 10; ++i)
38 {
39 x.push_back(static_cast<long double>(i));
40 y.push_back(static_cast<long double>(i));
41
42 expectedResult.push_back((x[i] * x[i]) / 2.0);
43 }
44
45 // ∫x = 1/2 x^2
46 auto integrationResult1 = DryChem::cumulativeTrapzIntegration(x.begin(), x.end(), y.begin(), y.end());
47 auto integrationResult2 = DryChem::cumulativeTrapzIntegration(x.begin(), x.end(), y.begin(), y.end(), 0.0);
48 auto integrationResult3 = DryChem::cumulativeTrapzIntegration(x.begin(), x.end(), y.begin(), y.end(), 5.0);
49
50 for (std::size_t i {}; i < expectedResult.size(); ++i)
51 {
52 ASSERT_EQ(expectedResult[i], integrationResult2[i]);
53
54 if (i == 0)
55 ASSERT_EQ(5.0, integrationResult3[i]);
56 else
57 ASSERT_EQ(expectedResult[i], integrationResult3[i]);
58
59 if (i == expectedResult.size() - 1)
60 break;
61 else
62 ASSERT_EQ(expectedResult[i + 1], integrationResult1[i]);
63 }
64}
65
66GTEST_TEST(testIntegrationMethods, insteadOfUsingIteratorsWeCanJustPassFullContainers)
67{
68 std::vector<long double> x, y, expectedResult;
69
70 for (std::size_t i {}; i <= 10; ++i)
71 {
72 x.push_back(static_cast<long double>(i));
73 y.push_back(static_cast<long double>(i));
74
75 expectedResult.push_back((x[i] * x[i]) / 2.0);
76 }
77
78 // ∫x = 1/2 x^2
79 auto integrationResult = DryChem::cumulativeTrapzIntegration(x, y, 0.0);
80
81 ASSERT_EQ(expectedResult, integrationResult);
82}
83
84GTEST_TEST(testIntegrationMethods, passingTwoDifferentlySizedContainersResultsInFatalException)
85{
86 std::stringstream deathRegex;
87
88 deathRegex << "Common-Utilities Fatal Error: ";
89
90#if GTEST_USES_POSIX_RE
91 deathRegex << "[(]integration.hpp: *[0-9]*[)]\n\t";
92#elif GTEST_USES_SIMPLE_RE
93 deathRegex << "\\(integration.hpp: \\d*\\)\n\t";
94#endif
95
96 deathRegex << "Input sizes for x and y containers must be the same.\n";
97
98 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};
99 std::vector<long double> y {2.0, 5.0, 3.0, 7.0, 8.0, 9.0, 12.0, 10.0, 15.0, 20.0};
100
101 ASSERT_DEATH(
102 {
103 try
104 {
105 DryChem::cumulativeTrapzIntegration(x.begin(), x.end(), y.begin(), y.end() - 2);
106 }
107 catch (const DryChem::InputSizeMismatch& except)
108 {
109 except.handleErrorWithMessage();
110 }
111 },
112 deathRegex.str());
113}
114
115#endif
GTEST_TEST(testIntegrationMethods, theTrapzMethodCanBeUsedInCompileTimeCalculations)
Definition testIntegrationMethods.hpp:19