static int test_less() { c_bool rt; c_map ta, tb; c_map_create(&ta, int_comparer); c_map_create(&tb, int_comparer); create_with_insert_equal(&ta); create_with_insert_equal(&tb); printf("*ta is :\n"); print_map(&ta); printf("*tb is :\n"); print_map(&tb); rt = c_map_less(&ta, &tb, pair_comparer); if(rt) printf("is less\n"); else printf("is not less\n"); c_map_clear(&ta); c_map_clear(&tb); create_with_insert_unique(&ta); create_with_insert_equal(&tb); printf("*ta is :\n"); print_map(&ta); printf("*tb is :\n"); print_map(&tb); rt = c_map_less(&ta, &tb, pair_comparer); if(rt) printf("is less\n"); else printf("is not less\n"); c_map_clear(&ta); c_map_clear(&tb); create_with_insert_equal(&ta); create_with_insert_unique(&tb); printf("*ta is :\n"); print_map(&ta); printf("*tb is :\n"); print_map(&tb); rt = c_map_less(&ta, &tb, pair_comparer); if(rt) printf("is less\n"); else printf("is not less\n"); return 0; }
Map* createMap(KeyType keyType){ Map *treeMap = (Map *)malloc(sizeof(Map)); COMPARER comparer; switch(keyType){ case Map_KeyType_INT: comparer = int_comparer; break; case Map_KeyType_LONG: comparer = long_comparer; break; case Map_KeyType_STRING: comparer = string_comparer; break; default: comparer = default_comparer; break; }; c_map_create(&treeMap->values, comparer); treeMap->entries = createMemento(); treeMap->getIterator = __map_getIterator; treeMap->put = __map_put; treeMap->remove = __map_remove; treeMap->dispose=__map_dispose; treeMap->hasNext = __map_hasNext; treeMap->next = __map_next; treeMap->containsKey = __map_containsKey; treeMap->get = __map_get; treeMap->size = __map_size; treeMap->isEmpty = __map_isEmpty; treeMap->clear = __map_clear; return treeMap; }
static int test_swap(c_pmap thiz) { c_map tr; c_map_create(&tr, int_comparer); c_map_swap(thiz, &tr); printf("after swap\n"); print_map(&tr); c_map_destroy(&tr); return 0; }
static int test_equal() { c_bool rt; c_map ta, tb; c_map_create(&ta, int_comparer); c_map_create(&tb, int_comparer); create_with_insert_equal(&ta); create_with_insert_equal(&tb); rt = c_map_equal(&ta, &tb, pair_comparer); if(rt) printf("is equal\n"); else printf("is not equal\n"); c_map_clear(&ta); c_map_clear(&tb); create_with_insert_unique(&ta); create_with_insert_equal(&tb); rt = c_map_equal(&ta, &tb, pair_comparer); if(rt) printf("is equal\n"); else printf("is not equal\n"); c_map_clear(&ta); c_map_clear(&tb); create_with_insert_equal(&ta); create_with_insert_unique(&tb); rt = c_map_equal(&ta, &tb, pair_comparer); if(rt) printf("is equal\n"); else printf("is not equal\n"); return 0; }
static int test_at() { c_map m; value_type val = NULL; int key = 84; int nonkeys[] = { 5, -1, -28, -998, -62356, 95238 }; int i = 0; c_map_create(&m, int_comparer); create_with_insert_unique(&m); val = c_map_at(&m, &key); if(val != NULL) { printf("map at %i, value is %i\n", key, *(int*)val); } for(i = 0; i < sizeof(keys) / sizeof(int); ++ i) { val = c_map_at(&m, &keys[i]); if(val != NULL) { printf("map at %i, value is %i\n", keys[i], *(int*)val); } } for(i = 0; i < sizeof(nonkeys) /sizeof(int); ++ i) { val = c_map_at(&m, &nonkeys[i]); if(val != NULL) assert(false); else printf("ok! pass!\n"); } c_map_destroy(&m); }
int t_map() { c_map map; c_map_create(&map, int_comparer); assert(__c_rb_tree_verify(map._l)); printf("0. test create with insert unique\n"); create_with_insert_unique(&map); print_map(&map); rprint_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n1. test clear\n"); test_clear(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n2. test size and empty\n"); test_size_empty(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n3. test create with insert equal\n"); c_map_clear(&map); create_with_insert_equal(&map); print_map(&map); rprint_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n4. test swap\n"); create_with_insert_unique(&map); test_swap(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n5. test create with insert unique1\n"); create_with_insert_unique1(&map); print_map(&map); rprint_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n6. test create with insert equal1\n"); c_map_clear(&map); create_with_insert_equal1(&map); print_map(&map); rprint_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n7. test create with insert unique2\n"); c_map_clear(&map); create_with_insert_unique2(&map); print_map(&map); rprint_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n8. test create with insert equal2\n"); c_map_clear(&map); create_with_insert_equal2(&map); print_map(&map); rprint_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n9. test erase\n"); c_map_clear(&map); create_with_insert_unique(&map); test_erase(&map); create_with_insert_unique(&map); test_reverse_erase(&map); print_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n10. test find and erase\n"); c_map_clear(&map); printf("* test_find_erase:\n"); create_with_insert_unique(&map); print_map(&map); test_find_erase(&map); print_map(&map); printf("* test_reverse_find_erase:\n"); create_with_insert_unique(&map); print_map(&map); test_reverse_find_erase(&map); print_map(&map); assert(__c_rb_tree_verify(map._l)); printf("\n\n11. test count:\n"); // 'lower_bound' 'upper_bound' 'equal_range' used create_with_insert_unique(&map); test_count(&map); printf("\n\n12. test less:\n"); test_less(); printf("\n\n13. test equal:\n"); test_equal(); printf("\n\n14. test at:\n"); test_at(); c_map_destroy(&map); printf("\n\nfinish testing map!\n"); return 0; }