50 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// REQUIRES: long_tests10 11// Not a portable test12 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 n18 19#include <__cxx03/__hash_table>20#include <cassert>21#include <cstddef>22 23#include "test_macros.h"24 25bool is_prime(std::size_t n) {26 switch (n) {27 case 0:28 case 1:29 return false;30 }31 for (std::size_t i = 2; i * i <= n; ++i) {32 if (n % i == 0)33 return false;34 }35 return true;36}37 38int main(int, char**) {39 assert(std::__next_prime(0) == 0);40 for (std::size_t n = 1; n <= 100000; ++n) {41 std::size_t p = std::__next_prime(n);42 assert(p >= n);43 for (std::size_t i = n; i < p; ++i)44 assert(!is_prime(i));45 assert(is_prime(p));46 }47 48 return 0;49}50