From: hama Date: Tue, 16 Aug 2016 21:23:07 +0000 (+0200) Subject: DynBuffer.fill(), dictionary tested X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=HEAD;p=cpidjinn DynBuffer.fill(), dictionary tested --- diff --git a/util/cudictionary.cpp b/util/cudictionary.cpp index 7d669d3..24b65e1 100644 --- a/util/cudictionary.cpp +++ b/util/cudictionary.cpp @@ -18,37 +18,31 @@ class TestDictionary : public UnitTest { 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" ) ); } } @@ -56,60 +50,43 @@ class TestDictionary : public UnitTest { 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() ); } @@ -117,12 +94,11 @@ class TestDictionary : public UnitTest { 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() ); } @@ -130,47 +106,32 @@ class TestDictionary : public UnitTest { 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){ @@ -181,16 +142,16 @@ class TestDictionary : public UnitTest { 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()" ); } @@ -198,8 +159,7 @@ class TestDictionary : public UnitTest { 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 ); @@ -210,73 +170,26 @@ class TestDictionary : public UnitTest { 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 ); @@ -284,55 +197,6 @@ class TestDictionary : public UnitTest { } 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; diff --git a/util/cudynbuffer.cpp b/util/cudynbuffer.cpp index 8b1fe0d..213e0f8 100644 --- a/util/cudynbuffer.cpp +++ b/util/cudynbuffer.cpp @@ -7,8 +7,11 @@ public: { } + virtual ~TestDynBuffer(){ + } public: virtual void run(){ + testFill(); testBasic(); testAppend(); testAppendFormatted(); @@ -23,6 +26,14 @@ public: testValueOfLE(); testMemory(); } + void testFill(){ + DynBuffer buf; + buf.fill('x', 5); + checkE("xxxxx", buf); + buf.fill('y'); + checkE("yyyyy", buf); + checkE("zz", buf.fill('z', 2)); + } void testBasic(){ DynBuffer buf("Hi", 0, 2); buf.setBlocksize(2); diff --git a/util/dictionary.cpp b/util/dictionary.cpp index 085855b..f6c545b 100644 --- a/util/dictionary.cpp +++ b/util/dictionary.cpp @@ -309,7 +309,9 @@ DynBuffer& CStringCString::join(DynBuffer& buffer, const char* separator){ for (int ix = 0; ix < m_count; ix++){ if (ix > 0 && separator != NULL) buffer.append(separator); - buffer.append(get(ix)); + buffer.append(reinterpret_cast(m_table[ix].m_key)); + buffer.append(":"); + buffer.append(reinterpret_cast(m_table[ix].m_value)); } return buffer; } @@ -321,8 +323,8 @@ DynBuffer& CStringCString::join(DynBuffer& buffer, const char* separator){ */ void CStringCString::dump(const char* title) const{ printf("=== %s: count: %d capacity: %d\n", title == NULL ? "" : title, count(), capacity()); - const char* ptr; for (int ii = 0; ii < m_count; ii++) - printf("%2d: %s\n", ii, (ptr = get(ii)) == NULL ? "" : ptr); + printf("%2d: %s:%s\n", ii, (char*) m_table[ii].m_key, + (char*) m_table[ii].m_value); } diff --git a/util/dictionary.hpp b/util/dictionary.hpp index 98453b7..2b284f6 100644 --- a/util/dictionary.hpp +++ b/util/dictionary.hpp @@ -161,7 +161,7 @@ public: * @return NULL: not found
* otherwise: the wanted item */ - inline Val* get(Key key) const{ + inline Val* get(const Key* key) const{ Val* rc = reinterpret_cast(BaseDictionary::get( reinterpret_cast(key))); return rc; @@ -222,7 +222,7 @@ public: * * C strings are strings delimited by '\0'. */ -class CStringInt : public Dictionary { +class CStringInt : public Dictionary { public: /** * Constructor. @@ -249,7 +249,7 @@ public: } return rc; } - inline CStringInt& put(const char* key, PtrInt_t value){ + inline CStringInt& put(const char* key, const PtrInt_t value){ int index; if (binarySearch(reinterpret_cast(key), index)){ m_table[index].m_value = (void*) value; diff --git a/util/dynbuffer.cpp b/util/dynbuffer.cpp index 306cc13..605ca6d 100644 --- a/util/dynbuffer.cpp +++ b/util/dynbuffer.cpp @@ -161,6 +161,22 @@ DynBuffer& DynBuffer::ensureSize(size_t size){ } return *this; } + +/** + * Fill the buffer with a given character. + * + * @param filler the character for filling + * @param length <= 0: use the current length
+ * otherwise: the new length + * @return + */ +DynBuffer& DynBuffer::fill(char filler, int length){ + if (length <= 0) + length = m_length; + setLength(length); + memset(m_buffer, filler, length); + return *this; +} /** * Gets a binary number in little endian format from the buffer. * diff --git a/util/dynbuffer.hpp b/util/dynbuffer.hpp index 65b544f..40b7a76 100644 --- a/util/dynbuffer.hpp +++ b/util/dynbuffer.hpp @@ -65,6 +65,14 @@ public: ensureSize(neededSize); return m_buffer; } + /** + * Returns the current size of the buffer. + * + * @return the current size + */ + inline size_t capacity() const{ + return m_capacity; + } /** * Remove the buffer contents. * @@ -76,6 +84,7 @@ public: return *this; } DynBuffer& ensureSize(size_t size); + DynBuffer& fill(char filler, int length = -1); /** * Returns the current buffer length. * @@ -116,14 +125,6 @@ public: m_buffer[m_length = length] = '\0'; return *this; } - /** - * Returns the current size of the buffer. - * - * @return the current size - */ - inline size_t capacity() const{ - return m_capacity; - } /** * Tests whether the buffer starts with a given string. * diff --git a/util/test.cpp b/util/test.cpp index 4e64743..ddabbbe 100644 --- a/util/test.cpp +++ b/util/test.cpp @@ -7,9 +7,11 @@ int main(int argc, char **argv) { extern void testTimer(); extern void testTimerUtils(); extern void testArrayList(); + extern void testDictionary(); - testArrayList(); testDynBuffer(); + testDictionary(); + testArrayList(); testTimer(); testTimerUtils(); return 0;