Skip to content

Commit

Permalink
Fix value range overflows in tests
Browse files Browse the repository at this point in the history
`random_integers<fp-type>()` may generate values not representable as an integer type,
which results in undefined behavior when we attempt to cast them to integer type later on.

To avoid that, generate random numbers using the integer type we'll cast to later on,
so we're guaranteed to have representable values.
  • Loading branch information
Artem-B committed Dec 3, 2024
1 parent 642976c commit d69788c
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions thrust/testing/for_each.cu
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void TestForEach(const size_t n)
{
const size_t output_size = std::min((size_t) 10, 2 * n);

thrust::host_vector<T> h_input = unittest::random_integers<T>(n);
thrust::host_vector<T> h_input = unittest::random_integers<size_t>(n);

for (size_t i = 0; i < n; i++)
{
Expand Down Expand Up @@ -199,7 +199,7 @@ void TestForEachN(const size_t n)
{
const size_t output_size = std::min((size_t) 10, 2 * n);

thrust::host_vector<T> h_input = unittest::random_integers<T>(n);
thrust::host_vector<T> h_input = unittest::random_integers<size_t>(n);

for (size_t i = 0; i < n; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_difference.cu
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetDifferenceEquivalentRanges);
template <typename T>
void TestSetDifferenceMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_difference_by_key.cu
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetDifferenceByKeyEquivalentRanges);
template <typename T>
void TestSetDifferenceByKeyMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_intersection.cu
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetIntersectionEquivalentRanges);
template <typename T>
void TestSetIntersectionMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_intersection_by_key.cu
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetIntersectionByKeyEquivalentRanges);
template <typename T>
void TestSetIntersectionByKeyMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_symmetric_difference.cu
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetSymmetricDifferenceEquivalentRanges);
template <typename T>
void TestSetSymmetricDifferenceMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_symmetric_difference_by_key.cu
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetSymmetricDifferenceByKeyEquivalentRanges);
template <typename T>
void TestSetSymmetricDifferenceByKeyMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion thrust/testing/set_union_by_key.cu
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ DECLARE_VARIABLE_UNITTEST(TestSetUnionByKeyEquivalentRanges);
template <typename T>
void TestSetUnionByKeyMultiset(const size_t n)
{
thrust::host_vector<T> vec = unittest::random_integers<T>(2 * n);
thrust::host_vector<T> vec = unittest::random_integers<int>(2 * n);

// restrict elements to [min,13)
for (typename thrust::host_vector<T>::iterator i = vec.begin(); i != vec.end(); ++i)
Expand Down

0 comments on commit d69788c

Please sign in to comment.