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_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; }
static void __map_dispose(Map *map){ c_map_destroy(&map->values); map->entries->dispose(map->entries); free(map); }