File: api_c.c

package info (click to toggle)
primecount 8.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,640 kB
  • sloc: cpp: 21,835; ansic: 121; sh: 99; makefile: 89
file content (123 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
///
/// @file   api_c.c
/// @brief  Test primecount's C API.
///
/// Copyright (C) 2025 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.h>

#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void check(int OK)
{
  if (OK)
    printf("   OK\n");
  else
  {
    printf("   ERROR\n");
    exit(1);
  }
}

int main(void)
{
  // Test 64-bit pi(-x)
  int64_t n = -1;
  int64_t res = primecount_pi(n);
  printf("primecount_pi(%"PRId64") = %"PRId64, n, res);
  check(res == 0);

  n = -9223372036854775807;
  res = primecount_pi(n);
  printf("primecount_pi(%"PRId64") = %"PRId64, n, res);
  check(res == 0);

  n = (int64_t) 1e10;
  res = primecount_pi(n);
  printf("primecount_pi(%"PRId64") = %"PRId64, n, res);
  check(res == 455052511);

  pc_int128_t n128;
  n128.lo = (uint64_t) 1e9;
  n128.hi = 0;
  pc_int128_t res128 = primecount_pi_128(n128);
  printf("primecount_pi_128(1e9) = %"PRId64, res128.lo);
  check(res128.lo == 50847534 && res128.hi == 0);

  // Check x >= primecount max x of 10^31.
  // primecount must detect issue and return -1 error.
  n128.lo = 0;
  n128.hi = 1ull << 50;
  res128 = primecount_pi_128(n128);
  printf("primecount_pi_128(2^114) = %"PRId64, res128.lo);
  check(res128.hi == -1 && ~res128.lo == 0);

  n = 455052511;
  res = primecount_nth_prime(n);
  printf("primecount_nth_prime(%"PRId64") = %"PRId64, n, res);
  check(res == 9999999967);

  // nth_prime(-1) is an error and should hence return -1
  // which indicates an error in the libprimecount C API.
  n = -1;
  res = primecount_nth_prime(n);
  printf("primecount_nth_prime(%"PRId64") = %"PRId64, n, res);
  check(res == -1);

  n128.lo = (uint64_t) 1e9;
  n128.hi = 0;
  res128 = primecount_nth_prime_128(n128);
  printf("primecount_nth_prime_128(1e9) = %"PRId64, res128.lo);
  check(res128.lo == 22801763489 && res128.hi == 0);

  // Check n >= primecount max n of ~ 10^29.
  // primecount must detect issue and return -1 error.
  n128.lo = 0;
  n128.hi = 1ull << 50;
  res128 = primecount_nth_prime_128(n128);
  printf("primecount_nth_prime_128(2^114) = %"PRId64, res128.hi);
  check(res128.hi == -1 && ~res128.lo == 0);

  n = (int64_t) 1e12;
  int64_t a = 78498;
  res = primecount_phi(n, a);
  printf("primecount_phi(%"PRId64", %"PRId64") = %"PRId64, n , a, res);
  check(res == 37607833521);

  n = -1;
  res = primecount_phi(n, a);
  printf("primecount_phi(%"PRId64", %"PRId64") = %"PRId64, n , a, res);
  check(res == 0);


  printf("primecount version: %s", primecount_version());
  check(strcmp(primecount_version(), PRIMECOUNT_VERSION) == 0);

  // If multi-threading is disabled setting the
  // number of threads must have no effect.
  if (primecount_get_num_threads() <= 1)
  {
    primecount_set_num_threads(2);
    printf("new threads: %d", primecount_get_num_threads());
    check(primecount_get_num_threads() == 1);
  }
  else
  {
    primecount_set_num_threads(2);
    printf("new threads: %d", primecount_get_num_threads());
    check(primecount_get_num_threads() == 2);
  }

  printf("\n");
  printf("All tests passed successfully!\n");

  return 0;
}