Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Mar 27, 2023
1 parent 9c95e45 commit 930073a
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ release fixes the same issue for 128-bit integers.
pi_deleglise_rivat(int128_t x), pi_gourdon(int128_t x).
* test/api.cpp: Add more pi(-n) tests.
* test/api_c.cpp: Add more primecount_pi(-n) tests.
* test/pi_cache.cpp: Add new test.
* test/pi_deleglise_rivat.cpp: Add new test.
* test/pi_gourdon.cpp: Add new test.
* test/pi_legendre.cpp: Add new test.
* test/pi_lmo5.cpp: Add new test.
* test/pi_lmo_parallel.cpp: Add new test.
* test/pi_meissel.cpp: Add new test.
* CMakeLists.txt: Disable building libprimesieve examples.

Changes in primecount-7.7, 2023-03-26
Expand Down
59 changes: 59 additions & 0 deletions test/pi_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
///
/// @file pi_cache.cpp
/// @brief Test the pi_cache(x) function.
///
/// Copyright (C) 2023 Kim Walisch, <[email protected]>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount-internal.hpp>

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

using namespace primecount;

std::vector<int64_t> pix =
{
0, 0, 1, 2, 2, 3, 3, 4, 4, 4,
4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
8, 8, 8, 9, 9, 9, 9, 9, 9, 10,
10, 11, 11, 11, 11, 11, 11, 12, 12, 12,
12, 13, 13, 14, 14, 14, 14, 15, 15, 15,
15, 15, 15, 16, 16, 16, 16, 16, 16, 17,
17, 18, 18, 18, 18, 18, 18, 19, 19, 19,
19, 20, 20, 21, 21, 21, 21, 21, 21
};

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

int main()
{
{
int64_t x = -1;
int64_t res = pi_cache(x);
std::cout << "pi_cache(" << x << ") = " << res;
check(res == 0);
}

for (int64_t x = 0; x < (int64_t) pix.size(); x++)
{
int64_t res = pi_cache(x);
std::cout << "pi_cache(" << x << ") = " << res;
check(res == pix[x]);
}

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

return 0;
}
76 changes: 76 additions & 0 deletions test/pi_deleglise_rivat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
///
/// @file pi_deleglise_rivat.cpp
/// @brief Test the pi_deleglise_rivat_64(x) function.
///
/// Copyright (C) 2023 Kim Walisch, <[email protected]>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.hpp>
#include <primecount-internal.hpp>

#include <stdint.h>
#include <iostream>
#include <cstdlib>
#include <random>
#include <vector>

using namespace primecount;

std::vector<int64_t> pix =
{
0, 0, 1, 2, 2, 3, 3, 4, 4, 4,
4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
8, 8, 8, 9, 9, 9, 9, 9, 9, 10,
10, 11, 11, 11, 11, 11, 11, 12, 12, 12,
12, 13, 13, 14, 14, 14, 14, 15, 15, 15,
15, 15, 15, 16, 16, 16, 16, 16, 16, 17,
17, 18, 18, 18, 18, 18, 18, 19, 19, 19,
19, 20, 20, 21, 21, 21, 21, 21, 21
};

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

int main()
{
int threads = get_num_threads();

{
int64_t x = -1;
int64_t res = pi_deleglise_rivat_64(x, threads);
std::cout << "pi_deleglise_rivat_64(" << x << ") = " << res;
check(res == 0);
}

for (int64_t x = 0; x < (int64_t) pix.size(); x++)
{
int64_t res = pi_deleglise_rivat_64(x, threads);
std::cout << "pi_deleglise_rivat_64(" << x << ") = " << res;
check(res == pix[x]);
}

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int64_t> dist(0, 1 << 28);

for (int i = 0; i < 1000; i++)
{
int64_t x = dist(gen);
int64_t res1 = pi_deleglise_rivat_64(x, threads);
int64_t res2 = pi_legendre(x, threads);
std::cout << "pi_deleglise_rivat_64(" << x << ") = " << res1;
check(res1 == res2);
}

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

return 0;
}
77 changes: 77 additions & 0 deletions test/pi_gourdon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
///
/// @file pi_gourdon.cpp
/// @brief Test the pi_gourdon_64(x) function.
///
/// Copyright (C) 2023 Kim Walisch, <[email protected]>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.hpp>
#include <primecount-internal.hpp>
#include <gourdon.hpp>

#include <stdint.h>
#include <iostream>
#include <cstdlib>
#include <random>
#include <vector>

using namespace primecount;

std::vector<int64_t> pix =
{
0, 0, 1, 2, 2, 3, 3, 4, 4, 4,
4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
8, 8, 8, 9, 9, 9, 9, 9, 9, 10,
10, 11, 11, 11, 11, 11, 11, 12, 12, 12,
12, 13, 13, 14, 14, 14, 14, 15, 15, 15,
15, 15, 15, 16, 16, 16, 16, 16, 16, 17,
17, 18, 18, 18, 18, 18, 18, 19, 19, 19,
19, 20, 20, 21, 21, 21, 21, 21, 21
};

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

