void doListOperations() { UtlSList testList; // fill the list appendCountItems(testList, NUM_PERFORMANCE_STRINGS); // take the first half off the front if (!testList.isEmpty()) { getCountItems(testList, NUM_PERFORMANCE_STRINGS / 2); } // take the rest off the end by reference if (!testList.isEmpty()) { UtlContainable* lastItem = testList.last(); delete dynamic_cast<UtlString*>(testList.removeReference(lastItem)); } // fill the list appendCountItems(testList, NUM_PERFORMANCE_STRINGS); // search the list for each item by value UtlString target; int targetIndex; for (targetIndex = 0; targetIndex < NUM_PERFORMANCE_STRINGS; targetIndex += 1) { target = string[targetIndex]; UtlString* found = dynamic_cast<UtlString*>(testList.find(&target)); if (found) { externalForSideEffects = found->length(); } } // get the object in the middle of the list by index, and remove it by value while(!testList.isEmpty()) { int numberLeft = testList.entries(); UtlString* middle = dynamic_cast<UtlString*>(testList.at((numberLeft / 2))); delete dynamic_cast<UtlString*>(testList.remove(middle)); } // fill the list appendCountItems(testList, NUM_PERFORMANCE_STRINGS); // iterate over each item in the list UtlSListIterator iterate(testList); UtlString* item; while ((item = dynamic_cast<UtlString*>(iterate()))) { externalForSideEffects = item->length(); delete item; } }
// Since the test setup / preconditions for the index, find, // contains and containsReference are all the same, these // tests have been combined into one utility function. Based // on the type argument, the method to be tested is varied. void utlTestIndex_Find_And_Contains(IndexOrContains type) { const int testCount = 7 ; const char* prefixIndex = "Test the index() method when the match " ; const char* prefixFind = "Test the find() method when the match " ; const char* prefixContains = "Test the contains() method when the match " ; const char* prefixContainsRef = "Test the containsReference() 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 a clone of the 4th element to the 1st position commonList.insertAt(1, commonContainables_Clone[4]) ; // The new index for a value match of commonContainables[4] must be 1. // insert another copy of the 3rd element to the 2nd position. commonList.insertAt(2, commonContainables[3]) ; // The new index for commonContainables[3] must be 2) ; // what used to be the second element has now moved to 4. UtlString noExist("This cannot and should not exist!!!") ; UtlContainable* searchValues[] = { \ commonContainables[0], commonContainables[5], commonContainables[2], \ commonContainables[4], commonContainables[3], \ commonContainables_Clone[2], &noExist \ } ; int expectedValues_Index[] = { 0, 7, 4, 1, 2, 4, INDEX_NOT_EXIST } ; bool expectedValues_Contains[] = {true, true, true, true, true, true, false } ; bool expectedValues_ContainsRef[] = {true, true, true, true, true, false, false} ; UtlContainable* searchValuesForFind[] = { \ commonContainables[0], commonContainables[5], commonContainables[2], \ commonContainables[4], commonContainables[3], \ commonContainables_Clone[1], &noExist \ } ; UtlContainable* expectedValuesForFind[] = { \ commonContainables[0], commonContainables[5], commonContainables[2], \ commonContainables_Clone[4], commonContainables[3], \ commonContainables[1], NULL \ } ; for (int i = 0 ; i < testCount ; i++) { string msg ; if (type == TEST_INDEX) { int actual = commonList.index(searchValues[i]) ; TestUtilities::createMessage(2, &msg, prefixIndex, Msgs[i]) ; CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.data(), expectedValues_Index[i], actual) ; } else if (type == TEST_FIND) { UtlContainable* actual = commonList.find(searchValuesForFind[i]) ; TestUtilities::createMessage(2, &msg, prefixFind, Msgs[i]) ; CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.data(), expectedValuesForFind[i], actual) ; } else if (type == TEST_CONTAINS) { UtlBoolean actual = commonList.contains(searchValues[i]) ; TestUtilities::createMessage(2, &msg, prefixContains, Msgs[i]) ; CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.data(), expectedValues_Contains[i], \ (TRUE == actual)) ; } else if (type == TEST_CONTAINS_REF) { UtlBoolean actual = commonList.containsReference(searchValues[i]) ; TestUtilities::createMessage(2, &msg, prefixContainsRef, Msgs[i]) ; CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.data(), expectedValues_ContainsRef[i], \ (TRUE == actual)) ; } } }//utlTestIndex