void testHashMapGetMissingKey()
{
  long * v = 0;
  char * k1 = "Hello";
  long value = 1000;
  HashMap * map = hashMapCreate(11);
  hashMapPut(map, k1, &value);
  v = hashMapGet(map, "xxx");
  assertIntEquals("testHashMapGetMissingKey", 0, (int)v);
  v = hashMapGet(map, k1);
  assertIntEquals("testHashMapGetMissingKey", 1000, *v);
  hashMapFree(map);
  map = NULL;
}
Example #2
0
int main(int argc, const char** argv)
{
    const char* fileName = "input1.txt";
    if (argc > 1)
    {
        fileName = argv[1];
    }
    printf("Opening file: %s\n", fileName);
    
    clock_t timer = clock();
    
    HashMap* map = hashMapNew(10);
    
    // --- Concordance code begins here ---
    printf ("Opened [%s]\n", fileName);
    FILE * file = fopen (fileName, "r");
    if (file == NULL)
    {
        printf ("File not found! [%s]\n", fileName);
    }
    while (!feof(file))
    {
        fgets (readBuffer, MAX_STRING_LENGTH, file);
        if (readBuffer[strlen(readBuffer) - 1] == '\n')
        {
            readBuffer[strlen(readBuffer) - 1] = 0;
        }
        printf ("Read Line [%s]\n", readBuffer);
        char * curString = strtok (readBuffer, STRING_SEPS);
        while (curString)
        {
            printf (" -- Token: [%s]\n", curString);
            int * value = hashMapGet(map, curString);
            if (value == NULL)
            {
                hashMapPut(map, curString, 1);
            }
            else
            {
                hashMapPut(map, curString, (*value)+1);                
            }
            curString = strtok (NULL, STRING_SEPS);
        }   
    }
    fclose (file);
    // --- Concordance code ends here ---
    
    hashMapPrint(map);
    
    timer = clock() - timer;
    printf("\nRan in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);
    printf("Empty buckets: %d\n", hashMapEmptyBuckets(map));
    printf("Number of links: %d\n", hashMapSize(map));
    printf("Number of buckets: %d\n", hashMapCapacity(map));
    printf("Table load: %f\n", hashMapTableLoad(map));
    
    hashMapDelete(map);
    return 0;
}
void testHashMapSixValues()
{
  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(3);
  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++]);
  
  v = hashMapGet(map, k1);

  assertIntEquals("testHashMapSixValues value 1", 1000, *v);
  v = hashMapGet(map, "hello");
  assertIntEquals("testHashMapSixValues value 2", 1001, *v);
  v = hashMapGet(map, "world");
  assertIntEquals("testHashMapSixValues value 3", 1002, *v);
  v = hashMapGet(map, "World");
  assertIntEquals("testHashMapSixValues value 4", 1003, *v);
  v = hashMapGet(map, "good");
  assertIntEquals("testHashMapSixValues value 5", 1004, *v);
  v = hashMapGet(map, "bye");
  assertIntEquals("testHashMapSixValues value 6", 1005, *v);
  hashMapFree(map);
  map = NULL;
}
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;
  
}
Example #5
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);
}
int main(int argc, const char** argv)
{
	// FIXME: implement
	const char* fileName = "dictionary.txt";
	if (argc > 1)
	{
		fileName = argv[1];
	}
	printf("Opening file: %s\n", fileName);

	HashMap* map = hashMapNew(10);

	// --- spellcheck code begins here ---
	/* open file */
	FILE *fp = fopen(fileName, "r");
	assert(fp != NULL);

	/* get the word */
	char *word = nextWord(fp);
	/* then loop through file */
	if (word != NULL)
	{
		do
		{
			//if already in hash map increment occurrence
			if (hashMapContainsKey(map, word))
			{
				int *i = hashMapGet(map, word);
				int count = *i;
				count++;
				hashMapPut(map, word, count);
			}
			//else put word in hash map with count of 1
			else
			{
				hashMapPut(map, word, 1);
			}

			//update word
			free(word);
			word = NULL;
			word = nextWord(fp);
		} while (word);
	}

	//now close file
	fclose(fp);

	//now check for user input
	int choice = 1;
	do {
		char uInp[256];
		printf("Enter a word: ");
		scanf("%s", uInp);

		for (int i = 0; uInp[i]; i++)
		{
			uInp[i] = tolower(uInp[i]);
		}

		if (hashMapContainsKey(map, uInp))
		{
			printf("You spelled it right! :) \n");
		}
		else
		{
			printf("I cannot find that word, are you sure you spelled it correctly? \n");
		}

		printf("Check another word? 1-yes, 0-no \n");
		scanf("%d", &choice);

		if (choice == 0)
		{
			printf("Goodbye!\n");
		}

	} while (choice);

	// Be sure to free the word after you are done with it here.
	// --- spellchecker ends here ---

	hashMapDelete(map);
	return 0;
}
void testHashMapTwelveValues()
{
  long * v = 0;
  char * k1 = "Hello";
  char * k2 = "hello";
  char * k3 = "world";
  char * k4 = "World";
  char * k5 = "good";
  char * k6 = "bye";
  char * k7 = "a";
  char * k8 = "b";
  char * k9 = "c";
  char * k10 = "d";
  char * k11 = "e";
  char * k12 = "f";
  long values[] = {1000, 1001,1002,1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012}; 
  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++]);
  hashMapPut(map, k7, &values[i++]);
  hashMapPut(map, k8, &values[i++]);
  hashMapPut(map, k9, &values[i++]);
  hashMapPut(map, k10, &values[i++]);
  hashMapPut(map, k11, &values[i++]);
  hashMapPut(map, k12, &values[i++]);
  v = hashMapGet(map, k1);
  assertIntEquals("testHashMapTwelveValues value 1", 1000, *v);
  v = hashMapGet(map, k2);
  assertIntEquals("testHashMapTwelveValues value 2", 1001, *v);
  v = hashMapGet(map, k3);
  assertIntEquals("testHashMapTwelveValues value 3", 1002, *v);
  v = hashMapGet(map, k4);
  assertIntEquals("testHashMapTwelveValues value 4", 1003, *v);
  v = hashMapGet(map, k5);
  assertIntEquals("testHashMapTwelveValues value 5", 1004, *v);
  v = hashMapGet(map, k6);
  assertIntEquals("testHashMapTwelveValues value 6", 1005, *v);
  v = hashMapGet(map, k6);
  assertIntEquals("testHashMapTwelveValues value 6", 1005, *v);
  v = hashMapGet(map, k7);
  assertIntEquals("testHashMapTwelveValues value 7", 1006, *v);
  v = hashMapGet(map, k8);
  assertIntEquals("testHashMapTwelveValues value 8", 1007, *v);
  v = hashMapGet(map, k9);
  assertIntEquals("testHashMapTwelveValues value 9", 1008, *v);
  v = hashMapGet(map, k10);
  assertIntEquals("testHashMapTwelveValues value 10", 1009, *v);
  v = hashMapGet(map, k11);
  assertIntEquals("testHashMapTwelveValues value 11", 1010, *v);
  v = hashMapGet(map, k12);
  assertIntEquals("testHashMapTwelveValues value 12", 1011, *v);
  hashMapFree(map);
  map = NULL;
}