diff --git a/test/main.h b/test/main.h index 59a777312..52c27117b 100644 --- a/test/main.h +++ b/test/main.h @@ -892,6 +892,14 @@ std::string type_name() { return type_name(T()); } +template +void setRandomDataInRange(DataContainer& data_container, typename DataContainer::Scalar min_value, + typename DataContainer::Scalar max_value) { + for (Eigen::Index i = 0; i < data_container.size(); ++i) { + data_container.data()[i] = Eigen::internal::random(min_value, max_value); + } +} + using namespace Eigen; /** diff --git a/unsupported/test/cxx11_tensor_assign.cpp b/unsupported/test/cxx11_tensor_assign.cpp index 47b6361d9..29d73c4cb 100644 --- a/unsupported/test/cxx11_tensor_assign.cpp +++ b/unsupported/test/cxx11_tensor_assign.cpp @@ -250,8 +250,13 @@ static void test_auto_resize() { static void test_compound_assign() { Tensor start_tensor(10); Tensor offset_tensor(10); - start_tensor.setRandom(); - offset_tensor.setRandom(); + setRandomDataInRange(start_tensor, -1000, 1000); + setRandomDataInRange(offset_tensor, -1000, 1000); + + // Avoid division by zero in the final compound assignment check. + for (int i = 0; i < 10; ++i) { + if (offset_tensor(i) == 0) offset_tensor(i) = 1; + } Tensor tensor = start_tensor; tensor += offset_tensor; diff --git a/unsupported/test/cxx11_tensor_block_eval.cpp b/unsupported/test/cxx11_tensor_block_eval.cpp index 0567b3c61..9281224a8 100644 --- a/unsupported/test/cxx11_tensor_block_eval.cpp +++ b/unsupported/test/cxx11_tensor_block_eval.cpp @@ -13,6 +13,16 @@ using Eigen::internal::TensorBlockDescriptor; using Eigen::internal::TensorExecutor; +template +static void setRandomForBinaryProduct(Tensor& tensor) { + EIGEN_IF_CONSTEXPR((std::is_integral::value && !std::is_same::value)) { + setRandomDataInRange(tensor, T(-1000), T(1000)); + } + else { + tensor.setRandom(); + } +} + // -------------------------------------------------------------------------- // // Utility functions to generate random tensors, blocks, and evaluate them. @@ -220,8 +230,8 @@ template static void test_eval_tensor_binary_expr_block() { DSizes dims = RandomDims(10, 20); Tensor lhs(dims), rhs(dims); - lhs.setRandom(); - rhs.setRandom(); + setRandomForBinaryProduct(lhs); + setRandomForBinaryProduct(rhs); VerifyBlockEvaluator(lhs * rhs, [&dims]() { return RandomBlock(dims, 1, 10); }); } diff --git a/unsupported/test/cxx11_tensor_intdiv.cpp b/unsupported/test/cxx11_tensor_intdiv.cpp index 3d818e077..358f98492 100644 --- a/unsupported/test/cxx11_tensor_intdiv.cpp +++ b/unsupported/test/cxx11_tensor_intdiv.cpp @@ -81,11 +81,15 @@ void test_unsigned_64bit() { } void test_powers_32bit() { + const int32_t int32_max = (std::numeric_limits::max)(); for (int expon = 1; expon < 31; expon++) { int32_t div = (1 << expon); for (int num_expon = 0; num_expon < 32; num_expon++) { - int32_t start_num = (1 << num_expon) - 100; - int32_t end_num = (1 << num_expon) + 100; + const int64_t pivot = int64_t(1) << num_expon; + int32_t start_num = pivot > int32_max ? int32_max : static_cast(pivot); + int32_t end_num = start_num; + start_num = (std::max)(int32_t(0), start_num - 100); + end_num = static_cast((std::min)(int64_t(int32_max), int64_t(end_num) + 100)); if (start_num < 0) start_num = 0; for (int32_t num = start_num; num < end_num; num++) { Eigen::internal::TensorIntDivisor divider = Eigen::internal::TensorIntDivisor(div); diff --git a/unsupported/test/cxx11_tensor_reduction.cpp b/unsupported/test/cxx11_tensor_reduction.cpp index 81dc0ce53..d28019991 100644 --- a/unsupported/test/cxx11_tensor_reduction.cpp +++ b/unsupported/test/cxx11_tensor_reduction.cpp @@ -335,7 +335,7 @@ static void test_tensor_maps() { TensorMap> tensor_map_const(inputs, 2, 3, 5, 7); const TensorMap> tensor_map_const_const(inputs, 2, 3, 5, 7); - tensor_map.setRandom(); + setRandomDataInRange(tensor_map, -1000, 1000); array reduction_axis; reduction_axis[0] = 1; reduction_axis[1] = 3; diff --git a/unsupported/test/cxx11_tensor_ref.cpp b/unsupported/test/cxx11_tensor_ref.cpp index 7696558c8..d8f99c16c 100644 --- a/unsupported/test/cxx11_tensor_ref.cpp +++ b/unsupported/test/cxx11_tensor_ref.cpp @@ -45,9 +45,9 @@ static void test_simple_lvalue_ref() { static void test_simple_rvalue_ref() { Tensor input1(6); - input1.setRandom(); + setRandomDataInRange(input1, -1000, 1000); Tensor input2(6); - input2.setRandom(); + setRandomDataInRange(input2, -1000, 1000); TensorRef> ref3(input1 + input2); TensorRef> ref4 = input1 + input2; @@ -110,7 +110,7 @@ static void test_slice() { static void test_ref_of_trace() { Tensor input(6, 6); - input.setRandom(); + setRandomDataInRange(input, -1000, 1000); int trace = 0; for (int i = 0; i < 6; ++i) { trace += input(i, i); diff --git a/unsupported/test/cxx11_tensor_scan.cpp b/unsupported/test/cxx11_tensor_scan.cpp index 30730f25e..8f0b3a243 100644 --- a/unsupported/test/cxx11_tensor_scan.cpp +++ b/unsupported/test/cxx11_tensor_scan.cpp @@ -85,7 +85,7 @@ template static void test_tensor_maps() { int inputs[20]; TensorMap > tensor_map(inputs, 20); - tensor_map.setRandom(); + setRandomDataInRange(tensor_map, -1000, 1000); Tensor result = tensor_map.cumsum(0);