more fixes, macros conflicts
[SXSI/XMLTree.git] / libcds / tests / static_bitsequence_tester.cpp
1 /* static_bitsequence_tester.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * static_bitsequence_tester
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <iostream>
23 #include <algorithm>
24 #include <sstream>
25 #include <basics.h>
26 #include <static_bitsequence.h>
27
28 using namespace std;
29
30 /* Time meassuring */
31 double ticks= (double)sysconf(_SC_CLK_TCK);
32 struct tms t1,t2;
33
34 void start_clock() {
35   times (&t1);
36 }
37
38
39 double stop_clock() {
40   times (&t2);
41   return (t2.tms_utime-t1.tms_utime)/ticks;
42 }
43 /* end Time meassuring */
44
45 uint NQUERIES=10000000;
46 uint SEED=47;
47
48 void load(char *fname, uint ** text, uint * n) {
49   FILE * fp = fopen(fname,"r");
50   if(fp==NULL) {
51     cout << "could not open " << fname << endl;
52     return;
53   }
54   if(fread(n,sizeof(uint),1,fp)!=1) {
55     cout << "Error reading file " << fname << endl;
56     return;
57   }
58   *text = new uint[uint_len(*n,1)];
59
60   if(fread(*text,sizeof(uint),uint_len(*n,1),fp)!=uint_len(*n,1)) {
61     cout << "Error reading file " << fname << endl;
62     return;
63   }
64 }
65
66 void test_bitsequence(uint * bitseq, uint len, static_bitsequence * bs) {
67   uint ones = 0;
68   bool error = false;
69   for(uint i=0;i<len && !error;i++) {
70     //cout << "i="<<i<< endl;
71     if(i>0) {
72       if(i%max((uint)1,(bs->length()/100))==0) { cout << "."; cout.flush(); }
73       if(i%max((uint)1,(bs->length()/10))==0) { cout << endl; cout.flush(); }
74     }
75     if(bitget(bitseq,i)) ones++;
76     if(bs->access(i) != (bitget(bitseq,i)!=0)) {
77       cout << "Access error for position " << i << endl;
78       cout << " got: " << bs->access(i) << " expected: " << (bitget(bitseq,i)!=0) << endl;
79       error = true;
80     }
81     if(bs->rank1(i) != ones) {
82       cout << "Rank1 error for position " << i << endl;
83       cout << " got: " << bs->rank1(i) << " expected: " << ones << endl;
84       error = true;
85     } 
86     if(bitget(bitseq,i) && bs->select1(ones) != i) {
87       cout << "Select1 error for position " << i << " ones:" << ones << endl;
88       cout << " got: " << bs->select1(ones) << " expected: " << i << endl;
89       error = true;
90     }
91     if(bs->rank0(i) != i+1-ones) {
92       cout << "Rank0 error for position " << i << endl;
93       cout << " got: " << bs->rank0(i) << " expected: " << ones << endl;
94       error = true;
95     }
96     if(!bitget(bitseq,i) && bs->select0(i+1-ones) != i) {
97       cout << "Select0 error for position " << i << endl;
98       cout << " got: " << bs->select0(i+1-ones) << " expected: " << i << endl;
99       error = true;
100     }
101   }
102         cout << "." << endl;
103 }
104
105 void speed_access(static_bitsequence * ss, uint * bitseq, uint n) {
106   uint acc=0;
107   srand(SEED);
108
109   start_clock();
110   for(uint i=0;i<NQUERIES;i++) {
111     uint pos = rand()%n;
112     acc += ss->access(pos);
113   }
114   double t = stop_clock();
115   cout << " * Time for " << NQUERIES << " accesses: " << t << " secs" << endl;
116   cout << " * Time per access: " << 1000*t/NQUERIES << " msecs" << endl;
117   cout << " - Check sum: " << acc << endl;
118 }
119
120 void speed_rank0(static_bitsequence * ss, uint * bitseq, uint n) {
121   uint acc=0;
122   srand(SEED);
123
124   start_clock();
125   for(uint i=0;i<NQUERIES;i++) {
126     uint pos = rand()%n;
127     acc += ss->rank0(pos);
128   }
129   double t = stop_clock();
130   cout << " * Time for " << NQUERIES << " rank0s: " << t << " secs" << endl;
131   cout << " * Time per rank0: " << 1000*t/NQUERIES << " msecs" << endl;
132   cout << " - Check sum: " << acc << endl;
133 }
134
135 void speed_rank1(static_bitsequence * ss, uint * bitseq, uint n) {
136   uint acc=0;
137   srand(SEED);
138
139   start_clock();
140   for(uint i=0;i<NQUERIES;i++) {
141     uint pos = rand()%n;
142     acc += ss->rank1(pos);
143   }
144   double t = stop_clock();
145   cout << " * Time for " << NQUERIES << " rank1s: " << t << " secs" << endl;
146   cout << " * Time per rank1: " << 1000*t/NQUERIES << " msecs" << endl;
147   cout << " - Check sum: " << acc << endl;
148 }
149
150 void speed_select0(static_bitsequence * ss, uint * bitseq, uint n) {
151   uint acc=0;
152   uint ones=ss->rank0(n-1);
153   srand(SEED);
154
155   start_clock();
156   for(uint i=0;i<NQUERIES;i++) {
157     uint pos = rand()%ones+1;
158     acc += ss->select0(pos);
159   }
160   double t = stop_clock();
161   cout << " * Time for " << NQUERIES << " select0s: " << t << " secs" << endl;
162   cout << " * Time per select0: " << 1000*t/NQUERIES << " msecs" << endl;
163   cout << " - Check sum: " << acc << endl;
164 }
165
166 void speed_select1(static_bitsequence * ss, uint * bitseq, uint n) {
167   uint acc=0;
168   uint ones=ss->rank1(n-1);
169   srand(SEED);
170
171   start_clock();
172   for(uint i=0;i<NQUERIES;i++) {
173     uint pos = rand()%ones+1;
174     acc += ss->select1(pos);
175   }
176   double t = stop_clock();
177   cout << " * Time for " << NQUERIES << " select1s: " << t << " secs" << endl;
178   cout << " * Time per select1: " << 1000*t/NQUERIES << " msecs" << endl;
179   cout << " - Check sum: " << acc << endl;
180 }