wibble  1.1
operators.test.h
Go to the documentation of this file.
1 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2  (c) 2007 Enrico Zini <enrico@enricozini.org> */
3 
4 #include <wibble/test.h>
5 #include <wibble/operators.h>
6 
7 namespace {
8 
9 using namespace std;
10 using namespace wibble::operators;
11 
12 static set<int> mkset(int i1)
13 {
14  set<int> a; a.insert(i1); return a;
15 }
16 static set<int> mkset(int i1, int i2)
17 {
18  set<int> a; a.insert(i1); a.insert(i2); return a;
19 }
20 #if 0
21 static set<int> mkset(int i1, int i2, int i3)
22 {
23  set<int> a; a.insert(i1); a.insert(i2); a.insert(i3); return a;
24 }
25 static set<int> mkset(int i1, int i2, int i3, int i4)
26 {
27  set<int> a; a.insert(i1); a.insert(i2); a.insert(i3); a.insert(i4); return a;
28 }
29 #endif
30 
31 struct TestOperators {
32 
33  Test binarySetOperations() {
34  set< int > a = mkset(4, 5);
35  set< int > b = mkset(5);
36  set< int > c = a & b;
37  assert_eq( c.size(), 1u );
38  assert( c.find( 4 ) == c.end() );
39  assert( c.find( 5 ) != c.end() );
40  c = a | b;
41  assert_eq( c.size(), 2u );
42  assert( c.find( 4 ) != c.end() );
43  assert( c.find( 5 ) != c.end() );
44  c = a - b;
45  assert_eq( c.size(), 1u );
46  assert( c.find( 4 ) != c.end() );
47  assert( c.find( 5 ) == c.end() );
48  }
49 
50  Test mutatingSetOperations() {
51  set< int > a = mkset(4, 3);
52  set< int > b = mkset(5);
53  b |= 3;
54  assert_eq( b.size(), 2u );
55  assert( b.find( 2 ) == b.end() );
56  assert( b.find( 3 ) != b.end() );
57  assert( b.find( 4 ) == b.end() );
58  assert( b.find( 5 ) != b.end() );
59  b |= a;
60  assert_eq( b.size(), 3u );
61  assert( b.find( 3 ) != b.end() );
62  assert( b.find( 4 ) != b.end() );
63  assert( b.find( 5 ) != b.end() );
64  b &= a;
65  assert_eq( b.size(), 2u );
66  assert( b.find( 3 ) != b.end() );
67  assert( b.find( 4 ) != b.end() );
68  assert( b.find( 5 ) == b.end() );
69  b.insert( b.begin(), 2 );
70  b -= a;
71  assert_eq( b.size(), 1u );
72  assert( b.find( 2 ) != b.end() );
73  assert( b.find( 3 ) == b.end() );
74  assert( b.find( 4 ) == b.end() );
75  }
76 
77  Test specialContainerOperations() {
78  set< int > a;
79 
80  a = a | wibble::Empty<int>();
81  assert_eq( a.size(), 0u );
82 
83  a = a | wibble::Singleton<int>(1);
84  assert_eq( a.size(), 1u );
85  assert( a.find( 1 ) != a.end() );
86 
87  a = a - wibble::Empty<int>();
88  assert_eq( a.size(), 1u );
89  assert( a.find( 1 ) != a.end() );
90 
91  a = a - wibble::Singleton<int>(1);
92  assert_eq( a.size(), 0u );
93  assert( a.find( 1 ) == a.end() );
94 
95  a |= wibble::Empty<int>();
96  assert_eq( a.size(), 0u );
97 
98  a |= wibble::Singleton<int>(1);
99  assert_eq( a.size(), 1u );
100  assert( a.find( 1 ) != a.end() );
101 
102  a -= wibble::Empty<int>();
103  assert_eq( a.size(), 1u );
104  assert( a.find( 1 ) != a.end() );
105 
106  a -= wibble::Singleton<int>(1);
107  assert_eq( a.size(), 0u );
108  assert( a.find( 1 ) == a.end() );
109  }
110 
111  Test emptySetInclusion() {
112  set< int > a, b;
113  assert( a <= b );
114  assert( b <= a );
115  }
116 
117  Test mutatingIntersectionBug() {
118  // Catches a past bug of in-place intersection that would delete too many
119  // items if the second set had items not present in the first
120  set<int> a = mkset(2);
121  set<int> b = mkset(1, 2);
122  set<int> c = mkset(2);
123 
124  set<int> d = a & b;
125  assert(c == d);
126 
127  d = a;
128  d &= b;
129  assert(c == d);
130  }
131 
132 };
133 
134 }
135 
136 // vim:set ts=4 sw=4: