1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "curlcheck.h"
28 static CURLcode unit_setup(void)
33 static void unit_stop(void)
38 static void splayprint(struct Curl_tree * t, int d, char output)
40 struct Curl_tree *node;
46 splayprint(t->larger, d + 1, output);
52 printf("%ld.%ld[%d]", (long)t->key.tv_sec,
53 (long)t->key.tv_usec, i);
56 for(count = 0, node = t->samen; node != t; node = node->samen, count++)
61 printf(" [%d more]\n", count);
66 splayprint(t->smaller, d + 1, output);
71 /* number of nodes to add to the splay tree */
74 struct Curl_tree *root, *removed;
75 struct Curl_tree nodes[NUM_NODES*3];
78 struct curltime tv_now = {0, 0};
79 root = NULL; /* the empty tree */
82 for(i = 0; i < NUM_NODES; i++) {
87 key.tv_usec = (541*i)%1023;
88 payload = (size_t) key.tv_usec;
91 nodes[i].payload = CURLX_INTEGER_TO_POINTER_CAST(payload);
92 root = Curl_splayinsert(key, root, &nodes[i]);
96 splayprint(root, 0, 1);
98 for(i = 0; i < NUM_NODES; i++) {
99 int rem = (i + 7)%NUM_NODES;
100 printf("Tree look:\n");
101 splayprint(root, 0, 1);
102 printf("remove pointer %d, payload %ld\n", rem,
103 CURLX_POINTER_TO_INTEGER_CAST(nodes[rem].payload));
104 rc = Curl_splayremovebyaddr(root, &nodes[rem], &root);
107 printf("remove %d failed!\n", rem);
112 fail_unless(root == NULL, "tree not empty after removing all nodes");
115 for(i = 0; i < NUM_NODES; i++) {
119 key.tv_usec = (541*i)%1023;
121 /* add some nodes with the same key */
122 for(j = 0; j <= i % 3; j++) {
123 size_t payload = key.tv_usec*10 + j;
125 nodes[i * 3 + j].payload = CURLX_INTEGER_TO_POINTER_CAST(payload);
126 root = Curl_splayinsert(key, root, &nodes[i * 3 + j]);
131 for(i = 0; i <= 1100; i += 100) {
132 printf("Removing nodes not larger than %d\n", i);
134 root = Curl_splaygetbest(tv_now, root, &removed);
135 while(removed != NULL) {
136 printf("removed payload %ld[%ld]\n",
137 CURLX_POINTER_TO_INTEGER_CAST(removed->payload) / 10,
138 CURLX_POINTER_TO_INTEGER_CAST(removed->payload) % 10);
139 root = Curl_splaygetbest(tv_now, root, &removed);
143 fail_unless(root == NULL, "tree not empty when it should be");