#include #include #include "common.h" #include "debug.h" #include "string.h" #include "collection.h" #include "function.h" #include "array.h" #include "dictionary.h" #include "unittest.h" enum { DEBUG_TESTDICTIONARY = false }; typedef struct TestDictionary { Dictionary *dict; } TestDictionary; // char *string_reverse_new(char *self) // { // int i; // int length; // int halflength; // int lastindex; // char temp; // char *result; // assert_valid(self); // // result = estrdup_fs(self); // length = strlen(result); // lastindex = length - 1; // halflength = length / 2; // for(i = 0; i < halflength; ++i) // { // temp = result[i]; // result[i] = result[lastindex - i]; // result[lastindex - i] = temp; // } // // NOISE(DEBUG_TESTDICTIONARY, "string_reverse_new self: %s reversed: %s", // self, result); // return result; // } char *testdictionary_words[] = { "acclimatized", "acclimatizes", "acclimatizing", "accolade", "accoladed", "accolades", "worsens", "worship", "worshiped", "worshiper" }; static void *testdictionary_setup(void) { int i; TestDictionary *result = (TestDictionary *)emalloc_fs(sizeof(*result)); NOISE(DEBUG_TESTDICTIONARY, "entering testdictionary_setup"); result->dict = dictionary_new(string_isequal, string_hash); for(i = 0; i < static_array_length(testdictionary_words); ++i) { dictionary_add(result->dict, estrdup_fs(testdictionary_words[i]), (void *)strlen(testdictionary_words[i])); } return result; } static void testdictionary_teardown(void *data) { TestDictionary *testdictionary = (TestDictionary *)data; dictionary_free_with(testdictionary->dict, free, nil); free(testdictionary); } static bool testdictionary_add(void *data) { int i; TestDictionary *testdictionary = (TestDictionary *)data; Dictionary *d; assert_valid(testdictionary); d = testdictionary->dict; assert_valid(d); NOISE(DEBUG_TESTDICTIONARY, "testdictionary_add entering with dict: %uX", d); test_assert(dictionary_size(d) == static_array_length(testdictionary_words)); for(i = 0; i < static_array_length(testdictionary_words); ++i) { NOISE(DEBUG_TESTDICTIONARY, "testdictionary_add testing [%d] = %s", i, testdictionary_words[i]); test_assert(dictionary_includes(d, testdictionary_words[i])); NOISE(DEBUG_TESTDICTIONARY, "testdictionary_add after includes"); test_assert((int)dictionary_at(d, testdictionary_words[i]) == strlen(testdictionary_words[i])); NOISE(DEBUG_TESTDICTIONARY, "testdictionary_add after at"); } return true; } static bool testdictionary_remove(void *data) { TestDictionary *testdictionary = (TestDictionary *)data; assert_valid(testdictionary); test_assert(dictionary_includes( testdictionary->dict, testdictionary_words[0])); test_assert(dictionary_remove(testdictionary->dict, testdictionary_words[0])); test_assert(dictionary_remove(testdictionary->dict, testdictionary_words[1])); test_assert(dictionary_remove(testdictionary->dict, testdictionary_words[2])); test_assert_false(dictionary_remove(testdictionary->dict, "nosuchword")); test_assert_false(dictionary_includes( testdictionary->dict, testdictionary_words[0])); return true; } static TestFixure testdictionary_fixure = { .setup = testdictionary_setup, .teardown = testdictionary_teardown }; static TestCaseNamePair testdictionary_testcases[] = { testcasenamepair_make(testdictionary_add) }; AbstractTest *testdictionary_testsuite(void) { return (AbstractTest *) testsuite_make("testdictionary", testdictionary_testcases, static_array_length(testdictionary_testcases), &testdictionary_fixure); }