• 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 // Not a portable test
11 
12 // <__hash_table>
13 
14 // size_t __next_prime(size_t n);
15 
16 // If n == 0, return 0, else return the lowest prime greater than or equal to n
17 
18 #include <__hash_table>
19 #include <cassert>
20 
21 bool
is_prime(size_t n)22 is_prime(size_t n)
23 {
24     switch (n)
25     {
26     case 0:
27     case 1:
28         return false;
29     }
30     for (size_t i = 2; i*i <= n; ++i)
31     {
32         if (n % i == 0)
33             return false;
34     }
35     return true;
36 }
37 
main()38 int main()
39 {
40     assert(std::__next_prime(0) == 0);
41     for (std::size_t n = 1; n <= 100000; ++n)
42     {
43         std::size_t p = std::__next_prime(n);
44         assert(p >= n);
45         for (std::size_t i = n; i < p; ++i)
46             assert(!is_prime(i));
47         assert(is_prime(p));
48     }
49 }
50