1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* Copyright 2008-2013 NVIDIA Corporation
* Modifications Copyright© 2019 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <thrust/complex.h>
#include <thrust/host_vector.h>
#include <gtest/gtest.h>
#include <cfloat>
#include <cmath>
template <typename T1, typename T2>
testing::AssertionResult CmpHelperEQQuite(const char* lhs_expression,
const char* rhs_expression,
const T1& lhs,
const T2& rhs)
{
if(lhs == rhs)
{
return testing::AssertionSuccess();
}
testing::Message msg;
msg << "Expressions during equality check:";
msg << "\n " << lhs_expression;
msg << "\n " << rhs_expression;
return testing::AssertionFailure() << msg;
}
#define ASSERT_EQ_QUIET(val1, val2) ASSERT_PRED_FORMAT2(CmpHelperEQQuite, val1, val2)
template <typename T>
testing::AssertionResult ComplexCompare(const char* expr1,
const char* expr2,
const char* abs_error_expr,
thrust::complex<T> val1,
thrust::complex<T> val2,
thrust::complex<double> abs_error)
{
double real_diff;
if(std::isinf(val1.real()))
{
real_diff = std::isinf(val2.real()) ? 0.0 : std::numeric_limits<double>::infinity();
}
else if(std::isnan(val1.real()))
{
real_diff = std::isnan(val2.real()) ? 0.0 : std::numeric_limits<double>::infinity();
}
else
{
real_diff = fabs((double)val1.real() - (double)val2.real());
}
double imag_diff;
if(std::isinf(val1.imag()))
{
imag_diff = std::isinf(val2.imag()) ? 0.0 : std::numeric_limits<double>::infinity();
}
else if(std::isnan(val1.imag()))
{
imag_diff = std::isnan(val2.imag()) ? 0.0 : std::numeric_limits<double>::infinity();
}
else
{
imag_diff = fabs((double)val1.imag() - (double)val2.imag());
}
if(real_diff == 0 && imag_diff == 0)
return testing::AssertionSuccess();
const thrust::complex<double> diff(real_diff, imag_diff);
const thrust::complex<double> tol_diff(
0.1 * (fabs(val1.real() + val2.real()) + abs_error.real()),
0.1 * (fabs(val1.imag() + val2.imag()) + abs_error.imag()));
if((diff.real() != 0 && diff.real() > tol_diff.real())
|| (diff.imag() != 0 && diff.imag() > tol_diff.imag()))
return testing::AssertionFailure()
<< "The difference between " << expr1 << " and " << expr2 << " is " << diff
<< ", which exceeds " << abs_error_expr << ", where\n"
<< expr1 << " evaluates to " << val1 << ",\n"
<< expr2 << " evaluates to " << val2 << ", and\n"
<< abs_error_expr << " evaluates to " << tol_diff << ".";
else
return testing::AssertionSuccess();
}
template <typename T>
testing::AssertionResult ComplexNearPredFormat(const char* expr1,
const char* expr2,
const char* abs_error_expr,
thrust::complex<T> val1,
thrust::complex<T> val2,
thrust::complex<double> abs_error)
{
return ComplexCompare(expr1, expr2, abs_error_expr, val1, val2, abs_error);
}
template <typename T>
testing::AssertionResult ComplexVectorNearPredFormat(const char* expr1,
const char* expr2,
const char* abs_error_expr,
thrust::host_vector<T> val1,
thrust::device_vector<T> val2,
thrust::complex<double> abs_error)
{
thrust::host_vector<T> vector1(val1);
thrust::host_vector<T> vector2(val2);
if(vector1.size() != vector2.size())
{
return testing::AssertionFailure()
<< "The difference between " << expr1 << " and " << expr2
<< " are the sizes: " << vector1.size() << ", " << vector2.size() << ".";
}
for(unsigned int i = 0; i < vector1.size(); i++)
{
testing::AssertionResult result
= ComplexCompare(expr1, expr2, abs_error_expr, vector1[i], vector2[i], abs_error);
if(testing::AssertionSuccess() != result)
return result;
}
return testing::AssertionSuccess();
}
#define ASSERT_NEAR_COMPLEX_ERROR(val1, val2, abs_error) \
ASSERT_PRED_FORMAT3( \
ComplexNearPredFormat<typename decltype(val1)::value_type>, val1, val2, abs_error)
#define ASSERT_NEAR_COMPLEX(val1, val2) \
ASSERT_NEAR_COMPLEX_ERROR( \
val1, \
val2, \
thrust::complex<T>(std::numeric_limits<T>::epsilon(), std::numeric_limits<T>::epsilon()))
#define ASSERT_NEAR_COMPLEX_VECTOR_ERROR(val1, val2, abs_error) \
ASSERT_PRED_FORMAT3( \
ComplexVectorNearPredFormat<typename decltype(val1)::value_type>, val1, val2, abs_error)
#define ASSERT_NEAR_COMPLEX_VECTOR(val1, val2) \
ASSERT_NEAR_COMPLEX_VECTOR_ERROR( \
val1, \
val2, \
thrust::complex<T>(std::numeric_limits<T>::epsilon(), std::numeric_limits<T>::epsilon()))
|