]> ruin.nu Git - germs.git/blob - src/test/genesortertest.cpp
we need a list of smart pointers
[germs.git] / src / test / genesortertest.cpp
1 #include <cppunit/TestFixture.h>
2 #include <cppunit/extensions/HelperMacros.h>
3
4 #include <genesorter.h>
5 #include <geneorder.h>
6 #include <genealgorithms.h>
7 #include <reverseaction.h>
8
9 #include <algorithm>
10 #include <iterator>
11 using namespace std;
12
13 /* 
14  * A test case that is designed to produce
15  * example errors and failures.
16  *
17  */
18
19 #define TESTNAME GeneSorterTest
20
21
22 class TESTNAME : public CPPUNIT_NS::TestFixture
23 {
24   CPPUNIT_TEST_SUITE( TESTNAME );
25   CPPUNIT_TEST( testCreate );
26   CPPUNIT_TEST( testSafeActions );
27   CPPUNIT_TEST( testSort );
28   CPPUNIT_TEST_SUITE_END();
29
30 protected:
31                 vector<int> _validPerm;
32                 vector<int> _validPerm2;
33                 vector<int> _validPerm3;
34                 vector<int> _validPerm4;
35
36 public:
37
38         void setUp (){
39                 int validPerm[] = {1,2,3,4};
40                 _validPerm.assign(validPerm,validPerm+4);
41                 int validPerm2[] = {1,-3,-2,4};
42                 _validPerm2.assign(validPerm2,validPerm2+4);
43                 int validPerm3[] = {0,-2,-1,4,3,5,-8,6,7,9};
44                 _validPerm3.assign(validPerm3,validPerm3+10);
45                 int validPerm4[] = {-3,1,2,4,6,5,7,-15,-13,-14,-12,-10,-11,-9,8};
46                 _validPerm4.assign(validPerm4,validPerm4+15);
47
48         }
49
50 protected:
51
52         void testCreate (){
53                 GeneSorter so;
54                 so = so;
55         }
56         void testSort (){
57                 GeneSorter so;
58                 GeneOrder go(_validPerm.begin(),_validPerm.end());
59                 GeneSorter::ActionList al = so.sort(go);
60                 CPPUNIT_ASSERT_EQUAL(0ul,al.size());
61
62                 GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
63                 al = so.sort(go2);
64                 CPPUNIT_ASSERT_EQUAL(1ul,al.size());
65                 CPPUNIT_ASSERT((*al[0]) == ReverseAction(2,3));
66
67                 (*al[0])(go2);
68                 CPPUNIT_ASSERT(equal(go.begin(),go.end(),go2.begin()));
69
70                 GeneOrder go3(_validPerm3.begin(),_validPerm3.end());
71                 al = so.sort(go3);
72                 CPPUNIT_ASSERT_EQUAL(5ul,al.size());
73                 for (size_t i = 0; i < al.size(); ++i)
74                         (*al[i])(go3);
75                 int perm[] = {0,1,2,3,4,5,6,7,8,9};
76                 CPPUNIT_ASSERT(equal(go3.begin(),go3.end(),perm));
77         }
78         void testSafeActions (){
79                 GeneSorter so;
80                 GeneOrder go(_validPerm.begin(),_validPerm.end());
81                 GeneSorter::ActionList al = so.safeActions(go);
82                 CPPUNIT_ASSERT_EQUAL(0ul,al.size());
83
84                 GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
85                 al = so.safeActions(go2);
86                 CPPUNIT_ASSERT_EQUAL(1ul,al.size());
87                 CPPUNIT_ASSERT((*al[0]) == ReverseAction(2,3));
88                 CPPUNIT_ASSERT(!((*al[0]) == ReverseAction(2,5)));
89         }
90
91 };
92
93 CPPUNIT_TEST_SUITE_REGISTRATION( TESTNAME );
94
95 #undef TESTNAME