STORMM Source Documentation
Loading...
Searching...
No Matches
approx.h
1// -*-c++-*-
2#ifndef STORMM_APPROX_H
3#define STORMM_APPROX_H
4
5#include <cmath>
6#include <string>
7#include <vector>
8#include "copyright.h"
9#include "Constants/scaling.h"
10#include "DataTypes/common_types.h"
11#include "DataTypes/stormm_vector_types.h"
12#include "Reporting/error_format.h"
13#include "unit_test_enumerators.h"
14
15namespace stormm {
16namespace testing {
17
18using data_types::isScalarType;
19
23class Approx {
24public:
25
32 template <typename T> Approx(const std::vector<T> &values_in,
33 ComparisonType style_in = ComparisonType::ABSOLUTE,
34 double tol_in = 1.0e-6);
35
46 Approx(double value_in, ComparisonType style_in = ComparisonType::ABSOLUTE,
47 double tol_in = 1.0e-6);
48 Approx(double value_in, double tol_in, ComparisonType style_in = ComparisonType::ABSOLUTE);
50
52 ~Approx() = default;
53
55 int size() const;
56
58 double getValue() const;
59
61 std::vector<double> getValues() const;
62
64 ComparisonType getStyle() const;
65
68 double getMargin() const;
69 double getTolerance() const;
70 double getTol() const;
72
86 void setValue(double value_in);
87 void setValues(const std::vector<double> &values_in);
88 void setValue(double value_in, size_t index);
90
98 void setMargin(double dtol_in);
99 void setTolerance(double dtol_in);
100 void setTol(double dtol_in);
102
113 Approx margin(double dtol_in) const;
114 Approx tolerance(double dtol_in) const;
115 Approx tol(double dtol_in) const;
117
121 bool test(const double test_value) const;
122
127 template <typename T> bool test(const std::vector<T> &test_values) const;
128
129private:
130 std::vector<double> values;
131 ComparisonType style;
132 double dtol;
133};
134
140template <typename T> bool verifyVectorApproxCompatibility(const std::vector<T> &test_values,
141 const Approx &cr);
142
149bool operator==(const double d, const Approx &cr);
150bool operator==(const Approx &cr, const double d);
151bool operator!=(const double d, const Approx &cr);
152bool operator!=(const Approx &cr, const double d);
153bool operator>(const double d, const Approx &cr);
154bool operator>(const Approx &cr, const double d);
155bool operator<(const double d, const Approx &cr);
156bool operator<(const Approx &cr, const double d);
157bool operator>=(const double d, const Approx &cr);
158bool operator>=(const Approx &cr, const double d);
159bool operator<=(const double d, const Approx &cr);
160bool operator<=(const Approx &cr, const double d);
162
168template <typename T> bool operator==(const std::vector<T> &tvec, const Approx &cr);
169template <typename T> bool operator==(const Approx &cr, const std::vector<T> &tvec);
170template <typename T> bool operator!=(const std::vector<T> &tvec, const Approx &cr);
171template <typename T> bool operator!=(const Approx &cr, const std::vector<T> &tvec);
172template <typename T> bool operator>(const std::vector<T> &tvec, const Approx &cr);
173template <typename T> bool operator>(const Approx &cr, const std::vector<T> &tvec);
174template <typename T> bool operator<(const std::vector<T> &tvec, const Approx &cr);
175template <typename T> bool operator<(const Approx &cr, const std::vector<T> &tvec);
176template <typename T> bool operator>=(const std::vector<T> &tvec, const Approx &cr);
177template <typename T> bool operator>=(const Approx &cr, const std::vector<T> &tvec);
178template <typename T> bool operator<=(const std::vector<T> &tvec, const Approx &cr);
179template <typename T> bool operator<=(const Approx &cr, const std::vector<T> &tvec);
181
182
183} // namespace testing
184} // namespace stormm
185
186#include "approx.tpp"
187
188#endif
Approx(const std::vector< T > &values_in, ComparisonType style_in=ComparisonType::ABSOLUTE, double tol_in=1.0e-6)
Constructors for real number comparisons based on a vector of multiple input values....
Class for handling comparisons of floating-point results versus expected values in unit tests....
Definition approx.h:23
Approx margin(double dtol_in) const
Set the tolerance. This is written in such a way as to mimic the Catch2 unit testing framework in som...
Definition approx.cpp:97
void setValue(double value_in)
Set the values that form the basis of the approximate comparison.
Definition approx.cpp:61
bool test(const double test_value) const
Test whether a real-valued scalar is the same as the value held for comparison.
Definition approx.cpp:112
~Approx()=default
Default destructor.
int size() const
Get the size of the collection of numbers in a real number comparison.
Definition approx.cpp:19
double getMargin() const
Get the margin or tolerance associated with this approximate comparison.
Definition approx.cpp:46
Approx(const std::vector< T > &values_in, ComparisonType style_in=ComparisonType::ABSOLUTE, double tol_in=1.0e-6)
Constructors for real number comparisons based on a vector of multiple input values....
double getValue() const
Get the expectation value, sans any tolerance.
Definition approx.cpp:24
bool test(const std::vector< T > &test_values) const
Test whether a vector of real-valued scalars is the same as as vector held for comparison.
void setMargin(double dtol_in)
Set the tolerance of the existing object. This will not emit a new object.
Definition approx.cpp:82
ComparisonType getStyle() const
Get the style used in this approximate comparison.
Definition approx.cpp:41
std::vector< double > getValues() const
Get all values of an approximate comparison, sans any tolerance.
Definition approx.cpp:36