wibble  0.1.28
operators.test.h
Go to the documentation of this file.
00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
00002                (c) 2007 Enrico Zini <enrico@enricozini.org> */
00003 
00004 #include <wibble/test.h>
00005 #include <wibble/operators.h>
00006 
00007 namespace {
00008 
00009 using namespace std;
00010 using namespace wibble::operators;
00011 
00012 static set<int> mkset(int i1)
00013 {
00014     set<int> a; a.insert(i1); return a;
00015 }
00016 static set<int> mkset(int i1, int i2)
00017 {
00018     set<int> a; a.insert(i1); a.insert(i2); return a;
00019 }
00020 #if 0
00021 static set<int> mkset(int i1, int i2, int i3)
00022 {
00023     set<int> a; a.insert(i1); a.insert(i2); a.insert(i3); return a;
00024 }
00025 static set<int> mkset(int i1, int i2, int i3, int i4)
00026 {
00027     set<int> a; a.insert(i1); a.insert(i2); a.insert(i3); a.insert(i4); return a;
00028 }
00029 #endif
00030 
00031 struct TestOperators {
00032 
00033     Test binarySetOperations() {
00034         set< int > a = mkset(4, 5);
00035     set< int > b = mkset(5);
00036         set< int > c = a & b;
00037         assert_eq( c.size(), 1u );
00038         assert( c.find( 4 ) == c.end() );
00039         assert( c.find( 5 ) != c.end() );
00040         c = a | b;
00041         assert_eq( c.size(), 2u );
00042         assert( c.find( 4 ) != c.end() );
00043         assert( c.find( 5 ) != c.end() );
00044         c = a - b;
00045         assert_eq( c.size(), 1u );
00046         assert( c.find( 4 ) != c.end() );
00047         assert( c.find( 5 ) == c.end() );
00048     }
00049 
00050     Test mutatingSetOperations() {
00051         set< int > a = mkset(4, 3);
00052     set< int > b = mkset(5);
00053         b |= 3;
00054         assert_eq( b.size(), 2u );
00055         assert( b.find( 2 ) == b.end() );
00056         assert( b.find( 3 ) != b.end() );
00057         assert( b.find( 4 ) == b.end() );
00058         assert( b.find( 5 ) != b.end() );
00059         b |= a;
00060         assert_eq( b.size(), 3u );
00061         assert( b.find( 3 ) != b.end() );
00062         assert( b.find( 4 ) != b.end() );
00063         assert( b.find( 5 ) != b.end() );
00064         b &= a;
00065         assert_eq( b.size(), 2u );
00066         assert( b.find( 3 ) != b.end() );
00067         assert( b.find( 4 ) != b.end() );
00068         assert( b.find( 5 ) == b.end() );
00069         b.insert( b.begin(), 2 );
00070         b -= a;
00071         assert_eq( b.size(), 1u );
00072         assert( b.find( 2 ) != b.end() );
00073         assert( b.find( 3 ) == b.end() );
00074         assert( b.find( 4 ) == b.end() );
00075     }
00076     
00077     Test specialContainerOperations() {
00078         set< int > a;
00079 
00080         a = a | wibble::Empty<int>();
00081         assert_eq( a.size(), 0u );
00082         
00083         a = a | wibble::Singleton<int>(1);
00084         assert_eq( a.size(), 1u );
00085         assert( a.find( 1 ) != a.end() );
00086         
00087         a = a - wibble::Empty<int>();
00088         assert_eq( a.size(), 1u );
00089         assert( a.find( 1 ) != a.end() );
00090         
00091         a = a - wibble::Singleton<int>(1);
00092         assert_eq( a.size(), 0u );
00093         assert( a.find( 1 ) == a.end() );
00094         
00095         a |= wibble::Empty<int>();
00096         assert_eq( a.size(), 0u );
00097         
00098         a |= wibble::Singleton<int>(1);
00099         assert_eq( a.size(), 1u );
00100         assert( a.find( 1 ) != a.end() );
00101         
00102         a -= wibble::Empty<int>();
00103         assert_eq( a.size(), 1u );
00104         assert( a.find( 1 ) != a.end() );
00105         
00106         a -= wibble::Singleton<int>(1);
00107         assert_eq( a.size(), 0u );
00108         assert( a.find( 1 ) == a.end() );
00109     }
00110     
00111     Test emptySetInclusion() {
00112         set< int > a, b;
00113         assert( a <= b );
00114         assert( b <= a );
00115     }
00116 
00117     Test mutatingIntersectionBug() {
00118     // Catches a past bug of in-place intersection that would delete too many
00119     // items if the second set had items not present in the first
00120     set<int> a = mkset(2);
00121     set<int> b = mkset(1, 2);
00122     set<int> c = mkset(2);
00123 
00124     set<int> d = a & b;
00125     assert(c == d);
00126 
00127     d = a;
00128     d &= b;
00129     assert(c == d);
00130     }
00131     
00132 };
00133 
00134 }
00135 
00136 // vim:set ts=4 sw=4: