DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
testLinearLeastSquaresFitting.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: testLinearLeastSquaresFitting.hpp
5// Author: crdrisko
6// Date: 10/26/2020-11:56:41
7// Description: Provides ~100% unit test coverage over the linear least squares fitting function
8
9#ifndef DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTSTATISTICS_TESTLINEARLEASTSQUARESFITTING_HPP
10#define DRYCHEM_COMMON_UTILITIES_LIBS_MATH_TESTS_TESTSTATISTICS_TESTLINEARLEASTSQUARESFITTING_HPP
11
12#include <cmath>
13#include <sstream>
14#include <vector>
15
16#include <common-utils/math.hpp>
17#include <gtest/gtest.h>
18
19GTEST_TEST(testLinearLeastSquaresFitting, linearLeastSquaresFittingReturnsMultipleValuesInAStruct)
20{
21 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};
22 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};
23
25 = DryChem::linearLeastSquaresFitting(x.begin(), x.end(), y.begin(), y.end());
26
27 ASSERT_NEAR(1.7152, result.slope, DryChem::findAbsoluteError(1.7152, 5));
28 ASSERT_NEAR(-0.33333, result.intercept, DryChem::findAbsoluteError(-0.33333, 5));
29 ASSERT_NEAR(0.2139317, std::sqrt(result.variance), DryChem::findAbsoluteError(0.2139317, 7));
30}
31
32GTEST_TEST(testLinearLeastSquaresFitting, linearLeastSquaresFittingCanReturnATypeCompatibleWithStructuredBinding)
33{
34 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};
35 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};
36
37 auto [slope, intercept, variance] = DryChem::linearLeastSquaresFitting(x.cbegin(), x.cend(), y.cbegin(), y.cend());
38
39 ASSERT_NEAR(1.7152, slope, DryChem::findAbsoluteError(1.7152, 5));
40 ASSERT_NEAR(-0.33333, intercept, DryChem::findAbsoluteError(-0.33333, 5));
41 ASSERT_NEAR(0.2139317, std::sqrt(variance), DryChem::findAbsoluteError(0.2139317, 7));
42}
43
44GTEST_TEST(testLinearLeastSquaresFitting, passingTwoDifferentlySizedContainersResultsInFatalException)
45{
46 std::stringstream deathRegex;
47
48 deathRegex << "Common-Utilities Fatal Error: ";
49
50#if GTEST_USES_POSIX_RE
51 deathRegex << "[(]linearLeastSquaresFitting.hpp: *[0-9]*[)]\n\t";
52#elif GTEST_USES_SIMPLE_RE
53 deathRegex << "\\(linearLeastSquaresFitting.hpp: \\d*\\)\n\t";
54#endif
55
56 deathRegex << "Input sizes for x and y containers must be the same.\n";
57
58 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};
59 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};
60
61 ASSERT_DEATH(
62 {
63 try
64 {
65 DryChem::linearLeastSquaresFitting(x.begin(), x.end(), y.begin(), y.end() - 2);
66 }
67 catch (const DryChem::InputSizeMismatch& except)
68 {
69 except.handleErrorWithMessage();
70 }
71 },
72 deathRegex.str());
73}
74
75#endif
Definition linearLeastSquaresFitting.hpp:35
T_variance variance
Definition linearLeastSquaresFitting.hpp:38
T_slope slope
Definition linearLeastSquaresFitting.hpp:36
T_intercept intercept
Definition linearLeastSquaresFitting.hpp:37
GTEST_TEST(testLinearLeastSquaresFitting, linearLeastSquaresFittingReturnsMultipleValuesInAStruct)
Definition testLinearLeastSquaresFitting.hpp:19