DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
testPhysicalQuantity.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: testPhysicalQuantity.hpp
5// Author: crdrisko
6// Date: 09/17/2020-09:54:44
7// Description: Provides ~100% unit test coverage over all miscellaneous PhysicalQuantity member functions
8
9#ifndef DRYCHEM_CPP_UNITS_TESTS_TESTTYPES_TESTPHYSICALQUANTITY_HPP
10#define DRYCHEM_CPP_UNITS_TESTS_TESTTYPES_TESTPHYSICALQUANTITY_HPP
11
12#include <exception>
13#include <sstream>
14#include <stdexcept>
15#include <string>
16
18#include <gtest/gtest.h>
19
21
22using namespace CppUnits;
23
24GTEST_TEST(testPhysicalQuantity, aPhysicalQuantityKnowsItsDimensionality)
25{
26 using MolarEntropyDimensionality = Dimensionality<2, 1, -2, 0, -1, -1>;
27
28 ASSERT_EQ(MolarEntropyDimensionality::Length, MolarEntropy::DimensionalityType::Length);
29 ASSERT_EQ(MolarEntropyDimensionality::Mass, MolarEntropy::DimensionalityType::Mass);
30 ASSERT_EQ(MolarEntropyDimensionality::Time, MolarEntropy::DimensionalityType::Time);
31 ASSERT_EQ(MolarEntropyDimensionality::ElectricCurrent, MolarEntropy::DimensionalityType::ElectricCurrent);
32 ASSERT_EQ(MolarEntropyDimensionality::Temperature, MolarEntropy::DimensionalityType::Temperature);
33 ASSERT_EQ(MolarEntropyDimensionality::AmountOfSubstance, MolarEntropy::DimensionalityType::AmountOfSubstance);
34 ASSERT_EQ(MolarEntropyDimensionality::LuminousIntensity, MolarEntropy::DimensionalityType::LuminousIntensity);
35}
36
37GTEST_TEST(testPhysicalQuantity, defaultInitializerSetsPhysicalQuantitysMagnitudeToZero)
38{
39 DimensionlessQuantity defaultInitialized {};
40 ASSERT_EQ(0.0, defaultInitialized.getMagnitude());
41}
42
43GTEST_TEST(testPhysicalQuantity, setterFunctionsCanTakeDoubleValuesAsInputParameters)
44{
45 using namespace CppUnits::Literals;
46
47 Length len = 2.0_m;
48 len.setMagnitude(1e5);
49
50 ASSERT_EQ(1e5, len.getMagnitude());
51}
52
53GTEST_TEST(testPhysicalQuantity, physicalQuantitiesCanBeConstructedFromStrings)
54{
55 DimensionlessQuantity dimensionlessQuantity {"1e5"};
56 ASSERT_DOUBLE_EQ(1e5, dimensionlessQuantity.getMagnitude());
57
58 Length length {"1e5_m"}; // Non-number part ignored - could be anything
59 ASSERT_DOUBLE_EQ(1e5, length.getMagnitude());
60}
61
62GTEST_TEST(testPhysicalQuantity, stringConstructorCanThrowAnException)
63{
64 std::stringstream deathRegex;
65 long double number {};
66
67 try
68 {
69 number = std::stold("Not a number");
70 }
71 catch (const std::invalid_argument& except)
72 {
73 deathRegex << "CPP Units Fatal Error:\n Exception message: " << except.what();
74 }
75
76 if (number != 0.0l)
77 number = 0.0l;
78
79 ASSERT_DEATH(
80 {
81 try
82 {
83 try
84 {
85 DimensionlessQuantity dimensionlessQuantity("Not a number");
86 }
87 catch (const std::exception& except)
88 {
90 error.programName = "CPP Units";
91 error.message = "Exception message: " + std::string {except.what()};
92
94 }
95 }
96 catch (const CppUtils::Errors::FatalException& except)
97 {
99 }
100 },
101 deathRegex.str());
102}
103
104#endif
constexpr long double getMagnitude() const noexcept
Definition physicalQuantity.hpp:50
constexpr void setMagnitude(long double Magnitude) noexcept
Definition physicalQuantity.hpp:51
Definition fatalException.hpp:28
void handleErrorWithMessage() const
Delegate our exception handling to the error handling classes.
Definition fatalException.hpp:49
Definition physicalQuantities.hpp:94
Definition basicMath.hpp:17
PhysicalQuantity< Dimensionality<> > DimensionlessQuantity
Definition physicalQuantities.hpp:22
PhysicalQuantity< Dimensionality< 1, 0, 0 > > Length
Definition physicalQuantities.hpp:37
Definition dimensionality.hpp:27
Definition errorTypes.hpp:32
std::string programName
The name of the program where the exception originated.
Definition errorTypes.hpp:33
std::string message
The main error message describing the error.
Definition errorTypes.hpp:34
GTEST_TEST(testPhysicalQuantity, aPhysicalQuantityKnowsItsDimensionality)
Definition testPhysicalQuantity.hpp:24