void testOneThousandInserts() { // Test case used to validate fix to issue XPL-169 const int COUNT = 1000; const char stringPrefix[] = "Apofur81Kb"; UtlHashBag bag; char tmpString[20]; for( int i = 0; i < COUNT; i++) { sprintf(tmpString, "%s%d", stringPrefix, i); UtlString *stringToInsert = new UtlString(); *stringToInsert = tmpString; CPPUNIT_ASSERT( ! bag.contains( stringToInsert ) ); bag.insert( stringToInsert ); CPPUNIT_ASSERT_EQUAL( i+1, (int)bag.entries() ); for( unsigned int j = 0; j < bag.entries(); j++ ) { // verify that all entries are indeed in the bag sprintf( tmpString, "%s%d", stringPrefix, j ); UtlString stringTolookUp( tmpString ); CPPUNIT_ASSERT_MESSAGE( tmpString, bag.contains( &stringTolookUp ) ); } } }
OsStatus OsConfigDb::updateFile(const char* filename) const { UtlString originalFileContents; long fileLength = OsFile::openAndRead(filename, originalFileContents); const char* unparsedBits = originalFileContents; int unparsedLength = originalFileContents.length(); // Loop through and try to preserve comments, space and order int lineStart = 0; int lineEnd = 0; UtlHashBag writtenNames; UtlString newFileContents; UtlString name; UtlString value; UtlString newValue; while(lineStart < unparsedLength) { lineEnd = UtlTokenizer::nextDelim(unparsedBits, lineStart, unparsedLength, "\n\r"); //printf("start: %d end: %d length: %d\n", lineStart, lineEnd, unparsedLength); UtlString oneLine(&unparsedBits[lineStart], lineEnd - lineStart); //printf("Line: <%s>\n", oneLine.data()); // If line contains a parameter if(parseLine(oneLine, mCapitalizeName, filename, name, value)) { //printf("name<%s> value<%s>\n", name.data(), value.data()); if(get(name, newValue) == OS_SUCCESS && !writtenNames.contains(&name)) { //printf("Wrote name<%s>\n", name.data()); // The parameter still exists in the configDb and we have not yet // written it out, write the potentially changed value newFileContents.appendFormat("%s : %s\n", name.data(), newValue.data()); // Save names/parameters written so that we can figure out what has not // been written out writtenNames.insert(new UtlString(name)); } // else the parameter was removed, do nothing } // The line was a comment or blank line, write it back out the same else { newFileContents.appendFormat("%s\n", oneLine.data()); } lineStart = lineEnd + 1; } int paramIndex; int paramCount = numEntries(); DbEntry* paramEntry; for (paramIndex = 0; paramIndex < paramCount; paramIndex++) { paramEntry = (DbEntry*) mDb.at(paramIndex); removeNewlineReturns(paramEntry->key); removeNewlineReturns(paramEntry->value); // We have not written the value yet if(!writtenNames.contains(&(paramEntry->key))) { newFileContents.appendFormat("%s : %s\n", paramEntry->key.data(), paramEntry->value.data()); writtenNames.insert(new UtlString(paramEntry->key)); } } fileLength = OsFile::openAndWrite(filename, newFileContents); writtenNames.destroyAll(); return(fileLength > 0 ? OS_SUCCESS : OS_INVALID_ARGUMENT); }
void testRemoveReference() { // the following two entries collide if the initial bucket size is 16 UtlInt int1(1); UtlInt int16(16); UtlInt int2a(2); UtlInt int2b(2); UtlInt int3(3); UtlHashBag bag; CPPUNIT_ASSERT( bag.numberOfBuckets() == 16 ); // check assumption of collision // Load all the test objects CPPUNIT_ASSERT( bag.insert(&int1) == &int1 ); CPPUNIT_ASSERT( bag.insert(&int16) == &int16 ); CPPUNIT_ASSERT( bag.insert(&int2a) == &int2a ); CPPUNIT_ASSERT( bag.insert(&int2b) == &int2b ); CPPUNIT_ASSERT( bag.insert(&int3) == &int3 ); // Check that everything is there CPPUNIT_ASSERT( bag.entries() == 5 ); CPPUNIT_ASSERT( bag.contains(&int1) ); CPPUNIT_ASSERT( bag.contains(&int16) ); CPPUNIT_ASSERT( bag.contains(&int2a) ); // cannot test for 2a and 2b independently CPPUNIT_ASSERT( bag.contains(&int3) ); // Take entry 1 out (might collide w/ 16) CPPUNIT_ASSERT( bag.removeReference(&int1) == &int1 ); // Check that everything except entry 1 is still there, and that 1 is gone CPPUNIT_ASSERT( bag.entries() == 4 ); CPPUNIT_ASSERT( ! bag.contains(&int1) ); CPPUNIT_ASSERT( bag.contains(&int16) ); CPPUNIT_ASSERT( bag.contains(&int2a) );// cannot test for 2a and 2b independently CPPUNIT_ASSERT( bag.contains(&int3) ); // Put entry 1 back in (so that 16 will collide w/ it again) CPPUNIT_ASSERT( bag.insert(&int1) == &int1 ); // Check that everything is there CPPUNIT_ASSERT( bag.entries() == 5 ); CPPUNIT_ASSERT( bag.contains(&int1) ); CPPUNIT_ASSERT( bag.contains(&int16) ); CPPUNIT_ASSERT( bag.contains(&int2a) ); CPPUNIT_ASSERT( bag.contains(&int3) ); // Take entry 16 out (might collide w/ 1) CPPUNIT_ASSERT( bag.removeReference(&int16) == &int16 ); // Check that everything except entry 16 is still there, and that 16 is gone CPPUNIT_ASSERT( bag.entries() == 4 ); CPPUNIT_ASSERT( bag.contains(&int1) ); CPPUNIT_ASSERT( ! bag.contains(&int16) ); CPPUNIT_ASSERT( bag.contains(&int2a) );// cannot test for 2a and 2b independently CPPUNIT_ASSERT( bag.contains(&int3) ); // remove 2a (and ensure that you don't get back 2b) CPPUNIT_ASSERT( bag.removeReference(&int2a) == &int2a ); // Check that everything that should be is still there CPPUNIT_ASSERT( bag.entries() == 3 ); CPPUNIT_ASSERT( bag.contains(&int1) ); CPPUNIT_ASSERT( ! bag.contains(&int16) ); CPPUNIT_ASSERT( bag.find(&int2a) == &int2b ); // equal values, but now there's only one CPPUNIT_ASSERT( bag.contains(&int3) ); // remove 3 (no collision for this one) CPPUNIT_ASSERT( bag.removeReference(&int3) == &int3 ); // Check that everything that should be is still there CPPUNIT_ASSERT( bag.entries() == 2 ); CPPUNIT_ASSERT( bag.contains(&int1) ); CPPUNIT_ASSERT( ! bag.contains(&int16) ); CPPUNIT_ASSERT( bag.find(&int2a) == &int2b ); // equal values, but now there's only one CPPUNIT_ASSERT( ! bag.contains(&int3) ); // remove 3 again - should fail this time CPPUNIT_ASSERT( bag.removeReference(&int3) == NULL ); // Check that everything that should be is still there CPPUNIT_ASSERT( bag.entries() == 2 ); CPPUNIT_ASSERT( bag.contains(&int1) ); CPPUNIT_ASSERT( ! bag.contains(&int16) ); CPPUNIT_ASSERT( bag.find(&int2a) == &int2b ); // equal values, but now there's only one CPPUNIT_ASSERT( ! bag.contains(&int3) ); }
void utlTestFind_And_Contains(FindOrContains type) { const int testCount = 7 ; const char* prefixFind = "Test the find() method when the match " ; const char* prefixContains = "Test the contains() method when the match " ; const char* Msgs[] = { \ "is the first element ", \ "is the last element ", \ "is a mid element (unique match) ", \ "has two value matches but a single ref match ", \ "has two ref matches", \ "has a value match but no ref match", \ "has no match at all" \ } ; // insert two elements to satisfy (d) and (e) commonList.insert( commonContainables_Clone[4]) ; commonList.insert( commonContainables[3]) ; UtlString noExist("This cannot and should not exist!!!") ; UtlContainable* searchValues[] = { \ commonContainables[0], commonContainables[5], commonContainables[2], \ commonContainables[4], commonContainables[3], \ commonContainables_Clone[2], &noExist \ } ; bool expectedValues_Contains[] = {true, true, true, true, true, true, false } ; UtlContainable* searchValuesForFind[] = { \ commonContainables[0], commonContainables[5], commonContainables[2], \ commonContainables[4], commonContainables[3], \ commonContainables_Clone[1], &noExist \ } ; // In case of the hashtable, searching for a key can return *ANY* of the values // that matches and we dont care about which one. so we need two set of expected values UtlContainable* expValues_Find[][2] = { \ {commonContainables[0], commonContainables[0]}, \ {commonContainables[5], commonContainables[2]}, \ {commonContainables[2], commonContainables[2]}, \ {commonContainables_Clone[4], commonContainables[4]}, \ {commonContainables[3], commonContainables[3]}, \ {commonContainables[1], commonContainables[1]}, \ {NULL, NULL} \ } ; for (int i = 0 ; i < testCount ; i++) { string msg ; if (type == TEST_FIND) { bool isFound = false ; UtlContainable* act = commonList.find(searchValuesForFind[i]) ; // If we are expecting either 'A' or 'B' for the search isFound = (act == expValues_Find[i][0]) || (act == expValues_Find[i][1]) ; TestUtilities::createMessage(2, &msg, prefixFind, Msgs[i]) ; CPPUNIT_ASSERT_MESSAGE(msg.data(), isFound) ; } else if (type == TEST_CONTAINS) { UtlBoolean act = commonList.contains(searchValues[i]) ; UtlBoolean exp = (UtlBoolean)expectedValues_Contains[i] ; TestUtilities::createMessage(2, &msg, prefixContains, Msgs[i]) ; CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.data(), exp, act) ; } } }//utlTestIndex