unit1309.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curlcheck.h"
  23. #include "splay.h"
  24. static CURLcode unit_setup(void)
  25. {
  26. return CURLE_OK;
  27. }
  28. static void unit_stop(void)
  29. {
  30. }
  31. static void splayprint(struct Curl_tree * t, int d, char output)
  32. {
  33. struct Curl_tree *node;
  34. int i;
  35. int count;
  36. if(t == NULL)
  37. return;
  38. splayprint(t->larger, d+1, output);
  39. for(i=0; i<d; i++)
  40. if(output)
  41. printf(" ");
  42. if(output) {
  43. printf("%ld.%ld[%d]", (long)t->key.tv_sec,
  44. (long)t->key.tv_usec, i);
  45. }
  46. for(count=0, node = t->samen; node != t; node = node->samen, count++)
  47. ;
  48. if(output) {
  49. if(count)
  50. printf(" [%d more]\n", count);
  51. else
  52. printf("\n");
  53. }
  54. splayprint(t->smaller, d+1, output);
  55. }
  56. UNITTEST_START
  57. /* number of nodes to add to the splay tree */
  58. #define NUM_NODES 50
  59. struct Curl_tree *root, *removed;
  60. struct Curl_tree nodes[NUM_NODES*3];
  61. int rc;
  62. int i, j;
  63. struct timeval tv_now = {0, 0};
  64. root = NULL; /* the empty tree */
  65. /* add nodes */
  66. for(i = 0; i < NUM_NODES; i++) {
  67. struct timeval key;
  68. key.tv_sec = 0;
  69. key.tv_usec = (541*i)%1023;
  70. nodes[i].payload = (void *)key.tv_usec; /* for simplicity */
  71. root = Curl_splayinsert(key, root, &nodes[i]);
  72. }
  73. puts("Result:");
  74. splayprint(root, 0, 1);
  75. for(i = 0; i < NUM_NODES; i++) {
  76. int rem = (i+7)%NUM_NODES;
  77. printf("Tree look:\n");
  78. splayprint(root, 0, 1);
  79. printf("remove pointer %d, payload %ld\n", rem,
  80. (long)(nodes[rem].payload));
  81. rc = Curl_splayremovebyaddr(root, &nodes[rem], &root);
  82. if(rc) {
  83. /* failed! */
  84. printf("remove %d failed!\n", rem);
  85. fail("remove");
  86. }
  87. }
  88. fail_unless(root == NULL, "tree not empty after removing all nodes");
  89. /* rebuild tree */
  90. for(i = 0; i < NUM_NODES; i++) {
  91. struct timeval key;
  92. key.tv_sec = 0;
  93. key.tv_usec = (541*i)%1023;
  94. /* add some nodes with the same key */
  95. for(j = 0; j <= i % 3; j++) {
  96. nodes[i*3+j].payload = (void *)(key.tv_usec*10 + j); /* for simplicity */
  97. root = Curl_splayinsert(key, root, &nodes[i*3+j]);
  98. }
  99. }
  100. removed = NULL;
  101. for(i = 0; i <= 1100; i+= 100) {
  102. printf("Removing nodes not larger than %d\n", i);
  103. tv_now.tv_usec = i;
  104. root = Curl_splaygetbest(tv_now, root, &removed);
  105. while(removed != NULL) {
  106. printf("removed payload %ld[%ld]\n", (long)(removed->payload) / 10,
  107. (long)(removed->payload) % 10);
  108. root = Curl_splaygetbest(tv_now, root, &removed);
  109. }
  110. }
  111. fail_unless(root == NULL, "tree not empty when it should be");
  112. UNITTEST_STOP