2009-04-28 16:11:55 +02:00
|
|
|
|
// basictests.cpp : basic unit tests
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 10gen Inc.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-27 21:27:52 +02:00
|
|
|
|
#include "pch.h"
|
2009-04-28 16:11:55 +02:00
|
|
|
|
|
|
|
|
|
#include "dbtests.h"
|
2009-10-01 05:08:33 +02:00
|
|
|
|
#include "../util/base64.h"
|
2010-02-20 22:35:26 +01:00
|
|
|
|
#include "../util/array.h"
|
2010-05-26 21:04:56 +02:00
|
|
|
|
#include "../util/text.h"
|
2010-09-13 06:34:31 +02:00
|
|
|
|
#include "../util/queue.h"
|
2010-12-19 06:56:44 +01:00
|
|
|
|
#include "../util/paths.h"
|
2009-04-28 16:11:55 +02:00
|
|
|
|
|
|
|
|
|
namespace BasicTests {
|
|
|
|
|
|
|
|
|
|
class Rarely {
|
|
|
|
|
public:
|
|
|
|
|
void run() {
|
|
|
|
|
int first = 0;
|
|
|
|
|
int second = 0;
|
|
|
|
|
int third = 0;
|
|
|
|
|
for( int i = 0; i < 128; ++i ) {
|
|
|
|
|
incRarely( first );
|
|
|
|
|
incRarely2( second );
|
|
|
|
|
ONCE ++third;
|
|
|
|
|
}
|
|
|
|
|
ASSERT_EQUALS( 1, first );
|
|
|
|
|
ASSERT_EQUALS( 1, second );
|
|
|
|
|
ASSERT_EQUALS( 1, third );
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
void incRarely( int &c ) {
|
|
|
|
|
RARELY ++c;
|
|
|
|
|
}
|
|
|
|
|
void incRarely2( int &c ) {
|
|
|
|
|
RARELY ++c;
|
|
|
|
|
}
|
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-10-01 05:08:33 +02:00
|
|
|
|
class Base64Tests {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
|
|
void roundTrip( string s ) {
|
2009-10-01 05:32:28 +02:00
|
|
|
|
ASSERT_EQUALS( s , base64::decode( base64::encode( s ) ) );
|
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
|
|
void roundTrip( const unsigned char * _data , int len ) {
|
2009-10-12 21:12:16 +02:00
|
|
|
|
const char *data = (const char *) _data;
|
2009-10-01 07:26:19 +02:00
|
|
|
|
string s = base64::encode( data , len );
|
|
|
|
|
string out = base64::decode( s );
|
2009-10-15 16:21:17 +02:00
|
|
|
|
ASSERT_EQUALS( out.size() , static_cast<size_t>(len) );
|
2009-10-01 07:26:19 +02:00
|
|
|
|
bool broke = false;
|
2011-01-04 06:40:41 +01:00
|
|
|
|
for ( int i=0; i<len; i++ ) {
|
2009-10-01 07:26:19 +02:00
|
|
|
|
if ( data[i] != out[i] )
|
|
|
|
|
broke = true;
|
|
|
|
|
}
|
|
|
|
|
if ( ! broke )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
cout << s << endl;
|
|
|
|
|
for ( int i=0; i<len; i++ )
|
|
|
|
|
cout << hex << ( data[i] & 0xFF ) << dec << " ";
|
|
|
|
|
cout << endl;
|
|
|
|
|
for ( int i=0; i<len; i++ )
|
|
|
|
|
cout << hex << ( out[i] & 0xFF ) << dec << " ";
|
|
|
|
|
cout << endl;
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-10-01 07:26:19 +02:00
|
|
|
|
ASSERT(0);
|
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
|
|
void run() {
|
2009-10-01 05:32:28 +02:00
|
|
|
|
|
2009-10-01 05:08:33 +02:00
|
|
|
|
ASSERT_EQUALS( "ZWxp" , base64::encode( "eli" , 3 ) );
|
|
|
|
|
ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" , 6 ) );
|
2009-10-01 05:32:28 +02:00
|
|
|
|
ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" ) );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-10-01 05:08:33 +02:00
|
|
|
|
ASSERT_EQUALS( "ZQ==" , base64::encode( "e" , 1 ) );
|
|
|
|
|
ASSERT_EQUALS( "ZWw=" , base64::encode( "el" , 2 ) );
|
2009-10-01 05:32:28 +02:00
|
|
|
|
|
|
|
|
|
roundTrip( "e" );
|
|
|
|
|
roundTrip( "el" );
|
|
|
|
|
roundTrip( "eli" );
|
|
|
|
|
roundTrip( "elio" );
|
|
|
|
|
roundTrip( "eliot" );
|
|
|
|
|
roundTrip( "eliots" );
|
|
|
|
|
roundTrip( "eliotsz" );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-10-12 21:12:16 +02:00
|
|
|
|
unsigned char z[] = { 0x1 , 0x2 , 0x3 , 0x4 };
|
2009-10-01 07:26:19 +02:00
|
|
|
|
roundTrip( z , 4 );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-10-12 21:12:16 +02:00
|
|
|
|
unsigned char y[] = {
|
2009-10-01 07:26:19 +02:00
|
|
|
|
0x01, 0x10, 0x83, 0x10, 0x51, 0x87, 0x20, 0x92, 0x8B, 0x30,
|
|
|
|
|
0xD3, 0x8F, 0x41, 0x14, 0x93, 0x51, 0x55, 0x97, 0x61, 0x96,
|
|
|
|
|
0x9B, 0x71, 0xD7, 0x9F, 0x82, 0x18, 0xA3, 0x92, 0x59, 0xA7,
|
|
|
|
|
0xA2, 0x9A, 0xAB, 0xB2, 0xDB, 0xAF, 0xC3, 0x1C, 0xB3, 0xD3,
|
|
|
|
|
0x5D, 0xB7, 0xE3, 0x9E, 0xBB, 0xF3, 0xDF, 0xBF
|
|
|
|
|
};
|
|
|
|
|
roundTrip( y , 4 );
|
|
|
|
|
roundTrip( y , 40 );
|
2009-10-01 05:08:33 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
2009-12-29 23:32:31 +01:00
|
|
|
|
|
|
|
|
|
namespace stringbuildertests {
|
|
|
|
|
#define SBTGB(x) ss << (x); sb << (x);
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-12-29 23:32:31 +01:00
|
|
|
|
class Base {
|
|
|
|
|
virtual void pop() = 0;
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-12-29 23:32:31 +01:00
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
Base() {}
|
|
|
|
|
virtual ~Base() {}
|
2009-12-29 23:32:31 +01:00
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2009-12-29 23:32:31 +01:00
|
|
|
|
pop();
|
|
|
|
|
ASSERT_EQUALS( ss.str() , sb.str() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringstream ss;
|
|
|
|
|
StringBuilder sb;
|
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-12-29 23:32:31 +01:00
|
|
|
|
class simple1 : public Base {
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void pop() {
|
2009-12-29 23:32:31 +01:00
|
|
|
|
SBTGB(1);
|
|
|
|
|
SBTGB("yo");
|
|
|
|
|
SBTGB(2);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class simple2 : public Base {
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void pop() {
|
2009-12-29 23:32:31 +01:00
|
|
|
|
SBTGB(1);
|
|
|
|
|
SBTGB("yo");
|
|
|
|
|
SBTGB(2);
|
|
|
|
|
SBTGB( 12123123123LL );
|
|
|
|
|
SBTGB( "xxx" );
|
|
|
|
|
SBTGB( 5.4 );
|
|
|
|
|
SBTGB( 5.4312 );
|
|
|
|
|
SBTGB( "yyy" );
|
|
|
|
|
SBTGB( (short)5 );
|
2009-12-30 03:26:55 +01:00
|
|
|
|
SBTGB( (short)(1231231231231LL) );
|
2009-12-29 23:32:31 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-12-29 23:32:31 +01:00
|
|
|
|
class reset1 {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2009-12-29 23:32:31 +01:00
|
|
|
|
StringBuilder sb;
|
|
|
|
|
sb << "1" << "abc" << "5.17";
|
|
|
|
|
ASSERT_EQUALS( "1abc5.17" , sb.str() );
|
|
|
|
|
ASSERT_EQUALS( "1abc5.17" , sb.str() );
|
|
|
|
|
sb.reset();
|
|
|
|
|
ASSERT_EQUALS( "" , sb.str() );
|
|
|
|
|
sb << "999";
|
|
|
|
|
ASSERT_EQUALS( "999" , sb.str() );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class reset2 {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2009-12-29 23:32:31 +01:00
|
|
|
|
StringBuilder sb;
|
|
|
|
|
sb << "1" << "abc" << "5.17";
|
|
|
|
|
ASSERT_EQUALS( "1abc5.17" , sb.str() );
|
|
|
|
|
ASSERT_EQUALS( "1abc5.17" , sb.str() );
|
|
|
|
|
sb.reset(1);
|
|
|
|
|
ASSERT_EQUALS( "" , sb.str() );
|
|
|
|
|
sb << "999";
|
|
|
|
|
ASSERT_EQUALS( "999" , sb.str() );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2010-01-14 21:35:29 +01:00
|
|
|
|
|
|
|
|
|
class sleeptest {
|
|
|
|
|
public:
|
2010-03-27 04:32:14 +01:00
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-01-14 21:35:29 +01:00
|
|
|
|
Timer t;
|
|
|
|
|
sleepsecs( 1 );
|
|
|
|
|
ASSERT_EQUALS( 1 , t.seconds() );
|
|
|
|
|
|
|
|
|
|
t.reset();
|
|
|
|
|
sleepmicros( 1527123 );
|
|
|
|
|
ASSERT( t.micros() > 1000000 );
|
|
|
|
|
ASSERT( t.micros() < 2000000 );
|
|
|
|
|
|
|
|
|
|
t.reset();
|
|
|
|
|
sleepmillis( 1727 );
|
|
|
|
|
ASSERT( t.millis() >= 1000 );
|
2010-08-03 15:29:58 +02:00
|
|
|
|
ASSERT( t.millis() <= 2500 );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-04-27 20:18:56 +02:00
|
|
|
|
{
|
|
|
|
|
int total = 1200;
|
|
|
|
|
int ms = 2;
|
|
|
|
|
t.reset();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
for ( int i=0; i<(total/ms); i++ ) {
|
2010-04-27 20:18:56 +02:00
|
|
|
|
sleepmillis( ms );
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
int x = t.millis();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
if ( x < 1000 || x > 2500 ) {
|
2010-04-27 20:18:56 +02:00
|
|
|
|
cout << "sleeptest x: " << x << endl;
|
|
|
|
|
ASSERT( x >= 1000 );
|
|
|
|
|
ASSERT( x <= 20000 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-27 04:32:14 +01:00
|
|
|
|
}
|
2010-04-27 20:18:56 +02:00
|
|
|
|
|
2010-04-27 21:14:32 +02:00
|
|
|
|
#ifdef __linux__
|
2010-03-27 06:11:43 +01:00
|
|
|
|
{
|
2010-04-27 20:18:56 +02:00
|
|
|
|
int total = 1200;
|
|
|
|
|
int micros = 100;
|
|
|
|
|
t.reset();
|
|
|
|
|
int numSleeps = 1000*(total/micros);
|
2011-01-04 06:40:41 +01:00
|
|
|
|
for ( int i=0; i<numSleeps; i++ ) {
|
2010-04-27 20:18:56 +02:00
|
|
|
|
sleepmicros( micros );
|
|
|
|
|
}
|
|
|
|
|
{
|
2010-04-27 21:14:32 +02:00
|
|
|
|
int y = t.millis();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
if ( y < 1000 || y > 2500 ) {
|
2010-04-27 21:14:32 +02:00
|
|
|
|
cout << "sleeptest y: " << y << endl;
|
|
|
|
|
ASSERT( y >= 1000 );
|
2010-08-10 22:28:50 +02:00
|
|
|
|
/* ASSERT( y <= 100000 ); */
|
2010-04-27 20:18:56 +02:00
|
|
|
|
}
|
2010-03-27 06:11:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-27 21:14:32 +02:00
|
|
|
|
#endif
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-01-14 21:35:29 +01:00
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-01-14 21:35:29 +01:00
|
|
|
|
};
|
2010-01-16 23:34:44 +01:00
|
|
|
|
|
|
|
|
|
class AssertTests {
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
int x;
|
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
AssertTests() {
|
2010-01-16 23:34:44 +01:00
|
|
|
|
x = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
string foo() {
|
2010-01-16 23:34:44 +01:00
|
|
|
|
x++;
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-01-16 23:34:44 +01:00
|
|
|
|
uassert( -1 , foo() , 1 );
|
2010-04-25 00:25:58 +02:00
|
|
|
|
if( x != 0 ) {
|
|
|
|
|
ASSERT_EQUALS( 0 , x );
|
|
|
|
|
}
|
2010-01-16 23:34:44 +01:00
|
|
|
|
try {
|
|
|
|
|
uassert( -1 , foo() , 0 );
|
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
catch ( ... ) {}
|
2010-01-16 23:34:44 +01:00
|
|
|
|
ASSERT_EQUALS( 1 , x );
|
|
|
|
|
}
|
|
|
|
|
};
|
2010-02-20 22:35:26 +01:00
|
|
|
|
|
|
|
|
|
namespace ArrayTests {
|
|
|
|
|
class basic1 {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-02-20 22:35:26 +01:00
|
|
|
|
FastArray<int> a(100);
|
|
|
|
|
a.push_back( 5 );
|
|
|
|
|
a.push_back( 6 );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-02-20 22:35:26 +01:00
|
|
|
|
ASSERT_EQUALS( 2 , a.size() );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-02-20 22:35:26 +01:00
|
|
|
|
FastArray<int>::iterator i = a.begin();
|
|
|
|
|
ASSERT( i != a.end() );
|
|
|
|
|
ASSERT_EQUALS( 5 , *i );
|
|
|
|
|
++i;
|
|
|
|
|
ASSERT( i != a.end() );
|
|
|
|
|
ASSERT_EQUALS( 6 , *i );
|
|
|
|
|
++i;
|
|
|
|
|
ASSERT( i == a.end() );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-02-26 18:07:46 +01:00
|
|
|
|
class ThreadSafeStringTest {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-02-26 18:07:46 +01:00
|
|
|
|
ThreadSafeString s;
|
|
|
|
|
s = "eliot";
|
|
|
|
|
ASSERT_EQUALS( s , "eliot" );
|
|
|
|
|
ASSERT( s != "eliot2" );
|
|
|
|
|
|
|
|
|
|
ThreadSafeString s2 = s;
|
|
|
|
|
ASSERT_EQUALS( s2 , "eliot" );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
|
|
|
2010-02-26 18:07:46 +01:00
|
|
|
|
{
|
|
|
|
|
string foo;
|
|
|
|
|
{
|
|
|
|
|
ThreadSafeString bar;
|
|
|
|
|
bar = "eliot2";
|
2010-07-19 19:03:57 +02:00
|
|
|
|
foo = bar.toString();
|
2010-02-26 18:07:46 +01:00
|
|
|
|
}
|
|
|
|
|
ASSERT_EQUALS( "eliot2" , foo );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-03-02 21:29:54 +01:00
|
|
|
|
class LexNumCmp {
|
|
|
|
|
public:
|
|
|
|
|
void run() {
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-08-26 00:30:51 +02:00
|
|
|
|
ASSERT( ! isNumber( (char)255 ) );
|
|
|
|
|
|
2010-03-02 21:29:54 +01:00
|
|
|
|
ASSERT_EQUALS( 0, lexNumCmp( "a", "a" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "a", "aa" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "aa", "a" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "a", "b" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "100", "50" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "50", "100" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "b", "a" ) );
|
|
|
|
|
ASSERT_EQUALS( 0, lexNumCmp( "aa", "aa" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "aa", "ab" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "ab", "aa" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "0", "a" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "a0", "aa" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "a", "0" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "aa", "a0" ) );
|
|
|
|
|
ASSERT_EQUALS( 0, lexNumCmp( "0", "0" ) );
|
|
|
|
|
ASSERT_EQUALS( 0, lexNumCmp( "10", "10" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "1", "10" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "10", "1" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "11", "10" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "10", "11" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "f11f", "f10f" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "f10f", "f11f" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "f11f", "f111" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "f111", "f11f" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "f12f", "f12g" ) );
|
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "f12g", "f12f" ) );
|
2010-03-02 22:16:20 +01:00
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aab" ) );
|
2010-08-26 00:30:51 +02:00
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "aa{", "aa1" ) );
|
2010-03-26 20:30:29 +01:00
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a11" ) );
|
2010-03-02 22:16:20 +01:00
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp( "a1{a", "a1{" ) );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a1{a" ) );
|
2010-03-03 16:28:20 +01:00
|
|
|
|
ASSERT_EQUALS( 1, lexNumCmp("21", "11") );
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp("11", "21") );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-03-26 20:30:29 +01:00
|
|
|
|
ASSERT_EQUALS( -1 , lexNumCmp( "a.0" , "a.1" ) );
|
|
|
|
|
ASSERT_EQUALS( -1 , lexNumCmp( "a.0.b" , "a.1" ) );
|
2010-08-26 00:30:51 +02:00
|
|
|
|
|
|
|
|
|
ASSERT_EQUALS( -1 , lexNumCmp( "b." , "b.|" ) );
|
|
|
|
|
ASSERT_EQUALS( -1 , lexNumCmp( "b.0e" , (string("b.") + (char)255).c_str() ) );
|
|
|
|
|
ASSERT_EQUALS( -1 , lexNumCmp( "b." , "b.0e" ) );
|
2010-08-26 22:30:10 +02:00
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
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"));
|
2010-08-26 22:30:10 +02:00
|
|
|
|
ASSERT_EQUALS( 0, lexNumCmp( "0", "000"));
|
|
|
|
|
ASSERT_EQUALS( -1, lexNumCmp( "0000", "0.0"));
|
2011-01-04 06:40:41 +01:00
|
|
|
|
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"));
|
2010-03-02 21:29:54 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2010-05-18 15:59:43 +02:00
|
|
|
|
|
|
|
|
|
class DatabaseValidNames {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-05-18 15:59:43 +02:00
|
|
|
|
ASSERT( Database::validDBName( "foo" ) );
|
|
|
|
|
ASSERT( ! Database::validDBName( "foo/bar" ) );
|
|
|
|
|
ASSERT( ! Database::validDBName( "foo.bar" ) );
|
2010-06-09 18:08:02 +02:00
|
|
|
|
|
2010-10-06 17:19:43 +02:00
|
|
|
|
ASSERT( isANormalNSName( "asdads" ) );
|
|
|
|
|
ASSERT( ! isANormalNSName( "asda$ds" ) );
|
|
|
|
|
ASSERT( isANormalNSName( "local.oplog.$main" ) );
|
2010-05-18 15:59:43 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
2010-09-22 20:55:57 +02:00
|
|
|
|
|
|
|
|
|
class DatabaseOwnsNS {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
|
|
|
|
|
2010-09-22 20:55:57 +02:00
|
|
|
|
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 );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-09-22 20:55:57 +02:00
|
|
|
|
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" ) );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-31 05:59:01 +01:00
|
|
|
|
class NSValidNames {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-12-31 05:59:01 +01:00
|
|
|
|
ASSERT( isValidNS( "test.foo" ) );
|
|
|
|
|
ASSERT( ! isValidNS( "test." ) );
|
|
|
|
|
ASSERT( ! isValidNS( "test" ) );
|
|
|
|
|
}
|
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-05-20 04:47:15 +02:00
|
|
|
|
class PtrTests {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-05-20 04:47:15 +02:00
|
|
|
|
scoped_ptr<int> p1 (new int(1));
|
2010-05-20 15:27:59 +02:00
|
|
|
|
boost::shared_ptr<int> p2 (new int(2));
|
2010-05-20 04:47:15 +02:00
|
|
|
|
scoped_ptr<const int> p3 (new int(3));
|
2010-05-20 15:27:59 +02:00
|
|
|
|
boost::shared_ptr<const int> p4 (new int(4));
|
2010-05-20 04:47:15 +02:00
|
|
|
|
|
|
|
|
|
//non-const
|
|
|
|
|
ASSERT_EQUALS( p1.get() , ptr<int>(p1) );
|
|
|
|
|
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
|
2011-01-04 06:40:41 +01:00
|
|
|
|
ASSERT_EQUALS( *p2 , *ptr<int>(p2));
|
2010-05-20 15:27:59 +02:00
|
|
|
|
ASSERT_EQUALS( p2.get() , ptr<boost::shared_ptr<int> >(&p2)->get() ); // operator->
|
2010-05-20 04:47:15 +02:00
|
|
|
|
|
|
|
|
|
//const
|
|
|
|
|
ASSERT_EQUALS( p1.get() , ptr<const int>(p1) );
|
|
|
|
|
ASSERT_EQUALS( p2.get() , ptr<const int>(p2) );
|
|
|
|
|
ASSERT_EQUALS( p2.get() , ptr<const int>(p2.get()) );
|
|
|
|
|
ASSERT_EQUALS( p3.get() , ptr<const int>(p3) );
|
|
|
|
|
ASSERT_EQUALS( p4.get() , ptr<const int>(p4) );
|
|
|
|
|
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
|
2011-01-04 06:40:41 +01:00
|
|
|
|
ASSERT_EQUALS( *p2 , *ptr<int>(p2));
|
2010-05-20 15:27:59 +02:00
|
|
|
|
ASSERT_EQUALS( p2.get() , ptr<const boost::shared_ptr<int> >(&p2)->get() );
|
2010-05-20 04:47:15 +02:00
|
|
|
|
|
|
|
|
|
//bool context
|
|
|
|
|
ASSERT( ptr<int>(p1) );
|
|
|
|
|
ASSERT( !ptr<int>(NULL) );
|
|
|
|
|
ASSERT( !ptr<int>() );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-05-20 04:47:15 +02:00
|
|
|
|
#if 0
|
|
|
|
|
// These shouldn't compile
|
|
|
|
|
ASSERT_EQUALS( p3.get() , ptr<int>(p3) );
|
|
|
|
|
ASSERT_EQUALS( p4.get() , ptr<int>(p4) );
|
|
|
|
|
ASSERT_EQUALS( p2.get() , ptr<int>(ptr<const int>(p2)) );
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-05-26 21:04:56 +02:00
|
|
|
|
struct StringSplitterTest {
|
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void test( string s ) {
|
2010-05-26 21:04:56 +02:00
|
|
|
|
vector<string> v = StringSplitter::split( s , "," );
|
|
|
|
|
ASSERT_EQUALS( s , StringSplitter::join( v , "," ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-05-26 21:04:56 +02:00
|
|
|
|
test( "a" );
|
|
|
|
|
test( "a,b" );
|
|
|
|
|
test( "a,b,c" );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-06-03 21:35:03 +02:00
|
|
|
|
struct IsValidUTF8Test {
|
|
|
|
|
// macros used to get valid line numbers
|
|
|
|
|
#define good(s) ASSERT(isValidUTF8(s));
|
|
|
|
|
#define bad(s) ASSERT(!isValidUTF8(s));
|
|
|
|
|
|
|
|
|
|
void run() {
|
|
|
|
|
good("A");
|
|
|
|
|
good("\xC2\xA2"); // cent: ¢
|
|
|
|
|
good("\xE2\x82\xAC"); // euro: €
|
|
|
|
|
good("\xF0\x9D\x90\x80"); // Blackboard A: 𝐀
|
|
|
|
|
|
|
|
|
|
//abrupt end
|
|
|
|
|
bad("\xC2");
|
|
|
|
|
bad("\xE2\x82");
|
|
|
|
|
bad("\xF0\x9D\x90");
|
|
|
|
|
bad("\xC2 ");
|
|
|
|
|
bad("\xE2\x82 ");
|
|
|
|
|
bad("\xF0\x9D\x90 ");
|
|
|
|
|
|
|
|
|
|
//too long
|
|
|
|
|
bad("\xF8\x80\x80\x80\x80");
|
|
|
|
|
bad("\xFC\x80\x80\x80\x80\x80");
|
|
|
|
|
bad("\xFE\x80\x80\x80\x80\x80\x80");
|
|
|
|
|
bad("\xFF\x80\x80\x80\x80\x80\x80\x80");
|
|
|
|
|
|
|
|
|
|
bad("\xF5\x80\x80\x80"); // U+140000 > U+10FFFF
|
|
|
|
|
bad("\x80"); //cant start with continuation byte
|
|
|
|
|
bad("\xC0\x80"); // 2-byte version of ASCII NUL
|
|
|
|
|
#undef good
|
|
|
|
|
#undef bad
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-05-26 21:04:56 +02:00
|
|
|
|
|
2010-09-13 06:34:31 +02:00
|
|
|
|
class QueueTest {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-09-13 06:34:31 +02:00
|
|
|
|
BlockingQueue<int> q;
|
|
|
|
|
Timer t;
|
2010-09-13 17:35:14 +02:00
|
|
|
|
int x;
|
|
|
|
|
ASSERT( ! q.blockingPop( x , 5 ) );
|
2010-09-13 06:34:31 +02:00
|
|
|
|
ASSERT( t.seconds() > 3 && t.seconds() < 9 );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
2010-05-26 21:04:56 +02:00
|
|
|
|
|
2010-12-14 08:40:27 +01:00
|
|
|
|
class StrTests {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
|
|
void run() {
|
2010-12-14 12:51:42 +01:00
|
|
|
|
ASSERT_EQUALS( 1u , str::count( "abc" , 'b' ) );
|
|
|
|
|
ASSERT_EQUALS( 3u , str::count( "babab" , 'b' ) );
|
2010-12-14 08:40:27 +01:00
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-12-14 08:40:27 +01:00
|
|
|
|
};
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-12-19 05:34:26 +01:00
|
|
|
|
class HostAndPortTests {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-12-19 05:34:26 +01:00
|
|
|
|
HostAndPort a( "x1" , 1000 );
|
|
|
|
|
HostAndPort b( "x1" , 1000 );
|
|
|
|
|
HostAndPort c( "x1" , 1001 );
|
|
|
|
|
HostAndPort d( "x2" , 1000 );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-12-19 05:34:26 +01:00
|
|
|
|
ASSERT( a == b );
|
|
|
|
|
ASSERT( a != c );
|
|
|
|
|
ASSERT( a != d );
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-12-19 05:34:26 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2010-12-14 08:40:27 +01:00
|
|
|
|
|
2010-12-19 06:56:44 +01:00
|
|
|
|
class RelativePathTest {
|
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
void run() {
|
2010-12-19 06:56:44 +01:00
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-05-12 22:37:23 +02:00
|
|
|
|
class All : public Suite {
|
2009-04-28 16:11:55 +02:00
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
|
All() : Suite( "basic" ) {
|
2009-09-17 23:23:38 +02:00
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
|
|
void setupTests() {
|
2009-04-28 16:11:55 +02:00
|
|
|
|
add< Rarely >();
|
2009-10-01 05:08:33 +02:00
|
|
|
|
add< Base64Tests >();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-12-29 23:32:31 +01:00
|
|
|
|
add< stringbuildertests::simple1 >();
|
|
|
|
|
add< stringbuildertests::simple2 >();
|
|
|
|
|
add< stringbuildertests::reset1 >();
|
|
|
|
|
add< stringbuildertests::reset2 >();
|
2010-01-14 21:35:29 +01:00
|
|
|
|
|
|
|
|
|
add< sleeptest >();
|
2010-05-18 15:59:43 +02:00
|
|
|
|
add< AssertTests >();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-02-20 22:35:26 +01:00
|
|
|
|
add< ArrayTests::basic1 >();
|
2010-03-02 21:29:54 +01:00
|
|
|
|
add< LexNumCmp >();
|
2010-05-18 15:59:43 +02:00
|
|
|
|
|
|
|
|
|
add< DatabaseValidNames >();
|
2010-09-22 20:55:57 +02:00
|
|
|
|
add< DatabaseOwnsNS >();
|
2010-05-20 04:47:15 +02:00
|
|
|
|
|
2010-12-31 05:59:01 +01:00
|
|
|
|
add< NSValidNames >();
|
|
|
|
|
|
2010-05-20 04:47:15 +02:00
|
|
|
|
add< PtrTests >();
|
2010-05-26 21:04:56 +02:00
|
|
|
|
|
|
|
|
|
add< StringSplitterTest >();
|
2010-06-03 21:35:03 +02:00
|
|
|
|
add< IsValidUTF8Test >();
|
2010-09-13 06:34:31 +02:00
|
|
|
|
|
|
|
|
|
add< QueueTest >();
|
2010-12-14 08:40:27 +01:00
|
|
|
|
|
|
|
|
|
add< StrTests >();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2010-12-19 05:34:26 +01:00
|
|
|
|
add< HostAndPortTests >();
|
2010-12-19 06:56:44 +01:00
|
|
|
|
add< RelativePathTest >();
|
2009-04-28 16:11:55 +02:00
|
|
|
|
}
|
2009-09-17 23:23:38 +02:00
|
|
|
|
} myall;
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
2009-04-28 16:11:55 +02:00
|
|
|
|
} // namespace BasicTests
|
|
|
|
|
|