diff options
Diffstat (limited to 'dbtests/basictests.cpp')
-rw-r--r-- | dbtests/basictests.cpp | 277 |
1 files changed, 187 insertions, 90 deletions
diff --git a/dbtests/basictests.cpp b/dbtests/basictests.cpp index f1e788a..3e0eecd 100644 --- a/dbtests/basictests.cpp +++ b/dbtests/basictests.cpp @@ -23,6 +23,8 @@ #include "../util/base64.h" #include "../util/array.h" #include "../util/text.h" +#include "../util/queue.h" +#include "../util/paths.h" namespace BasicTests { @@ -49,21 +51,21 @@ namespace BasicTests { RARELY ++c; } }; - + class Base64Tests { public: - - void roundTrip( string s ){ + + void roundTrip( string s ) { ASSERT_EQUALS( s , base64::decode( base64::encode( s ) ) ); } - - void roundTrip( const unsigned char * _data , int len ){ + + void roundTrip( const unsigned char * _data , int len ) { const char *data = (const char *) _data; string s = base64::encode( data , len ); string out = base64::decode( s ); ASSERT_EQUALS( out.size() , static_cast<size_t>(len) ); bool broke = false; - for ( int i=0; i<len; i++ ){ + for ( int i=0; i<len; i++ ) { if ( data[i] != out[i] ) broke = true; } @@ -77,16 +79,16 @@ namespace BasicTests { for ( int i=0; i<len; i++ ) cout << hex << ( out[i] & 0xFF ) << dec << " "; cout << endl; - + ASSERT(0); } - - void run(){ + + void run() { ASSERT_EQUALS( "ZWxp" , base64::encode( "eli" , 3 ) ); ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" , 6 ) ); ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" ) ); - + ASSERT_EQUALS( "ZQ==" , base64::encode( "e" , 1 ) ); ASSERT_EQUALS( "ZWw=" , base64::encode( "el" , 2 ) ); @@ -97,10 +99,10 @@ namespace BasicTests { roundTrip( "eliot" ); roundTrip( "eliots" ); roundTrip( "eliotsz" ); - + unsigned char z[] = { 0x1 , 0x2 , 0x3 , 0x4 }; roundTrip( z , 4 ); - + unsigned char y[] = { 0x01, 0x10, 0x83, 0x10, 0x51, 0x87, 0x20, 0x92, 0x8B, 0x30, 0xD3, 0x8F, 0x41, 0x14, 0x93, 0x51, 0x55, 0x97, 0x61, 0x96, @@ -115,15 +117,15 @@ namespace BasicTests { namespace stringbuildertests { #define SBTGB(x) ss << (x); sb << (x); - + class Base { virtual void pop() = 0; - + public: - Base(){} - virtual ~Base(){} + Base() {} + virtual ~Base() {} - void run(){ + void run() { pop(); ASSERT_EQUALS( ss.str() , sb.str() ); } @@ -131,9 +133,9 @@ namespace BasicTests { stringstream ss; StringBuilder sb; }; - + class simple1 : public Base { - void pop(){ + void pop() { SBTGB(1); SBTGB("yo"); SBTGB(2); @@ -141,7 +143,7 @@ namespace BasicTests { }; class simple2 : public Base { - void pop(){ + void pop() { SBTGB(1); SBTGB("yo"); SBTGB(2); @@ -154,10 +156,10 @@ namespace BasicTests { SBTGB( (short)(1231231231231LL) ); } }; - + class reset1 { public: - void run(){ + void run() { StringBuilder sb; sb << "1" << "abc" << "5.17"; ASSERT_EQUALS( "1abc5.17" , sb.str() ); @@ -171,7 +173,7 @@ namespace BasicTests { class reset2 { public: - void run(){ + void run() { StringBuilder sb; sb << "1" << "abc" << "5.17"; ASSERT_EQUALS( "1abc5.17" , sb.str() ); @@ -188,12 +190,19 @@ namespace BasicTests { class sleeptest { public: - void run(){ + void run() { Timer t; - sleepsecs( 1 ); - ASSERT_EQUALS( 1 , t.seconds() ); + int matches = 0; + for( int p = 0; p < 3; p++ ) { + sleepsecs( 1 ); + int sec = t.seconds(); + if( sec == 1 ) + matches++; + ASSERT( sec >= 0 && sec <= 2 ); + t.reset(); + } + ASSERT( matches >= 2 ); - t.reset(); sleepmicros( 1527123 ); ASSERT( t.micros() > 1000000 ); ASSERT( t.micros() < 2000000 ); @@ -202,17 +211,17 @@ namespace BasicTests { sleepmillis( 1727 ); ASSERT( t.millis() >= 1000 ); ASSERT( t.millis() <= 2500 ); - + { int total = 1200; int ms = 2; t.reset(); - for ( int i=0; i<(total/ms); i++ ){ + for ( int i=0; i<(total/ms); i++ ) { sleepmillis( ms ); } { int x = t.millis(); - if ( x < 1000 || x > 2500 ){ + if ( x < 1000 || x > 2500 ) { cout << "sleeptest x: " << x << endl; ASSERT( x >= 1000 ); ASSERT( x <= 20000 ); @@ -226,12 +235,12 @@ namespace BasicTests { int micros = 100; t.reset(); int numSleeps = 1000*(total/micros); - for ( int i=0; i<numSleeps; i++ ){ + for ( int i=0; i<numSleeps; i++ ) { sleepmicros( micros ); } { int y = t.millis(); - if ( y < 1000 || y > 2500 ){ + if ( y < 1000 || y > 2500 ) { cout << "sleeptest y: " << y << endl; ASSERT( y >= 1000 ); /* ASSERT( y <= 100000 ); */ @@ -239,9 +248,9 @@ namespace BasicTests { } } #endif - + } - + }; class AssertTests { @@ -249,15 +258,15 @@ namespace BasicTests { int x; - AssertTests(){ + AssertTests() { x = 0; } - string foo(){ + string foo() { x++; return ""; } - void run(){ + void run() { uassert( -1 , foo() , 1 ); if( x != 0 ) { ASSERT_EQUALS( 0 , x ); @@ -265,7 +274,7 @@ namespace BasicTests { try { uassert( -1 , foo() , 0 ); } - catch ( ... ){} + catch ( ... ) {} ASSERT_EQUALS( 1 , x ); } }; @@ -273,13 +282,13 @@ namespace BasicTests { namespace ArrayTests { class basic1 { public: - void run(){ + void run() { FastArray<int> a(100); a.push_back( 5 ); a.push_back( 6 ); - + ASSERT_EQUALS( 2 , a.size() ); - + FastArray<int>::iterator i = a.begin(); ASSERT( i != a.end() ); ASSERT_EQUALS( 5 , *i ); @@ -291,10 +300,10 @@ namespace BasicTests { } }; }; - + class ThreadSafeStringTest { public: - void run(){ + void run() { ThreadSafeString s; s = "eliot"; ASSERT_EQUALS( s , "eliot" ); @@ -302,8 +311,8 @@ namespace BasicTests { ThreadSafeString s2 = s; ASSERT_EQUALS( s2 , "eliot" ); - - + + { string foo; { @@ -315,11 +324,11 @@ namespace BasicTests { } } }; - + class LexNumCmp { public: void run() { - + ASSERT( ! isNumber( (char)255 ) ); ASSERT_EQUALS( 0, lexNumCmp( "a", "a" ) ); @@ -355,7 +364,7 @@ namespace BasicTests { ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a1{a" ) ); ASSERT_EQUALS( 1, lexNumCmp("21", "11") ); ASSERT_EQUALS( -1, lexNumCmp("11", "21") ); - + ASSERT_EQUALS( -1 , lexNumCmp( "a.0" , "a.1" ) ); ASSERT_EQUALS( -1 , lexNumCmp( "a.0.b" , "a.1" ) ); @@ -363,52 +372,78 @@ namespace BasicTests { ASSERT_EQUALS( -1 , lexNumCmp( "b.0e" , (string("b.") + (char)255).c_str() ) ); ASSERT_EQUALS( -1 , lexNumCmp( "b." , "b.0e" ) ); - ASSERT_EQUALS( 0, lexNumCmp( "238947219478347782934718234", "238947219478347782934718234")); - ASSERT_EQUALS( 0, lexNumCmp( "000238947219478347782934718234", "238947219478347782934718234")); - ASSERT_EQUALS( 1, lexNumCmp( "000238947219478347782934718235", "238947219478347782934718234")); - ASSERT_EQUALS( -1, lexNumCmp( "238947219478347782934718234", "238947219478347782934718234.1")); - ASSERT_EQUALS( 0, lexNumCmp( "238", "000238")); - ASSERT_EQUALS( 0, lexNumCmp( "002384", "0002384")); - ASSERT_EQUALS( 0, lexNumCmp( "00002384", "0002384")); - ASSERT_EQUALS( 0, lexNumCmp( "0", "0")); - ASSERT_EQUALS( 0, lexNumCmp( "0000", "0")); + ASSERT_EQUALS( 0, lexNumCmp( "238947219478347782934718234", "238947219478347782934718234")); + ASSERT_EQUALS( 0, lexNumCmp( "000238947219478347782934718234", "238947219478347782934718234")); + ASSERT_EQUALS( 1, lexNumCmp( "000238947219478347782934718235", "238947219478347782934718234")); + ASSERT_EQUALS( -1, lexNumCmp( "238947219478347782934718234", "238947219478347782934718234.1")); + ASSERT_EQUALS( 0, lexNumCmp( "238", "000238")); + ASSERT_EQUALS( 0, lexNumCmp( "002384", "0002384")); + ASSERT_EQUALS( 0, lexNumCmp( "00002384", "0002384")); + ASSERT_EQUALS( 0, lexNumCmp( "0", "0")); + ASSERT_EQUALS( 0, lexNumCmp( "0000", "0")); ASSERT_EQUALS( 0, lexNumCmp( "0", "000")); ASSERT_EQUALS( -1, lexNumCmp( "0000", "0.0")); - ASSERT_EQUALS( 1, lexNumCmp( "2380", "238")); - ASSERT_EQUALS( 1, lexNumCmp( "2385", "2384")); - ASSERT_EQUALS( 1, lexNumCmp( "2385", "02384")); - ASSERT_EQUALS( 1, lexNumCmp( "2385", "002384")); - ASSERT_EQUALS( -1, lexNumCmp( "123.234.4567", "00238")); - ASSERT_EQUALS( 0, lexNumCmp( "123.234", "00123.234")); - ASSERT_EQUALS( 0, lexNumCmp( "a.123.b", "a.00123.b")); - ASSERT_EQUALS( 1, lexNumCmp( "a.123.b", "a.b.00123.b")); - ASSERT_EQUALS( -1, lexNumCmp( "a.00.0", "a.0.1")); - ASSERT_EQUALS( 0, lexNumCmp( "01.003.02", "1.3.2")); - ASSERT_EQUALS( -1, lexNumCmp( "1.3.2", "10.300.20")); - ASSERT_EQUALS( 0, lexNumCmp( "10.300.20", "000000000000010.0000300.000000020")); - ASSERT_EQUALS( 0, lexNumCmp( "0000a", "0a")); - ASSERT_EQUALS( -1, lexNumCmp( "a", "0a")); - ASSERT_EQUALS( -1, lexNumCmp( "000a", "001a")); - ASSERT_EQUALS( 0, lexNumCmp( "010a", "0010a")); + ASSERT_EQUALS( 1, lexNumCmp( "2380", "238")); + ASSERT_EQUALS( 1, lexNumCmp( "2385", "2384")); + ASSERT_EQUALS( 1, lexNumCmp( "2385", "02384")); + ASSERT_EQUALS( 1, lexNumCmp( "2385", "002384")); + ASSERT_EQUALS( -1, lexNumCmp( "123.234.4567", "00238")); + ASSERT_EQUALS( 0, lexNumCmp( "123.234", "00123.234")); + ASSERT_EQUALS( 0, lexNumCmp( "a.123.b", "a.00123.b")); + ASSERT_EQUALS( 1, lexNumCmp( "a.123.b", "a.b.00123.b")); + ASSERT_EQUALS( -1, lexNumCmp( "a.00.0", "a.0.1")); + ASSERT_EQUALS( 0, lexNumCmp( "01.003.02", "1.3.2")); + ASSERT_EQUALS( -1, lexNumCmp( "1.3.2", "10.300.20")); + ASSERT_EQUALS( 0, lexNumCmp( "10.300.20", "000000000000010.0000300.000000020")); + ASSERT_EQUALS( 0, lexNumCmp( "0000a", "0a")); + ASSERT_EQUALS( -1, lexNumCmp( "a", "0a")); + ASSERT_EQUALS( -1, lexNumCmp( "000a", "001a")); + ASSERT_EQUALS( 0, lexNumCmp( "010a", "0010a")); } }; class DatabaseValidNames { public: - void run(){ + void run() { ASSERT( Database::validDBName( "foo" ) ); ASSERT( ! Database::validDBName( "foo/bar" ) ); ASSERT( ! Database::validDBName( "foo.bar" ) ); - ASSERT( nsDollarCheck( "asdads" ) ); - ASSERT( ! nsDollarCheck( "asda$ds" ) ); - ASSERT( nsDollarCheck( "local.oplog.$main" ) ); + ASSERT( isANormalNSName( "asdads" ) ); + ASSERT( ! isANormalNSName( "asda$ds" ) ); + ASSERT( isANormalNSName( "local.oplog.$main" ) ); + } + }; + + class DatabaseOwnsNS { + public: + void run() { + + bool isNew = false; + // this leaks as ~Database is private + // if that changes, should put this on the stack + Database * db = new Database( "dbtests_basictests_ownsns" , isNew ); + assert( isNew ); + + ASSERT( db->ownsNS( "dbtests_basictests_ownsns.x" ) ); + ASSERT( db->ownsNS( "dbtests_basictests_ownsns.x.y" ) ); + ASSERT( ! db->ownsNS( "dbtests_basictests_ownsn.x.y" ) ); + ASSERT( ! db->ownsNS( "dbtests_basictests_ownsnsa.x.y" ) ); + } + }; + + class NSValidNames { + public: + void run() { + ASSERT( isValidNS( "test.foo" ) ); + ASSERT( ! isValidNS( "test." ) ); + ASSERT( ! isValidNS( "test" ) ); } }; - + class PtrTests { public: - void run(){ + void run() { scoped_ptr<int> p1 (new int(1)); boost::shared_ptr<int> p2 (new int(2)); scoped_ptr<const int> p3 (new int(3)); @@ -419,7 +454,7 @@ namespace BasicTests { ASSERT_EQUALS( p2.get() , ptr<int>(p2) ); ASSERT_EQUALS( p2.get() , ptr<int>(p2.get()) ); // T* constructor ASSERT_EQUALS( p2.get() , ptr<int>(ptr<int>(p2)) ); // copy constructor - ASSERT_EQUALS( *p2 , *ptr<int>(p2)); + ASSERT_EQUALS( *p2 , *ptr<int>(p2)); ASSERT_EQUALS( p2.get() , ptr<boost::shared_ptr<int> >(&p2)->get() ); // operator-> //const @@ -431,14 +466,14 @@ namespace BasicTests { ASSERT_EQUALS( p4.get() , ptr<const int>(p4.get()) ); ASSERT_EQUALS( p2.get() , ptr<const int>(ptr<const int>(p2)) ); ASSERT_EQUALS( p2.get() , ptr<const int>(ptr<int>(p2)) ); // constizing copy constructor - ASSERT_EQUALS( *p2 , *ptr<int>(p2)); + ASSERT_EQUALS( *p2 , *ptr<int>(p2)); ASSERT_EQUALS( p2.get() , ptr<const boost::shared_ptr<int> >(&p2)->get() ); //bool context ASSERT( ptr<int>(p1) ); ASSERT( !ptr<int>(NULL) ); ASSERT( !ptr<int>() ); - + #if 0 // These shouldn't compile ASSERT_EQUALS( p3.get() , ptr<int>(p3) ); @@ -450,12 +485,12 @@ namespace BasicTests { struct StringSplitterTest { - void test( string s ){ + void test( string s ) { vector<string> v = StringSplitter::split( s , "," ); ASSERT_EQUALS( s , StringSplitter::join( v , "," ) ); } - void run(){ + void run() { test( "a" ); test( "a,b" ); test( "a,b,c" ); @@ -496,16 +531,68 @@ namespace BasicTests { }; + class QueueTest { + public: + void run() { + BlockingQueue<int> q; + Timer t; + int x; + ASSERT( ! q.blockingPop( x , 5 ) ); + ASSERT( t.seconds() > 3 && t.seconds() < 9 ); + + } + }; + + class StrTests { + public: + + void run() { + ASSERT_EQUALS( 1u , str::count( "abc" , 'b' ) ); + ASSERT_EQUALS( 3u , str::count( "babab" , 'b' ) ); + } + + }; + + class HostAndPortTests { + public: + void run() { + HostAndPort a( "x1" , 1000 ); + HostAndPort b( "x1" , 1000 ); + HostAndPort c( "x1" , 1001 ); + HostAndPort d( "x2" , 1000 ); + + ASSERT( a == b ); + ASSERT( a != c ); + ASSERT( a != d ); + + } + }; + + class RelativePathTest { + public: + void run() { + RelativePath a = RelativePath::fromRelativePath( "a" ); + RelativePath b = RelativePath::fromRelativePath( "a" ); + RelativePath c = RelativePath::fromRelativePath( "b" ); + RelativePath d = RelativePath::fromRelativePath( "a/b" ); + + + ASSERT( a == b ); + ASSERT( a != c ); + ASSERT( a != d ); + ASSERT( c != d ); + } + }; class All : public Suite { public: - All() : Suite( "basic" ){ + All() : Suite( "basic" ) { } - - void setupTests(){ + + void setupTests() { add< Rarely >(); add< Base64Tests >(); - + add< stringbuildertests::simple1 >(); add< stringbuildertests::simple2 >(); add< stringbuildertests::reset1 >(); @@ -513,18 +600,28 @@ namespace BasicTests { add< sleeptest >(); add< AssertTests >(); - + add< ArrayTests::basic1 >(); add< LexNumCmp >(); add< DatabaseValidNames >(); + add< DatabaseOwnsNS >(); + + add< NSValidNames >(); add< PtrTests >(); add< StringSplitterTest >(); add< IsValidUTF8Test >(); + + add< QueueTest >(); + + add< StrTests >(); + + add< HostAndPortTests >(); + add< RelativePathTest >(); } } myall; - + } // namespace BasicTests |