blob: c0e322fba78d6edede82d6332ff6df476f3b4a4f (
plain)
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
|
/***************************************************************************
begin : 2004-05-25
copyright : (C) 2004 by Sebastian Stein
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// for BOOST testing
#include <boost/test/unit_test.hpp>
using boost::unit_test_framework::test_suite;
using boost::unit_test_framework::test_case;
// the class to be tested
#include "primenumber.h"
class primenumber_test
{
public:
// constructor
primenumber_test()
{
}
/** test the prime number algorithm */
void test_isPrimeNumber()
{
BOOST_REQUIRE(m_primenumber.isPrimeNumber(0) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(2) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(3) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(4) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(5) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(6) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(7) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(8) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(9) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(23) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(9) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(9) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(6) == 0);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(101) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(323) == 0); // 17 * 19
BOOST_REQUIRE(m_primenumber.isPrimeNumber(1001) == 0); // 7 * 143
BOOST_REQUIRE(m_primenumber.isPrimeNumber(1002) == 0); // 2 * 501
BOOST_REQUIRE(m_primenumber.isPrimeNumber(3) == 1);
BOOST_REQUIRE(m_primenumber.isPrimeNumber(2) == 1);
}
/** test the get_first() function */
void test_get_first()
{
BOOST_REQUIRE(m_primenumber.get_first() == 2);
}
/** test the move and get functions */
void test_move_get_func()
{
m_primenumber.move_first();
BOOST_REQUIRE(m_primenumber.get_current() == 2);
BOOST_REQUIRE(m_primenumber.get_next() == 3);
m_primenumber.move_forward();
BOOST_REQUIRE(m_primenumber.get_current() == 5);
m_primenumber.move_back();
BOOST_REQUIRE(m_primenumber.get_current() == 3);
unsigned int tmp = m_primenumber.get_last();
m_primenumber.move_last();
BOOST_REQUIRE(m_primenumber.get_current() == tmp);
m_primenumber.move_forward();
BOOST_REQUIRE(m_primenumber.get_last() != tmp);
}
private:
// instance of primenumber
primenumber m_primenumber;
};
class primenumber_test_suite : public test_suite
{
public:
primenumber_test_suite() : test_suite("primenumber_test_suite")
{
// create an instance of the test cases class
boost::shared_ptr<primenumber_test> instance(new primenumber_test());
// create the test cases
test_case* isPrimeNumber_test_case = BOOST_CLASS_TEST_CASE(
&primenumber_test::test_isPrimeNumber, instance );
test_case* get_first_test_case = BOOST_CLASS_TEST_CASE(
&primenumber_test::test_get_first, instance );
test_case* move_get_func_test_case = BOOST_CLASS_TEST_CASE(
&primenumber_test::test_move_get_func, instance );
// add the test cases to the test suite
add(isPrimeNumber_test_case);
add(get_first_test_case);
add(move_get_func_test_case);
}
};
|