diff --git a/ChangeLog b/ChangeLog index b6d3d01d..9b3fc505 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/test/pi_cache.cpp b/test/pi_cache.cpp new file mode 100644 index 00000000..466a97fd --- /dev/null +++ b/test/pi_cache.cpp @@ -0,0 +1,59 @@ +/// +/// @file pi_cache.cpp +/// @brief Test the pi_cache(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include + +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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; +} diff --git a/test/pi_deleglise_rivat.cpp b/test/pi_deleglise_rivat.cpp new file mode 100644 index 00000000..7be96cd8 --- /dev/null +++ b/test/pi_deleglise_rivat.cpp @@ -0,0 +1,76 @@ +/// +/// @file pi_deleglise_rivat.cpp +/// @brief Test the pi_deleglise_rivat_64(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include +#include + +#include +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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 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; +} diff --git a/test/pi_gourdon.cpp b/test/pi_gourdon.cpp new file mode 100644 index 00000000..533b5903 --- /dev/null +++ b/test/pi_gourdon.cpp @@ -0,0 +1,77 @@ +/// +/// @file pi_gourdon.cpp +/// @brief Test the pi_gourdon_64(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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 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; +} diff --git a/test/pi_legendre.cpp b/test/pi_legendre.cpp new file mode 100644 index 00000000..49499c9c --- /dev/null +++ b/test/pi_legendre.cpp @@ -0,0 +1,77 @@ +/// +/// @file pi_legendre.cpp +/// @brief Test the pi_legendre(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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 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; +} diff --git a/test/pi_lmo5.cpp b/test/pi_lmo5.cpp new file mode 100644 index 00000000..db75557b --- /dev/null +++ b/test/pi_lmo5.cpp @@ -0,0 +1,76 @@ +/// +/// @file pi_lmo5.cpp +/// @brief Test the pi_lmo5(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include +#include + +#include +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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 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; +} diff --git a/test/pi_lmo_parallel.cpp b/test/pi_lmo_parallel.cpp new file mode 100644 index 00000000..824eef84 --- /dev/null +++ b/test/pi_lmo_parallel.cpp @@ -0,0 +1,76 @@ +/// +/// @file pi_lmo_parallel.cpp +/// @brief Test the pi_lmo_parallel(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include +#include + +#include +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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_lmo_parallel(x, threads); + std::cout << "pi_lmo_parallel(" << x << ") = " << res; + check(res == 0); + } + + for (int64_t x = 0; x < (int64_t) pix.size(); x++) + { + int64_t res = pi_lmo_parallel(x, threads); + std::cout << "pi_lmo_parallel(" << x << ") = " << res; + check(res == pix[x]); + } + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dist(0, 1 << 28); + + for (int i = 0; i < 1000; i++) + { + int64_t x = dist(gen); + int64_t res1 = pi_lmo_parallel(x, threads); + int64_t res2 = pi_legendre(x, threads); + std::cout << "pi_lmo_parallel(" << x << ") = " << res1; + check(res1 == res2); + } + + std::cout << std::endl; + std::cout << "All tests passed successfully!" << std::endl; + + return 0; +} diff --git a/test/pi_meissel.cpp b/test/pi_meissel.cpp new file mode 100644 index 00000000..375846bc --- /dev/null +++ b/test/pi_meissel.cpp @@ -0,0 +1,76 @@ +/// +/// @file pi_meissel.cpp +/// @brief Test the pi_meissel(x) function. +/// +/// Copyright (C) 2023 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#include +#include + +#include +#include +#include +#include +#include + +using namespace primecount; + +std::vector 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_meissel(x, threads); + std::cout << "pi_meissel(" << x << ") = " << res; + check(res == 0); + } + + for (int64_t x = 0; x < (int64_t) pix.size(); x++) + { + int64_t res = pi_meissel(x, threads); + std::cout << "pi_meissel(" << x << ") = " << res; + check(res == pix[x]); + } + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dist(0, 1 << 28); + + for (int i = 0; i < 1000; i++) + { + int64_t x = dist(gen); + int64_t res1 = pi_meissel(x, threads); + int64_t res2 = pi_legendre(x, threads); + std::cout << "pi_meissel(" << x << ") = " << res1; + check(res1 == res2); + } + + std::cout << std::endl; + std::cout << "All tests passed successfully!" << std::endl; + + return 0; +}