libStatGen Software  1
MemoryMapArrayTest.cpp
1 /*
2  * Copyright (C) 2010-2012 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 "MemoryMapArray.h"
22 #include "MemoryMapArrayTest.h"
23 
24 #include <assert.h>
25 #include <stdlib.h>
26 
27 #define TEST_FILE_NAME "results/testMemoryMapArray.vector"
28 
30 {
31 public:
32  MemoryMapArrayTest(const char *title) : UnitTest(title) {;}
33  void testBool();
34  void test2Bit();
35  void test4Bit();
36  void test32Bit();
37 
38  void test() {
39  testBool();
40  test2Bit();
41  test4Bit();
42  test32Bit();
43  }
44 };
45 
46 void MemoryMapArrayTest::testBool(void)
47 {
48  mmapArrayBool_t testVector;
49 
50  // ignore return code here
51  if(unlink(TEST_FILE_NAME) == 0)
52  {
53  // Nothing to do, just deleting previous test file
54  }
55 
56 
57  check(m_failures, ++m_testNum, "Create 1 bit vector file", 0,
58  testVector.create(TEST_FILE_NAME, 11));
59  testVector.set(0,0);
60  testVector.set(1,1);
61  testVector.set(2,0);
62  testVector.set(3,1);
63  testVector.set(4,1);
64  testVector.set(5,0);
65  testVector.set(6,1);
66  testVector.set(7,0);
67  testVector.set(8,0);
68  testVector.set(9,0);
69  testVector.set(10,1);
70  check(m_failures, ++m_testNum, "Access 1 bit element 0", 0U, testVector[0]);
71  check(m_failures, ++m_testNum, "Access 1 bit element 1", 1U, testVector[1]);
72  check(m_failures, ++m_testNum, "Access 1 bit element 2", 0U, testVector[2]);
73  check(m_failures, ++m_testNum, "Access 1 bit element 3", 1U, testVector[3]);
74  check(m_failures, ++m_testNum, "Access 1 bit element 4", 1U, testVector[4]);
75  check(m_failures, ++m_testNum, "Access 1 bit element 5", 0U, testVector[5]);
76  check(m_failures, ++m_testNum, "Access 1 bit element 6", 1U, testVector[6]);
77  check(m_failures, ++m_testNum, "Access 1 bit element 7", 0U, testVector[7]);
78  check(m_failures, ++m_testNum, "Access 1 bit element 8", 0U, testVector[8]);
79  check(m_failures, ++m_testNum, "Access 1 bit element 9", 0U, testVector[9]);
80  check(m_failures, ++m_testNum, "Access 1 bit element 10", 1U, testVector[10]);
81  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
82  check(m_failures, ++m_testNum, "Re-open vector file", false, testVector.open(TEST_FILE_NAME));
83  check(m_failures, ++m_testNum, "Access 1 bit element 0", 0U, testVector[0]);
84  check(m_failures, ++m_testNum, "Access 1 bit element 1", 1U, testVector[1]);
85  check(m_failures, ++m_testNum, "Access 1 bit element 2", 0U, testVector[2]);
86  check(m_failures, ++m_testNum, "Access 1 bit element 3", 1U, testVector[3]);
87  check(m_failures, ++m_testNum, "Access 1 bit element 4", 1U, testVector[4]);
88  check(m_failures, ++m_testNum, "Access 1 bit element 5", 0U, testVector[5]);
89  check(m_failures, ++m_testNum, "Access 1 bit element 6", 1U, testVector[6]);
90  check(m_failures, ++m_testNum, "Access 1 bit element 7", 0U, testVector[7]);
91  check(m_failures, ++m_testNum, "Access 1 bit element 8", 0U, testVector[8]);
92  check(m_failures, ++m_testNum, "Access 1 bit element 9", 0U, testVector[9]);
93  check(m_failures, ++m_testNum, "Access 1 bit element 10", 1U, testVector[10]);
94 
95  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
96  check(m_failures, ++m_testNum, "Unlink vector file", 0, unlink(TEST_FILE_NAME));
97 
98 
99 }
100 
101 void MemoryMapArrayTest::test2Bit(void)
102 {
103  mmapArray2Bit_t testVector;
104 
105  // ignore return code here
106  if(unlink(TEST_FILE_NAME) == 0)
107  {
108  // Nothing to do, just deleting previous test file
109  }
110 
111  check(m_failures, ++m_testNum, "Create 2 bit vector file", 0,
112  testVector.create(TEST_FILE_NAME, 11));
113 
114  testVector.set(0,0);
115  testVector.set(1,1);
116  testVector.set(2,2);
117  testVector.set(3,3);
118  testVector.set(4,3);
119  testVector.set(5,2);
120  testVector.set(6,1);
121  testVector.set(7,0);
122  testVector.set(8,2);
123  testVector.set(9,1);
124  testVector.set(10,3);
125  check(m_failures, ++m_testNum, "Access 2 bit element 0", 0U, testVector[0]);
126  check(m_failures, ++m_testNum, "Access 2 bit element 1", 1U, testVector[1]);
127  check(m_failures, ++m_testNum, "Access 2 bit element 2", 2U, testVector[2]);
128  check(m_failures, ++m_testNum, "Access 2 bit element 3", 3U, testVector[3]);
129  check(m_failures, ++m_testNum, "Access 2 bit element 4", 3U, testVector[4]);
130  check(m_failures, ++m_testNum, "Access 2 bit element 5", 2U, testVector[5]);
131  check(m_failures, ++m_testNum, "Access 2 bit element 6", 1U, testVector[6]);
132  check(m_failures, ++m_testNum, "Access 2 bit element 7", 0U, testVector[7]);
133  check(m_failures, ++m_testNum, "Access 2 bit element 8", 2U, testVector[8]);
134  check(m_failures, ++m_testNum, "Access 2 bit element 9", 1U, testVector[9]);
135  check(m_failures, ++m_testNum, "Access 2 bit element 10", 3U, testVector[10]);
136  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
137  check(m_failures, ++m_testNum, "Re-open vector file", false, testVector.open(TEST_FILE_NAME));
138  check(m_failures, ++m_testNum, "Access 2 bit element 0", 0U, testVector[0]);
139  check(m_failures, ++m_testNum, "Access 2 bit element 1", 1U, testVector[1]);
140  check(m_failures, ++m_testNum, "Access 2 bit element 2", 2U, testVector[2]);
141  check(m_failures, ++m_testNum, "Access 2 bit element 3", 3U, testVector[3]);
142  check(m_failures, ++m_testNum, "Access 2 bit element 4", 3U, testVector[4]);
143  check(m_failures, ++m_testNum, "Access 2 bit element 5", 2U, testVector[5]);
144  check(m_failures, ++m_testNum, "Access 2 bit element 6", 1U, testVector[6]);
145  check(m_failures, ++m_testNum, "Access 2 bit element 7", 0U, testVector[7]);
146  check(m_failures, ++m_testNum, "Access 2 bit element 8", 2U, testVector[8]);
147  check(m_failures, ++m_testNum, "Access 2 bit element 9", 1U, testVector[9]);
148  check(m_failures, ++m_testNum, "Access 2 bit element 10", 3U, testVector[10]);
149 
150  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
151  check(m_failures, ++m_testNum, "Unlink vector file", 0, unlink(TEST_FILE_NAME));
152 
153 }
154 
155 void MemoryMapArrayTest::test4Bit(void)
156 {
157  mmapArray4Bit_t testVector;
158 
159  // ignore return code here
160  if(unlink(TEST_FILE_NAME) == 0)
161  {
162  // Nothing to do, just deleting previous test file
163  }
164 
165  check(m_failures, ++m_testNum, "Create 4 bit vector file", 0,
166  testVector.create(TEST_FILE_NAME, 11));
167 
168  testVector.set(0,0);
169  testVector.set(1,1);
170  testVector.set(2,2);
171  testVector.set(3,3);
172  testVector.set(4,4);
173  testVector.set(5,5);
174  testVector.set(6,6);
175  testVector.set(7,7);
176  testVector.set(8,8);
177  testVector.set(9,9);
178  testVector.set(10,10);
179 
180  check(m_failures, ++m_testNum, "Access 4 bit element 0", 0U, testVector[0]);
181  check(m_failures, ++m_testNum, "Access 4 bit element 1", 1U, testVector[1]);
182  check(m_failures, ++m_testNum, "Access 4 bit element 2", 2U, testVector[2]);
183  check(m_failures, ++m_testNum, "Access 4 bit element 3", 3U, testVector[3]);
184  check(m_failures, ++m_testNum, "Access 4 bit element 4", 4U, testVector[4]);
185  check(m_failures, ++m_testNum, "Access 4 bit element 5", 5U, testVector[5]);
186  check(m_failures, ++m_testNum, "Access 4 bit element 6", 6U, testVector[6]);
187  check(m_failures, ++m_testNum, "Access 4 bit element 7", 7U, testVector[7]);
188  check(m_failures, ++m_testNum, "Access 4 bit element 8", 8U, testVector[8]);
189  check(m_failures, ++m_testNum, "Access 4 bit element 9", 9U, testVector[9]);
190  check(m_failures, ++m_testNum, "Access 4 bit element 10", 10U, testVector[10]);
191 
192  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
193  check(m_failures, ++m_testNum, "Re-open vector file", false, testVector.open(TEST_FILE_NAME));
194  check(m_failures, ++m_testNum, "Access 4 bit element 0", 0U, testVector[0]);
195  check(m_failures, ++m_testNum, "Access 4 bit element 1", 1U, testVector[1]);
196  check(m_failures, ++m_testNum, "Access 4 bit element 2", 2U, testVector[2]);
197  check(m_failures, ++m_testNum, "Access 4 bit element 3", 3U, testVector[3]);
198  check(m_failures, ++m_testNum, "Access 4 bit element 4", 4U, testVector[4]);
199  check(m_failures, ++m_testNum, "Access 4 bit element 5", 5U, testVector[5]);
200  check(m_failures, ++m_testNum, "Access 4 bit element 6", 6U, testVector[6]);
201  check(m_failures, ++m_testNum, "Access 4 bit element 7", 7U, testVector[7]);
202  check(m_failures, ++m_testNum, "Access 4 bit element 8", 8U, testVector[8]);
203  check(m_failures, ++m_testNum, "Access 4 bit element 9", 9U, testVector[9]);
204  check(m_failures, ++m_testNum, "Access 4 bit element 10", 10U, testVector[10]);
205 
206  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
207  check(m_failures, ++m_testNum, "Unlink vector file", 0, unlink(TEST_FILE_NAME));
208 }
209 
210 void MemoryMapArrayTest::test32Bit(void)
211 {
212 
213  mmapArrayUint32_t testVector;
214 
215  // ignore return code here
216  if(unlink(TEST_FILE_NAME) == 0)
217  {
218  // Nothing to do, just deleting previous test file
219  }
220 
221  check(m_failures, ++m_testNum, "Create 32 bit vector file", 0,
222  testVector.create(TEST_FILE_NAME, 11));
223 
224  testVector.set(0,0);
225  testVector.set(1,1);
226  testVector.set(2,2);
227  testVector.set(3,3);
228  testVector.set(4,4);
229  testVector.set(5,5);
230  testVector.set(6,6);
231  testVector.set(7,7);
232  testVector.set(8,8);
233  testVector.set(9,9);
234  testVector.set(10,10);
235  check(m_failures, ++m_testNum, "Access 32 bit element 0", 0U, testVector[0]);
236  check(m_failures, ++m_testNum, "Access 32 bit element 1", 1U, testVector[1]);
237  check(m_failures, ++m_testNum, "Access 32 bit element 2", 2U, testVector[2]);
238  check(m_failures, ++m_testNum, "Access 32 bit element 3", 3U, testVector[3]);
239  check(m_failures, ++m_testNum, "Access 32 bit element 4", 4U, testVector[4]);
240  check(m_failures, ++m_testNum, "Access 32 bit element 5", 5U, testVector[5]);
241  check(m_failures, ++m_testNum, "Access 32 bit element 6", 6U, testVector[6]);
242  check(m_failures, ++m_testNum, "Access 32 bit element 7", 7U, testVector[7]);
243  check(m_failures, ++m_testNum, "Access 32 bit element 8", 8U, testVector[8]);
244  check(m_failures, ++m_testNum, "Access 32 bit element 9", 9U, testVector[9]);
245  check(m_failures, ++m_testNum, "Access 32 bit element 10", 10U, testVector[10]);
246 
247 
248  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
249  check(m_failures, ++m_testNum, "Re-open vector file", false, testVector.open(TEST_FILE_NAME));
250  check(m_failures, ++m_testNum, "Access 32 bit element 0", 0U, testVector[0]);
251  check(m_failures, ++m_testNum, "Access 32 bit element 1", 1U, testVector[1]);
252  check(m_failures, ++m_testNum, "Access 32 bit element 2", 2U, testVector[2]);
253  check(m_failures, ++m_testNum, "Access 32 bit element 3", 3U, testVector[3]);
254  check(m_failures, ++m_testNum, "Access 32 bit element 4", 4U, testVector[4]);
255  check(m_failures, ++m_testNum, "Access 32 bit element 5", 5U, testVector[5]);
256  check(m_failures, ++m_testNum, "Access 32 bit element 6", 6U, testVector[6]);
257  check(m_failures, ++m_testNum, "Access 32 bit element 7", 7U, testVector[7]);
258  check(m_failures, ++m_testNum, "Access 32 bit element 8", 8U, testVector[8]);
259  check(m_failures, ++m_testNum, "Access 32 bit element 9", 9U, testVector[9]);
260  check(m_failures, ++m_testNum, "Access 32 bit element 10", 10U, testVector[10]);
261 
262  check(m_failures, ++m_testNum, "Close vector file", false, testVector.close());
263  check(m_failures, ++m_testNum, "Unlink vector file", 0, unlink(TEST_FILE_NAME));
264 }
265 
266 int main(int argc, char **argv)
267 {
268  MemoryMapArrayTest test("MemoryMapArrayTest");
269 #if 0
270  bool showAllCasesFlag = false;
271  int opt;
272 
273  while(( opt = getopt(argc, (char **) argv, "v")) != -1) {
274  switch(opt) {
275  case 'v':
276  showAllCasesFlag = true;
277  break;
278  default:
279  std::cerr << "usage: testSW [-v]" << std::endl;
280  exit(1);
281  }
282  }
283 
284 #endif
285 
286  test.test();
287 
288  std::cout << test;
289 
290  exit(test.getFailureCount());
291 }
UnitTest
Definition: MemoryMapArrayTest.h:20
MemoryMapArrayTest
Definition: MemoryMapArrayTest.cpp:29
MemoryMapArray::open
bool open(const char *file, int flags=O_RDONLY)
open a previously created mapped vector
Definition: MemoryMapArray.h:269
MemoryMapArray
Definition: MemoryMapArray.h:141
MemoryMapArray::create
int create(const char *file, indexT elementCount, int optionalHeaderCount=0)
Create a vector with elementCount memebers.
Definition: MemoryMapArray.h:208