libStatGen Software  1
PackedVectorTest.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <getopt.h>
19 #include "Generic.h"
20 #include <stdio.h>
21 #include "PackedVector.h"
22 #include "UnitTest.h"
23 
24 #include <assert.h>
25 #include <stdlib.h>
26 
27 class PackedArrayTest : public UnitTest
28 {
29 public:
30  PackedArrayTest(const char *title) : UnitTest(title) {;}
31  void testBool();
32  void test2Bit();
33  void test4Bit();
34  void testResize();
35 
36  void test() {
37  testBool();
38  test2Bit();
39  test4Bit();
40  testResize();
41  }
42 };
43 
44 void PackedArrayTest::testBool(void)
45 {
46  PackedVectorBool_t testVector;
47 
48  testVector.resize(11);
49  testVector.set(0,0);
50  testVector.set(1,1);
51  testVector.set(2,0);
52  testVector.set(3,1);
53  testVector.set(4,1);
54  testVector.set(5,0);
55  testVector.set(6,1);
56  testVector.set(7,0);
57  testVector.set(8,0);
58  testVector.set(9,0);
59  testVector.set(10,1);
60  check(m_failures, ++m_testNum, "Access 1 bit element 0", 0U, testVector[0]);
61  check(m_failures, ++m_testNum, "Access 1 bit element 1", 1U, testVector[1]);
62  check(m_failures, ++m_testNum, "Access 1 bit element 2", 0U, testVector[2]);
63  check(m_failures, ++m_testNum, "Access 1 bit element 3", 1U, testVector[3]);
64  check(m_failures, ++m_testNum, "Access 1 bit element 4", 1U, testVector[4]);
65  check(m_failures, ++m_testNum, "Access 1 bit element 5", 0U, testVector[5]);
66  check(m_failures, ++m_testNum, "Access 1 bit element 6", 1U, testVector[6]);
67  check(m_failures, ++m_testNum, "Access 1 bit element 7", 0U, testVector[7]);
68  check(m_failures, ++m_testNum, "Access 1 bit element 8", 0U, testVector[8]);
69  check(m_failures, ++m_testNum, "Access 1 bit element 9", 0U, testVector[9]);
70  check(m_failures, ++m_testNum, "Access 1 bit element 10", 1U, testVector[10]);
71 }
72 
73 void PackedArrayTest::test2Bit(void)
74 {
75  PackedVector2Bit_t testVector;
76 
77  testVector.resize(11);
78  testVector.set(0,0);
79  testVector.set(1,1);
80  testVector.set(2,2);
81  testVector.set(3,3);
82  testVector.set(4,3);
83  testVector.set(5,2);
84  testVector.set(6,1);
85  testVector.set(7,0);
86  testVector.set(8,2);
87  testVector.set(9,1);
88  testVector.set(10,3);
89  check(m_failures, ++m_testNum, "Access 2 bit element 0", 0U, testVector[0]);
90  check(m_failures, ++m_testNum, "Access 2 bit element 1", 1U, testVector[1]);
91  check(m_failures, ++m_testNum, "Access 2 bit element 2", 2U, testVector[2]);
92  check(m_failures, ++m_testNum, "Access 2 bit element 3", 3U, testVector[3]);
93  check(m_failures, ++m_testNum, "Access 2 bit element 4", 3U, testVector[4]);
94  check(m_failures, ++m_testNum, "Access 2 bit element 5", 2U, testVector[5]);
95  check(m_failures, ++m_testNum, "Access 2 bit element 6", 1U, testVector[6]);
96  check(m_failures, ++m_testNum, "Access 2 bit element 7", 0U, testVector[7]);
97  check(m_failures, ++m_testNum, "Access 2 bit element 8", 2U, testVector[8]);
98  check(m_failures, ++m_testNum, "Access 2 bit element 9", 1U, testVector[9]);
99  check(m_failures, ++m_testNum, "Access 2 bit element 10", 3U, testVector[10]);
100 }
101 
102 void PackedArrayTest::test4Bit(void)
103 {
104  PackedVector4Bit_t testVector;
105 
106  testVector.resize(11);
107  testVector.set(0,0);
108  testVector.set(1,1);
109  testVector.set(2,2);
110  testVector.set(3,3);
111  testVector.set(4,4);
112  testVector.set(5,5);
113  testVector.set(6,6);
114  testVector.set(7,7);
115  testVector.set(8,8);
116  testVector.set(9,9);
117  testVector.set(10,10);
118 
119  check(m_failures, ++m_testNum, "Access 4 bit element 0", 0U, testVector[0]);
120  check(m_failures, ++m_testNum, "Access 4 bit element 1", 1U, testVector[1]);
121  check(m_failures, ++m_testNum, "Access 4 bit element 2", 2U, testVector[2]);
122  check(m_failures, ++m_testNum, "Access 4 bit element 3", 3U, testVector[3]);
123  check(m_failures, ++m_testNum, "Access 4 bit element 4", 4U, testVector[4]);
124  check(m_failures, ++m_testNum, "Access 4 bit element 5", 5U, testVector[5]);
125  check(m_failures, ++m_testNum, "Access 4 bit element 6", 6U, testVector[6]);
126  check(m_failures, ++m_testNum, "Access 4 bit element 7", 7U, testVector[7]);
127  check(m_failures, ++m_testNum, "Access 4 bit element 8", 8U, testVector[8]);
128  check(m_failures, ++m_testNum, "Access 4 bit element 9", 9U, testVector[9]);
129  check(m_failures, ++m_testNum, "Access 4 bit element 10", 10U, testVector[10]);
130 }
131 
132 void PackedArrayTest::testResize(void)
133 {
134  PackedVector4Bit_t testVector;
135 
136  testVector.resize(0);
137  check(m_failures, ++m_testNum, "New size is 0", 0U, testVector.size());
138 
139  testVector.push_back(0);
140  testVector.push_back(1);
141  testVector.push_back(2);
142  testVector.push_back(3);
143  testVector.push_back(4);
144  testVector.push_back(5);
145  testVector.push_back(6);
146  testVector.push_back(7);
147  testVector.push_back(8);
148  testVector.push_back(9);
149  testVector.push_back(10);
150 
151  check(m_failures, ++m_testNum, "New size is 11", 11U, testVector.size());
152 
153  check(m_failures, ++m_testNum, "Access 4 bit element 0", 0U, testVector[0]);
154  check(m_failures, ++m_testNum, "Access 4 bit element 1", 1U, testVector[1]);
155  check(m_failures, ++m_testNum, "Access 4 bit element 2", 2U, testVector[2]);
156  check(m_failures, ++m_testNum, "Access 4 bit element 3", 3U, testVector[3]);
157  check(m_failures, ++m_testNum, "Access 4 bit element 4", 4U, testVector[4]);
158  check(m_failures, ++m_testNum, "Access 4 bit element 5", 5U, testVector[5]);
159  check(m_failures, ++m_testNum, "Access 4 bit element 6", 6U, testVector[6]);
160  check(m_failures, ++m_testNum, "Access 4 bit element 7", 7U, testVector[7]);
161  check(m_failures, ++m_testNum, "Access 4 bit element 8", 8U, testVector[8]);
162  check(m_failures, ++m_testNum, "Access 4 bit element 9", 9U, testVector[9]);
163  check(m_failures, ++m_testNum, "Access 4 bit element 10", 10U, testVector[10]);
164 }
165 
166 int main(int argc, char **argv)
167 {
168  PackedArrayTest test("PackedArrayTest");
169 #if 0
170  bool showAllCasesFlag = false;
171  int opt;
172 
173  while(( opt = getopt(argc, (char **) argv, "v")) != -1) {
174  switch(opt) {
175  case 'v':
176  showAllCasesFlag = true;
177  break;
178  default:
179  std::cerr << "usage: testSW [-v]" << std::endl;
180  exit(1);
181  }
182  }
183 
184 #endif
185 
186  test.test();
187 
188  std::cout << test;
189 
190  exit(test.getFailureCount());
191 }
PackedArrayTest
Definition: PackedVectorTest.cpp:28
PackedVector
Definition: PackedVector.h:42
UnitTest
Definition: MemoryMapArrayTest.h:21