Exemple #1
0
void HashMapTest::testRemove() {

    {
        HashMap<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);
    }
    {
        HashMap<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.
        HashMap<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());
    }
}
Exemple #2
0
int main() {
	/* Pull the Wikipedia data into the program. */
	Vector<string> articles;
	HashMap<string, int> articleToIndex;
	HashMap<int, Vector<int> > links;
	readData(articles, articleToIndex, links);
	
	/* Get two article names, then find a path between them. */
	while (true) {
		string first = getLine("Enter article title: ");
		if (!articleToIndex.containsKey(first)) {
			cout << "Sorry, I don't know that article." << endl;
			continue;
		}
		
		string second = getLine("Enter article title: ");
		if (!articleToIndex.containsKey(second)) {
			cout << "Sorry, I don't know that article." << endl;
			continue;
		}
		
		findPathFrom(articleToIndex[first], articleToIndex[second], articles, links);
	}
	
	return 0;
}
Exemple #3
0
void HashMapTest::testContainsKey() {

    HashMap<int, std::string> hashMap;

    hashMap.put(876, "test");

    CPPUNIT_ASSERT_MESSAGE("Returned false for valid key", hashMap.containsKey(876));
    CPPUNIT_ASSERT_MESSAGE("Returned true for invalid key", !hashMap.containsKey(1));

    HashMap<int, std::string> hashMap2;
    hashMap2.put(0, "test");
    CPPUNIT_ASSERT_MESSAGE("Failed with key", hashMap2.containsKey(0));
    CPPUNIT_ASSERT_MESSAGE("Failed with missing key matching hash", !hashMap2.containsKey(1));
}
ESR_ReturnCode SR_NametagsAddImpl(SR_Nametags* self, SR_Nametag* nametag)
{
  SR_NametagsImpl* impl = (SR_NametagsImpl*) self;
  HashMap* nametags = impl->value;
  LCHAR* id;
  ESR_BOOL exists;
  ESR_ReturnCode rc;

  CHKLOG(rc, nametag->getID(nametag, &id));
  CHKLOG(rc, nametags->containsKey(nametags, id, &exists));
  if (exists)
  {
    CHKLOG(rc, SR_EventLogToken_BASIC(impl->eventLog, impl->logLevel, L("id"), id));
    rc = ESR_IDENTIFIER_COLLISION;
    CHKLOG(rc, SR_EventLogToken_BASIC(impl->eventLog, impl->logLevel, L("rc"), ESR_rc2str(rc)));
    CHKLOG(rc, SR_EventLogEvent_BASIC(impl->eventLog, impl->logLevel, L("SR_NametagsAdd")));
    rc = ESR_IDENTIFIER_COLLISION;
    PLogError(ESR_rc2str(rc));
    goto CLEANUP;
  }
  CHKLOG(rc, nametags->put(nametags, id, nametag));

  CHKLOG(rc, SR_EventLogToken_BASIC(impl->eventLog, impl->logLevel, L("id"), id));
  CHKLOG(rc, SR_EventLogEvent_BASIC(impl->eventLog, impl->logLevel, L("SR_NametagsAdd")));
  return ESR_SUCCESS;
CLEANUP:
  return rc;
}
Exemple #5
0
void HashMapTest::testConstructor() {

    HashMap<int, std::string> map;
    CPPUNIT_ASSERT(map.isEmpty());
    CPPUNIT_ASSERT_EQUAL(0, map.size());
    CPPUNIT_ASSERT_EQUAL(false, map.containsKey(1));
    CPPUNIT_ASSERT_EQUAL(false, map.containsValue("test"));
}
ESR_ReturnCode SR_NametagsContainsImpl(SR_Nametags* self, const LCHAR* id, ESR_BOOL* result)
{
  SR_NametagsImpl* impl = (SR_NametagsImpl*) self;
  HashMap* nametags = impl->value;
  ESR_ReturnCode rc;

  CHKLOG(rc, nametags->containsKey(nametags, id, result));
  return ESR_SUCCESS;
CLEANUP:
  return rc;
}
Exemple #7
0
void HashMapTest::testValues() {

    HashMap<int, std::string> hashMap;
    populateMap(hashMap);

    Collection<std::string>& c = hashMap.values();
    CPPUNIT_ASSERT_MESSAGE("Returned collection of incorrect size()", c.size() == hashMap.size());
    for (int i = 0; i < MAP_SIZE; i++) {
        CPPUNIT_ASSERT_MESSAGE("Returned collection does not contain all keys",
                               c.contains(Integer::toString(i)));
    }

    c.remove("10");
    CPPUNIT_ASSERT_MESSAGE("Removing from collection should alter Map",
                           !hashMap.containsKey(10));
}
Exemple #8
0
HashMap<KeyType,vector<ValueType>>
  operator * (const HashMap<KeyType,ValueType> &hm1,
  			  const HashMap<KeyType,ValueType> &hm2)
{

	HashMap<KeyType,vector<ValueType>>
	  result(hm2.size(),hm2.getHashFunction());

	for (const KeyType &key : hm1)
	{
		//key е в сечението на ключовете
		if (hm2.containsKey (key))
		{
			result[key].push_back(hm1[key]);
			result[key].push_back(hm2[key]);
		}
	}
	return result;
}
Exemple #9
0
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());
}
Exemple #10
0
/* Finds a path from the first node to the second. */
void findPathFrom(int from, int to, Vector<string>& articles,
                  HashMap<int, Vector<int> >& links) {
	/* This queue holds the current set of articles to work with.
	 * If we change it to a stack, we get a depth-first search.
	 */
	Queue<int> worklist;
	
	/* Information about what the parents of each article are in our
	 * breadth-first search tree.
	 */
	HashMap<int, int> visited;
	
	/* Enqueue the start article and mark that it's been visited.  The
	 * choice of making it its own parent is entirely arbitrary.
	 */
	worklist.enqueue(from);
	visited[from] = from;
	
	while (!worklist.isEmpty()) {
		/* Get the current node.  If it's the destination, we're done! */
		int curr = worklist.dequeue();
		if (curr == to) {
			printPath(from, to, visited, articles);
			return;
		}
		
		/* Consider each outgoing link.  If we haven't marked the article
		 * as visited, go follow the link to visit it.
		 */
		foreach (int link in links[curr]) {
			if (!visited.containsKey(link)) {
				worklist.enqueue(link);
				visited[link] = curr;
			}
		}
	}
}
 void testContainsKey ()
 {
   map->put ((char*)"one", 1);
   CPPUNIT_ASSERT_EQUAL (map->containsKey ("one"), true);
   CPPUNIT_ASSERT_EQUAL (map->containsKey ("two"), false);
 }
