1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*------------------------------------------------------------------------------
*
* Copyright (c) 2011-2025, EURid vzw. All rights reserved.
* The YADIFA TM software product is provided under the BSD 3-clause license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of EURid nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*----------------------------------------------------------------------------*/
#include "yatest.h"
#include <dnscore/dnscore.h>
#include <dnscore/host_address.h>
#include <dnscore/ptr_treeset_debug.h>
#define FIBONACCI_INT64_MAX 92
#define FIBONACCI_INT32_MAX 46
#if __SIZEOF_POINTER__ == 8
#define FIBONACCI_PTR_MAX FIBONACCI_INT64_MAX
#else
#define FIBONACCI_PTR_MAX FIBONACCI_INT32_MAX
#endif
static char tmp_text_buffer[256];
static void *ptr_key_generator(int64_t value) { return (void *)(intptr_t)value; }
static char *ptr_key_to_text(void *key)
{
snprintf(tmp_text_buffer, sizeof(tmp_text_buffer), "%p", key);
return tmp_text_buffer;
}
static void *asciizp_key_generator(int64_t value)
{
char tmp[24];
snprintf(tmp, sizeof(tmp), "K%019" PRIi64, value);
return yatest_strdup(tmp);
}
static char *asciizp_key_to_text(void *key)
{
if(key != NULL)
{
return key;
}
else
{
strcpy(tmp_text_buffer, "NULL");
return tmp_text_buffer;
}
}
static void *dnsname_key_generator(int64_t value)
{
char tmp[24];
int n = snprintf(tmp + 1, sizeof(tmp) - 1, "K%019" PRIi64, value);
tmp[0] = n;
return dnsname_dup((uint8_t *)tmp);
}
static char *dnsname_key_to_text(void *key)
{
if(key != NULL)
{
cstr_init_with_dnsname(tmp_text_buffer, key);
}
else
{
strcpy(tmp_text_buffer, "NULL");
}
return tmp_text_buffer;
}
static void *host_address_key_generator(int64_t value)
{
host_address_t *ha = host_address_new_instance();
ha->ip.v6.lohi[0] = 0;
ha->ip.v6.lohi[1] = bswap_64(value);
ha->ip.v6.bytes[0] = 0x20;
ha->ip.v6.bytes[1] = 0x02;
ha->version = 6;
ha->port = 0;
return ha;
}
static char *host_address_key_to_text(void *key)
{
host_address_t *ha = key;
host_address_to_str(ha, tmp_text_buffer, sizeof(tmp_text_buffer), 0);
return tmp_text_buffer;
}
static int64_t fibonacci(int64_t value)
{
if(value < 2)
{
return value;
}
static int64_t *fibonacci_memorised = NULL;
if(fibonacci_memorised == NULL)
{
fibonacci_memorised = yatest_malloc((FIBONACCI_PTR_MAX + 1) * sizeof(int64_t));
fibonacci_memorised[0] = 0;
fibonacci_memorised[1] = 1;
for(int64_t i = 2; i <= FIBONACCI_PTR_MAX; ++i)
{
fibonacci_memorised[i] = fibonacci_memorised[i - 1] + fibonacci_memorised[i - 2];
}
}
if(value < FIBONACCI_PTR_MAX)
{
return fibonacci_memorised[value];
}
else // beyond 64 bits anyway ...
{
return INT64_MAX;
}
}
static int add_del_test_common(ptr_treeset_node_debug_compare_t *comparator, void *(*key_generator)(int64_t), char *(*key_to_text)(void *), bool is_nullable)
{
dnscore_init();
ptr_treeset_debug_t tree;
tree.root = NULL;
tree.compare = comparator;
for(int64_t i = 2; i <= FIBONACCI_PTR_MAX; ++i)
{
char *key = key_generator(fibonacci(i));
yatest_log("inserting key: %s (%lli)", key_to_text(key), i);
/*ptr_treeset_node_debug_t *node = */ ptr_treeset_debug_insert(&tree, key);
}
if(is_nullable)
{
/*ptr_treeset_node_debug_t *node = */ ptr_treeset_debug_insert(&tree, NULL);
}
ptr_treeset_debug_iterator_t iter;
ptr_treeset_debug_iterator_init(&tree, &iter);
while(ptr_treeset_debug_iterator_hasnext(&iter))
{
ptr_treeset_node_debug_t *node = ptr_treeset_debug_iterator_next_node(&iter);
yatest_log("iterated: %s", key_to_text(node->key));
}
for(int64_t i = 2; i <= FIBONACCI_PTR_MAX; ++i)
{
char *key = key_generator(fibonacci(i));
yatest_log("finding key: %s", key_to_text(key));
ptr_treeset_node_debug_t *node = ptr_treeset_debug_find(&tree, key);
if(node == NULL)
{
yatest_err("expected to find node for key %s", key_to_text(key));
ptr_treeset_debug_iterator_init(&tree, &iter);
while(ptr_treeset_debug_iterator_hasnext(&iter))
{
ptr_treeset_node_debug_t *node = ptr_treeset_debug_iterator_next_node(&iter);
yatest_err("iterated: %s", key_to_text(node->key));
}
return 1;
}
yatest_log("deleting key: %s", key_to_text(key));
ptr_treeset_debug_delete(&tree, key);
}
if(is_nullable)
{
ptr_treeset_debug_delete(&tree, NULL);
}
if(!ptr_treeset_debug_isempty(&tree))
{
yatest_err("expected the treeset to be empty");
return 1;
}
dnscore_finalize();
return 0;
}
static int add_del_ptr_test() { return add_del_test_common(ptr_treeset_debug_ptr_node_compare, ptr_key_generator, ptr_key_to_text, false); }
static int add_del_asciizp_test() { return add_del_test_common(ptr_treeset_debug_asciizp_node_compare, asciizp_key_generator, asciizp_key_to_text, false); }
static int add_del_nullable_asciizcasep_test() { return add_del_test_common(ptr_treeset_debug_nullable_asciizp_node_compare, asciizp_key_generator, asciizp_key_to_text, true); }
static int add_del_dnsname_test() { return add_del_test_common(ptr_treeset_debug_dnsname_node_compare, dnsname_key_generator, dnsname_key_to_text, false); }
static int add_del_dnslabel_test() { return add_del_test_common(ptr_treeset_debug_dnslabel_node_compare, dnsname_key_generator, dnsname_key_to_text, false); }
static int add_del_nullable_dnsname_test() { return add_del_test_common(ptr_treeset_debug_nullable_dnsname_node_compare, dnsname_key_generator, dnsname_key_to_text, true); }
static int add_del_host_address_test() { return add_del_test_common(ptr_treeset_debug_host_address_node_compare, host_address_key_generator, host_address_key_to_text, true); }
YATEST_TABLE_BEGIN
YATEST(add_del_ptr_test)
YATEST(add_del_asciizp_test)
YATEST(add_del_nullable_asciizcasep_test)
YATEST(add_del_dnsname_test)
YATEST(add_del_nullable_dnsname_test)
YATEST(add_del_dnslabel_test)
YATEST(add_del_host_address_test)
YATEST_TABLE_END
|