map.c 2.83 KB
Newer Older
Chris Müller's avatar
Chris Müller committed
1
#include "unittest.h"
2 3
#include "structures/map.h"
#include "utf8.h"
Chris Müller's avatar
Chris Müller committed
4 5 6
#include <assert.h>


7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static cry_ordering_funptr cmp = cry_cast(cry_ordering_funptr, cry_utf8_compare);


static void
test_map_init(const_pointer nothing)
{
	struct CryMap* map = cry_map_new(cmp);

        assert(map != 0);
        assert(cry_map_size(map) == 0);

        cry_map_clear(map, 0, 0);
        cry_free(map);
}


static void
test_map_lookup(const_pointer nothing)
{
	struct CryMap* map = cry_map_new(cmp);
        int i[] = { 1, 2, 3, 4, 5, 6 };

        cry_map_insert(map, "1", i);
        cry_map_insert(map, "2", i + 1);
        cry_map_insert(map, "3", i + 2);
        cry_map_insert(map, "4", i + 3);
        cry_map_insert(map, "5", i + 4);
        cry_map_insert(map, "6", i + 5);

        assert(cry_map_size(map) == 6);

        assert(cry_map_lookup(map, "1") == i);
        assert(cry_map_lookup(map, "2") == i + 1);
        assert(cry_map_lookup(map, "3") == i + 2);
        assert(cry_map_lookup(map, "4") == i + 3);
        assert(cry_map_lookup(map, "5") == i + 4);
        assert(cry_map_lookup(map, "6") == i + 5);

        cry_map_clear(map, 0, 0);

        assert(cry_map_size(map) == 0);

        assert(cry_map_lookup(map, "1") == 0);
        assert(cry_map_lookup(map, "2") == 0);
        assert(cry_map_lookup(map, "3") == 0);
        assert(cry_map_lookup(map, "4") == 0);
        assert(cry_map_lookup(map, "5") == 0);
        assert(cry_map_lookup(map, "6") == 0);

        cry_free(map);
}


static void
test_map_removal(const_pointer nothing)
{
        struct CryMap* map = cry_map_new(cmp);
        int i[] = { 1, 2, 3, 4, 5, 6 };

        assert(map != 0);

        cry_map_insert(map, "1", i);
        cry_map_insert(map, "2", i + 1);
        cry_map_insert(map, "3", i + 2);
        cry_map_insert(map, "4", i + 3);
        cry_map_insert(map, "5", i + 4);
        cry_map_insert(map, "6", i + 5);

        assert(cry_map_size(map) == 6);
        
        assert(cry_map_lookup(map, "1") == i);
        assert(cry_map_lookup(map, "2") == i + 1);
        assert(cry_map_lookup(map, "3") == i + 2);
        assert(cry_map_lookup(map, "4") == i + 3);
        assert(cry_map_lookup(map, "5") == i + 4);
        assert(cry_map_lookup(map, "6") == i + 5);

        assert(cry_map_remove(map, "3").data == i + 2);
        assert(cry_map_remove(map, "5").data == i + 4);
        assert(cry_map_remove(map, "6").data == i + 5);

	assert(cry_map_lookup(map, "3") == 0);
	assert(cry_map_lookup(map, "5") == 0);
	assert(cry_map_lookup(map, "6") == 0);

        cry_map_clear(map, 0, 0);

        assert(cry_map_size(map) == 0);

        cry_free(map);
}


Chris Müller's avatar
Chris Müller committed
100 101 102
void 
cry_test_maps(void)
{
103 104 105
	cry_unittest_run("structures.map.initialization", test_map_init, 0, 10);
	cry_unittest_run("structures.map.lookup", test_map_lookup, 0, 10);
	cry_unittest_run("structures.map.removal", test_map_removal, 0, 10);
Chris Müller's avatar
Chris Müller committed
106
}