libStatGen Software  1
MemoryMapArray.cpp
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <stdio.h>
00019 #include "MemoryMapArray.h"
00020 
00021 void MemoryMapArrayHeader::debugPrint(FILE *f)
00022 {
00023     time_t local = creationDate;
00024     fprintf(f, "typeCookie = %08x\n", typeCookie);
00025     fprintf(f, "typeVersion = %08x\n", typeVersion);
00026     fprintf(f, "contentCookie = %08x\n", contentCookie);
00027     fprintf(f, "contentVersion = %08x\n", contentVersion);
00028     fprintf(f, "Created on %s", asctime(localtime(&local)));
00029     fprintf(f, "Created by user %s on host %s for application '%s'.\n",
00030             creationUser,
00031             creationHost,
00032             application);
00033 }
00034 
00035 std::ostream &operator << (std::ostream &stream, MemoryMapArrayHeader &h)
00036 {
00037     time_t local = h.creationDate;
00038     stream << "typeCookie = " << h.typeCookie << "\n";
00039     stream << "typeVersion = " << h.typeVersion << "\n";
00040     stream << "contentCookie = " << h.contentCookie << "\n";
00041     stream << "contentVersion = " << h.contentVersion << "\n";
00042     stream << "headerSize = " << h.headerSize << "\n";
00043     stream << "elementCount = " << h.elementCount << "\n";
00044 
00045     stream << "Created on " << asctime(localtime(&local)) << "\n";
00046     stream << "Created by user " << h.creationUser << " on host " << h.creationHost << " for application '" << h.application << "'.\n";
00047     return stream;
00048 }
00049 
00050 #if defined(TEST)
00051 #include <assert.h>
00052 #include <stdlib.h>
00053 
00054 void test32()
00055 {
00056     mmapArrayUint32_t   test;
00057 
00058     unlink("twinkypie");
00059     assert(test.create("twinkypie", 11)==0);
00060     test.set(0,0);
00061     test.set(1,1);
00062     test.set(2,2);
00063     test.set(3,3);
00064     test.set(4,4);
00065     test.set(5,5);
00066     test.set(6,6);
00067     test.set(7,7);
00068     test.set(8,8);
00069     test.set(9,9);
00070     test.set(10,10);
00071     assert(test[0]==0);
00072     assert(test[10]==10);
00073     test.close();
00074     assert(test.open("twinkypie")==0);
00075     assert(test[0]==0);
00076     assert(test[10]==10);
00077     test.close();
00078     unlink("twinkypie");
00079 }
00080 
00081 void testbool()
00082 {
00083     mmapArrayBool_t   test;
00084 
00085     unlink("twinkypie");
00086     assert(test.create("twinkypie", 11)==0);
00087     test.set(0,0);
00088     test.set(1,1);
00089     test.set(2,0);
00090     test.set(3,1);
00091     test.set(4,0);
00092     test.set(5,1);
00093     test.set(6,0);
00094     test.set(7,1);
00095     test.set(8,0);
00096     test.set(9,0);
00097     test.set(10,1);
00098     assert(test[0]==0);
00099     assert(test[1]==1);
00100     assert(test[10]==1);
00101     test.close();
00102     assert(test.open("twinkypie")==0);
00103     assert(test[0]==0);
00104     assert(test[10]==1);
00105     test.close();
00106     unlink("twinkypie");
00107 }
00108 
00109 void test2bit()
00110 {
00111     mmapArray2Bit_t   test;
00112 
00113     unlink("twinkypie");
00114     assert(test.create("twinkypie", 11)==0);
00115     test.set(0,0);
00116     test.set(1,1);
00117     test.set(2,2);
00118     test.set(3,3);
00119     test.set(4,3);
00120     test.set(5,2);
00121     test.set(6,1);
00122     test.set(7,0);
00123     test.set(8,2);
00124     test.set(9,1);
00125     test.set(10,3);
00126     test.setApplication("testing 2 bit values!");
00127     assert(test[0]==0);
00128     assert(test[1]==1);
00129     assert(test[2]==2);
00130     assert(test[3]==3);
00131     assert(test[4]==3);
00132     assert(test[5]==2);
00133     assert(test[6]==1);
00134     assert(test[7]==0);
00135     assert(test[8]==2);
00136     assert(test[9]==1);
00137     assert(test[10]==3);
00138     test.close();
00139     assert(test.open("twinkypie")==0);
00140     test.debugPrint(stdout);
00141     test.close();
00142     unlink("twinkypie");
00143 }
00144 
00145 void test4bit()
00146 {
00147     mmapArray4Bit_t   test;
00148 
00149     unlink("twinkypie");
00150     assert(test.create("twinkypie", 11)==0);
00151     test.set(0,0);
00152     test.set(1,1);
00153     test.set(2,2);
00154     test.set(3,3);
00155     test.set(4,4);
00156     test.set(5,5);
00157     test.set(6,6);
00158     test.set(7,7);
00159     test.set(8,8);
00160     test.set(9,9);
00161     test.set(10,10);
00162     test.setApplication("testing 4 bit values!");
00163     assert(test[0]==0);
00164     assert(test[1]==1);
00165     assert(test[7]==7);
00166     assert(test[10]==10);
00167     test.close();
00168     assert(test.open("twinkypie")==0);
00169     assert(test[0]==0);
00170     assert(test[1]==1);
00171     assert(test[7]==7);
00172     assert(test[10]==10);
00173     test.debugPrint(stdout);
00174     test.close();
00175     unlink("twinkypie");
00176 }
00177 
00178 int main(int argc, char **argv)
00179 {
00180 
00181     test32();
00182     testbool();
00183     test2bit();
00184     test4bit();
00185     exit(0);
00186 }
00187 
00188 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends