libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
prime_numbers.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2012 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include "core/prime_numbers.hpp"
25 
26 using namespace libjmmcg;
27 
28 BOOST_AUTO_TEST_SUITE(prime_numbers_tests)
29 
30 BOOST_AUTO_TEST_CASE(zero_dyn)
31 {
32  const auto primes=dyn::sieve_of_eratosthenes(0);
33  BOOST_CHECK(primes.empty());
34 }
35 
36 BOOST_AUTO_TEST_CASE(zero_mpl)
37 {
38  const auto primes=mpl::sieve_of_eratosthenes<unsigned long, 0>::result();
39  BOOST_CHECK(primes.empty());
40 }
41 
42 BOOST_AUTO_TEST_CASE(one_dyn)
43 {
44  const auto primes=dyn::sieve_of_eratosthenes(1);
45  BOOST_CHECK(primes.empty());
46 }
47 
48 BOOST_AUTO_TEST_CASE(one_mpl)
49 {
50  const auto primes=mpl::sieve_of_eratosthenes<unsigned long, 1>::result();
51  BOOST_CHECK(primes.empty());
52 }
53 
54 BOOST_AUTO_TEST_CASE(two_dyn)
55 {
56  const auto primes=dyn::sieve_of_eratosthenes(2);
57  BOOST_CHECK(primes.empty());
58 }
59 
60 BOOST_AUTO_TEST_CASE(two_mpl)
61 {
62  const auto primes=mpl::sieve_of_eratosthenes<unsigned long, 2>::result();
63  BOOST_CHECK(primes.empty());
64 }
65 
66 BOOST_AUTO_TEST_CASE(three_dyn)
67 {
68  const auto primes=dyn::sieve_of_eratosthenes(3);
69  BOOST_CHECK_EQUAL(primes.size(), 1UL);
70  BOOST_CHECK_EQUAL(primes[0], 2UL);
71 }
72 
73 BOOST_AUTO_TEST_CASE(three_mpl)
74 {
75  const auto primes=mpl::sieve_of_eratosthenes<unsigned long, 3>::result();
76  BOOST_CHECK_EQUAL(primes.size(), 1UL);
77  BOOST_CHECK_EQUAL(primes[0], 2UL);
78 }
79 
80 BOOST_AUTO_TEST_CASE(four_dyn)
81 {
82  const auto primes=dyn::sieve_of_eratosthenes(4);
83  BOOST_CHECK_EQUAL(primes.size(), 2UL);
84  BOOST_CHECK_EQUAL(primes[0], 2UL);
85  BOOST_CHECK_EQUAL(primes[1], 3UL);
86 }
87 
88 BOOST_AUTO_TEST_CASE(four_mpl)
89 {
90  const auto primes=mpl::sieve_of_eratosthenes<unsigned long, 4>::result();
91  BOOST_CHECK_EQUAL(primes.size(), 2UL);
92  BOOST_CHECK_EQUAL(primes[0], 2UL);
93  BOOST_CHECK_EQUAL(primes[1], 3UL);
94 }
95 
96 BOOST_AUTO_TEST_CASE(ten_dyn)
97 {
98  const auto primes=dyn::sieve_of_eratosthenes(10);
99  BOOST_CHECK_EQUAL(primes.size(), 4UL);
100  BOOST_CHECK_EQUAL(primes[0], 2UL);
101  BOOST_CHECK_EQUAL(primes[1], 3UL);
102  BOOST_CHECK_EQUAL(primes[2], 5UL);
103  BOOST_CHECK_EQUAL(primes[3], 7UL);
104 }
105 
106 BOOST_AUTO_TEST_CASE(ten_mpl)
107 {
108  const auto primes=mpl::sieve_of_eratosthenes<unsigned long, 10>::result();
109  BOOST_CHECK_EQUAL(primes.size(), 4UL);
110  BOOST_CHECK_EQUAL(primes[0], 2UL);
111  BOOST_CHECK_EQUAL(primes[1], 3UL);
112  BOOST_CHECK_EQUAL(primes[2], 5UL);
113  BOOST_CHECK_EQUAL(primes[3], 7UL);
114 }
115 
116 BOOST_AUTO_TEST_SUITE_END()