Exemple #12
0
void HashMapTest::testPut() {

    {
        HashMap<std::string, std::string> hashMap(101);
        hashMap.put("KEY", "VALUE");
        CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to install key/value pair",
                                     std::string("VALUE"), hashMap.get("KEY"));
    }
    {
        // Check my actual key instance is returned
        HashMap<int, std::string> map;
        for (int i = -32767; i < 32768; i++) {
            map.put(i, "foobar");
        }
        int myKey = 0;
        // Put a new value at the old key position
        map.put(myKey, "myValue");
        CPPUNIT_ASSERT(map.containsKey(myKey));
        CPPUNIT_ASSERT_EQUAL(std::string("myValue"), map.get(myKey));
        bool found = false;
        Set<int>& intSet = map.keySet();
        Pointer< Iterator<int> > itr(intSet.iterator());
        while (itr->hasNext()) {
            int key = itr->next();
            found = (key == myKey);
            if (found) {
                break;
            }
        }
        CPPUNIT_ASSERT_MESSAGE("Should find new key instance in hashashMap", found);

        // Add a new key instance and check it is returned
        CPPUNIT_ASSERT_NO_THROW(map.remove(myKey));
        map.put(myKey, "myValue");
        CPPUNIT_ASSERT(map.containsKey(myKey));
        CPPUNIT_ASSERT_EQUAL(std::string("myValue"), map.get(myKey));
        itr.reset(intSet.iterator());
        while (itr->hasNext()) {
            int key = itr->next();
            found = (key == myKey);
            if (found) {
                break;
            }
        }
        CPPUNIT_ASSERT_MESSAGE("Did not find new key instance in hashashMap", found);
    }
    {
        // Ensure keys with identical hashcode are stored separately
        HashMap<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));
    }
}