• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // REQUIRES: long_tests
11 
12 // Not a portable test
13 
14 // <__hash_table>
15 
16 // size_t __next_prime(size_t n);
17 
18 // If n == 0, return 0, else return the lowest prime greater than or equal to n
19 
20 #include <__hash_table>
21 #include <cassert>
22 
23 bool
is_prime(size_t n)24 is_prime(size_t n)
25 {
26     switch (n)
27     {
28     case 0:
29     case 1:
30         return false;
31     }
32     for (size_t i = 2; i*i <= n; ++i)
33     {
34         if (n % i == 0)
35             return false;
36     }
37     return true;
38 }
39 
main()40 int main()
41 {
42     assert(std::__next_prime(0) == 0);
43     for (std::size_t n = 1; n <= 100000; ++n)
44     {
45         std::size_t p = std::__next_prime(n);
46         assert(p >= n);
47         for (std::size_t i = n; i < p; ++i)
48             assert(!is_prime(i));
49         assert(is_prime(p));
50     }
51 }
52