libStatGen Software  1
MemoryMapArray.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 <stdio.h>
19 #include "MemoryMapArray.h"
20 
21 void MemoryMapArrayHeader::debugPrint(FILE *f)
22 {
23  time_t local = creationDate;
24  fprintf(f, "typeCookie = %08x\n", typeCookie);
25  fprintf(f, "typeVersion = %08x\n", typeVersion);
26  fprintf(f, "contentCookie = %08x\n", contentCookie);
27  fprintf(f, "contentVersion = %08x\n", contentVersion);
28  fprintf(f, "Created on %s", asctime(localtime(&local)));
29  fprintf(f, "Created by user %s on host %s for application '%s'.\n",
30  creationUser,
31  creationHost,
32  application);
33 }
34 
35 std::ostream &operator << (std::ostream &stream, MemoryMapArrayHeader &h)
36 {
37  time_t local = h.creationDate;
38  stream << "typeCookie = " << h.typeCookie << "\n";
39  stream << "typeVersion = " << h.typeVersion << "\n";
40  stream << "contentCookie = " << h.contentCookie << "\n";
41  stream << "contentVersion = " << h.contentVersion << "\n";
42  stream << "headerSize = " << h.headerSize << "\n";
43  stream << "elementCount = " << h.elementCount << "\n";
44 
45  stream << "Created on " << asctime(localtime(&local)) << "\n";
46  stream << "Created by user " << h.creationUser << " on host " << h.creationHost << " for application '" << h.application << "'.\n";
47  return stream;
48 }
49 
50 #if defined(TEST)
51 #include <assert.h>
52 #include <stdlib.h>
53 
54 void test32()
55 {
56  mmapArrayUint32_t test;
57 
58  unlink("twinkypie");
59  assert(test.create("twinkypie", 11)==0);
60  test.set(0,0);
61  test.set(1,1);
62  test.set(2,2);
63  test.set(3,3);
64  test.set(4,4);
65  test.set(5,5);
66  test.set(6,6);
67  test.set(7,7);
68  test.set(8,8);
69  test.set(9,9);
70  test.set(10,10);
71  assert(test[0]==0);
72  assert(test[10]==10);
73  test.close();
74  assert(test.open("twinkypie")==0);
75  assert(test[0]==0);
76  assert(test[10]==10);
77  test.close();
78  unlink("twinkypie");
79 }
80 
81 void testbool()
82 {
83  mmapArrayBool_t test;
84 
85  unlink("twinkypie");
86  assert(test.create("twinkypie", 11)==0);
87  test.set(0,0);
88  test.set(1,1);
89  test.set(2,0);
90  test.set(3,1);
91  test.set(4,0);
92  test.set(5,1);
93  test.set(6,0);
94  test.set(7,1);
95  test.set(8,0);
96  test.set(9,0);
97  test.set(10,1);
98  assert(test[0]==0);
99  assert(test[1]==1);
100  assert(test[10]==1);
101  test.close();
102  assert(test.open("twinkypie")==0);
103  assert(test[0]==0);
104  assert(test[10]==1);
105  test.close();
106  unlink("twinkypie");
107 }
108 
109 void test2bit()
110 {
111  mmapArray2Bit_t test;
112 
113  unlink("twinkypie");
114  assert(test.create("twinkypie", 11)==0);
115  test.set(0,0);
116  test.set(1,1);
117  test.set(2,2);
118  test.set(3,3);
119  test.set(4,3);
120  test.set(5,2);
121  test.set(6,1);
122  test.set(7,0);
123  test.set(8,2);
124  test.set(9,1);
125  test.set(10,3);
126  test.setApplication("testing 2 bit values!");
127  assert(test[0]==0);
128  assert(test[1]==1);
129  assert(test[2]==2);
130  assert(test[3]==3);
131  assert(test[4]==3);
132  assert(test[5]==2);
133  assert(test[6]==1);
134  assert(test[7]==0);
135  assert(test[8]==2);
136  assert(test[9]==1);
137  assert(test[10]==3);
138  test.close();
139  assert(test.open("twinkypie")==0);
140  test.debugPrint(stdout);
141  test.close();
142  unlink("twinkypie");
143 }
144 
145 void test4bit()
146 {
147  mmapArray4Bit_t test;
148 
149  unlink("twinkypie");
150  assert(test.create("twinkypie", 11)==0);
151  test.set(0,0);
152  test.set(1,1);
153  test.set(2,2);
154  test.set(3,3);
155  test.set(4,4);
156  test.set(5,5);
157  test.set(6,6);
158  test.set(7,7);
159  test.set(8,8);
160  test.set(9,9);
161  test.set(10,10);
162  test.setApplication("testing 4 bit values!");
163  assert(test[0]==0);
164  assert(test[1]==1);
165  assert(test[7]==7);
166  assert(test[10]==10);
167  test.close();
168  assert(test.open("twinkypie")==0);
169  assert(test[0]==0);
170  assert(test[1]==1);
171  assert(test[7]==7);
172  assert(test[10]==10);
173  test.debugPrint(stdout);
174  test.close();
175  unlink("twinkypie");
176 }
177 
178 int main(int argc, char **argv)
179 {
180 
181  test32();
182  testbool();
183  test2bit();
184  test4bit();
185  exit(0);
186 }
187 
188 #endif
MemoryMapArrayHeader
Definition: MemoryMapArray.h:65
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:142
MemoryMapArray::create
int create(const char *file, indexT elementCount, int optionalHeaderCount=0)
Create a vector with elementCount memebers.
Definition: MemoryMapArray.h:208
operator<<
InputFile & operator<<(InputFile &stream, const std::string &str)
Write to a file using streaming.
Definition: InputFile.h:736