diff --git a/src/range_tree/tests/test_00050_predecessorsuccessor.c b/src/range_tree/tests/test_00050_predecessorsuccessor.c index 446c91e57ef..3ab864d9beb 100644 --- a/src/range_tree/tests/test_00050_predecessorsuccessor.c +++ b/src/range_tree/tests/test_00050_predecessorsuccessor.c @@ -25,6 +25,12 @@ toku_range* init_range(toku_range* range, int left, int right, int data) { return range; } +void* init_point(int left) { + assert(left >= 0); + assert(left < sizeof(nums) / sizeof(nums[0])); + return (&nums[left]); +} + void setup_tree(BOOL allow_overlaps, BOOL insert, int left, int right, int data) { int r; toku_range range; @@ -68,95 +74,175 @@ void runlimitsearch(toku_range* query, unsigned limit, unsigned findexpect) { assert(found == findexpect); } -void tests(BOOL allow_overlaps) { - toku_range query; - toku_range insert; - /* - Limited/Unlimited Queries +typedef enum {PRED=0, SUCC=1} predsucc; +void runtest(predsucc testtype, void* query, BOOL findexpect, + int left, int right, int data) { + int r; + BOOL found; + toku_range out; + assert(data >= 0 && data < sizeof(letters) / sizeof(letters[0])); + assert(left >= 0 && left < sizeof(nums) / sizeof(nums[0])); + assert(right >= 0 && right < sizeof(nums) / sizeof(nums[0])); + if (testtype == PRED) { + r = toku_rt_predecessor(tree, query, &out, &found); - Limit of k does not produce all, but limit of 0 does. Single point overlaps + } + else { + assert(testtype == SUCC); + r = toku_rt_successor(tree, query, &out, &found); + } + CKERR(r); + assert(found == findexpect); + if (findexpect) { + assert(int_cmp(out.left, &nums[left]) == 0); + assert(int_cmp(out.right, &nums[right]) == 0); + assert(char_cmp(out.data, &letters[data]) == 0); + } +} + + +void tests(BOOL allow_overlaps) { + toku_range insert; + + /* + Empty + Only empty space test. + 1 element + standard tree + Pred/Succ: + 1: In empty space + * Nothing to the left/Right (pred/succ) respectively. + * something to the left/right (pred/succ) respectively. + 2: On left endpoint. + * Nothing to the left/Right (pred/succ) respectively. + * something to the left/right (pred/succ) respectively. + 3: On right endpoint. + * Nothing to the left/Right (pred/succ) respectively. + * something to the left/right (pred/succ) respectively. + 4: In middle of range. + * Nothing to the left/Right (pred/succ) respectively. + * something to the left/right (pred/succ) respectively. */ - /* Tree: {|0-1|,|2-3|,|4-5|,|6-7|,|8-9|}, query of |2-7|, limit 2 finds 2, - limit 3 finds 3, limit 4 finds 3, limit 0 finds 3 */ - setup_tree(allow_overlaps, TRUE, 0, 1, 0); - runinsert(0, init_range(&insert, 2, 3, 0)); - runinsert(0, init_range(&insert, 4, 5, 0)); - runinsert(0, init_range(&insert, 6, 7, 0)); - runinsert(0, init_range(&insert, 8, 9, 0)); - - runlimitsearch(init_range(&query, 2, 7, 0), 0, 3); - runlimitsearch(init_range(&query, 2, 7, 0), 1, 1); - runlimitsearch(init_range(&query, 2, 7, 0), 2, 2); - runlimitsearch(init_range(&query, 2, 7, 0), 3, 3); - runlimitsearch(init_range(&query, 2, 7, 0), 4, 3); - close_tree(); - - /* Tree is empty (return none) */ + /* Empty tree. */ setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runlimitsearch(init_range(&query, 0, 0, 0), 0, 0); - close_tree(); - - /* Tree contains only elements to the left. */ - setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runinsert(0, init_range(&insert, 1, 2, 0)); - runinsert(0, init_range(&insert, 3, 4, 0)); - runlimitsearch(init_range(&query, 8, 30, 0), 0, 0); - close_tree(); - - /* Tree contains only elements to the right. */ - setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runinsert(0, init_range(&insert, 10, 20, 0)); - runinsert(0, init_range(&insert, 30, 40, 0)); - runlimitsearch(init_range(&query, 5, 7, 0), 0, 0); + runtest(PRED, init_point(5), FALSE, 0, 0, 0); + runtest(SUCC, init_point(5), FALSE, 0, 0, 0); close_tree(); - /* Tree contains only elements to the left and to the right. */ + /* Single element tree. Before left, left end point, middle, + right end point, after right. */ setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runinsert(0, init_range(&insert, 10, 20, 0)); - runinsert(0, init_range(&insert, 30, 40, 0)); - runinsert(0, init_range(&insert, 70, 80, 0)); - runinsert(0, init_range(&insert, 90, 100, 0)); - runlimitsearch(init_range(&query, 60, 65, 0), 0, 0); - close_tree(); - - /* Tree contains overlaps and elements to the left. */ - setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runinsert(0, init_range(&insert, 10, 20, 0)); - runinsert(0, init_range(&insert, 30, 40, 0)); - runinsert(0, init_range(&insert, 60, 80, 0)); - runinsert(0, init_range(&insert, 90, 100, 0)); - runlimitsearch(init_range(&query, 70, 95, 0), 0, 2); + runinsert(0, init_range(&insert, 10, 20, 0)); + runtest(PRED, init_point(5), FALSE, 0, 0, 0); + runtest(PRED, init_point(10), FALSE, 0, 0, 0); + runtest(PRED, init_point(15), FALSE, 0, 0, 0); + runtest(PRED, init_point(20), FALSE, 0, 0, 0); + runtest(PRED, init_point(25), TRUE, 10, 20, 0); + runtest(SUCC, init_point(5), TRUE, 10, 20, 0); + runtest(SUCC, init_point(10), FALSE, 0, 0, 0); + runtest(SUCC, init_point(15), FALSE, 0, 0, 0); + runtest(SUCC, init_point(20), FALSE, 0, 0, 0); + runtest(SUCC, init_point(25), FALSE, 0, 0, 0); close_tree(); - /* Tree contains overlaps and elements to the right. */ - setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runinsert(0, init_range(&insert, 110, 120, 0)); - runinsert(0, init_range(&insert, 130, 140, 0)); - runinsert(0, init_range(&insert, 60, 80, 0)); - runinsert(0, init_range(&insert, 90, 100, 0)); - runlimitsearch(init_range(&query, 70, 95, 0), 0, 2); - close_tree(); + /* + Swap left and right for succ. + Multi element tree. + * In empty space. + * Something on left. + * Nothing on left. - /* Tree contains overlaps and elements to the left and to the right. */ + * At a left end point. + * Something on left. + * Nothing on left. + + * Inside a range. + * Something on left. + * Nothing on left. + + * At a right end point. + * Something on left. + * Nothing on left. + */ setup_tree(allow_overlaps, FALSE, 0, 0, 0); - runinsert(0, init_range(&insert, 10, 20, 0)); - runinsert(0, init_range(&insert, 30, 40, 0)); - runinsert(0, init_range(&insert, 110, 120, 0)); - runinsert(0, init_range(&insert, 130, 140, 0)); - runinsert(0, init_range(&insert, 60, 80, 0)); - runinsert(0, init_range(&insert, 90, 100, 0)); - runlimitsearch(init_range(&query, 70, 95, 0), 0, 2); + runinsert(0, init_range(&insert, 10, 20, 0)); + runinsert(0, init_range(&insert, 30, 40, 0)); + + /* + * In empty space. + * Something on left. + * Nothing on left. + */ + runtest(PRED, init_point(25), TRUE, 10, 20, 0); + runtest(PRED, init_point(5), FALSE, 0, 0, 0); + + /* + * At a left end point. + * Something on left. + * Nothing on left. + */ + runtest(PRED, init_point(30), TRUE, 10, 20, 0); + runtest(PRED, init_point(10), FALSE, 0, 0, 0); + + /* + * Inside a range. + * Something on left. + * Nothing on left. + */ + runtest(PRED, init_point(35), TRUE, 10, 20, 0); + runtest(PRED, init_point(15), FALSE, 0, 0, 0); + + /* + * At a right end point. + * Something on left. + * Nothing on left. + */ + runtest(PRED, init_point(40), TRUE, 10, 20, 0); + runtest(PRED, init_point(20), FALSE, 0, 0, 0); + + /* + * In empty space. + * Something on right. + * Nothing on right. + */ + runtest(SUCC, init_point(25), TRUE, 30, 40, 0); + runtest(SUCC, init_point(45), FALSE, 0, 0, 0); + + /* + * At a right end point. + * Something on right. + * Nothing on right. + */ + runtest(SUCC, init_point(20), TRUE, 30, 40, 0); + runtest(SUCC, init_point(40), FALSE, 0, 0, 0); + + /* + * Inside a range. + * Something on right. + * Nothing on right. + */ + runtest(SUCC, init_point(15), TRUE, 30, 40, 0); + runtest(SUCC, init_point(35), FALSE, 0, 0, 0); + + /* + * At a right end point. + * Something on right. + * Nothing on right. + */ + runtest(SUCC, init_point(20), TRUE, 30, 40, 0); + runtest(SUCC, init_point(40), FALSE, 0, 0, 0); + 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);