|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cuco/detail/prime.hpp> |
| 18 | + |
| 19 | +#include <catch2/catch_test_macros.hpp> |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | + |
| 23 | +TEST_CASE("detail::is_prime", "") |
| 24 | +{ |
| 25 | + using cuco::detail::is_prime; |
| 26 | + |
| 27 | + SECTION("Values below 2 are not prime") |
| 28 | + { |
| 29 | + STATIC_REQUIRE(not is_prime(0)); |
| 30 | + STATIC_REQUIRE(not is_prime(1)); |
| 31 | + } |
| 32 | + |
| 33 | + SECTION("Small primes and composites") |
| 34 | + { |
| 35 | + STATIC_REQUIRE(is_prime(2)); |
| 36 | + STATIC_REQUIRE(is_prime(3)); |
| 37 | + STATIC_REQUIRE(not is_prime(4)); |
| 38 | + STATIC_REQUIRE(is_prime(5)); |
| 39 | + STATIC_REQUIRE(not is_prime(9)); |
| 40 | + STATIC_REQUIRE(is_prime(11)); |
| 41 | + STATIC_REQUIRE(is_prime(97)); |
| 42 | + STATIC_REQUIRE(not is_prime(100)); |
| 43 | + } |
| 44 | + |
| 45 | + SECTION("Carmichael numbers are correctly rejected") |
| 46 | + { |
| 47 | + // Strong pseudoprime candidates that fool weak primality tests |
| 48 | + REQUIRE(not is_prime(561)); // 3 * 11 * 17 |
| 49 | + REQUIRE(not is_prime(1105)); // 5 * 13 * 17 |
| 50 | + REQUIRE(not is_prime(1729)); // 7 * 13 * 19 |
| 51 | + REQUIRE(not is_prime(2465)); // 5 * 17 * 29 |
| 52 | + REQUIRE(not is_prime(41041)); // 7 * 11 * 13 * 41 |
| 53 | + REQUIRE(not is_prime(825265)); // 5 * 7 * 17 * 19 * 73 |
| 54 | + } |
| 55 | + |
| 56 | + SECTION("Large primes") |
| 57 | + { |
| 58 | + // Mersenne prime 2^31 - 1 |
| 59 | + REQUIRE(is_prime(2147483647ull)); |
| 60 | + // Near uint32 max |
| 61 | + REQUIRE(is_prime(4294967291ull)); |
| 62 | + // Large 64-bit prime |
| 63 | + REQUIRE(is_prime(18446744073709551557ull)); |
| 64 | + // Adjacent composite |
| 65 | + REQUIRE(not is_prime(18446744073709551556ull)); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +TEST_CASE("detail::next_prime", "") |
| 70 | +{ |
| 71 | + using cuco::detail::next_prime; |
| 72 | + |
| 73 | + SECTION("Values at or below 2 map to 2") |
| 74 | + { |
| 75 | + STATIC_REQUIRE(next_prime(0) == 2ull); |
| 76 | + STATIC_REQUIRE(next_prime(1) == 2ull); |
| 77 | + STATIC_REQUIRE(next_prime(2) == 2ull); |
| 78 | + } |
| 79 | + |
| 80 | + SECTION("Already-prime inputs are returned unchanged") |
| 81 | + { |
| 82 | + STATIC_REQUIRE(next_prime(3) == 3ull); |
| 83 | + STATIC_REQUIRE(next_prime(13) == 13ull); |
| 84 | + STATIC_REQUIRE(next_prime(101) == 101ull); |
| 85 | + } |
| 86 | + |
| 87 | + SECTION("Composite inputs advance to the next prime") |
| 88 | + { |
| 89 | + STATIC_REQUIRE(next_prime(4) == 5ull); |
| 90 | + STATIC_REQUIRE(next_prime(14) == 17ull); |
| 91 | + STATIC_REQUIRE(next_prime(100) == 101ull); |
| 92 | + STATIC_REQUIRE(next_prime(155) == 157ull); // used by extent_test |
| 93 | + } |
| 94 | + |
| 95 | + SECTION("Large composite inputs") |
| 96 | + { |
| 97 | + REQUIRE(next_prime(1ull << 20) == 1048583ull); |
| 98 | + REQUIRE(next_prime(1ull << 32) == 4294967311ull); |
| 99 | + } |
| 100 | + |
| 101 | + SECTION("Result is always >= input and prime") |
| 102 | + { |
| 103 | + using cuco::detail::is_prime; |
| 104 | + for (std::uint64_t n : {0ull, 1ull, 42ull, 1000ull, 999983ull, 1ull << 40}) { |
| 105 | + auto const p = next_prime(n); |
| 106 | + REQUIRE(p >= n); |
| 107 | + REQUIRE(is_prime(p)); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments