Subversion Repositories HelenOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2007 Vojtech Mencl
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <test.h>
  30. #include <print.h>
  31. #include <adt/favl.h>
  32. #include <debug.h>
  33.  
  34.  
  35. #define NODE_COUNT 100
  36.  
  37. /*
  38.  * wrapper structure with a pointer to favl tree root
  39.  */
  40. static favltree_t favltree;
  41.  
  42. /*
  43.  * favl tree nodes in array for faster allocating
  44.  */
  45. static favltree_node_t favltree_nodes[NODE_COUNT];
  46.  
  47. /*
  48.  * head of free nodes' list:
  49.  */
  50. static favltree_node_t *first_free_node = NULL;
  51.  
  52.  
  53.  
  54. static int test_tree_balance(favltree_node_t *node);
  55. static favltree_node_t *test_tree_parents(favltree_node_t *node);
  56. static void print_tree_structure_flat (favltree_node_t *node, int level);
  57. static favltree_node_t *alloc_favltree_node(void);
  58.  
  59.  
  60.  
  61. static favltree_node_t *test_tree_parents(favltree_node_t *node)
  62. {
  63.     favltree_node_t *tmp;
  64.    
  65.     if (!node) return NULL;
  66.  
  67.     if (node->lft) {
  68.         tmp = test_tree_parents(node->lft);
  69.         if (tmp != node) {
  70.             printf("Bad parent pointer key: %d, address: %p\n",tmp->key,node->lft);
  71.         }
  72.     }
  73.     if (node->rgt) {
  74.         tmp = test_tree_parents(node->rgt);
  75.         if (tmp != node) {
  76.             printf("Bad parent pointer key: %d, address: %p\n",tmp->key,node->rgt);
  77.         }
  78.     }
  79.     return node->par;
  80. }
  81.  
  82. int test_tree_balance(favltree_node_t *node)
  83. {
  84.     int h1, h2, diff;
  85.  
  86.     if (!node) return 0;
  87.     h1 = test_tree_balance(node->lft);
  88.     h2 = test_tree_balance(node->rgt);
  89.     diff = h2 - h1;
  90.     if (diff != node->balance || (diff != -1 && diff != 0 && diff != 1)) {
  91.         printf("Bad balance\n");
  92.     }
  93.     return h1 > h2 ? h1 + 1 : h2 + 1;
  94. }
  95.  
  96. /**
  97.  * Prints the structure of node, which is level levels from the top of the tree.
  98.  */
  99. static void print_tree_structure_flat (favltree_node_t *node, int level)
  100. {
  101.     /* You can set the maximum level as high as you like.
  102.          Most of the time, you'll want to debug code using small trees,
  103.          so that a large level indicates a loop, which is a bug. */
  104.     if (level > 16)
  105.     {
  106.         printf ("[...]");
  107.         return;
  108.     }
  109.  
  110.     if (node == NULL)
  111.         return;
  112.  
  113.     printf ("%d[%d]", node->key,node->balance);
  114.     if (node->lft != NULL || node->rgt != NULL)
  115.     {
  116.         printf("(");
  117.  
  118.         print_tree_structure_flat (node->lft, level + 1);
  119.         if (node->rgt != NULL)
  120.         {
  121.             printf(",");
  122.             print_tree_structure_flat (node->rgt, level + 1);
  123.         }
  124.  
  125.         printf(")");
  126.     }
  127. }
  128.  
  129.  
  130. //****************************************************************
  131. static void alloc_favltree_node_prepare(void)
  132. {
  133.     int i;
  134.  
  135.     for (i = 0; i < NODE_COUNT - 1; i++) {
  136.         favltree_nodes[i].par = &(favltree_nodes[i+1]);
  137.     }
  138.     /*
  139.      * Node keys which will be used for insertion. Up to NODE_COUNT size of array.
  140.      */
  141.  
  142.     // First tree node and same key
  143.     favltree_nodes[0].key = 60;
  144.     favltree_nodes[1].key = 60;
  145.     favltree_nodes[2].key = 60;
  146.     //LL rotation
  147.     favltree_nodes[3].key = 50;
  148.     favltree_nodes[4].key = 40;
  149.     favltree_nodes[5].key = 30;
  150.     //LR rotation
  151.     favltree_nodes[6].key = 20;
  152.     favltree_nodes[7].key = 20;
  153.     favltree_nodes[8].key = 25;
  154.     favltree_nodes[9].key = 25;
  155.     //LL rotation in lower floor
  156.     favltree_nodes[10].key = 35;
  157.     //RR rotation
  158.     favltree_nodes[11].key = 70;
  159.     favltree_nodes[12].key = 80;
  160.     //RL rotation
  161.     favltree_nodes[13].key = 90;
  162.     favltree_nodes[14].key = 85;
  163.     //Insert 0 key
  164.     favltree_nodes[15].key = 0;
  165.     favltree_nodes[16].key = 0;
  166.     //Insert reverse
  167.     favltree_nodes[17].key = 600;
  168.     favltree_nodes[18].key = 500;
  169.     favltree_nodes[19].key = 400;
  170.     favltree_nodes[20].key = 300;
  171.  
  172.     for (i = 21; i < NODE_COUNT; i++)
  173.         favltree_nodes[i].key = i * 3;
  174.    
  175.     favltree_nodes[i].par = NULL;
  176.     first_free_node = &(favltree_nodes[0]);
  177. }
  178.  
  179. static favltree_node_t *alloc_favltree_node(void)
  180. {
  181.     favltree_node_t *node;
  182.  
  183.     node = first_free_node;
  184.     first_free_node = first_free_node->par;
  185.  
  186.     return node;
  187. }
  188. //****************************************************************
  189.  
  190. static void test_tree_insert(favltree_t *tree, unsigned int node_count, int quiet)
  191. {
  192.     unsigned int i;
  193.     favltree_node_t *newnode;
  194.  
  195.     /*
  196.      * Initialize tree before using.
  197.      */
  198.     favltree_create(tree);
  199.    
  200.     if (!quiet) printf("\nInserting %d nodes ...\n", node_count);
  201.  
  202.     for (i = 0; i < node_count; i++) {
  203.         newnode = alloc_favltree_node();
  204.         //if (!quiet) printf("[[[%d]]]\n",newnode->key);
  205.        
  206.         favltree_insert(tree, newnode);
  207.         if (!quiet) {
  208.             test_tree_parents(tree->root);
  209.             test_tree_balance(tree->root);
  210.         }
  211.     }
  212.        
  213.     if (!quiet) printf("Inserting was finished\n");
  214. }
  215.  
  216.  
  217. static void test_tree_delete(favltree_t *tree, int node_count, int node_position, bool quiet)
  218. {
  219.     favltree_node_t *delnode;
  220.     unsigned int i;
  221.    
  222.     //aktualni pocet tiku:
  223.     if (!quiet) printf("Deleting tree...\n");
  224.  
  225.     switch(node_position) {
  226.         case 0: //mazani vzdy korene
  227.             if (!quiet) printf("\nDelete root nodes\n");
  228.             while(tree->root != NULL) {
  229.                 delnode = tree->root;
  230.                 favltree_delete(tree,delnode);
  231.                 if (!quiet) {
  232.                     test_tree_parents(tree->root);
  233.                     test_tree_balance(tree->root);
  234.                 }
  235.             }
  236.            
  237.             break;
  238.         case 1:
  239.             if (!quiet) printf("\nDelete nodes according to their time of origin\n");
  240.             for (i = 0; i < node_count; i++) {
  241.                 favltree_delete(tree,&(favltree_nodes[i]));
  242.                 if (!quiet) {
  243.                     test_tree_parents(tree->root);
  244.                     test_tree_balance(tree->root);
  245.                 }
  246.             }
  247.  
  248.             break; 
  249.     }
  250.    
  251.     if (!quiet) printf("Deletion was finished\n");
  252. }
  253.  
  254. static void timeout_tree(favltree_t *tree, int node_count, bool quiet)
  255. {
  256.     int i = 0;
  257.    
  258.     if (!quiet) printf("\nTimeout tree ...\n");
  259.    
  260.     while(tree->root != NULL) {
  261.         i++;
  262.         favltree_delete_min(tree);
  263.         if (!quiet) {
  264.             test_tree_parents(tree->root);
  265.             test_tree_balance(tree->root);
  266.         }
  267.     }
  268.  
  269.     if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!\n");
  270.  
  271.     if (!quiet) printf("Timeout tree finished\n");
  272. }
  273.  
  274. char * test_favltree1(bool quiet)
  275. {
  276.     alloc_favltree_node_prepare();
  277.     test_tree_insert(&favltree, NODE_COUNT, quiet);
  278.     test_tree_delete(&favltree, NODE_COUNT, 0, quiet);
  279.  
  280.     alloc_favltree_node_prepare();
  281.     test_tree_insert(&favltree, NODE_COUNT, quiet);
  282.     test_tree_delete(&favltree, NODE_COUNT, 1, quiet);
  283.  
  284.     alloc_favltree_node_prepare();
  285.     test_tree_insert(&favltree, NODE_COUNT, quiet);
  286.     timeout_tree(&favltree, NODE_COUNT, quiet);
  287.  
  288.     return NULL;
  289. }
  290.