unit1309.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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. #include "warnless.h"
  25. static CURLcode unit_setup(void)
  26. {
  27. return CURLE_OK;
  28. }
  29. static void unit_stop(void)
  30. {
  31. }
  32. static void splayprint(struct Curl_tree *t, int d, char output)
  33. {
  34. struct Curl_tree *node;
  35. int i;
  36. int count;
  37. if(!t)
  38. return;
  39. splayprint(t->larger, d + 1, output);
  40. for(i = 0; i<d; i++)
  41. if(output)
  42. printf(" ");
  43. if(output) {
  44. printf("%ld.%ld[%d]", (long)t->key.tv_sec,
  45. (long)t->key.tv_usec, i);
  46. }
  47. for(count = 0, node = t->samen; node != t; node = node->samen, count++)
  48. ;
  49. if(output) {
  50. if(count)
  51. printf(" [%d more]\n", count);
  52. else
  53. printf("\n");
  54. }
  55. splayprint(t->smaller, d + 1, output);
  56. }
  57. UNITTEST_START
  58. /* number of nodes to add to the splay tree */
  59. #define NUM_NODES 50
  60. struct Curl_tree *root, *removed;
  61. struct Curl_tree nodes[NUM_NODES*3];
  62. size_t storage[NUM_NODES*3];
  63. int rc;
  64. int i, j;
  65. struct curltime tv_now = {0, 0};
  66. root = NULL; /* the empty tree */
  67. /* add nodes */
  68. for(i = 0; i < NUM_NODES; i++) {
  69. struct curltime key;
  70. key.tv_sec = 0;
  71. key.tv_usec = (541*i)%1023;
  72. storage[i] = key.tv_usec;
  73. nodes[i].payload = &storage[i];
  74. root = Curl_splayinsert(key, root, &nodes[i]);
  75. }
  76. puts("Result:");
  77. splayprint(root, 0, 1);
  78. for(i = 0; i < NUM_NODES; i++) {
  79. int rem = (i + 7)%NUM_NODES;
  80. printf("Tree look:\n");
  81. splayprint(root, 0, 1);
  82. printf("remove pointer %d, payload %zu\n", rem,
  83. *(size_t *)nodes[rem].payload);
  84. rc = Curl_splayremove(root, &nodes[rem], &root);
  85. if(rc) {
  86. /* failed! */
  87. printf("remove %d failed!\n", rem);
  88. fail("remove");
  89. }
  90. }
  91. fail_unless(root == NULL, "tree not empty after removing all nodes");
  92. /* rebuild tree */
  93. for(i = 0; i < NUM_NODES; i++) {
  94. struct curltime key;
  95. key.tv_sec = 0;
  96. key.tv_usec = (541*i)%1023;
  97. /* add some nodes with the same key */
  98. for(j = 0; j <= i % 3; j++) {
  99. storage[i * 3 + j] = key.tv_usec*10 + j;
  100. nodes[i * 3 + j].payload = &storage[i * 3 + j];
  101. root = Curl_splayinsert(key, root, &nodes[i * 3 + j]);
  102. }
  103. }
  104. removed = NULL;
  105. for(i = 0; i <= 1100; i += 100) {
  106. printf("Removing nodes not larger than %d\n", i);
  107. tv_now.tv_usec = i;
  108. root = Curl_splaygetbest(tv_now, root, &removed);
  109. while(removed != NULL) {
  110. printf("removed payload %zu[%zu]\n",
  111. (*(size_t *)removed->payload) / 10,
  112. (*(size_t *)removed->payload) % 10);
  113. root = Curl_splaygetbest(tv_now, root, &removed);
  114. }
  115. }
  116. fail_unless(root == NULL, "tree not empty when it should be");
  117. UNITTEST_STOP