Advertisement
Rummeris

test_bst.c

Nov 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.01 KB | None | 0 0
  1. //
  2. // Created by Zitai Chen on 28/10/2019.
  3. //
  4. #include<pthread.h>
  5. #include<stdlib.h>
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include <unistd.h>
  9.  
  10. #include "bst.h"
  11.  
  12. Node *root=NULL;
  13. Node *root_balanced=NULL;
  14.  
  15. #include "unique_rng.c"
  16. #include "serve_client.c"
  17.  
  18. #ifdef CYCLE_TEST
  19. #include "cpucycles.c"
  20. #endif
  21.  
  22. int f_verbose = 0;
  23.  
  24. void clean(){
  25.     /*****************************************************/
  26.     /******   Free resources before program ends  ********/
  27.     /*****************************************************/
  28.  
  29.     root=freeSubtree(root);
  30.     root_balanced = freeSubtree(root_balanced);
  31.     root= NULL;
  32.     root_balanced = NULL;
  33.     return;
  34. }
  35.  
  36. #ifdef CYCLE_TEST
  37. // DO NOT add any test functions here, as they will not be compiled
  38. void test_cycle(){
  39.     unsigned int COUNTER, REPEAT=1000;
  40.     unsigned int i, r, sum, sum_balanced;
  41.  
  42.     init_rand();
  43.  
  44.     // Create a random BST rooted at 'root'
  45.     for(i=0; i<10000; i++){
  46.         r = unique_random_number();
  47.         root=insertNode(root, r);
  48.     }
  49.  
  50.     long int CLOCK_total, CLOCK_end, CLOCK_start;
  51.     // Create a balanced BST from the unbalanced BST
  52.     root_balanced = balanceTree(root);
  53.  
  54.  
  55.     // Cycle count for sum on unbalanced BST
  56.     CLOCK_total=0;
  57.     for(COUNTER=0; COUNTER<REPEAT; COUNTER++)
  58.     {
  59.         CLOCK_start = cpucycles();
  60.         sum = sumSubtree(root);
  61.         CLOCK_end = cpucycles();
  62.         CLOCK_total = CLOCK_total + CLOCK_end - CLOCK_start;
  63.     }
  64.     printf("Avg cycle count for unbalanced BST = %ld\n", CLOCK_total/REPEAT);
  65.     printf("Sum of unbalanced BST = %u\n", sum);
  66.  
  67.     // Cycle count for sum on balanced BST
  68.     CLOCK_total=0;
  69.     for(COUNTER=0; COUNTER<REPEAT; COUNTER++)
  70.     {
  71.         CLOCK_start = cpucycles();
  72.         sum_balanced = sumSubtree(root_balanced);
  73.         CLOCK_end = cpucycles();
  74.         CLOCK_total = CLOCK_total + CLOCK_end - CLOCK_start;
  75.     }
  76.     printf("Avg cycle count for balanced BST = %ld\n", CLOCK_total/REPEAT);
  77.     printf("Sum of balanced BST = %u\n", sum_balanced);
  78.  
  79.     printf("Difference in sums = %d\n", sum - sum_balanced);
  80.  
  81.     clean();
  82.  
  83. }
  84. #else
  85. void test_task12(){
  86.     unsigned int i,r, sum, sum_balanced;
  87.     int failed = 0;
  88.     init_rand();
  89.     // Create a random BST rooted at 'root'
  90.     for(i=0; i<10000; i++){
  91.         r = unique_random_number();     // This will give you the same set of random numbers every time
  92.         root=insertNode(root, r);
  93.     }
  94.  
  95.     /*****************************************************/
  96.     /******   Part 1 of Exercise 2 Starts here    ********/
  97.     /*****************************************************/
  98.  
  99.     printf("/******** TEST OF PART 1 ********/\n\n");
  100.  
  101.     // Create a balanced BST from the unbalanced BST
  102.     root_balanced = balanceTree(root);
  103.  
  104.     // Sum on unbalanced BST
  105.  
  106.     sum = sumSubtree(root);
  107.  
  108.     printf("Sum of unbalanced BST = %u\n", sum);
  109.  
  110.     // Sum on balanced BST
  111.     sum_balanced = sumSubtree(root_balanced);
  112.  
  113.     printf("Sum of balanced BST = %u\n", sum_balanced);
  114.  
  115.     printf("Difference in sums = %d\n", sum - sum_balanced);
  116.     destroy_rand();
  117.  
  118.     failed = (sum - sum_balanced) + (sum - 289060379);
  119.  
  120.     if(failed || f_verbose)
  121.     {
  122.       printf("\n/******** START DEBUG MODE *********/\n");
  123.  
  124.       printf("\nUnbalanced tree:\n");
  125.       printf("label1\n");
  126.       printSubtree(root);
  127.       printf("label2\n");
  128.       printf("\nBalanced tree:\n");
  129.       printf("label3\n");
  130.       printSubtree(root_balanced);
  131.       printf("label4\n");
  132.       printf("\n/******** END DEBUG MODE ********/\n");
  133.     }
  134.  
  135.     printf("\n /******** END OF PART 1 ********/\n\n");
  136.  
  137.     clean();
  138. }
  139.  
  140. void test_tack34(){
  141.  
  142.     printf("/******** TEST OF PART 2 ********/\n\n");
  143.     unsigned int i;
  144.     char *client_names[5] = {"client1_commands", "client2_commands", "client3_commands",
  145.                              "client4_commands", "client5_commands"};
  146.  
  147.     pthread_t threads[6];
  148.  
  149.     /*****************************************************/
  150.     /******   Part 2 of Exercise 2 Starts here    ********/
  151.     /*****************************************************/
  152.  
  153.     // spawn all threads
  154.     pthread_create(&threads[0], NULL, (void *) ServeClient, client_names[0]);
  155.     pthread_create(&threads[1], NULL, (void *) ServeClient, client_names[1]);
  156.     pthread_create(&threads[2], NULL, (void *) ServeClient, client_names[2]);
  157.     pthread_create(&threads[3], NULL, (void *) ServeClient, client_names[3]);
  158.     pthread_create(&threads[4], NULL, (void *) ServeClient, client_names[4]);
  159. //  pthread_create(&threads[5], NULL, (void *) downtime, NULL);
  160.  
  161.  
  162.     // join all readers
  163.     for (i = 0; i < 6; i++) {
  164.         pthread_join(threads[i], NULL);
  165.     }
  166.  
  167.     // The tree should only have one node now
  168.     int count = countNodes(root);
  169.     int sum = sumSubtree(root);
  170.  
  171.     if (count == 1 && sum == 1){
  172.         printf("Test for Part2 seems OK\n");
  173.     } else{
  174.         printf("Test for Part2 fail\n");
  175.     }
  176.  
  177.     // Free the tree
  178.     clean();
  179. }
  180.  
  181. // TODO: You could add more test functions here
  182.  
  183. #endif
  184. int main(int argc, char *argv[]){
  185.  
  186.     if(argc == 2){
  187.       if(strcmp(argv[1],"-v") == 0)
  188.         {
  189.           f_verbose = 1;
  190.         }
  191.     }
  192.  
  193. #ifdef CYCLE_TEST
  194.     // DO NOT add any test functions here, as they will not be compiled
  195.     test_cycle();
  196. #else
  197.     test_task12();
  198.     test_tack34();
  199.  
  200.     // TODO: You could call your test functions at here
  201.  
  202. #endif
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement