static bool _oom_test(ls_err *err) { ls_htable *table; // initial bucket count of 1 to force a resize and increase coverage if (!ls_htable_create(1, ls_str_hashcode, ls_str_compare, &table, err)) { return false; } if (!ls_htable_put(table, "key1", "value one", NULL, err) || !ls_htable_put(table, "key1", "value one again", NULL, err) || !ls_htable_put(table, "key2", "value two", NULL, err) || !ls_htable_put(table, "key3", "value three", NULL, err) || !ls_htable_put(table, "key4", "value four", NULL, err)) { ls_htable_destroy(table); return false; } if (4 != ls_htable_get_count(table)) { ls_htable_destroy(table); return false; } ls_htable_destroy(table); return true; }
CTEST(ls_htable, walk) { ls_htable *table; walk_data wd; unsigned int idx; ls_err err; ASSERT_TRUE(ls_htable_create(7, ls_str_hashcode, ls_str_compare, &table, &err)); ASSERT_TRUE(ls_htable_put(table, "key1", "value one", NULL, &err)); ASSERT_TRUE(ls_htable_put(table, "key2", "value two", NULL, &err)); ASSERT_TRUE(ls_htable_put(table, "key3", "value three", NULL, &err)); wd.table = table; wd.cmp = ls_str_compare; wd.visited = 0; wd.results = (int*)malloc(3 * sizeof(int)); ls_htable_walk(table, test_htable_walk, &wd); ASSERT_EQUAL(ls_htable_get_count(table), wd.visited); idx = 0; while (idx < wd.visited) { ASSERT_TRUE(wd.results[idx++]); } free(wd.results); ls_htable_destroy(table); }
CTEST(ls_htable, iteration) { ls_htable *table; ls_hnode *node; unsigned int count; ls_err err; // initial bucket count of 1 to force a resize and increase coverage ASSERT_TRUE(ls_htable_create(1, ls_str_hashcode, ls_str_compare, &table, &err)); node = ls_htable_get_first_node(table); ASSERT_NULL(node ); ASSERT_TRUE(ls_htable_put(table, "key1", ls_data_strdup("value one"), ls_htable_free_data_cleaner, NULL)); ASSERT_TRUE(ls_htable_put(table, "key2", ls_data_strdup("value two"), ls_htable_free_data_cleaner, NULL)); ASSERT_TRUE(ls_htable_put(table, "key3", ls_data_strdup("value three"), ls_htable_free_data_cleaner, NULL)); count = 1; node = ls_htable_get_first_node(table); while ((node = ls_htable_get_next_node(table, node)) != NULL) { count++; ASSERT_NOT_NULL(ls_hnode_get_key(node) ); ASSERT_NOT_NULL(ls_hnode_get_value(node) ); } ASSERT_TRUE(count == ls_htable_get_count(table)); ls_htable_remove(table, "key2"); count = 1; node = ls_htable_get_first_node(table); while ((node = ls_htable_get_next_node(table, node)) != NULL) { count++; ASSERT_NOT_NULL(ls_hnode_get_key(node) ); ASSERT_NOT_NULL(ls_hnode_get_value(node) ); } ASSERT_EQUAL(count, ls_htable_get_count(table)); ls_htable_destroy(table); }
CTEST(ls_htable, basics) { ls_htable* table; ls_err err; ASSERT_TRUE( ls_htable_create(7, ls_str_hashcode, ls_str_compare, &table, &err) ); ASSERT_EQUAL(ls_htable_get_count(table), 0); ASSERT_NULL( ls_htable_get(table, "key1") ); ASSERT_NULL( ls_htable_get(table, "key2") ); pvalue = NULL; ASSERT_TRUE( ls_htable_put(table, "key1", "value one", test_htable_store_pvalue, &err) ); ASSERT_NULL(pvalue); ASSERT_EQUAL(ls_htable_get_count(table), 1); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "value one"), 0); ASSERT_NULL( ls_htable_get(table, "key2") ); ASSERT_TRUE( ls_htable_put(table, "key2", "value two", test_htable_store_pvalue, &err) ); ASSERT_NULL(pvalue); ASSERT_EQUAL(ls_htable_get_count(table), 2); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "value one"), 0); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0); ASSERT_TRUE( ls_htable_put(table, "key1", "val 1", test_htable_store_pvalue, &err) ); ASSERT_EQUAL(strcmp( (const char*)pvalue, "value one" ), 0); ASSERT_EQUAL(ls_htable_get_count(table), 2); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "val 1"), 0); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0); pvalue = NULL; ls_htable_remove(table, "key1"); ASSERT_EQUAL(strcmp(pvalue, "val 1"), 0); ASSERT_EQUAL(ls_htable_get_count(table), 1); ASSERT_NULL( ls_htable_get(table, "key1") ); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0); pvalue = NULL; ASSERT_TRUE( ls_htable_put(table, "key1", "first value", test_htable_store_pvalue, &err) ); ASSERT_NULL(pvalue); ASSERT_EQUAL(ls_htable_get_count(table), 2); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "first value"), 0); ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0); ls_htable_clear(table); ASSERT_EQUAL(ls_htable_get_count(table), 0); ASSERT_NULL( ls_htable_get(table, "key1") ); ASSERT_NULL( ls_htable_get(table, "key2") ); ls_htable_destroy(table); }
CTEST(ls_htable, node) { ls_htable *table; ls_hnode *node; ls_err err; ASSERT_TRUE(ls_htable_create(7, ls_str_hashcode, ls_str_compare, &table, &err)); ls_htable_put(table, "key1", "value one", test_htable_store_pvalue, &err); ls_htable_put(table, "key2", "value two", test_htable_store_pvalue, &err); ls_htable_put(table, "key3", "value three", test_htable_store_pvalue, &err); node = ls_htable_get_node(table, "key1"); ASSERT_EQUAL(strcmp(ls_hnode_get_key(node), "key1") , 0); ASSERT_EQUAL(strcmp(ls_hnode_get_value(node), "value one") , 0); pvalue = NULL; ls_hnode_put_value(node, "val1", test_htable_store_pvalue); ASSERT_EQUAL(strcmp(pvalue, "value one") , 0); ASSERT_EQUAL(strcmp(ls_hnode_get_value(node), "val1") , 0); pvalue = NULL; ls_hnode_put_value(node, "value 1", test_htable_store_pvalue); ASSERT_EQUAL(strcmp(pvalue, "val1") , 0); ASSERT_EQUAL(strcmp(ls_hnode_get_value(node), "value 1") , 0); pvalue = NULL; ls_hnode_put_value(node, "first value", NULL); ASSERT_EQUAL(strcmp(pvalue, "value 1") , 0); ASSERT_EQUAL(strcmp(ls_hnode_get_value(node), "first value") , 0); pvalue = NULL; ls_hnode_put_value(node, "value #1", test_htable_store_pvalue); ASSERT_NULL(pvalue); ASSERT_EQUAL(strcmp(ls_hnode_get_value(node), "value #1") , 0); ls_htable_remove_node(table, node); ASSERT_EQUAL(ls_htable_get_count(table), 2); ls_htable_destroy(table); }