test/avl_tests.c

changeset 245
db732f8c083a
parent 244
98dc2d3a9b1d
child 250
b7d1317b138e
     1.1 --- a/test/avl_tests.c	Sat Jul 15 20:46:18 2017 +0200
     1.2 +++ b/test/avl_tests.c	Sat Jul 15 22:36:29 2017 +0200
     1.3 @@ -282,3 +282,36 @@
     1.4      
     1.5      ucx_avl_free(tree);
     1.6  }
     1.7 +
     1.8 +UCX_TEST(test_ucx_avl_traverse) {
     1.9 +    UcxAVLTree *tree = ucx_avl_new(ucx_ptrcmp);
    1.10 +    
    1.11 +    size_t len = 12;
    1.12 +
    1.13 +    int val[] = {10, 15, 3, 4, -30, 20, 14, -11, 12, -5, 1, 13};
    1.14 +    int val_ordered[] = {-30, -11, -5, 1, 3, 4, 10, 12, 13, 14, 15, 20};
    1.15 +    
    1.16 +    for (size_t i = 0 ; i < len ; i++) {
    1.17 +        ucx_avl_put(tree, val[i], &(val[i]));
    1.18 +    }
    1.19 +    
    1.20 +    UCX_TEST_BEGIN
    1.21 +    
    1.22 +    UcxAVLNode* node = ucx_avl_get_node(tree, (intptr_t) val_ordered[0]);
    1.23 +    for (size_t i = 0 ; i < len ; i++) {
    1.24 +        UCX_TEST_ASSERT(node->key == val_ordered[i], "AVL successor failed");
    1.25 +        node = ucx_avl_succ(node);
    1.26 +    }
    1.27 +    UCX_TEST_ASSERT(!node, "AVL maximum incorrectly has a successor");
    1.28 +    
    1.29 +    node = ucx_avl_get_node(tree, (intptr_t) val_ordered[len-1]);
    1.30 +    for (size_t i = len ; i > 0 ; i--) {
    1.31 +        UCX_TEST_ASSERT(node->key == val_ordered[i-1],"AVL predecessor failed");
    1.32 +        node = ucx_avl_pred(node);
    1.33 +    }
    1.34 +    UCX_TEST_ASSERT(!node, "AVL minimum incorrectly has a predecessor");
    1.35 +    
    1.36 +    UCX_TEST_END
    1.37 +    
    1.38 +    ucx_avl_free(tree);
    1.39 +}

mercurial