-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathunit.cpp
2132 lines (1990 loc) · 65.5 KB
/
unit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// See LICENSE file for license information.
#define EWAHASSERT
#include "ewah/boolarray.h"
#include "ewah/ewah.h"
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
#define SSTR(x) (to_string(x))
using namespace std;
using namespace ewah;
static string testfailed = "---\ntest failed.\n\n\n\n\n\n";
// for Microsoft compilers
#if _MSC_VER >= 1400
#define unlink _unlink
#endif
template <class uword> bool testAndNotCompactionEWAHBoolArray() {
cout << "[testing testAndNotCompactionEWAHBoolArray] sizeof(uword)="
<< sizeof(uword) << endl;
EWAHBoolArray<uword> one;
EWAHBoolArray<uword> other;
one.set(71);
other.set(130);
other = other.logicaland(one);
one = one.logicalandnot(other);
one.set(155);
other.set(251);
if (*other.begin() != 251) {
return false;
}
other = other.logicalor(one);
// {71,131,155}
one = one.logicaland(other);
// {155}
other = other.logicalandnot(one);
typename EWAHBoolArray<uword>::const_iterator i = other.begin();
auto val = *i;
return val == 251;
}
template <class uword> bool testInEqualityEWAHBoolArray() {
cout << "[testing testInEqualityEWAHBoolArray] sizeof(uword)="
<< sizeof(uword) << endl;
EWAHBoolArray<uword> b1;
EWAHBoolArray<uword> b = EWAHBoolArray<uword>::bitmapOf(3, 1, 10, 11);
EWAHBoolArray<uword> b3 = EWAHBoolArray<uword>::bitmapOf(3, 1, 10, 11);
EWAHBoolArray<uword> b4 = EWAHBoolArray<uword>::bitmapOf(4, 1, 10, 11, 10000);
if (b3 != b) {
cout << "should be equal!" << endl;
return false;
}
if (b == b1) {
cout << "should be different!" << endl;
return false;
}
if (b4 == b1) {
cout << "should be different!" << endl;
return false;
}
if (b4 == b3) {
cout << "should be different!" << endl;
return false;
}
return true;
}
template <class uword> bool testEmpty() {
cout << "[testing SizeInBits] sizeof(uword)=" << sizeof(uword) << endl;
EWAHBoolArray<uword> ewah0;
if (!ewah0.empty()) {
std::cout << "empty is buggy " << std::endl;
return false;
}
EWAHBoolArray<uword> ewah1 = EWAHBoolArray<uword>::bitmapOf(1, 1);
if (ewah1.empty()) {
std::cout << "empty is buggy " << std::endl;
return false;
}
return true;
}
template <class uword> bool testSizeInBits() {
cout << "[testing SizeInBits] sizeof(uword)=" << sizeof(uword) << endl;
EWAHBoolArray<uword> ewah1 = EWAHBoolArray<uword>::bitmapOf(1, 1);
EWAHBoolArray<uword> ewah2 = EWAHBoolArray<uword>::bitmapOf(1, 2);
EWAHBoolArray<uword> ewah3 = EWAHBoolArray<uword>::bitmapOf(1, 3);
EWAHBoolArray<uword> ewah4 = EWAHBoolArray<uword>::bitmapOf(1, 4);
EWAHBoolArray<uword> result1 = ewah1 | ewah2 | ewah3 | ewah4;
EWAHBoolArray<uword> result12 = ewah1 & ewah2 & ewah3 & ewah4;
EWAHBoolArray<uword> result13 = ewah1 ^ ewah2 ^ ewah3 ^ ewah4;
EWAHBoolArray<uword> result2 = EWAHBoolArray<uword>::bitmapOf(4, 1, 2, 3, 4);
if (result1 != result2) {
cout << testfailed << endl;
std::cout << "should be equal " << std::endl;
return false;
}
if ((result1.sizeInBits() != result2.sizeInBits()) ||
(result12.sizeInBits() != result2.sizeInBits()) ||
(result13.sizeInBits() != result2.sizeInBits())) {
cout << testfailed << endl;
std::cout << "bad sizeInBits " << std::endl;
return false;
}
return true;
}
template <class uword> bool testSerialSize() {
cout << "[testing SerialSize] sizeof(uword)=" << sizeof(uword) << endl;
size_t bitval = 55555;
EWAHBoolArray<uword> b =
EWAHBoolArray<uword>::bitmapOf(1, bitval); // set just one bit
// generic write method in EWAHBoolArray can be wasteful for single bits...
stringstream ss;
b.write(ss);
size_t compressedsize = ss.tellp();
size_t expectedsize = b.sizeOnDisk();
if (compressedsize != expectedsize) {
cout << "bad size, was expected " << expectedsize << " got "
<< compressedsize << endl;
return false; // unexpected size
}
return true;
}
template <class uword> bool testCardinalityBoolArray() {
cout << "[testing CardinalityBoolArray] sizeof(uword)=" << sizeof(uword)
<< endl;
BoolArray<uword> b1 = BoolArray<uword>::bitmapOf(1, 1);
if (b1.numberOfOnes() != 1) {
return false;
}
b1.inplace_logicalnot();
if (b1.numberOfOnes() != 1) {
return false;
}
BoolArray<uword> b = BoolArray<uword>::bitmapOf(2, 1, 100);
if (b.numberOfOnes() != 2) {
return false;
}
BoolArray<uword> bout;
b.logicalnot(bout);
if (bout.numberOfOnes() != 99) {
return false;
}
b.inplace_logicalnot();
if (b.numberOfOnes() != 99) {
return false;
}
return true;
}
template <class uword> bool testAndNotBoolArray() {
cout << "[testing AndNotBoolArray] sizeof(uword)=" << sizeof(uword) << endl;
BoolArray<uword> b1 = BoolArray<uword>::bitmapOf(1, 1);
BoolArray<uword> b = BoolArray<uword>::bitmapOf(2, 1, 100);
BoolArray<uword> bout;
b.logicalandnot(b1, bout);
if (bout.numberOfOnes() != 1) {
return false;
}
return true;
}
template <class uword> bool testIntersects() {
cout << "[testing intersects] sizeof(uword)=" << sizeof(uword) << endl;
cout << "constructing b1..." << endl;
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>::bitmapOf(2, 2, 1000);
cout << "constructing b2..." << endl;
EWAHBoolArray<uword> b2 = EWAHBoolArray<uword>::bitmapOf(2, 1000, 1100);
cout << "constructing b3..." << endl;
EWAHBoolArray<uword> b3 = EWAHBoolArray<uword>::bitmapOf(2, 1100, 2000);
cout << "About to intersect..." << endl;
cout << "all of them: " << b1.intersects(b2) << " " << b2.intersects(b3)
<< " " << b1.intersects(b3) << "done" << endl;
if (!b1.intersects(b2))
return false;
if (!b2.intersects(b3))
return false;
if (b1.intersects(b3))
return false;
return true;
}
template <class uword> bool testCardinalityEWAHBoolArray() {
cout << "[testing CardinalityEWAHBoolArray] sizeof(uword)=" << sizeof(uword)
<< endl;
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>::bitmapOf(1, 1);
if (b1.numberOfOnes() != 1) {
return false;
}
b1.inplace_logicalnot();
if (b1.numberOfOnes() != 1) {
cout << "b1 " << b1 << endl;
return false;
}
EWAHBoolArray<uword> b = EWAHBoolArray<uword>::bitmapOf(2, 1, 100);
if (b.numberOfOnes() != 2) {
cout << "b " << b << endl;
return false;
}
EWAHBoolArray<uword> bout;
b.logicalnot(bout);
if (bout.numberOfOnes() != 99) {
cout << "bout " << bout << endl;
return false;
}
b.inplace_logicalnot();
if (b.numberOfOnes() != 99) {
cout << "b neg " << b << endl;
return false;
}
return true;
}
template <class uword> bool testLargeDirty() {
cout << "[testing LargeDirty] sizeof(uword)=" << sizeof(uword) << endl;
size_t N = 200000000;
vector<uint32_t> bigarray1(N);
vector<uint32_t> bigarray2(N);
for (size_t k = 0; k < N; k++) {
bigarray1[k] = (uint32_t)(8 * k);
bigarray2[k] = (uint32_t)(32 * k);
}
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>();
EWAHBoolArray<uword> b2 = EWAHBoolArray<uword>();
for (size_t k = 0; (8 * k) < N; k++) {
b1.set(8 * k);
}
for (size_t k = 0; (64 * k) < N; k++) {
b1.set(64 * k);
}
EWAHBoolArray<uword> b3 = EWAHBoolArray<uword>::bitmapOf(3, 1, 10, 1001);
EWAHBoolArray<uword> b4 = EWAHBoolArray<uword>::bitmapOf(3, 1, 10, 1001);
if (b3.numberOfOnes() != 3) {
std::cout << "bad b3 count" << b4.numberOfOnes() << std::endl;
return false;
}
b3 = b3 | b1;
b4 = b4 | b2;
b3 = b3 | b2;
b4 = b4 | b1;
if (b3 != b4) {
std::cout << "b3 != b4" << std::endl;
return false;
}
b4 = b4 - b1;
if (b4.numberOfOnes() != 3) {
std::cout << "bad b4 count" << b4.numberOfOnes() << std::endl;
return false;
}
b3 = b3 ^ b4;
if (b3 != b1) {
std::cout << "b3 != b1" << std::endl;
return false;
}
return true;
}
template <class uword> bool testAndNotEWAHBoolArray() {
cout << "[testing AndNotEWAHBoolArray] sizeof(uword)=" << sizeof(uword)
<< endl;
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>::bitmapOf(1, 1);
EWAHBoolArray<uword> b = EWAHBoolArray<uword>::bitmapOf(2, 1, 100);
EWAHBoolArray<uword> bout;
b.logicalandnot(b1, bout);
cout << bout << endl;
if (bout.numberOfOnes() != 1) {
cout << "expected answer : 1 " << endl;
cout << "got answer : " << bout.numberOfOnes() << endl;
return false;
}
return true;
}
template <class uword> bool testNanJiang() {
cout << "[testing NanJiang] sizeof(uword)=" << sizeof(uword) << endl;
EWAHBoolArray<uword> b;
b.set(5);
if (b.numberOfOnes() != 1)
return false;
b.inplace_logicalnot();
if (b.numberOfOnes() != 5)
return false;
BoolArray<uword> b2;
b2.set(5);
if (b2.numberOfOnes() != 1)
return false;
b2.inplace_logicalnot();
if (b2.numberOfOnes() != 5)
return false;
return true;
}
template <class uword> bool testGet() {
cout << "[testing Get] sizeof(uword)=" << sizeof(uword) << endl;
bool isOk = true;
for (size_t gap = 29; gap < 10000; gap *= 10) {
EWAHBoolArray<uword> x;
for (uint32_t k = 0; k < 100; ++k)
x.set(k * gap);
for (size_t k = 0; k < 100 * gap; ++k)
if (x.get(k)) {
if (k % gap != 0) {
cout << "spotted an extra set bit at " << k << " gap = " << gap
<< endl;
return false;
}
} else if (k % gap == 0) {
cout << "missed a set bit " << k << " gap = " << gap << endl;
return false;
}
}
return isOk;
}
template <class uword> bool testLucaDeri() {
cout << "[testing LucaDeri] sizeof(uword)=" << sizeof(uword) << endl;
bool isOk = true;
EWAHBoolArray<uword> bitset1;
bitset1.set(1);
bitset1.set(2);
bitset1.set(2);
bitset1.set(1000);
bitset1.set(1001);
if (bitset1.numberOfOnes() != 4) {
cout << "Failed LucaDeri test" << endl;
isOk = false;
}
return isOk;
}
template <class uword> bool testSetGet() {
cout << "[testing EWAH set/get] sizeof(uword)=" << sizeof(uword) << endl;
EWAHBoolArray<uword> ewcb;
uint32_t val[] = {5, 4400, 44600, 55400, 1000000};
for (int k = 0; k < 5; ++k) {
ewcb.set(val[k]);
}
size_t counter = 0;
bool isOk = true;
for (typename EWAHBoolArray<uword>::const_iterator i = ewcb.begin();
i != ewcb.end(); ++i) {
if (val[counter++] != *i) {
cout << "Failed test set/get" << endl;
isOk = false;
}
}
return isOk;
}
template <class uword> bool testRunningLengthWord() {
cout << "[testing RunningLengthWord]" << endl;
bool isOk(true);
uword somenumber(0xABCD);
RunningLengthWord<uword> rlw(somenumber);
rlw.setRunningBit(true);
if (rlw.getRunningBit() != true) {
cout << "failed to set the running bit " << sizeof(uword) << endl;
isOk = false;
}
for (uword myrl = 0;
myrl <= RunningLengthWord<uword>::largestrunninglengthcount;
myrl = static_cast<uword>(
myrl + RunningLengthWord<uword>::largestrunninglengthcount / 10)) {
rlw.setRunningLength(myrl);
if (rlw.getRunningBit() != true) {
cout << "failed to set the running bit (2) " << sizeof(uword) << endl;
isOk = false;
}
if (rlw.getRunningLength() != myrl) {
cout << "failed to set the running length " << sizeof(uword) << endl;
isOk = false;
}
}
rlw.setRunningLength(12);
for (uword mylw = 0; mylw <= RunningLengthWord<uword>::largestliteralcount;
mylw = static_cast<uword>(
mylw + RunningLengthWord<uword>::largestliteralcount / 10)) {
rlw.setNumberOfLiteralWords(mylw);
if (rlw.getRunningBit() != true) {
cout << "failed to set the running bit (3) " << sizeof(uword) << endl;
isOk = false;
}
if (rlw.getRunningLength() != 12) {
cout << "failed to set the running length (2) " << sizeof(uword) << endl;
isOk = false;
}
if (rlw.getNumberOfLiteralWords() != mylw) {
cout << "failed to set the LiteralWords " << mylw << " " << sizeof(uword)
<< " " << rlw.getNumberOfLiteralWords() << endl;
isOk = false;
}
}
rlw.setNumberOfLiteralWords(43);
rlw.setRunningBit(false);
if (rlw.getRunningBit() != false) {
cout << "failed to set the running bit (4) " << sizeof(uword) << endl;
isOk = false;
}
if (rlw.getRunningLength() != 12) {
cout << "failed to set the running length (3) " << sizeof(uword) << endl;
isOk = false;
}
if (rlw.getNumberOfLiteralWords() != 43) {
cout << "failed to set the LiteralWords (2) " << sizeof(uword) << endl;
isOk = false;
}
if (!isOk)
cout << testfailed << endl;
return isOk;
}
template <class uword> bool testEWAHBoolArrayAppend() {
cout << "[testing EWAHBoolArrayAppend]" << endl;
bool isOk(true);
uword zero = 0;
uword specialval =
1UL + (1UL << 4) + (static_cast<uword>(1) << (sizeof(uword) * 8 - 1));
uword notzero = static_cast<uword>(~zero);
EWAHBoolArray<uword> myarray1;
BoolArray<uword> ba1;
myarray1.addWord(zero);
ba1.addWord(zero);
myarray1.addWord(zero);
ba1.addWord(zero);
myarray1.addWord(zero);
ba1.addWord(zero);
myarray1.addWord(specialval);
ba1.addWord(specialval);
myarray1.addWord(specialval);
ba1.addWord(specialval);
myarray1.addWord(notzero);
ba1.addWord(notzero);
myarray1.addWord(zero);
ba1.addWord(zero);
EWAHBoolArray<uword> myarray2;
BoolArray<uword> ba2;
myarray2.addWord(notzero);
ba2.addWord(notzero);
myarray2.addWord(zero);
ba2.addWord(zero);
myarray2.addWord(notzero);
ba2.addWord(notzero);
myarray2.addWord(specialval);
ba2.addWord(specialval);
myarray2.addWord(specialval);
ba2.addWord(specialval);
myarray2.addWord(notzero);
ba2.addWord(notzero);
BoolArray<uword> aggregate1(ba1);
BoolArray<uword> aggregate2(ba2);
aggregate1.append(ba2);
aggregate2.append(ba1);
EWAHBoolArray<uword> caggregate1;
caggregate1.append(myarray1);
EWAHBoolArray<uword> caggregate2;
caggregate2.append(myarray2);
caggregate1.append(myarray2);
caggregate2.append(myarray1);
if (caggregate1 != aggregate1) {
cout << "aggregate 1 failed" << endl;
isOk = false;
}
if (caggregate2 != aggregate2) {
cout << "aggregate 2 failed" << endl;
isOk = false;
}
if (!isOk)
cout << testfailed << endl;
return isOk;
}
// unit test contributed by Joerg Bukowski ✆
template <class uword> bool testJoergBukowski() {
cout << "[testing JoergBukowski]" << endl;
bool isOk(true);
vector<uint32_t> positions;
positions.push_back(0);
positions.push_back(36778);
positions.push_back(51863);
positions.push_back(134946);
positions.push_back(137330);
positions.push_back(147726);
positions.push_back(147990);
positions.push_back(151884);
positions.push_back(156404);
positions.push_back(158486);
positions.push_back(159622);
positions.push_back(163159);
positions.push_back(164599);
string indexfile("testingewahboolarray.bin");
::remove(indexfile.c_str());
EWAHBoolArray<uword> mytestarray;
for (vector<uint32_t>::const_iterator i = positions.begin();
i != positions.end(); ++i) {
mytestarray.set(*i);
}
size_t count = 0;
for (typename EWAHBoolArray<uword>::const_iterator j = mytestarray.begin();
j != mytestarray.end(); ++j) {
if (count >= positions.size()) {
std::cout << " Should be done by now!" << std::endl;
isOk = false;
assert(false);
break;
}
if (*j != positions[count]) {
isOk = false;
std::cout << " Expected " << positions[count] << std::endl;
break;
}
count++;
}
if (count < positions.size()) {
std::cout << "counted " << count << " values but expected "
<< positions.size() << std::endl;
isOk = false;
}
assert(count == positions.size());
EWAHBoolArray<uword> myarray;
for (vector<uint32_t>::const_iterator i = positions.begin();
i != positions.end(); ++i) {
myarray.set(*i);
ofstream out(indexfile.c_str(), ios::out | ios::binary);
myarray.write(out);
out.close();
EWAHBoolArray<uword> recovered;
ifstream in(indexfile.c_str(), ios::binary);
recovered.read(in);
in.close();
vector<size_t> vals;
recovered.appendSetBits(vals);
if (vals.size() != static_cast<size_t>(i - positions.begin() + 1)) {
cout << "failed to recover right number" << endl;
isOk = false;
}
if (!equal(vals.begin(), vals.end(), positions.begin())) {
cout << "failed to recover" << endl;
isOk = false;
}
vals.clear();
for (typename EWAHBoolArray<uword>::const_iterator j = recovered.begin();
j != recovered.end(); ++j) {
vals.push_back(static_cast<uint32_t>(*j));
}
if (vals.size() != static_cast<size_t>(i - positions.begin() + 1)) {
cout << "failed to recover right number -- iterator" << endl;
isOk = false;
}
if (!equal(vals.begin(), vals.end(), positions.begin())) {
cout << "failed to recover -- iterator" << endl;
isOk = false;
}
}
if (isOk)
::remove(indexfile.c_str());
if (!isOk)
cout << testfailed << endl;
return isOk;
}
// unit test contributed by Phong Tran
bool testPhongTran() {
cout << "[testing PhongTran]" << endl;
bool isOk(true);
EWAHBoolArray<uint32_t> myarray;
for (uint32_t x = 0; x < 10000; x++) {
myarray.addWord(x);
}
string indexfile("testingewahboolarray.bin");
::remove(indexfile.c_str());
ofstream out(indexfile.c_str(), ios::out | ios::binary);
myarray.write(out);
out.close();
EWAHBoolArray<uint32_t> lmyarray;
ifstream in(indexfile.c_str(), ios::binary);
lmyarray.read(in);
in.close();
EWAHBoolArrayIterator<uint32_t> i = myarray.uncompress();
EWAHBoolArrayIterator<uint32_t> j = lmyarray.uncompress();
while (i.hasNext() or j.hasNext()) {
if ((!j.hasNext()) or (!i.hasNext())) {
cout << "the two arrays don't have the same size?" << endl;
isOk = false;
break;
}
uint32_t val = i.next();
uint32_t val2 = j.next();
if (val != val2) {
cout << "the two arrays differ" << endl;
isOk = false;
break;
}
}
if (isOk)
::remove(indexfile.c_str());
if (!isOk)
cout << testfailed << endl;
return isOk;
}
// another unit test contributed by Phong Tran
template <class uword> bool testHemeury() {
cout << "[testing Hemeury]" << endl;
bool isOk(true);
EWAHBoolArray<uword> test, test1, test2;
for (uint32_t i = 0; i <= 10000; ++i) {
test.set(i);
test.logicaland(test1, test2);
// because test1 is empty, test2 should be empty as well
vector<size_t> vals;
test2.appendSetBits(vals);
if (vals.size() != 0) {
isOk = false;
cout << "failed!" << endl;
break;
}
}
return isOk;
}
// another unit test contributed by Phong Tran
template <class uword> bool testPhongTran2() {
cout << "[testing PhongTran2]" << endl;
bool isOk(true);
uword iTotal = static_cast<uword>(
1000); // when 1000 does not fit in uword, then it will be casted
EWAHBoolArray<uword> myarray;
for (uword x = static_cast<uword>(100); x < iTotal; x++) {
myarray.addWord(x);
}
string indexfile("testingewahboolarray.bin");
::remove(indexfile.c_str());
ofstream out(indexfile.c_str(), ios::out | ios::binary);
myarray.write(out);
out.close();
EWAHBoolArray<uword> lmyarray;
ifstream in(indexfile.c_str(), ios::binary);
lmyarray.read(in);
in.close();
if (!(myarray == lmyarray)) {
cout << "bad news, they are not equal" << endl;
cout << "size in bits: " << myarray.sizeInBits() << " vs. "
<< lmyarray.sizeInBits() << endl;
isOk = false;
}
EWAHBoolArrayIterator<uword> i = myarray.uncompress();
EWAHBoolArrayIterator<uword> j = lmyarray.uncompress();
while (i.hasNext()) {
if (!j.hasNext()) {
cout << "the two arrays don't have the same size?" << endl;
isOk = false;
break;
}
uword val = i.next();
uword val2 = j.next();
if (val != val2) {
cout << "the two arrays differ " << endl;
isOk = false;
}
}
if (isOk)
::remove(indexfile.c_str());
if (!isOk)
cout << testfailed << endl;
return isOk;
}
template <class uword> bool testEWAHBoolArray() {
cout << "[testing EWAHBoolArray]" << endl;
bool isOk(true);
EWAHBoolArray<uword> myarray;
BoolArray<uword> ba(10 * sizeof(uword) * 8);
uword zero = 0;
uword notzero = static_cast<uword>(~zero);
myarray.addWord(zero);
ba.setWord(0, zero);
myarray.addWord(zero);
ba.setWord(1, zero);
myarray.addWord(zero);
ba.setWord(2, zero);
uword specialval =
1UL + (1UL << 4) + (static_cast<uword>(1) << (sizeof(uword) * 8 - 1));
myarray.addWord(specialval);
ba.setWord(3, specialval);
myarray.addWord(notzero);
ba.setWord(4, notzero);
myarray.addWord(notzero);
ba.setWord(5, notzero);
myarray.addWord(notzero);
ba.setWord(6, notzero);
myarray.addWord(notzero);
ba.setWord(7, notzero);
myarray.addWord(specialval);
ba.setWord(8, specialval);
myarray.addWord(zero);
ba.setWord(9, zero);
if (myarray.sizeInBits() != 10 * sizeof(uword) * 8) {
cout << "expected " << 10 * sizeof(uword) * 8 << " bits but found "
<< myarray.sizeInBits() << endl;
isOk = false;
}
string indexfile("testingewahboolarray.bin");
::remove(indexfile.c_str());
ofstream out(indexfile.c_str(), ios::out | ios::binary);
myarray.write(out);
out.close();
EWAHBoolArray<uword> lmyarray;
ifstream in(indexfile.c_str(), ios::binary);
lmyarray.read(in);
in.close();
if (!(myarray == lmyarray)) {
cout << "bad news, they are not equal" << endl;
cout << "size in bits: " << myarray.sizeInBits() << " vs. "
<< lmyarray.sizeInBits() << endl;
isOk = false;
}
EWAHBoolArrayIterator<uword> i = myarray.uncompress();
EWAHBoolArrayIterator<uword> j = lmyarray.uncompress();
uint32_t k = 0;
while (i.hasNext()) {
if (!j.hasNext()) {
cout << "the two arrays don't have the same size?" << endl;
isOk = false;
break;
}
uword val = i.next();
uword val2 = j.next();
uword valref = ba.getWord(k++);
if (val != valref) {
cout << "the two arrays differ from uncompressed array at " << k << " "
<< val << " " << val2 << " " << valref << endl;
isOk = false;
}
if (val != val2) {
cout << "the two arrays differ at " << k << " " << val << " " << val2
<< " " << valref << endl;
isOk = false;
}
}
if (isOk)
::remove(indexfile.c_str());
if (!isOk)
cout << testfailed << endl;
return isOk;
}
template <class uword> bool testNot() {
cout << "[testing Not]" << endl;
bool isOk = true;
EWAHBoolArray<uword> bitset;
for (int i = 0; i <= 184; i++) {
bitset.set(i);
}
if (bitset.numberOfOnes() != 185) {
isOk = false;
}
bitset.inplace_logicalnot();
if (bitset.numberOfOnes() != 0) {
isOk = false;
}
return isOk;
}
template <class uword> bool testSTLCompatibility() {
cout << "[testing STL compatibility]" << endl;
bool isOk = true;
EWAHBoolArray<uword> bitset1;
bitset1.set(1);
bitset1.set(2);
bitset1.set(1000);
bitset1.set(1001);
vector<EWAHBoolArray<uword>> testVec(1);
testVec[0].set(1);
testVec[0].set(2);
testVec[0].set(1000);
testVec[0].set(1001);
if (testVec[0] != bitset1) {
isOk = false;
}
return isOk;
}
template <class uword> bool testEWAHBoolArrayLogical() {
cout << "[testing EWAHBoolArrayLogical] word size = " << sizeof(uword)
<< endl;
bool isOk(true);
EWAHBoolArray<uword> myarray1;
EWAHBoolArray<uword> myarray2;
uword allones = static_cast<uword>(~0LL);
const uint32_t N = 16;
uword x1[N] = {1, 0, 54, 24, 145, 0, 0, 0,
allones, allones, allones, 43, 0, 0, 0, 1};
uword x2[N] = {allones, 1, 0, 0, 0, 0, 0, 0,
0, allones, allones, allones, 0, 4, 0, 0};
uword xand[N];
uword xxor[N];
size_t usedN = 10;
if (sizeof(uword) > 2)
return true;
for (uint32_t k = 0; k < usedN; ++k) {
myarray1.addWord(x1[k]);
myarray2.addWord(x2[k]);
xand[k] = static_cast<uword>(x1[k] & x2[k]);
xxor[k] = static_cast<uword>(x1[k] | x2[k]);
}
EWAHBoolArray<uword> myand;
EWAHBoolArray<uword> myor;
EWAHBoolArray<uword> myxor;
EWAHBoolArray<uword> myxoralt;
myarray1.logicaland(myarray2, myand);
myarray1.logicalor(myarray2, myor);
myarray1.logicalxor(myarray2, myxor);
EWAHBoolArray<uword> tmp(myand);
tmp.inplace_logicalnot();
myor.logicaland(tmp, myxoralt);
if (myxoralt != myxor) {
isOk = false;
if (!isOk)
cout << testfailed << endl;
return isOk;
}
EWAHBoolArrayIterator<uword> i = myand.uncompress();
EWAHBoolArrayIterator<uword> j = myor.uncompress();
EWAHBoolArrayIterator<uword> it1 = myarray1.uncompress();
EWAHBoolArrayIterator<uword> it2 = myarray2.uncompress();
for (uint32_t k = 0; k < usedN; ++k) {
const uword m1 = it1.next();
const uword m2 = it2.next();
if (!i.hasNext()) {
if ((m1 & m2) != 0) {
cout << "type 1 error" << endl;
isOk = false;
break;
}
} else {
const uword inter = i.next();
if (inter != xand[k]) {
cout << "type 4 error" << endl;
isOk = false;
break;
}
}
if (!j.hasNext()) {
if ((m1 | m2) != 0) {
cout << "type 3 error" << endl;
isOk = false;
break;
}
} else {
const uword jor = j.next();
if (jor != xxor[k]) {
cout << "type 6 error OR" << endl;
isOk = false;
break;
}
}
}
if (!isOk)
cout << testfailed << endl;
return isOk;
}
#define N_ENTRIES(arr) (sizeof(arr) / sizeof((arr)[0]))
template <class uword>
void init(EWAHBoolArray<uword> &ba, size_t N, size_t x[]) {
for (size_t ix = 0; ix < N; ++ix)
ba.set(x[ix]);
}
template <class uword>
std::ostream &operator<<(std::ostream &os, const EWAHBoolArray<uword> &ba) {
os << " (" << ba.sizeInBits() << ") ";
typename EWAHBoolArray<uword>::const_iterator it = ba.begin(),
last = ba.end();
for (int ix = 0; it != last; ++it, ++ix) {
if (ix > 0)
os << ", ";
os << *it;
}
os << endl;
size_t ixBit = 0;
const uword wordInBits = EWAHBoolArray<uword>::wordinbits;
EWAHBoolArrayRawIterator<uword> ir = ba.raw_iterator();
for (int jx = 0; ir.hasNext(); ++jx) {
BufferedRunningLengthWord<uword> &brlw(ir.next());
string tf = (brlw.getRunningBit() ? "true" : "false");
size_t runBits = static_cast<size_t>(brlw.getRunningLength() * wordInBits);
size_t litBits =
static_cast<size_t>(brlw.getNumberOfLiteralWords() * wordInBits);
os << jx << ", " << ixBit << ": " << tf << " for "
<< brlw.getRunningLength() << " words(" << runBits << " bits), "
<< brlw.getNumberOfLiteralWords() << " literals (" << litBits << " bits)"
<< endl;
ixBit += (runBits + litBits);
}
string eq = (ixBit == ba.sizeInBits() ? "==" : "!=");
os << "[" << ixBit << eq << ba.sizeInBits() << "]" << endl;
return os;
}
template <class uword> bool testSerialization() {
cout << "[testing Serialization] word size = " << sizeof(uword) << endl;
EWAHBoolArray<uword> bitmap;
for (int i = 0; i < (1 << 30); i = 2 * i + 3) {
bitmap.set(static_cast<size_t>(i));
}
stringstream ss;
EWAHBoolArray<uword> lmyarray;
for (int k = 0; k < 10; ++k) {
size_t w1 = bitmap.write(ss);
if (w1 != bitmap.sizeOnDisk()) {
std::cout << " bitmap.sizeOnDisk() = " << bitmap.sizeOnDisk()
<< std::endl;
std::cout << " Effective result size = " << w1 << std::endl;
return false;
}
size_t w2 = lmyarray.read(ss);
if (w2 != bitmap.sizeOnDisk()) {
std::cout << " bitmap.sizeOnDisk() = " << bitmap.sizeOnDisk()
<< std::endl;
std::cout << " Effective result size = " << w1 << std::endl;
return false;
}
if (lmyarray != bitmap) {
std::cout << " Same size, but the bitmaps differ." << std::endl;
return false;
}
typename EWAHBoolArray<uword>::const_iterator i = bitmap.begin();
typename EWAHBoolArray<uword>::const_iterator j = lmyarray.begin();
for (; i != bitmap.end(); ++i, ++j) {
if (*i != *j) {
std::cout << " Same size, but the bitmaps differ over iterators."
<< std::endl;
return false;
}
}
}
return true;
}
template <class uword> bool testRawSerialization() {
cout << "[testing Raw Serialization] word size = " << sizeof(uword) << endl;
EWAHBoolArray<uword> bitmap;
for (int i = 0; i < (1 << 30); i = 2 * i + 3) {
bitmap.set(static_cast<size_t>(i));
}
vector<char> buffer(bitmap.sizeOnDisk());
stringstream ss;
EWAHBoolArray<uword> lmyarray;
for (int k = 0; k < 10; ++k) {
size_t w1 = bitmap.write(buffer.data(), buffer.size());
if (w1 != bitmap.sizeOnDisk()) {
return false;
}
size_t w2 = lmyarray.read(buffer.data(), buffer.size());
if (w2 != bitmap.sizeOnDisk()) {
return false;
}
if (lmyarray != bitmap) {
return false;
}
typename EWAHBoolArray<uword>::const_iterator i = bitmap.begin();
typename EWAHBoolArray<uword>::const_iterator j = lmyarray.begin();
for (; i != bitmap.end(); ++i, ++j) {
if (*i != *j)
return false;
}
}
return true;