1 #include "adlib/lib.h"
2 #include "adlib/bitset.h"
3
4 #define CHECK_MAT(mat, prop, msg) \
5 { \
6 Int rows = mat->len(); \
7 Int cols = mat->at(0)->len(); \
8 bool ok = true; \
9 for (Int row = 0; row < rows; row++) { \
10 for (Int col = 0; col < cols; col++) { \
11 ok &= mat->at(row)->test(col) == (prop); \
12 } \
13 } \
14 Check(ok, msg); \
15 }
16
Main()17 void Main() {
18 BitSet *set = new BitSet(256);
19 BitSet *set2 = new BitSet(256);
20 for (Int i = 0; i < 200; i += 2) {
21 set->set(i);
22 set2->set(i + 1);
23 }
24 for (Int i = 0; i < 200; i += 4) {
25 set2->set(i);
26 set2->clear(i + 1);
27 }
28 Check(set->count() == 100, "bitset count");
29 Check(set->union_with(set2)->count() == 150, "bitset union");
30 Check(set->intersect_with(set2)->count() == 50, "bitset intersection");
31 Check(set->diff_with(set2)->count() == 50, "bitset difference");
32 Int sum = 0;
33 for (BitSet::Each it(set); it; it++) {
34 sum += *it;
35 }
36 Check(sum == 99 * 100, "bitset iteration");
37 const Int n = 1000;
38 BitMatrix *mat = MakeBitMatrix(n, n);
39 for (Int i = 0; i < n; i++) {
40 mat->at(i)->set(i);
41 }
42 BitMatrix *mat2 = Transpose(mat);
43 CHECK_MAT(mat2, row == col, "bit matrix transposition");
44 mat2 = TransitiveClosure(mat);
45 CHECK_MAT(mat2, row == col, "transitive closure 1");
46 mat = MakeBitMatrix(n, n);
47 for (Int i = 1; i < n; i++) {
48 mat->at(i - 1)->set(i);
49 }
50 mat->at(n - 1)->set(0);
51 mat2 = TransitiveClosure(mat);
52 CHECK_MAT(mat2, true, "transitive closure 2");
53 mat = MakeBitMatrix(2, 2);
54 mat->at(1)->set(0);
55 mat->at(1)->set(0);
56 mat->at(1)->set(1);
57 mat2 = TransitiveClosure(mat);
58 CHECK_MAT(mat2, mat->at(row)->test(col), "transitive closure 3");
59 }
60