Esempio n. 1
0
// test for large number of elements
static char *test_operations() {
    size_t N = 200;
    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "failed to make the map");
    mu_assert(make_random(map), "did not make a random fake radix map");

    RadixMap_sort(map);
    mu_assert(check_order(map), "failed to properly sort the RadixMap");
    mu_assert(test_search(map), "failed the search test");
    mu_assert(check_order(map), "RadixMap not sorted after seach");

    while (map->end > 0) {
        RMElement *el = RadixMap_find(map, map->contents[map->end / 2].data.key);
        mu_assert(el != NULL, "should get a result");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "did not delete element");
        mu_assert(old_end - 1 == map->end, "wrong size after delete");
        mu_assert(check_order(map), "did not stay sorted after delete");
    }
    RadixMap_destroy(map);

    return NULL;
}
Esempio n. 2
0
static char *test_operations()
{
    size_t N = 200;

    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make map");
    mu_assert(make_random(map), "Didn't make a random fake radix map");

    RadixMap_sort(map);
    mu_assert(check_order(map),
            "Failed to properly sort the RadixMap");

    mu_assert(test_search(map), "Failed the search test");
    mu_assert(check_order(map), "RadixMap didn't stay sorted after search");

    while (map->end > 0) {
        RMElement *el = RadixMap_find(map,
                map->contents[map->end/2].data.key);
        mu_assert(el != NULL, "Should get a result");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "Didn't delete it");
        mu_assert(old_end - 1 == map->end, "Wrong size after delete");

        // test that the end is now the old value
        // but uint32 max so it trails off
        mu_assert(check_order(map),
                "RadixMap didn't stay sorted after delete");
    }
    RadixMap_destroy(map);

    return NULL;
}
Esempio n. 3
0
char *test_speed() {
    size_t N = 100000;
    int i = 0;
    clock_t fastest = LONG_MAX;

    RadixMap *source_map = RadixMap_create(N);
    mu_assert(source_map != NULL, "Failed to make the map.");
    mu_assert(make_random(source_map), "Didn't make a random fake radix map.");

    for(i = 0; i < 100; i++) {
        RadixMap *map = RadixMap_create(N);
        mu_assert(map != NULL, "Failed to make the map.");

        mu_assert(RadixMap_copy(source_map, map) == 0,
                "Copying unsorted RadixMap failed.");

        clock_t elapsed = -clock();
        RadixMap_sort(map, 0, map->end);
        elapsed += clock();

        if(elapsed < fastest) fastest = elapsed;
        mu_assert(check_order(map), "Failed to sort the RadixMap.");

        RadixMap_destroy(map);
    }

    RadixMap_destroy(source_map);
    debug("Fastest time for size %zu: %f", N, ((float)fastest)/CLOCKS_PER_SEC);

    return NULL;
}
Esempio n. 4
0
int main(int argc, char const *argv[])
{
	RadixMap *map = RadixMap_create(N);
	assert(map != NULL);
	make_random(map);
	RadixMap_printer(map);
	RadixMap_sort(map);
	printf("\n\n");
	RadixMap_printer(map);



	return 0;
}
Esempio n. 5
0
int RadixMap_add(RadixMap *map, uint32_t key, uint32_t value) {
    check(key < UINT32_MAX, "Key can't be equal to UINT32_MAX");

    RMElement element = {.data = {.key = key, .value = value}};
    check(map->end + 1 < map->max, "RadixMap is full");

    map->contents[map->end++] = element;

    RadixMap_sort(map);

    return 0;
error:
    return -1;
}
Esempio n. 6
0
int RadixMap_delete(RadixMap *map, RMElement *el) {
    check(map->end > 0, "nothing to delete");
    check(el != NULL, "can't delete NULL element");

    el->data.key = UINT32_MAX;

    if (map->end > 1) {
        // do not sort if length 1
        RadixMap_sort(map);
    }
    map->end--;

    return 0;
error:
    return -1;
}
Esempio n. 7
0
int RadixMap_delete(RadixMap *map, RMElement *el)
{
    check(map->end > 0, "There is nothing to delete.");
    check(el != NULL, "Can't delete a NULL element.");

    el->data.key = UINT32_MAX;

    if(map->end > 1) {
        // don't bother resorting a map of 1 length
        RadixMap_sort(map);
    }

    map->end--;

    return 0;
error:
    return -1;
}
Esempio n. 8
0
// test for big number of elements
static char *test_RadixMap_operations()
{
    size_t N = 200;

    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make the map.");
    mu_assert(make_random(map), "Didn't make a random fake radix map.");

    RadixMap_sort(map);
    mu_assert(check_order(map), "Failed to properly sort the RadixMap.");

    mu_assert(test_search(map), "Failed the search test.");
    mu_assert(check_order(map), "RadixMap didn't stay sorted after search.");

    RadixMap_destroy(map);

    map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make the map.");

    debug("PUSHING VALUES");
    mu_assert(push_random_values(map), "Didn't push random values.");
    debug("VALUES PUSHED!");

    mu_assert(check_order(map), "Map wasn't sorted after pushes.");

    debug("DOING DELETES");
    while(map->end > 0) {
        RMElement *el = RadixMap_find(map, map->contents[map->end / 2].data.key);
        mu_assert(el != NULL, "Should get a result.");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "Didn't delete it.");
        mu_assert(old_end - 1 == map->end, "Wrong size after delete.");

        // test that the end is now the old value, but uint32 max so it trails off
        mu_assert(check_order(map), "RadixMap didn't stay sorted after delete.");
    }

    RadixMap_destroy(map);

    return NULL;
}