xref: /llvm-project/libcxx/test/libcxx/containers/unord/next_prime.pass.cpp (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // REQUIRES: long_tests
10 
11 // Not a portable test
12 
13 // <__hash_table>
14 
15 // size_t __next_prime(size_t n);
16 
17 // If n == 0, return 0, else return the lowest prime greater than or equal to n
18 
19 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
20 
21 #include <__hash_table>
22 #include <cassert>
23 #include <cstddef>
24 
25 #include "test_macros.h"
26 
27 bool
28 is_prime(std::size_t n)
29 {
30     switch (n)
31     {
32     case 0:
33     case 1:
34         return false;
35     }
36     for (std::size_t i = 2; i*i <= n; ++i)
37     {
38         if (n % i == 0)
39             return false;
40     }
41     return true;
42 }
43 
44 int main(int, char**)
45 {
46     assert(std::__next_prime(0) == 0);
47     for (std::size_t n = 1; n <= 100000; ++n)
48     {
49         std::size_t p = std::__next_prime(n);
50         assert(p >= n);
51         for (std::size_t i = n; i < p; ++i)
52             assert(!is_prime(i));
53         assert(is_prime(p));
54     }
55 
56   return 0;
57 }
58