Commit 478d6ca0 authored by Yoni Fogel's avatar Yoni Fogel

Addresses #288

Modified test cases to finish 100% gcov of linear.c

git-svn-id: file:///svn/tokudb@1906 c7de825b-a66e-492c-adef-691d508d4ae1
parent 60ecabba
......@@ -2,33 +2,52 @@
#include "test.h"
int mallocced = 0;
int failon = -1;
void* fail_malloc(size_t size) {
if (++mallocced == failon) {
errno = ENOMEM;
return NULL;
}
return malloc(size);
}
int main(int argc, const char *argv[]) {
int r;
toku_range_tree *tree;
BOOL allow_overlaps;
BOOL temp;
parse_args(argc, argv);
/* Test no overlap */
r = toku_rt_create(&tree, dummy_cmp, dummy_cmp, FALSE, malloc, free, realloc);
CKERR(r);
for (allow_overlaps = 0; allow_overlaps < 2; allow_overlaps++) {
r = toku_rt_create(&tree, dummy_cmp, dummy_cmp, allow_overlaps, malloc, free, realloc);
CKERR(r);
assert(tree!=NULL);
r = toku_rt_close(tree);
CKERR(r);
tree = NULL;
/* Test overlap */
r = toku_rt_create(&tree, dummy_cmp, dummy_cmp, TRUE, malloc, free, realloc);
CKERR(r);
assert(tree!=NULL);
r = toku_rt_get_allow_overlaps(tree, &temp);
CKERR(r);
assert((temp != 0) == (allow_overlaps != 0));
assert(tree!=NULL);
r = toku_rt_close(tree);
CKERR(r);
tree = NULL;
r = toku_rt_close(tree);
CKERR(r);
tree = NULL;
}
for (allow_overlaps = 0; allow_overlaps < 2; allow_overlaps++) {
int i;
for (i = 1; i <= 2; i++) {
mallocced = 0;
failon = i;
r = toku_rt_create(&tree, dummy_cmp, dummy_cmp, allow_overlaps,
fail_malloc, free, realloc);
CKERR2(r, ENOMEM);
assert(tree==NULL);
}
}
return 0;
}
/* We are going to test whether we can insert and delete. */
#include "test.h"
void verify_all_overlap(toku_range* query, toku_range* list, unsigned listlen) {
unsigned i;
for (i = 0; i < listlen; i++) {
/* Range A and B overlap iff A.left <= B.right && B.left <= A.right */
assert(int_cmp(query->left, list[i].right) <= 0 &&
int_cmp(list[i].left, query->right) <= 0);
}
}
int nums[64 << 3];
const int numlen = sizeof(nums) / sizeof(nums[0]);
char letters[2] = {'A','B'};
toku_range_tree *tree;
toku_range* buf;
unsigned buflen;
toku_range* init_range(toku_range* range, int left, int right, int data) {
range->left = &nums[left];
range->right = &nums[right];
if (data < 0) range->data = NULL;
else range->data = &letters[data];
return range;
}
void setup_tree(BOOL allow_overlaps, BOOL insert, int left, int right, int data) {
int r;
toku_range range;
r = toku_rt_create(&tree, int_cmp, char_cmp, allow_overlaps, malloc, free, realloc);
CKERR(r);
if (insert) {
r = toku_rt_insert(tree, init_range(&range, left, right, data));
CKERR(r);
}
}
void close_tree(void) {
int r;
r = toku_rt_close(tree); CKERR(r);
}
void runsearch(int rexpect, toku_range* query, toku_range* expect) {
int r;
unsigned found;
r = toku_rt_find(tree, query, 0, &buf, &buflen, &found);
CKERR2(r, rexpect);
if (rexpect != 0) return;
assert(found == 1);
assert(int_cmp(buf[0].left, expect->left) == 0 &&
int_cmp(buf[0].right, expect->right) == 0 &&
char_cmp(buf[0].data, expect->data) == 0);
}
void runinsert(int rexpect, toku_range* toinsert) {
int r;
r = toku_rt_insert(tree, toinsert);
CKERR2(r, rexpect);
}
void rundelete(int rexpect, toku_range* todelete) {
int r;
r = toku_rt_delete(tree, todelete);
CKERR2(r, rexpect);
}
void runlimitsearch(toku_range* query, unsigned limit, unsigned findexpect) {
int r;
unsigned found;
r=toku_rt_find(tree, query, limit, &buf, &buflen, &found); CKERR(r);
verify_all_overlap(query, buf, found);
assert(found == findexpect);
}
void tests(BOOL allow_overlaps) {
toku_range insert;
unsigned i;
/* Force buf to increase. */
setup_tree(allow_overlaps, FALSE, 0, 0, 0);
for (i = 0; i < numlen / 2; i++) {
runinsert(0, init_range(&insert, i, i, 0));
unsigned j = numlen /2 + i;
runinsert(0, init_range(&insert, j, j, 1));
}
int k;
for (k = numlen/2 -1; k >= 0; k--) {
i = k;
rundelete(0, init_range(&insert, i, i, 0));
unsigned j = numlen /2 + i;
rundelete(0, init_range(&insert, j, j, 1));
}
close_tree();
}
int main(int argc, const char *argv[]) {
int i;
for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) nums[i] = i;
buflen = 2;
buf = (toku_range*)toku_malloc(2 * sizeof(toku_range));
tests(FALSE);
tests(TRUE);
tree = NULL;
toku_free(buf);
buf = NULL;
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment