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/>.
|
|
|
|
*/
|
|
|
|
|
2009-09-30 20:32:17 +02:00
|
|
|
#include "stdafx.h"
|
2009-04-28 16:11:55 +02:00
|
|
|
|
|
|
|
#include "dbtests.h"
|
2009-10-01 05:08:33 +02:00
|
|
|
#include "../util/base64.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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-01 05:08:33 +02:00
|
|
|
class Base64Tests {
|
|
|
|
public:
|
2009-10-01 05:32:28 +02:00
|
|
|
|
|
|
|
void roundTrip( string s ){
|
|
|
|
ASSERT_EQUALS( s , base64::decode( base64::encode( s ) ) );
|
|
|
|
}
|
|
|
|
|
2009-10-01 07:26:19 +02:00
|
|
|
void roundTrip( const char * data , int len ){
|
|
|
|
string s = base64::encode( data , len );
|
|
|
|
string out = base64::decode( s );
|
|
|
|
ASSERT_EQUALS( out.size() , 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);
|
|
|
|
}
|
|
|
|
|
2009-10-01 05:08:33 +02: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" ) );
|
|
|
|
|
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" );
|
2009-10-01 07:26:19 +02:00
|
|
|
|
|
|
|
char z[] = { 0x1 , 0x2 , 0x3 , 0x4 };
|
|
|
|
roundTrip( z , 4 );
|
|
|
|
|
|
|
|
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 );
|
2009-10-01 05:08:33 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-12 22:37:23 +02:00
|
|
|
class All : public Suite {
|
2009-04-28 16:11:55 +02:00
|
|
|
public:
|
2009-09-17 23:23:38 +02:00
|
|
|
All() : Suite( "basic" ){
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupTests(){
|
2009-04-28 16:11:55 +02:00
|
|
|
add< Rarely >();
|
2009-10-01 05:08:33 +02:00
|
|
|
add< Base64Tests >();
|
2009-04-28 16:11:55 +02:00
|
|
|
}
|
2009-09-17 23:23:38 +02:00
|
|
|
} myall;
|
2009-04-28 16:11:55 +02:00
|
|
|
|
|
|
|
} // namespace BasicTests
|
|
|
|
|