File: nth_prime2.cpp

package info (click to toggle)
primesieve 12.12%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,952 kB
  • sloc: cpp: 16,515; ansic: 723; sh: 531; makefile: 91
file content (108 lines) | stat: -rw-r--r-- 2,292 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
///
/// @file   nth_prime2.cpp
/// @brief  Test nth_prime edge cases
///
/// Copyright (C) 2022 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve.hpp>

#include <stdint.h>
#include <iostream>
#include <exception>
#include <cstdlib>

using namespace primesieve;

void check(bool OK)
{
  std::cout << "   " << (OK ? "OK" : "ERROR") << "\n";
  if (!OK)
    std::exit(1);
}

int main()
{
  uint64_t res;
  uint64_t start;
  int64_t n;

  n = 1;
  start = 1;
  res = nth_prime(n, start);
  std::cout << "nth_prime(" << n << ", " << start << ") = " << res;
  check(res == 2);

  n = 1;
  start = 2;
  res = nth_prime(n, start);
  std::cout << "nth_prime(" << n << ", " << start << ") = " << res;
  check(res == 3);

  n = -1;
  start = 102;
  res = nth_prime(n, start);
  std::cout << "nth_prime(" << n << ", " << start << ") = " << res;
  check(res == 101);

  n = -1;
  start = 101;
  res = nth_prime(n, start);
  std::cout << "nth_prime(" << n << ", " << start << ") = " << res;
  check(res == 97);

  n = -9592;
  start = 100000;
  res = nth_prime(n, start);
  std::cout << "nth_prime(" << n << ", " << start << ") = " << res;
  check(res == 2);

  n = -9591;
  start = 100000;
  res = nth_prime(n, start);
  std::cout << "nth_prime(" << n << ", " << start << ") = " << res;
  check(res == 3);

  try
  {
    n = -1;
    start = 2;
    res = nth_prime(n, start);
    std::cerr << "ERROR: nth_prime(" << n << ", " << start << ") = " << res;
    return 1;
  }
  catch (primesieve_error& e)
  {
    std::cout << "OK: " << e.what() << std::endl;
  }

// This test triggers a GCC bug if GCC version <= 12,
// hence we avoid running this test with GCC <= 12.
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627
#if __GNUC__ >= 14 || \
    !defined(__GNUC__) || \
    defined(__clang__)

  try
  {
    n = 1;
    start = 18446744073709551557ull;
    res = nth_prime(n, start);
    std::cerr << "ERROR: nth_prime(" << n << ", " << start << ") = " << res;
    return 1;
  }
  catch (primesieve_error& e)
  {
    std::cout << "OK: " << e.what() << std::endl;
  }

#endif

  std::cout << std::endl;
  std::cout << "All tests passed successfully!" << std::endl;

  return 0;
}