|
1 | 1 | #include <array>
|
| 2 | +#include <limits> |
2 | 3 |
|
3 | 4 | #include <catch.hpp>
|
4 | 5 |
|
@@ -45,15 +46,27 @@ TEST_CASE("reserve calc", "[resize]") {
|
45 | 46 | REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
|
46 | 47 | 2500000 * slot_per_bucket) == 22);
|
47 | 48 |
|
| 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; |
48 | 63 | REQUIRE(UnitTestInternalAccess::reserve_calc<IntIntTable>(
|
49 |
| - (1ULL << 31) * slot_per_bucket) == 31); |
| 64 | + max_elems_before_max_hashpower) == (max_hashpower - 1)); |
50 | 65 | 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); |
57 | 70 | }
|
58 | 71 |
|
59 | 72 | struct my_type {
|
|
0 commit comments