void HashMapTest::testEntrySetIterator() { HashMap<int, std::string> map; populateMap(map); int count = 0; Pointer< Iterator<MapEntry<int, std::string> > > iterator(map.entrySet().iterator()); while (iterator->hasNext()) { MapEntry<int, std::string> entry = iterator->next(); CPPUNIT_ASSERT_EQUAL(count, entry.getKey()); CPPUNIT_ASSERT_EQUAL(Integer::toString(count), entry.getValue()); count++; } CPPUNIT_ASSERT_MESSAGE("Iterator didn't cover the expected range", count++ == MAP_SIZE); iterator.reset(map.entrySet().iterator()); CPPUNIT_ASSERT_THROW_MESSAGE( "Should throw an IllegalStateException", iterator->remove(), IllegalStateException); count = 0; while (iterator->hasNext()) { iterator->next(); iterator->remove(); count++; } CPPUNIT_ASSERT_MESSAGE("Iterator didn't remove the expected range", count++ == MAP_SIZE); CPPUNIT_ASSERT_THROW_MESSAGE( "Should throw an IllegalStateException", iterator->remove(), IllegalStateException); }
void HashMapTest::testEntrySet() { HashMap<int, std::string> hashMap; for (int i = 0; i < 50; i++) { hashMap.put(i, Integer::toString(i)); } Set<MapEntry<int, std::string> >& set = hashMap.entrySet(); Pointer< Iterator<MapEntry<int, std::string> > > iterator(set.iterator()); CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size", hashMap.size() == set.size()); while (iterator->hasNext()) { MapEntry<int, std::string> entry = iterator->next(); CPPUNIT_ASSERT_MESSAGE("Returned incorrect entry set", hashMap.containsKey(entry.getKey()) && hashMap.containsValue(entry.getValue())); } iterator.reset(set.iterator()); set.remove(iterator->next()); CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove on set didn't take", 49, set.size()); }