Skip to content

Commit 2362469

Browse files
committed
Fix test_resize so that it works for 32-bit architectures.
Instead of hardcoding a maximum number of expected buckets, we base our calculations off the actual SIZE_T_MAX value, so that it should be architecture-agnostic.
1 parent cae6170 commit 2362469

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

tests/unit-tests/test_resize.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <array>
2+
#include <limits>
23

34
#include <catch.hpp>
45

@@ -45,15 +46,27 @@ TEST_CASE("reserve calc", "[resize]") {
4546
REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
4647
2500000 * slot_per_bucket) == 22);
4748

49+
// The maximum number of elements we can ask to reserve without incurring
50+
// rounding error when computing a number of buckets is
51+
// SIZE_T_MAX-slot_per_bucket(), which will come out to int_div(SIZE_T_MAX -
52+
// 1, slot_per_bucket()) buckets.
53+
const size_t max_buckets = (
54+
std::numeric_limits<size_t>::max() - 1)/slot_per_bucket;
55+
// Since the table is always sized in powers of two, our maximum hashpower
56+
// comes out to max_hashpower = floor(log2(max_buckets)). We compute this in
57+
// a numerically-stable fashion.
58+
size_t max_hashpower = 0;
59+
for (; (static_cast<size_t>(1) << (max_hashpower + 1)) <= max_buckets; ++max_hashpower);
60+
// Test the boundary between max_hashpower-1 and max_hashpower.
61+
const size_t max_elems_before_max_hashpower = (
62+
static_cast<size_t>(1) << (max_hashpower - 1)) * slot_per_bucket;
4863
REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
49-
(1ULL << 31) * slot_per_bucket) == 31);
64+
max_elems_before_max_hashpower) == (max_hashpower - 1));
5065
REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
51-
((1ULL << 31) + 1) * slot_per_bucket) == 32);
52-
53-
REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
54-
(1ULL << 61) * slot_per_bucket) == 61);
55-
REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
56-
((1ULL << 61) + 1) * slot_per_bucket) == 62);
66+
max_elems_before_max_hashpower + 1) == max_hashpower);
67+
// Test the maximum number of elements.
68+
const size_t max_elems = (static_cast<size_t>(1) << max_hashpower) * slot_per_bucket;
69+
REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(max_elems) == max_hashpower);
5770
}
5871

5972
struct my_type {

0 commit comments

Comments
 (0)