void testHashMapOneValue()
{
  HashMap * map = hashMapCreate(11);
  long value = 1999;
  hashMapPut(map, "Hello", &value);
  long * p = hashMapGet(map, "Hello");
  assertIntEquals("testHashMapOneValue", 1999, *p);

  // cleanup
  hashMapRemove(map, "Hello");
  hashMapFree(map);
  map = NULL;
  
}
void testHashMapRemoveSixValues()
{
  long * v = 0;
  long values[] = {1000, 1001,1002,1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012};
  char * k1 = "Hello";
  char * k2 = "hello";
  char * k3 = "world";
  char * k4 = "World";
  char * k5 = "good";
  char * k6 = "bye";
  int i = 0;
  HashMap * map = hashMapCreate(11);
  hashMapPut(map, k1, &values[i++]);
  hashMapPut(map, k2, &values[i++]);
  hashMapPut(map, k3, &values[i++]);
  hashMapPut(map, k4, &values[i++]);
  hashMapPut(map, k5, &values[i++]);
  hashMapPut(map, k6, &values[i++]);
  hashMapRemove(map, k1);
  hashMapRemove(map, k2);
  hashMapRemove(map, k3);
  hashMapRemove(map, k4);
  hashMapRemove(map, k5);
  hashMapRemove(map, k6);
  v = hashMapGet(map, k1);
  assertIntEquals("testHashMapRemoveSixValues value 1", 0, (int)v);
  v = hashMapGet(map, "hello");
  assertIntEquals("testHashMapRemoveSixValues value 2", 0, (int)v);
  v = hashMapGet(map, "world");
  assertIntEquals("testHashMapRemoveSixValues value 3", 0, (int)v);
  v = hashMapGet(map, "World");
  assertIntEquals("testHashMapRemoveSixValues value 4", 0, (int)v);
  v = hashMapGet(map, "good");
  assertIntEquals("testHashMapRemoveSixValues value 5", 0, (int)v);
  v = hashMapGet(map, "bye");
  assertIntEquals("testHashMapRemoveSixValues value 6", 0, (int)v);

  hashMapFree(map);
  map = NULL;
}
Пример #3
0
/**
 * Tests all hash map functions after adding and removing all of the given keys
 * and values.
 * @param test
 * @param links The key-value pairs to be added and removed.
 * @param notKeys Some keys not in the table to test contains and get.
 * @param numLinks The number of key-value pairs to be added and removed.
 * @param numNotKeys The number of keys not in the table.
 * @param numBuckets The initial number of buckets (capacity) in the table.
 */
void testCase(CuTest* test, HashLink* links, const char** notKeys, int numLinks,
              int numNotKeys, int numBuckets)
{
    HashMap* map = hashMapNew(numBuckets);
    Histogram hist;
    
    // Add links
    for (int i = 0; i < numLinks; i++)
    {
        hashMapPut(map, links[i].key, links[i].value);
    }
    
    // Print table
    printf("\nAfter adding all key-value pairs:");
    hashMapPrint(map);
    
    // Check size
    CuAssertIntEquals(test, numLinks, hashMapSize(map));
    
    // Check capacity
    CuAssertIntEquals(test, map->capacity, hashMapCapacity(map));
    
    // Check empty buckets
    int sum = 0;
    for (int i = 0; i < map->capacity; i++)
    {
        if (map->table[i] == NULL)
        {
            sum++;
        }
    }
    CuAssertIntEquals(test, sum, hashMapEmptyBuckets(map));
    
    // Check table load
    CuAssertIntEquals(test, (float)numLinks / map->capacity, hashMapTableLoad(map));
    
    // Check contains and get on valid keys.
    for (int i = 0; i < numLinks; i++)
    {
        CuAssertIntEquals(test, 1, hashMapContainsKey(map, links[i].key));
        int* value = hashMapGet(map, links[i].key);
        CuAssertPtrNotNull(test, value);
        CuAssertIntEquals(test, links[i].value, *value);
    }
    
    // Check contains and get on invalid keys.
    for (int i = 0; i < numNotKeys; i++)
    {
        CuAssertIntEquals(test, 0, hashMapContainsKey(map, notKeys[i]));
        CuAssertPtrEquals(test, NULL, hashMapGet(map, notKeys[i]));
    }
    
    // Check that all links are present and have a unique key.
    histFromTable(&hist, map);
    CuAssertIntEquals(test, numLinks, hist.size);
    assertHistCounts(test, &hist);
    histCleanUp(&hist);
    
    // Remove keys
    for (int i = 0; i < numLinks; i++)
    {
        hashMapRemove(map, links[i].key);
    }
    
    // Print table
    printf("\nAfter removing all key-value pairs:");
    hashMapPrint(map);
    
    // Check size
    CuAssertIntEquals(test, 0, hashMapSize(map));
    
    // Check capacity
    CuAssertIntEquals(test, map->capacity, hashMapCapacity(map));
    
    // Check empty buckets
    CuAssertIntEquals(test, map->capacity, hashMapEmptyBuckets(map));
    
    // Check table load
    CuAssertIntEquals(test, 0, hashMapTableLoad(map));
    
    // Check contains and get on valid keys.
    for (int i = 0; i < numLinks; i++)
    {
        CuAssertIntEquals(test, 0, hashMapContainsKey(map, links[i].key));
        CuAssertPtrEquals(test, NULL, hashMapGet(map, links[i].key));
    }
    
    // Check contains and get on invalid keys.
    for (int i = 0; i < numNotKeys; i++)
    {
        CuAssertIntEquals(test, 0, hashMapContainsKey(map, notKeys[i]));
        CuAssertPtrEquals(test, NULL, hashMapGet(map, notKeys[i]));
    }
    
    // Check that there are no links in the table.
    histFromTable(&hist, map);
    CuAssertIntEquals(test, 0, hist.size);
    assertHistCounts(test, &hist);
    histCleanUp(&hist);
    
    hashMapDelete(map);
}