-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
#include "map.h" | ||
|
||
static void swap(int *x, int *y) | ||
{ | ||
int tmp = *x; | ||
*x = *y; | ||
*y = tmp; | ||
} | ||
|
||
enum { N_NODES = 10000 }; | ||
|
||
bool test_mix_insert() | ||
{ | ||
bool failed = false; | ||
|
||
map_t tree = map_init(int, int, map_cmp_uint); | ||
|
||
int key[N_NODES], val[N_NODES]; | ||
|
||
/* | ||
* Generate data for insertion | ||
*/ | ||
for (int i = 0; i < N_NODES; i++) { | ||
key[i] = i; | ||
val[i] = i + 1; | ||
} | ||
|
||
/* TODO: This is not a reconmended way to randomize stuff, just a simple | ||
* test. Using MT19937 might be better | ||
*/ | ||
srand((unsigned) time(NULL)); | ||
for (int i = 0; i < N_NODES; i++) { | ||
int pos_a = rand() % N_NODES; | ||
int pos_b = rand() % N_NODES; | ||
swap(&key[pos_a], &key[pos_b]); | ||
swap(&val[pos_a], &val[pos_b]); | ||
} | ||
|
||
/* add first 1/2 items */ | ||
for (int i = 0; i < N_NODES / 2; i++) { | ||
map_iter_t my_it; | ||
map_insert(tree, key + i, val + i); | ||
map_find(tree, &my_it, key + i); | ||
assert((*(int *) (my_it.node->data)) == val[i]); | ||
} | ||
|
||
/* remove first 1/4 items */ | ||
for (int i = 0; i < N_NODES / 4; i++) { | ||
map_iter_t my_it; | ||
map_find(tree, &my_it, key + i); | ||
if (map_at_end(tree, &my_it)) | ||
continue; | ||
map_erase(tree, &my_it); | ||
map_find(tree, &my_it, key + i); | ||
assert(!my_it.node); | ||
} | ||
|
||
/* add the rest */ | ||
for (int i = N_NODES / 2 + 1; i < N_NODES; i++) { | ||
map_iter_t my_it; | ||
map_insert(tree, key + i, val + i); | ||
map_find(tree, &my_it, key + i); | ||
assert((*(int *) (my_it.node->data)) == val[i]); | ||
} | ||
|
||
|
||
/* remove 2nd quarter of items */ | ||
for (int i = N_NODES / 4 + 1; i < N_NODES / 2; i++) { | ||
map_iter_t my_it; | ||
map_find(tree, &my_it, key + i); | ||
if (map_at_end(tree, &my_it)) | ||
continue; | ||
map_erase(tree, &my_it); | ||
map_find(tree, &my_it, key + i); | ||
assert(!my_it.node); | ||
} | ||
|
||
map_clear(tree); | ||
map_delete(tree); | ||
|
||
return failed; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
(void) argc; | ||
(void) argv; | ||
bool failed = test_mix_insert(); | ||
printf("%d", failed); | ||
|
||
return 0; | ||
} |