File: nth_prime2.cpp

package info (click to toggle)
primesieve 7.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,080 kB
  • sloc: cpp: 7,270; ansic: 455; sh: 199; makefile: 86
file content (100 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download | duplicates (2)
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
///
/// @file   nth_prime2.cpp
/// @brief  Test nth_prime edge cases
///
/// Copyright (C) 2017 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 std;
using namespace primesieve;

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}