/* * TDRB Tree benchmarks */ #include #include #include #include #include #include #include "timer.h" #include "tdrb.h" #include "mt.h" #include "driver.h" #define COUNT 1000000 // 1,000,000 #define MAX_RANGE 1000000 // 1,000,000 void test_insert(Node* root) { // Create a timer Timer t; timer_init(&t); // Generate COUNT random numbers uint32_t random[COUNT]; Node nodes[COUNT]; shuffle_random(random, nodes, COUNT, MAX_RANGE); // Start the timer timer_start(&t); // Insert COUNT random numbers into the tree int i; for (i = 0; i < COUNT; i++) { insert(random[i], nodes[i], root); } // Stop the timer and print out some statistics timer_end(&t); printf("Inserted %d elements (%d unique):\n", COUNT, treeSize(*root)); display_statistics(&t); } void test_find(Node* root) { // Create a timer Timer t; timer_init(&t); // Generate COUNT random numbers uint32_t random[COUNT]; generate_random(random, COUNT, MAX_RANGE); // Start the timer timer_start(&t); // Get COUNT random numbers into the tree int i; for (i = 0; i < COUNT; get(random[i++], *root)); // Stop the timer and print out some statistics timer_end(&t); printf("Retrieved %d elements:\n", COUNT); display_statistics(&t); } int main(void) { Node root = NULL; // int i = 0; /* printf("testing single rotation on left child with root\n"); root = NULL; insert(10, &root); insert(5, &root); insert(1, &root); printTree(root); printf("testing single rotation on left child\n"); root = NULL; insert(10, &root); insert(15, &root); insert(5, &root); insert(3, &root); insert(1, &root); printTree(root); printf("testing single rotation on right child with root\n"); root = NULL; insert(10, &root); insert(15, &root); insert(20, &root); printTree(root); printf("testing single rotation on right child\n"); root = NULL; insert(10, &root); insert(5, &root); insert(20, &root); insert(25, &root); insert(30, &root); printTree(root); printf("testing double rotation on left child with root\n"); root = NULL; insert(10, &root); insert(5, &root); insert(7, &root); printTree(root); printf("testing double rotation on left child\n"); root = NULL; insert(10, &root); insert(15, &root); insert(5, &root); insert(3, &root); insert(4, &root); printTree(root); printf("testing double rotation on right child with root\n"); root = NULL; insert(10, &root); insert(15, &root); insert(12, &root); printTree(root); printf("testing double rotation on right child\n"); root = NULL; insert(10, &root); insert(5, &root); insert(20, &root); insert(25, &root); insert(22, &root); printTree(root); // more testing printf("testing removal: single rotation on left child with root\n"); root = NULL; insert(10, &root); insert(15, &root); insert(5, &root); insert(1, &root); removeNode(15, &root); printTree(root); printf("testing removal: single rotation on right child with root\n"); root = NULL; insert(10, &root); insert(15, &root); insert(5, &root); insert(20, &root); removeNode(5, &root); printTree(root); printf("testing removal: double rotation on left child with root\n"); root = NULL; insert(10, &root); insert(15, &root); insert(5, &root); insert(7, &root); removeNode(15, &root); printTree(root); printf("testing removal: double rotation on right child with root\n"); root = NULL; insert(10, &root); insert(15, &root); insert(5, &root); insert(12, &root); removeNode(5, &root); printTree(root); printf("testing removal: single rotation on left child\n"); root = NULL; insert(10, &root); insert(5, &root); insert(15, &root); insert(2, &root); insert(7, &root); insert(20, &root); insert(1, &root); removeNode(7, &root); printTree(root); printf("testing removal: single rotation on right child\n"); root = NULL; insert(10, &root); insert(5, &root); insert(15, &root); insert(2, &root); insert(12, &root); insert(20, &root); insert(25, &root); removeNode(12, &root); printTree(root); printf("testing removal: double rotation on left child\n"); root = NULL; insert(10, &root); insert(5, &root); insert(16, &root); insert(7, &root); insert(2, &root); insert(12, &root); insert(20, &root); insert(14, &root); insert(25, &root); insert(1, &root); insert(11, &root); insert(13, &root); insert(15, &root); printTree(root); removeNode(20, &root); printTree(root); printf("testing removal: double rotation on right child\n"); root = NULL; insert(10, &root); insert(3, &root); insert(16, &root); insert(2, &root); insert(7, &root); insert(12, &root); insert(20, &root); insert(1, &root); insert(5, &root); insert(8, &root); insert(25, &root); insert(4, &root); insert(6, &root); printTree(root); removeNode(2, &root); printTree(root); int i; printf("testing stress insert, increasing order\n"); root = NULL; for (i = 1; i < 128; i++) insert(i, &root); printTree(root); printf("testing stress insert, decreasing order\n"); root = NULL; for (i = 127; i >= 1; i--) insert(i, &root); printTree(root); printf("testing small tree remove, increasing order\n"); root = NULL; for (i = 1; i < 8; i++) insert(i, &root); printTree(root); for (i = 1; i < 8; i++) { removeNode(i, &root); printf("After removing %d:\n", i); printTree(root); } printf("testing small tree remove, decreasing order\n"); root = NULL; for (i = 1; i < 8; i++) insert(i, &root); printTree(root); for (i = 7; i >= 1; i--) { removeNode(i, &root); printf("After removing %d:\n", i); printTree(root); } printf("testing stress remove, increasing order\n"); root = NULL; for (i = 1; i < 128; i++) insert(i, &root); for (i = 1; i < 128; i++) { removeNode(i, &root); } printTree(root); printf("testing stress remove, decreasing order\n"); root = NULL; for (i = 1; i < 128; i++) insert(i, &root); for (i = 127; i >= 1; i--) { removeNode(i, &root); } printTree(root); printf("Strees testing double rotations on insert:\n"); root = NULL; int maxx = 64; int num = maxx / 2; int offset = num; int start = offset; insert(0, &root); while (num > 0){ while (start < maxx) { insert(start*2, &root); insert(start*2-1, &root); start += offset; } offset = num; num = num/2; start = num; } // for (i = 80; i >= 1; i--) removeNode(i, &root); printTree(root); */ test_insert(&root); printf("Height of tree: %d\n", height(root)); test_find(&root); // test_remove(&root); // printf("Height of tree: %d\n", height(root)); return EXIT_SUCCESS; } // Populates array with count numbers and then shuffles them // creates nodes filled with the number. void shuffle_random(uint32_t* array, Node* nodes, uint32_t count, uint32_t maxRange) { int i; // Set the seed mt_seed(time(NULL) * mt_rand(maxRange)); // Populate the array for (i = 0; i < count; array[i++] = i); // Shuffle the elements int i1, i2; int temp; for (i = 0; i < count; i++) { i1 = (mt_rand(maxRange)); i2 = (mt_rand(maxRange)); temp = array[i1]; array[i1] = array[i2]; array[i2] = temp; } for (i = 0; i < count; i++) nodes[i] = makeNode(array[i]); } // Populates array with count number of rand() generated pseudorandom // numbers up to maxRange void generate_random(uint32_t* array, uint32_t count, uint32_t maxRange) { int i; // Set the seed mt_seed(time(NULL) * mt_rand(maxRange)); // ask Nick // Populate the array for (i = 0; i < count; array[i++] = (mt_rand(maxRange))); }