int main()
{
int threads = get_num_threads();

{
int64_t x = -1;
int64_t res = pi_gourdon_64(x, threads);
std::cout << "pi_gourdon_64(" << x << ") = " << res;
check(res == 0);
}

for (int64_t x = 0; x < (int64_t) pix.size(); x++)
{
int64_t res = pi_gourdon_64(x, threads);
std::cout << "pi_gourdon_64(" << x << ") = " << res;
check(res == pix[x]);
}

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int64_t> dist(0, 1 << 28);

for (int i = 0; i < 1000; i++)
{
int64_t x = dist(gen);
int64_t res1 = pi_gourdon_64(x, threads);
int64_t res2 = pi_legendre(x, threads);
std::cout << "pi_gourdon_64(" << x << ") = " << res1;
check(res1 == res2);
}

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

return 0;
}
77 changes: 77 additions & 0 deletions test/pi_legendre.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
///
/// @file pi_legendre.cpp
/// @brief Test the pi_legendre(x) function.
///
/// Copyright (C) 2023 Kim Walisch, <[email protected]>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.hpp>
#include <primecount-internal.hpp>
#include <PiTable.hpp>

#include <stdint.h>
#include <iostream>
#include <cstdlib>
#include <random>
#include <vector>

using namespace primecount;

std::vector<int64_t> pix =
{
0, 0, 1, 2, 2, 3, 3, 4, 4, 4,
4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
8, 8, 8, 9, 9, 9, 9, 9, 9, 10,
10, 11, 11, 11, 11, 11, 11, 12, 12, 12,
12, 13, 13, 14, 14, 14, 14, 15, 15, 15,
15, 15, 15, 16, 16, 16, 16, 16, 16, 17,
17, 18, 18, 18, 18, 18, 18, 19, 19, 19,
19, 20, 20, 21, 21, 21, 21, 21, 21
};

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

int main()
{
int threads = get_num_threads();

{
int64_t x = -1;
int64_t res = pi_legendre(x, threads);
std::cout << "pi_legendre(" << x << ") = " << res;
check(res == 0);
}

for (int64_t x = 0; x < (int64_t) pix.size(); x++)
{
int64_t res = pi_legendre(x, threads);
std::cout << "pi_legendre(" << x << ") = " << res;
check(res == pix[x]);
}

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int64_t> dist(0, PiTable::max_cached());

for (int i = 0; i < 1000; i++)
{
int64_t x = dist(gen);
int64_t res1 = pi_legendre(x, threads);
int64_t res2 = pi_cache(x);
std::cout << "pi_legendre(" << x << ") = " << res1;
check(res1 == res2);
}

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

return 0;
}
76 changes: 76 additions & 0 deletions test/pi_lmo5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
///
/// @file pi_lmo5.cpp
/// @brief Test the pi_lmo5(x) function.
///
/// Copyright (C) 2023 Kim Walisch, <[email protected]>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.hpp>
#include <primecount-internal.hpp>

#include <stdint.h>
#include <iostream>
#include <cstdlib>
#include <random>
#include <vector>

using namespace primecount;

std::vector<int64_t> pix =
{
0, 0, 1, 2, 2, 3, 3, 4, 4, 4,
4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
8, 8, 8, 9, 9, 9, 9, 9, 9, 10,
10, 11, 11, 11, 11, 11, 11, 12, 12, 12,
12, 13, 13, 14, 14, 14, 14, 15, 15, 15,
15, 15, 15, 16, 16, 16, 16, 16, 16, 17,
17, 18, 18, 18, 18, 18, 18, 19, 19, 19,
19, 20, 20, 21, 21, 21, 21, 21, 21
};

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

int main()
{
int threads = get_num_threads();

{
int64_t x = -1;
int64_t res = pi_lmo5(x);
std::cout << "pi_lmo5(" << x << ") = " << res;
check(res == 0);
}

for (int64_t x = 0; x < (int64_t) pix.size(); x++)
{
int64_t res = pi_lmo5(x);
std::cout << "pi_lmo5(" << x << ") = " << res;
check(res == pix[x]);
}

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int64_t> dist(0, 1 << 28);

for (int i = 0; i < 1000; i++)
{
int64_t x = dist(gen);
int64_t res1 = pi_lmo5(x);
int64_t res2 = pi_legendre(x, threads);
std::cout << "pi_lmo5(" << x << ") = " << res1;
check(res1 == res2);
}

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

return 0;
}
Loading

0 comments on commit 930073a

Please sign in to comment.