void LinkedHashMapTest::testIsEmpty() { LinkedHashMap<int, std::string> hashMap; CPPUNIT_ASSERT_MESSAGE("Returned false for new map", hashMap.isEmpty()); hashMap.put(1, "1"); CPPUNIT_ASSERT_MESSAGE("Returned true for non-empty", !hashMap.isEmpty()); hashMap.clear(); CPPUNIT_ASSERT_MESSAGE("Returned false for cleared map", hashMap.isEmpty()); }
void LinkedHashMapTest::testRemove() { { LinkedHashMap<int, std::string> hashMap; populateMap(hashMap); int size = hashMap.size(); CPPUNIT_ASSERT_NO_THROW_MESSAGE("Remove returned incorrect value", hashMap.remove(9)); CPPUNIT_ASSERT_THROW_MESSAGE( "Should have thrown a NoSuchElementException on get of non-existent key.", hashMap.get(9), NoSuchElementException); CPPUNIT_ASSERT_MESSAGE("Failed to decrement size", hashMap.size() == (size - 1)); CPPUNIT_ASSERT_THROW_MESSAGE( "Should have thrown a NoSuchElementException on remove of non-existent key.", hashMap.remove(9), NoSuchElementException); } { LinkedHashMap<int, std::string> hashMap; for (int i = 0; i < 8192; i++) { hashMap.put(i, "const"); } for (int i = 0; i < 8192; i++) { hashMap.put(i, Integer::toString(i)); } for (int i = 8191; i >= 0; i--) { std::string iValue = Integer::toString(i); CPPUNIT_ASSERT_MESSAGE(std::string("Failed to replace value: ") + iValue, hashMap.containsValue(iValue)); hashMap.remove(i); CPPUNIT_ASSERT_MESSAGE(std::string("Failed to remove same value: ") + iValue, !hashMap.containsValue(iValue)); } } { // Ensure keys with identical hashcode are stored separately and removed correctly. LinkedHashMap<MyKey, std::string> map; // Put non-equal object with same hashcode MyKey aKey; CPPUNIT_ASSERT(!map.containsKey(aKey)); map.put(aKey, "value"); MyKey aKey2; CPPUNIT_ASSERT_THROW_MESSAGE( "Should have thrown NoSuchElementException", map.remove(aKey2), NoSuchElementException); MyKey aKey3; map.put(aKey3, "foobar"); CPPUNIT_ASSERT_EQUAL(std::string("foobar"), map.get(aKey3)); CPPUNIT_ASSERT_EQUAL(std::string("value"), map.get(aKey)); map.remove(aKey); map.remove(aKey3); CPPUNIT_ASSERT(!map.containsKey(aKey)); CPPUNIT_ASSERT(map.isEmpty()); } }
void LinkedHashMapTest::testConstructor() { LinkedHashMap<int, std::string> map; CPPUNIT_ASSERT(map.isEmpty()); CPPUNIT_ASSERT(map.size() == 0); }