TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gtest_tools.hpp
Go to the documentation of this file.
1// Copyright (c) 2019--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#ifndef NDA_DEBUG
14#define NDA_DEBUG
15#endif
16
17#include "./nda.hpp"
18
19#include <gtest/gtest.h>
20
21#include <cstdlib>
22#include <iostream>
23#include <sstream>
24
29
41template <typename X, typename Y>
42::testing::AssertionResult complex_are_close(X const &x, Y const &y, double precision = 1.e-10) {
43 using std::abs;
44 if (abs(x - y) < precision)
45 return ::testing::AssertionSuccess();
46 else
47 return ::testing::AssertionFailure() << "abs(x - y) = " << abs(x - y) << "\n x = " << x << "\n y = " << y;
48}
49
51#define EXPECT_COMPLEX_NEAR(X, ...) EXPECT_TRUE(complex_are_close(X, __VA_ARGS__))
52
63template <typename X, typename Y>
64::testing::AssertionResult array_are_equal(X const &x, Y const &y) {
65 if (x.shape() != y.shape())
66 return ::testing::AssertionFailure() << "Comparing two arrays of different size "
67 << "\n X = " << x << "\n Y = " << y;
68 if (x == y)
69 return ::testing::AssertionSuccess();
70 else
71 return ::testing::AssertionFailure() << "Arrays have different elements\n X = " << x << "\n Y = " << y;
72}
73
75#define EXPECT_EQ_ARRAY(X, Y) EXPECT_TRUE(array_are_equal(X, Y));
76
78#define EXPECT_ARRAY_EQ(X, Y) EXPECT_TRUE(array_are_equal(X, Y));
79
92template <typename X, typename Y>
93::testing::AssertionResult array_are_close(X const &x, Y const &y, double precision = 1.e-10) {
96
97 // check their shapes
98 if (x_reg.shape() != y_reg.shape())
99 return ::testing::AssertionFailure() << "Comparing two arrays of different size "
100 << "\n X = " << x_reg << "\n Y = " << y_reg;
101
102 // empty arrays are considered equal
103 if (x_reg.size() == 0) return ::testing::AssertionSuccess();
104
105 // check their difference
106 const auto maxdiff = max_element(abs(make_regular(x_reg - y_reg)));
107 if (maxdiff < precision)
108 return ::testing::AssertionSuccess();
109 else
110 return ::testing::AssertionFailure() << "max_element(abs(X - Y)) = " << maxdiff << "\n X = " << x_reg << "\n Y = " << y_reg;
111}
112
114#define EXPECT_ARRAY_NEAR(X, ...) EXPECT_TRUE(array_are_close(X, __VA_ARGS__))
115
133template <nda::MemoryArray X, nda::MemoryArray Y>
134::testing::AssertionResult array_are_rel_close(X const &x, Y const &y, double precision = 1.e-10) {
135 // check their shapes
136 if (x.shape() != y.shape())
137 return ::testing::AssertionFailure() << "Comparing two arrays of different size "
138 << "\n X = " << x << "\n Y = " << y;
139
140 // empty arrays are considered equal
141 if (x.size() == 0) return ::testing::AssertionSuccess();
142
143 // find the largest elementwise relative difference |x - y| / max(|x|, |y|)
144 double maxreldiff = 0.0;
145 nda::for_each(x.shape(), [&](auto... idx) {
146 using std::abs, std::max;
147 auto const scale = max(abs(x(idx...)), abs(y(idx...)));
148 if (scale != 0) maxreldiff = max(maxreldiff, abs(x(idx...) - y(idx...)) / scale);
149 });
150
151 if (maxreldiff < precision)
152 return ::testing::AssertionSuccess();
153 else
154 return ::testing::AssertionFailure() << "max(abs(X - Y) / max(abs(X), abs(Y))) = " << maxreldiff << "\n X = " << x << "\n Y = " << y;
155}
156
158#define EXPECT_ARRAY_REL_NEAR(X, ...) EXPECT_TRUE(array_are_rel_close(X, __VA_ARGS__))
159
168template <typename X>
169::testing::AssertionResult array_almost_zero(X const &x) {
171
172 constexpr double eps = 1.e-10;
173 const auto max = max_element(abs(x_reg));
174 if (x_reg.size() == 0 || max < eps)
175 return ::testing::AssertionSuccess();
176 else
177 return ::testing::AssertionFailure() << "max_element(abs(X)) = " << max << "\n X = " << x_reg;
178}
179
181#define EXPECT_ARRAY_ZERO(X) EXPECT_TRUE(array_almost_zero(X))
182
193template <typename X, typename Y>
194::testing::AssertionResult generic_are_near(X const &x, Y const &y) {
195 double precision = 1.e-12;
196 using std::abs;
197 if (abs(x - y) > precision)
198 return ::testing::AssertionFailure() << "X = " << x << " and Y = " << y << " are different. \n Difference is: " << abs(x - y);
199 return ::testing::AssertionSuccess();
200}
201
203#define EXPECT_CLOSE(X, Y) EXPECT_TRUE(generic_are_near(X, Y));
204
auto const & shape() const noexcept
Get the shape of the view/array.
long size() const noexcept
Get the total size of the view/array.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:147
__inline__ void for_each(std::array< Int, R > const &shape, F &&f)
Loop over all possible index values of a given shape and apply a function to them.
Definition for_each.hpp:116
::testing::AssertionResult array_almost_zero(X const &x)
Check that an array/view is close to zero, i.e. that its largest absolute element is less than 1e-10.
::testing::AssertionResult generic_are_near(X const &x, Y const &y)
Check that that two generic objects are close, i.e. that their absolute difference is less than 1e-12...
::testing::AssertionResult array_are_rel_close(X const &x, Y const &y, double precision=1.e-10)
Check that two arrays/views are close in a relative sense, i.e. that they have the same shape and tha...
::testing::AssertionResult array_are_close(X const &x, Y const &y, double precision=1.e-10)
Check that two arrays/views are close, i.e. that they have the same shape and that the largest elemen...
::testing::AssertionResult array_are_equal(X const &x, Y const &y)
Check that two arrays/views are equal, i.e. that they have the same shape and the same elements.
::testing::AssertionResult complex_are_close(X const &x, Y const &y, double precision=1.e-10)
Check the absolute difference of two (complex) numbers.
Includes all relevant headers for the core nda library.