inline Tsegment& Get(Key& key, bool *found) { Tsegment *item = m_map.Find(key); if (item == NULL) { *found = false; item = new (m_heap.Append()) Tsegment(key); m_map.Push(*item); } else { *found = true; } return *item; }
int main(int argc, const char * argv[]) { HashTable * ht = iHashTable.Create(64, sizeof(int)); const char test_key0[] = "test_key0"; const char test_key1[] = "test_key1"; const char test_key2[] = "test_key2"; const char invalid_key[] = "invalid_key"; int a = 1; int b = 2; int c = 3; // Test Insert & Retrieve printf ("Test Insert & Retrieve...\n"); ht->Insert(ht, (void*)test_key0, strlen(test_key0), &a); ht->Insert(ht, (void*)test_key1, strlen(test_key1), &b); ht->Insert(ht, (void*)test_key2, strlen(test_key2), &c); assert(ht->cnt == 3); int data; ht->Retrieve(ht, (void*)test_key0, strlen(test_key0), &data); assert(data == a); ht->Retrieve(ht, (void*)test_key1, strlen(test_key1), &data); assert(data == b); ht->Retrieve(ht, (void*)test_key2, strlen(test_key2), &data); assert(data == c); int ret = ht->Retrieve(ht, (void*)invalid_key, strlen(invalid_key), &data); assert(ret == E_NOT_FOUND); assert(ht->cnt == 3); // Test replacement printf ("Test Replace...\n"); ht->Insert(ht, (void*)test_key0, strlen(test_key0), &b); assert(ht->cnt == 3); ht->Retrieve(ht, (void*)test_key0, strlen(test_key0), &data); assert(data == b); // Test Continas, Find & Erase printf ("Test Contains, Find, Erase...\n"); void *key_ret = NULL; size_t key_len; assert(ht->Contains(ht, &c) == RET_OK); ht->Find(ht, &c, &key_ret, &key_len); assert(key_ret != NULL && key_len != 0); ht->Erase(ht, key_ret, key_len); free (key_ret); assert(ht->cnt == 2); assert(ht->Contains(ht, &c) == E_NOT_FOUND); ret = ht->Retrieve(ht, (void*)test_key2, strlen(test_key2), &data); assert(ret == E_NOT_FOUND); // Test destroy printf ("Test Destroy...\n"); ht->Destroy(ht); printf("All tests passed\n"); }