2011-06-11 02:19:35 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2022-05-17 17:16:50 +08:00
|
|
|
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-06-11 02:19:35 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-06-11 02:19:35 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2022-05-17 17:16:50 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
|
|
|
*
|
2011-06-11 02:19:35 +08:00
|
|
|
***************************************************************************/
|
|
|
|
#include "curlcheck.h"
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "splay.h"
|
2018-02-26 04:16:32 +08:00
|
|
|
#include "warnless.h"
|
2011-06-11 02:19:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
static CURLcode unit_setup(void)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unit_stop(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:56:53 +08:00
|
|
|
static void splayprint(struct Curl_tree *t, int d, char output)
|
2011-06-11 02:19:35 +08:00
|
|
|
{
|
|
|
|
struct Curl_tree *node;
|
|
|
|
int i;
|
|
|
|
int count;
|
2021-04-19 16:46:11 +08:00
|
|
|
if(!t)
|
2011-06-11 02:19:35 +08:00
|
|
|
return;
|
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
splayprint(t->larger, d + 1, output);
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i<d; i++)
|
2011-06-11 02:19:35 +08:00
|
|
|
if(output)
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
if(output) {
|
|
|
|
printf("%ld.%ld[%d]", (long)t->key.tv_sec,
|
|
|
|
(long)t->key.tv_usec, i);
|
|
|
|
}
|
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(count = 0, node = t->samen; node != t; node = node->samen, count++)
|
2011-06-11 02:19:35 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
if(output) {
|
|
|
|
if(count)
|
|
|
|
printf(" [%d more]\n", count);
|
|
|
|
else
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
splayprint(t->smaller, d + 1, output);
|
2011-06-11 02:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UNITTEST_START
|
|
|
|
|
|
|
|
/* number of nodes to add to the splay tree */
|
|
|
|
#define NUM_NODES 50
|
|
|
|
|
2017-04-03 15:16:21 +08:00
|
|
|
struct Curl_tree *root, *removed;
|
|
|
|
struct Curl_tree nodes[NUM_NODES*3];
|
2018-04-07 17:03:18 +08:00
|
|
|
size_t storage[NUM_NODES*3];
|
2011-06-11 02:19:35 +08:00
|
|
|
int rc;
|
2017-04-03 15:16:21 +08:00
|
|
|
int i, j;
|
2017-07-28 21:49:36 +08:00
|
|
|
struct curltime tv_now = {0, 0};
|
2011-06-11 02:19:35 +08:00
|
|
|
root = NULL; /* the empty tree */
|
|
|
|
|
2017-04-03 15:16:21 +08:00
|
|
|
/* add nodes */
|
2011-06-11 02:19:35 +08:00
|
|
|
for(i = 0; i < NUM_NODES; i++) {
|
2017-07-28 21:49:36 +08:00
|
|
|
struct curltime key;
|
2011-06-11 02:19:35 +08:00
|
|
|
|
|
|
|
key.tv_sec = 0;
|
|
|
|
key.tv_usec = (541*i)%1023;
|
2018-04-07 17:03:18 +08:00
|
|
|
storage[i] = key.tv_usec;
|
|
|
|
nodes[i].payload = &storage[i];
|
2011-06-11 15:10:09 +08:00
|
|
|
root = Curl_splayinsert(key, root, &nodes[i]);
|
2011-06-11 02:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
puts("Result:");
|
|
|
|
splayprint(root, 0, 1);
|
|
|
|
|
|
|
|
for(i = 0; i < NUM_NODES; i++) {
|
2017-09-10 05:55:08 +08:00
|
|
|
int rem = (i + 7)%NUM_NODES;
|
2011-06-11 02:19:35 +08:00
|
|
|
printf("Tree look:\n");
|
|
|
|
splayprint(root, 0, 1);
|
2018-05-07 03:20:32 +08:00
|
|
|
printf("remove pointer %d, payload %zu\n", rem,
|
2018-04-07 17:03:18 +08:00
|
|
|
*(size_t *)nodes[rem].payload);
|
2020-12-01 00:51:26 +08:00
|
|
|
rc = Curl_splayremove(root, &nodes[rem], &root);
|
2011-06-11 15:10:09 +08:00
|
|
|
if(rc) {
|
2011-06-11 02:19:35 +08:00
|
|
|
/* failed! */
|
|
|
|
printf("remove %d failed!\n", rem);
|
2011-06-11 15:10:09 +08:00
|
|
|
fail("remove");
|
|
|
|
}
|
2011-06-11 02:19:35 +08:00
|
|
|
}
|
|
|
|
|
2017-04-03 15:16:21 +08:00
|
|
|
fail_unless(root == NULL, "tree not empty after removing all nodes");
|
|
|
|
|
|
|
|
/* rebuild tree */
|
|
|
|
for(i = 0; i < NUM_NODES; i++) {
|
2017-07-28 21:49:36 +08:00
|
|
|
struct curltime key;
|
2017-04-03 15:16:21 +08:00
|
|
|
|
|
|
|
key.tv_sec = 0;
|
|
|
|
key.tv_usec = (541*i)%1023;
|
|
|
|
|
|
|
|
/* add some nodes with the same key */
|
|
|
|
for(j = 0; j <= i % 3; j++) {
|
2018-04-07 17:03:18 +08:00
|
|
|
storage[i * 3 + j] = key.tv_usec*10 + j;
|
|
|
|
nodes[i * 3 + j].payload = &storage[i * 3 + j];
|
2017-09-10 05:55:08 +08:00
|
|
|
root = Curl_splayinsert(key, root, &nodes[i * 3 + j]);
|
2017-04-03 15:16:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removed = NULL;
|
2017-09-10 05:55:08 +08:00
|
|
|
for(i = 0; i <= 1100; i += 100) {
|
2017-04-03 15:16:21 +08:00
|
|
|
printf("Removing nodes not larger than %d\n", i);
|
|
|
|
tv_now.tv_usec = i;
|
|
|
|
root = Curl_splaygetbest(tv_now, root, &removed);
|
2021-12-26 04:41:14 +08:00
|
|
|
while(removed) {
|
2018-05-07 03:20:32 +08:00
|
|
|
printf("removed payload %zu[%zu]\n",
|
2018-04-07 17:03:18 +08:00
|
|
|
(*(size_t *)removed->payload) / 10,
|
|
|
|
(*(size_t *)removed->payload) % 10);
|
2017-04-03 15:16:21 +08:00
|
|
|
root = Curl_splaygetbest(tv_now, root, &removed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail_unless(root == NULL, "tree not empty when it should be");
|
|
|
|
|
2011-06-11 02:19:35 +08:00
|
|
|
UNITTEST_STOP
|