public:
virtual void run() {
testCopy();
- testDestroy();
testBasic();
- testAddSorted();
- testAddUnsorted();
+ testPut();
testBlocksize();
testCapacity();
testClear();
testCount();
- testCompareItems();
testEnsuresSize();
testGet();
testRemove();
- testRemoveAt();
- testSorted();
- testSetSorted();
testDestroyItem();
}
void testCopy() {
{
- CStringInt table1;
- table1.put ( "Hi", 4711 );
+ CStringCString table1;
+ table1.put ( "Hi", "123" );
- CStringInt table2 ( table1 );
+ CStringCString table2 ( table1 );
checkE ( 1, table2.count() );
- checkE ( 4711, table2.get ( "Hi" ) );
+ checkE ( "123", table2.get ( "Hi" ) );
- table1.put ( "Hello", 0xdeadbeef );
+ table1.put ( "Hello", "." );
table2 = table1;
checkE ( 2, table2.count() );
- checkE ( 4711, table2.get ( "Hi" ) );
- checkE ( 0xdeadbeef, table2.get ( "Hello" ) );
+ checkE ( "123", table2.get ( "Hi" ) );
+ checkE ( ".", table2.get ( "Hello" ) );
}
}
int capacity = 2;
int blocksize = 4;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
- table.add ( "hi" );
- table.add ( " " );
- table.add ( "world" );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
+ table.put ( "hi", "443" );
+ table.put ( "556", "bcd" );
+ table.put ( "...", "" );
DynBuffer buffer;
- checkE ( "hi world", table.join ( buffer ) );
+ checkE ( "...: 556:bcd hi:443", table.join ( buffer, " " ) );
table.clear();
buffer.clear();
checkE ( "", table.join ( buffer ) );
}
- void testAddUnsorted() {
+ void testPut() {
int capacity = 2;
int blocksize = 4;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
- DynBuffer buffer;
- for ( int ix = 0; ix < 1024; ix++ ) {
- buffer.clear().appendInt ( ix );
- table.add ( buffer.str() );
- }
- for ( int ix = 0; ix < 1024; ix++ ) {
- buffer.clear().appendInt ( ix );
- checkE ( buffer.str(), table.get ( ix ) );
- }
- }
- void testAddSorted() {
- int capacity = 2;
- int blocksize = 4;
- int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, sorted );
- DynBuffer buffer;
- for ( int ix = 0; ix < m_permutationSize; ix++ ) {
- buffer.clear().appendInt ( m_permutation[ix] );
- table.add ( buffer.str() );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
+ DynBuffer key;
+ DynBuffer value;
+ for ( int ix = 1; ix <= 1024; ix++ ) {
+ key.fill ( ' ' + ix % 95, ix );
+ value.fill('!' + ix % 93, 1025 - ix);
+ table.put ( key.str(), value.str() );
}
- //table.dump(NULL);
- for ( int ix = 0; ix < m_permutationSize; ix++ ) {
- buffer.clear().appendInt ( ix );
- checkE ( buffer.str(), table.get ( ix ) );
+ for ( int ix = 1; ix <= 1024; ix++ ) {
+ key.fill ( ' ' + ix % 95, ix );
+ value.fill('!' + ix % 93, 1025 - ix);
+ checkE ( value.str(), table.get ( key.str() ) );
}
}
- void testBlocksize() {
+ void testBlocksize() {
int capacity = 1;
int blocksize = 1;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
DynBuffer buffer;
for ( int ix = 1; ix < 1024; ix++ ) {
buffer.clear().appendInt ( ix );
- table.add ( buffer.str() );
+ table.put ( buffer.str(), buffer.str() );
}
checkE ( 512, table.blocksize() );
}
int capacity = 16;
int blocksize = 16;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
DynBuffer buffer;
for ( int ix = 1; ix < 1024; ix++ ) {
buffer.clear().appendInt ( ix );
- table.add ( buffer.str() );
+ table.put ( buffer.str(), buffer.str() );
}
checkE ( 2032, table.capacity() );
}
int capacity = 16;
int blocksize = 16;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
DynBuffer buffer;
for ( int ix = 1; ix < 1024; ix++ ) {
buffer.clear().appendInt ( ix );
- table.add ( buffer.str() );
+ table.put ( buffer.str(), "" );
}
checkE ( 1023, table.count() );
checkE ( 0, table.clear().count() );
- }
- void testCompareItems() {
- CStringFactory& factory = *CStringFactory::instance();
- // same length:
- checkT ( factory.compareItems ( "abc", "abd" ) < 0 );
- checkT ( factory.compareItems ( "abc", "abc" ) == 0 );
- checkT ( factory.compareItems ( "abc", "abb" ) > 0 );
- // different length:
- checkT ( factory.compareItems ( "abc", "abc " ) < 0 );
- checkT ( factory.compareItems ( "abc ", "abc" ) > 0 );
-
}
void testCount() {
int capacity = 16;
int blocksize = 16;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
DynBuffer buffer;
for ( int ix = 1; ix <= 1024; ix++ ) {
buffer.clear().appendInt ( ix );
- table.add ( buffer.str() );
+ table.put ( buffer.str(), "" );
checkE ( ix, table.count() );
}
}
void testDestroyItem() {
DynBuffer big;
- int size = 100*1024*1024;
- big.ensureSize ( size );
- memset ( big.buffer(), 'x', size -1 );
- big.setLength ( size - 1 );
+ int size = 25*1024*1024;
+ big.fill ( 'A', size );
printf("waiting for 128...\n");
for ( int ix = 0; ix < 128; ix++ ) {
if (ix % 10 == 0){
putc('.', stdout);
fflush(stdout);
}
- CStringInt table1;
- table1.add ( big.str() );
- table1.add ( big.str() );
- checkE ( size - 1, strlen ( table1.get ( 0 ) ) );
- checkE ( size - 1, strlen ( table1.get ( 1 ) ) );
+ CStringCString table1;
+ table1.put ( big.str(), "123" );
+ table1.put ( "abc", big.str() );
+ checkE ( "123", table1.get ( big.str() ) );
+ checkE ( size, strlen(table1.get ( "abc" ) ) );
- CStringInt table2 ( table1 );
+ CStringCString table2 ( table1 );
checkE ( 2, table2.count() );
- checkE ( size - 1, strlen ( table2.get ( 0 ) ) );
- checkE ( size - 1, strlen ( table2.get ( 1 ) ) );
+ checkE ( "123", table2.get ( big.str() ) );
+ checkE ( size, strlen(table2.get ( "abc" ) ) );
}
m_logger.say ( LOG_INFO, "end of testDestroyItem()" );
}
int capacity = 1;
int blocksize = 1;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
DynBuffer buffer;
for ( int ix = 1; ix < 1024; ix++ ) {
table.ensuresSize ( 7*ix );
int capacity = 2;
int blocksize = 4;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
- table.add ( "joe" );
- table.add ( "adam" );
- table.add ( "charly" );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
+ table.put ( "joe", "Mozart" );
+ table.put ( "adam", "Wolverine" );
+ table.put ( "charly", "Brown" );
DynBuffer buffer;
checkE ( 3, table.count() );
- checkE ( "joe", table.get ( 0 ) );
- checkE ( "adam", table.get ( 1 ) );
- checkE ( "charly", table.get ( 2 ) );
+ checkE ( "Mozart", table.get ( "joe" ) );
+ checkE ( "Wolverine", table.get ( "adam" ) );
+ checkE ( "Brown", table.get ( "charly" ) );
table.clear();
-
- table.setSorted ( true );
- table.add ( "joe" );
- table.add ( "adam" );
- table.add ( "charly" );
- checkE ( 3, table.count() );
- checkE ( "adam", table.get ( 0 ) );
- checkE ( "charly", table.get ( 1 ) );
- checkE ( "joe", table.get ( 2 ) );
- }
- void testIndexOfUnsorted() {
- {
- int capacity = 16;
- int blocksize = 16;
- int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
- DynBuffer buffer;
- int count = 1;
- for ( int ix = 1; ix <= count; ix++ ) {
- buffer.clear().appendInt ( 0x42ab7*ix );
- table.add ( buffer.str() );
- }
- for ( int ix = 1; ix <= count; ix++ ) {
- buffer.clear().appendInt ( 0x42ab7*ix );
- checkE ( ix - 1, table.indexOf ( buffer.str() ) );
- }
- count++;
- }
- }
- void testIndexOfSorted() {
- int capacity = 16;
- int blocksize = 16;
- int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, sorted );
- DynBuffer buffer;
- for ( int ix = 1; ix <= 1024; ix++ ) {
- buffer.clear().appendInt ( 0x1*ix, "%08x" );
- table.add ( buffer.str() );
- }
- for ( int ix = 1; ix <= 1024; ix++ ) {
- buffer.clear().appendInt ( 0x1*ix, "%08x" );
- checkE ( ix - 1, table.indexOf ( buffer.str() ));
- }
}
void testRemove() {
int capacity = 2;
int blocksize = 4;
int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
+ CStringCString table ( capacity, blocksize, maxBlocksize );
DynBuffer buffer;
for ( int ix = 0; ix < m_permutationSize; ix++ ) {
buffer.clear().appendInt ( m_permutation[ix] );
- table.add ( buffer.str() );
+ table.put ( buffer.str(), "" );
}
for ( int ix = 0; ix < m_permutationSize; ix++ ) {
buffer.clear().appendInt ( ix );
}
checkE ( 0, table.count() );
}
- void testRemoveAt() {
- int capacity = 16;
- int blocksize = 16;
- int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
- DynBuffer buffer;
- for ( int ix = 0; ix < 1024; ix++ ) {
- buffer.clear().appendInt ( ix );
- table.add ( buffer.str() );
- }
- int count = 1024;
- checkE(count, table.count());
- for ( int ix = 1020; ix >= 3; ix-- ) {
- table.removeAt ( ix );
- if (--count != table.count())
- checkE(count, table.count());
- }
- table.dump("reduced table:");
- checkE ( count, table.count() );
- for ( int ix = 0; ix < 3; ix++ ) {
- buffer.clear().appendInt ( ix );
- checkE ( buffer.str(), table.get ( ix ) );
- }
- for ( int ix = 3; ix < table.count(); ix++ ) {
- buffer.clear().appendInt ( ix + 1020 - 3 + 1 );
- checkE ( buffer.str(), table.get ( ix ) );
- }
- }
- void testSorted() {
- int capacity = 16;
- int blocksize = 16;
- int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, ! sorted );
- checkF ( table.sorted() );
- table.setSorted ( sorted );
- checkT ( table.sorted() );
- }
- void testSetSorted() {
- int capacity = 16;
- int blocksize = 16;
- int maxBlocksize = 1024*1024;
- bool sorted = true;
- CStringInt table ( capacity, blocksize, maxBlocksize, sorted );
- checkT ( table.sorted() );
- table.setSorted ( !sorted );
- checkF ( table.sorted() );
- }
};
int TestDictionary::m_permutation[] = { 2, 7, 3, 1, 0, 9, 6, 8, 4, 5 };
int TestDictionary::m_permutationSize = 10;