Fixed the complex
MODIFY
test/CMakeLists.txt
test/CMakeLists.txt
@@ -62,8 +62,8 @@ add_rocthrust_test("thrust.hip.binary_search" test_binary_search.cpp)
add_rocthrust_test("thrust.hip.binary_search_descending" test_binary_search_descending.cpp)
add_rocthrust_test("thrust.hip.binary_search_vector" test_binary_search_vector.cpp)
add_rocthrust_test("thrust.hip.binary_search_vector_descending" test_binary_search_vector_descending.cpp)
-#add_rocthrust_test("thrust.hip.complex" test_complex.cpp)
-#add_rocthrust_test("thrust.hip.complex_transform" test_complex_transform.cpp)
+add_rocthrust_test("thrust.hip.complex" test_complex.cpp)
+add_rocthrust_test("thrust.hip.complex_transform" test_complex_transform.cpp)
add_rocthrust_test("thrust.hip.constant_iterator" test_constant_iterator.cpp)
add_rocthrust_test("thrust.hip.copy" test_copy.cpp)
add_rocthrust_test("thrust.hip.copy_n" test_copy_n.cpp)

MODIFY
test/test_assertions.hpp
test/test_assertions.hpp
// Google Test
#include <gtest/gtest.h>
-// TODO: Fix the complex
-//#include <thrust/complex.h>
+#include <thrust/complex.h>
+#include <thrust/host_vector.h>
+
+#include <cmath>
+#include <cfloat>
template <typename T1, typename T2>
testing::AssertionResult CmpHelperEQQuite(const char* lhs_expression,
@@ -42,26 +45,99 @@ testing::AssertionResult CmpHelperEQQuite(const char* lhs_expression,
ASSERT_PRED_FORMAT2(CmpHelperEQQuite, \
val1, val2)
-// TODO: Fix the complex
-/*template <typename T>
-testing::AssertionResult ComplexNearPredFormat(const char* expr1,
+
+
+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){
- const thrust::complex<double> diff(fabs((double)val1.real() - (double)val2.real())
- ,fabs((double)val1.imag() - (double)val2.imag()));
- if (diff.real() <= abs_error.real() &&
- diff.imag() <= abs_error.imag())
+
+ 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 " << abs_error << ".";
+ << 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)\
@@ -71,4 +147,11 @@ testing::AssertionResult ComplexNearPredFormat(const char* expr1,
#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()))

MODIFY
test/test_complex.cpp
test/test_complex.cpp
@@ -27,12 +27,13 @@ TYPED_TEST(ComplexTests, TestComplexConstructors)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ T(-1000),
+ T(1000));
thrust::complex<T> a(data[0],data[1]);
thrust::complex<T> b(a);
+ thrust::complex<float> float_b(a);
a = thrust::complex<T>(data[0],data[1]);
ASSERT_NEAR_COMPLEX(a, b);
@@ -43,17 +44,16 @@ TYPED_TEST(ComplexTests, TestComplexConstructors)
a = thrust::complex<T>();
ASSERT_NEAR_COMPLEX(a,std::complex<T>(0));
- a = thrust::complex<T>(thrust::complex<float>(data[0],data[1]));
- ASSERT_NEAR_COMPLEX(a, b);
+ a = thrust::complex<T>(thrust::complex<float>(static_cast<float>(data[0]),static_cast<float>(data[1])));
+ ASSERT_NEAR_COMPLEX(a, float_b);
- a = thrust::complex<T>(thrust::complex<double>(data[0],data[1]));
+ a = thrust::complex<T>(thrust::complex<double>(static_cast<double>(data[0]),static_cast<double>(data[1])));
ASSERT_NEAR_COMPLEX(a, b);
- a = thrust::complex<T>(std::complex<float>(data[0],data[1]));
- ASSERT_NEAR_COMPLEX(a, b);
+ a = thrust::complex<T>(std::complex<float>(static_cast<float>(data[0]),static_cast<float>(data[1])));
+ ASSERT_NEAR_COMPLEX(a, float_b);
- a = thrust::complex<T>(std::complex<double>(data[0],data[1]));
- ASSERT_NEAR_COMPLEX(a, b);
+ a = thrust::complex<T>(std::complex<double>(static_cast<double>(data[0]),static_cast<double>(data[1])));
ASSERT_NEAR_COMPLEX(a, b);
}
@@ -91,12 +91,12 @@ TYPED_TEST(ComplexTests, TestComplexMemberOperators)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data_a = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ 10000,
+ 10000);
thrust::host_vector<T> data_b = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ 10000,
+ 10000);
thrust::complex<T> a(data_a[0], data_a[1]);
thrust::complex<T> b(data_b[0], data_b[1]);
@@ -130,24 +130,23 @@ TYPED_TEST(ComplexTests, TestComplexBasicArithmetic)
thrust::host_vector<T> data = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ T(-100),
+ T(100));
thrust::complex<T> a(data[0], data[1]);
std::complex<T> b(a);
// Test the basic arithmetic functions against std
- ASSERT_NEAR(abs(a),abs(b),std::numeric_limits<T>::epsilon());
+ ASSERT_NEAR(abs(a), abs(b), T(0.01));
- ASSERT_NEAR(arg(a),arg(b),std::numeric_limits<T>::epsilon());
+ ASSERT_NEAR(arg(a), arg(b), T(0.01));
- ASSERT_NEAR(norm(a),norm(b),std::numeric_limits<T>::epsilon());
+ ASSERT_NEAR(norm(a), norm(b), T(0.01));
ASSERT_EQ(conj(a),conj(b));
- // TODO: Find a good assert for this.
- //ASSERT_NEAR(thrust::polar(data[0],data[1]),std::polar(data[0],data[1]));
+ ASSERT_NEAR_COMPLEX(thrust::polar(data[0],data[1]),std::polar(data[0],data[1]));
// random_samples does not seem to produce infinities so proj(z) == z
ASSERT_EQ(proj(a),a);
@@ -158,31 +157,31 @@ TYPED_TEST(ComplexTests, TestComplexBinaryArithmetic)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data_a = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ -10000,
+ 10000);
thrust::host_vector<T> data_b = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ -10000,
+ 10000);
thrust::complex<T> a(data_a[0], data_a[1]);
thrust::complex<T> b(data_b[0], data_b[1]);
ASSERT_NEAR_COMPLEX(a * b,std::complex<T>(a) * std::complex<T>(b));
ASSERT_NEAR_COMPLEX(a * data_b[0],std::complex<T>(a) * data_b[0]);
- ASSERT_NEAR_COMPLEX(data_a[0]*b,data_b[0] * std::complex<T>(b));
+ ASSERT_NEAR_COMPLEX(data_a[0]*b,data_a[0] * std::complex<T>(b));
ASSERT_NEAR_COMPLEX(a / b, std::complex<T>(a) / std::complex<T>(b));
ASSERT_NEAR_COMPLEX(a / data_b[0], std::complex<T>(a) / data_b[0]);
- ASSERT_NEAR_COMPLEX(data_a[0] / b, data_b[0] / std::complex<T>(b));
+ ASSERT_NEAR_COMPLEX(data_a[0] / b, data_a[0] / std::complex<T>(b));
ASSERT_EQ(a + b, std::complex<T>(a) + std::complex<T>(b));
ASSERT_EQ(a + data_b[0], std::complex<T>(a) + data_b[0]);
- ASSERT_EQ(data_a[0] + b, data_b[0] + std::complex<T>(b));
+ ASSERT_EQ(data_a[0] + b, data_a[0] + std::complex<T>(b));
ASSERT_EQ(a - b, std::complex<T>(a) - std::complex<T>(b));
ASSERT_EQ(a - data_b[0], std::complex<T>(a) - data_b[0]);
- ASSERT_EQ(data_a[0] - b, data_b[0] - std::complex<T>(b));
+ ASSERT_EQ(data_a[0] - b, data_a[0] - std::complex<T>(b));
}
TYPED_TEST(ComplexTests, TestComplexUnaryArithmetic)
@@ -204,8 +203,8 @@ TYPED_TEST(ComplexTests, TestComplexExponentialFunctions)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data_a = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ -100,
+ 100);
thrust::complex<T> a(data_a[0], data_a[1]);
std::complex<T> b(a);
@@ -220,12 +219,12 @@ TYPED_TEST(ComplexTests, TestComplexPowerFunctions)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data_a = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ -100,
+ 100);
thrust::host_vector<T> data_b = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ -100,
+ 100);
thrust::complex<T> a(data_a[0], data_a[1]);
thrust::complex<T> b(data_b[0], data_b[1]);
@@ -244,8 +243,8 @@ TYPED_TEST(ComplexTests, TestComplexTrigonometricFunctions)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data_a = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ T(-1),
+ T(1));
thrust::complex<T> a(data_a[0], data_a[1]);
std::complex<T> c(a);
@@ -270,8 +269,8 @@ TYPED_TEST(ComplexTests, TestComplexStreamOperators)
using T = typename TestFixture::input_type;
thrust::host_vector<T> data_a = get_random_data<T>(2,
- std::numeric_limits<T>::min(),
- std::numeric_limits<T>::max());
+ T(-1000),
+ T(1000));
thrust::complex<T> a(data_a[0], data_a[1]);
std::stringstream out;

MODIFY
test/test_complex_transform.cpp
test/test_complex_transform.cpp
@@ -263,7 +263,7 @@ TYPED_TEST(ComplexTransformTests, TestComplexArithmeticTransform)
thrust::transform(h_p1.begin(), h_p1.end(), h_p2.begin(), h_result.begin(), basic_arithmetic_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_p2.begin(), d_result.begin(), basic_arithmetic_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
}
}
@@ -283,7 +283,7 @@ TYPED_TEST(ComplexTransformTests, TestComplexPlaneTransform)
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), complex_plane_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), complex_plane_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
}
}
@@ -306,13 +306,11 @@ TYPED_TEST(ComplexTransformTests, TestComplexPowerTransform)
thrust::transform(h_p1.begin(), h_p1.end(), h_p2.begin(), h_result.begin(), pow_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_p2.begin(), d_result.begin(), pow_functor());
- // pow can be very innacurate there's no point trying to check for equality
- // Currently just checking for compilation
- // ASSERT_NEAR(h_result, d_result,epsilonComplex<T>());
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), sqrt_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), sqrt_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
}
}
@@ -332,15 +330,15 @@ TYPED_TEST(ComplexTransformTests, TestComplexExponentialTransform)
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), exp_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), exp_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), log_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), log_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), log10_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), log10_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
}
}
@@ -361,54 +359,54 @@ TYPED_TEST(ComplexTransformTests, TestComplexTrigonometricTransform)
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), sin_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), sin_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), cos_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), cos_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), tan_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), tan_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), sinh_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), sinh_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), cosh_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), cosh_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), tanh_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), tanh_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), asin_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), asin_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), acos_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), acos_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), atan_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), atan_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), asinh_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), asinh_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), acosh_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), acosh_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), atanh_functor());
thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), atanh_functor());
- ASSERT_NEAR_COMPLEX(h_result, d_result);
+ ASSERT_NEAR_COMPLEX_VECTOR(h_result, d_result);
}
}

MODIFY
testing/CMakeLists.txt
testing/CMakeLists.txt
@@ -64,9 +64,9 @@ add_thrust_test("binary_search")
add_thrust_test("binary_search_descending")
add_thrust_test("binary_search_vector")
add_thrust_test("binary_search_vector_descending")
-# add_thrust_test("complex")
-# add_thrust_test("complex_transform")
-# add_thrust_test("constant_iterator")
+add_thrust_test("complex")
+add_thrust_test("complex_transform")
+add_thrust_test("constant_iterator")
add_thrust_test("copy")
add_thrust_test("copy_n")
add_thrust_test("count")

Fixed the complex