unit1309.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 http://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->same; node; node = node->same, 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;
  60. struct Curl_tree nodes[NUM_NODES];
  61. int rc;
  62. int i;
  63. root = NULL; /* the empty tree */
  64. for(i = 0; i < NUM_NODES; i++) {
  65. struct timeval key;
  66. key.tv_sec = 0;
  67. key.tv_usec = (541*i)%1023;
  68. nodes[i].payload = (void *)key.tv_usec; /* for simplicity */
  69. root = Curl_splayinsert(key, root, &nodes[i]);
  70. }
  71. puts("Result:");
  72. splayprint(root, 0, 1);
  73. for(i = 0; i < NUM_NODES; i++) {
  74. int rem = (i+7)%NUM_NODES;
  75. printf("Tree look:\n");
  76. splayprint(root, 0, 1);
  77. printf("remove pointer %d, payload %ld\n", rem,
  78. (long)(nodes[rem].payload));
  79. rc = Curl_splayremovebyaddr(root, &nodes[rem], &root);
  80. if(rc) {
  81. /* failed! */
  82. printf("remove %d failed!\n", rem);
  83. fail("remove");
  84. }
  85. }
  86. UNITTEST_STOP