OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
TestHelpers.hpp
1//
2// Created by jstark on 28.05.21.
3//
4
5#ifndef OPENFPM_PDATA_TESTHELPERS_HPP
6#define OPENFPM_PDATA_TESTHELPERS_HPP
7
8// modified, based on https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
9template <typename T>
10bool AlmostEqualRelativeAndAbs(T A, T B,
11 T maxDiff, T maxRelDiff = std::numeric_limits<T>::epsilon())
12{
13 // Check if the numbers are really close -- needed
14 // when comparing numbers near zero.
15 T diff = fabs(A - B);
16 if (diff <= maxDiff)
17 return true;
18
19 A = fabs(A);
20 B = fabs(B);
21 T largest = (B > A) ? B : A;
22
23 if (diff <= largest * maxRelDiff)
24 return true;
25 return false;
26}
27
28
29template <size_t Field, typename grid_type_1, typename grid_type_2, std::string Field_type>
30bool are_equal_grid(grid_type_1 & grid_1, grid_type_2 & grid_2)
31{
32 bool equal = true;
33 assert(grid_type_1::dims == grid_type_2::dims);
34 assert(grid_1.size() == grid_2.size());
35
36 for (int d = 0; d < grid_dest_type::dims; ++d)
37 {
38 auto dom_1 = grid_1.getDomainIterator();
39 auto dom_2 = grid_2.getDomainIterator();
40 while (dom_sc.isNext())
41 {
42 auto key_1 = dom_1.get();
43 auto key_2 = dom_2.get();
44
45 auto v1 = grid_1.template get<attr_ds>(key_1)[d];
46 auto v2 = grid_2.template get<attr_sc>(key_2)[d];
47
48 if (!AlmostEqualRelativeAndAbs(v1, v2, std::numeric_limits<Field_type>::epsilon()))
49 {
50 equal = false;
51 break;
52 }
53
54 ++dom_1;
55 ++dom_2;
56 }
57 }
58 return equal;
59}
60
61
62
63
64
65#endif //OPENFPM_PDATA_TESTHELPERS_HPP