1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// 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/>.
*/
#include "stdafx.h"
#include "dbtests.h"
#include "../util/base64.h"
#include "../util/array.h"
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;
}
};
class Base64Tests {
public:
void roundTrip( string s ){
ASSERT_EQUALS( s , base64::decode( base64::encode( s ) ) );
}
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++ ){
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;
ASSERT(0);
}
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 ) );
roundTrip( "e" );
roundTrip( "el" );
roundTrip( "eli" );
roundTrip( "elio" );
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,
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 );
}
};
namespace stringbuildertests {
#define SBTGB(x) ss << (x); sb << (x);
class Base {
virtual void pop() = 0;
public:
Base(){}
virtual ~Base(){}
void run(){
pop();
ASSERT_EQUALS( ss.str() , sb.str() );
}
stringstream ss;
StringBuilder sb;
};
class simple1 : public Base {
void pop(){
SBTGB(1);
SBTGB("yo");
SBTGB(2);
}
};
class simple2 : public Base {
void pop(){
SBTGB(1);
SBTGB("yo");
SBTGB(2);
SBTGB( 12123123123LL );
SBTGB( "xxx" );
SBTGB( 5.4 );
SBTGB( 5.4312 );
SBTGB( "yyy" );
SBTGB( (short)5 );
SBTGB( (short)(1231231231231LL) );
}
};
class reset1 {
public:
void run(){
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:
void run(){
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() );
}
};
}
class sleeptest {
public:
void run(){
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 );
ASSERT( t.millis() <= 2000 );
}
};
class AssertTests {
public:
int x;
AssertTests(){
x = 0;
}
string foo(){
x++;
return "";
}
void run(){
uassert( -1 , foo() , 1 );
ASSERT_EQUALS( 0 , x );
try {
uassert( -1 , foo() , 0 );
}
catch ( ... ){}
ASSERT_EQUALS( 1 , x );
}
};
namespace ArrayTests {
class basic1 {
public:
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 );
++i;
ASSERT( i != a.end() );
ASSERT_EQUALS( 6 , *i );
++i;
ASSERT( i == a.end() );
}
};
};
class ThreadSafeStringTest {
public:
void run(){
ThreadSafeString s;
s = "eliot";
ASSERT_EQUALS( s , "eliot" );
ASSERT( s != "eliot2" );
ThreadSafeString s2 = s;
ASSERT_EQUALS( s2 , "eliot" );
{
string foo;
{
ThreadSafeString bar;
bar = "eliot2";
foo = bar;
}
ASSERT_EQUALS( "eliot2" , foo );
}
}
};
class LexNumCmp {
public:
void run() {
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" ) );
ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aab" ) );
ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aa1" ) );
ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a11" ) );
ASSERT_EQUALS( 1, lexNumCmp( "a1{a", "a1{" ) );
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" ) );
}
};
class All : public Suite {
public:
All() : Suite( "basic" ){
}
void setupTests(){
add< Rarely >();
add< Base64Tests >();
add< stringbuildertests::simple1 >();
add< stringbuildertests::simple2 >();
add< stringbuildertests::reset1 >();
add< stringbuildertests::reset2 >();
add< sleeptest >();
add< AssertTests >();
add< ArrayTests::basic1 >();
add< LexNumCmp >();
}
} myall;
} // namespace BasicTests
|