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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2013 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2006-2010 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
* reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <stdint.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <sys/_time.h>
#endif
#include <string.h>
#include "support.h"
#include "opal/class/opal_rb_tree.h"
#include "opal/mca/mpool/base/base.h"
#define NUM_KEYS 10000
#define SEED 1
int keys[] = {
0, 1, 2, 3, 4, 5, 6, 7
};
int values[] = {
10, 11, 12, 13, 14, 15, 16, 17
};
int comp_fn(void * ele1, void * ele2);
void test1(void);
int comp_key(void* key1, void* key2);
void test_keys(void);
int comp_fn(void * ele1, void * ele2)
{
if(*((int *) ele1) > *((int *) ele2)) {
return(1);
}
if(*((int *) ele1) < *((int *) ele2)) {
return(-1);
}
return(0);
}
struct my_key_t{
void *base;
void *bound;
}; typedef struct my_key_t my_key_t;
struct my_val_t{
my_key_t* key;
int val;
}; typedef struct my_val_t my_val_t;
int comp_key(void* key1, void* key2) {
if( ((my_key_t*) key1)->base <
((my_key_t*) key2)->base) {
return -1;
}
else if ( ((my_key_t*) key1)->base >
((my_key_t*) key2)->bound) {
return 1;
}
else {
return 0;
}
}
void test_keys(void)
{
opal_rb_tree_t tree;
int rc, i;
my_key_t keys[NUM_KEYS];
my_val_t vals[NUM_KEYS];
char buf[200];
my_key_t *cur_key;
my_val_t *cur_val;
long tmp;
OBJ_CONSTRUCT(&tree, opal_rb_tree_t);
rc = opal_rb_tree_init(&tree, comp_key);
srand(SEED);
for(i = 0; i < NUM_KEYS; i++) {
cur_key = &(keys[i]);
cur_val = &(vals[i]);
cur_val->key = cur_key;
cur_val->val = i;
tmp = (long) rand();
cur_key->base = (void*) tmp;
tmp += (long) rand();
cur_key->bound = (void*) tmp;
rc = opal_rb_tree_insert(&tree, cur_key, cur_val);
if(OPAL_SUCCESS != rc) {
test_failure("error inserting element in the tree");
}
}
for(i = 0; i < NUM_KEYS; i+=2) {
cur_key = &(keys[i]);
rc = opal_rb_tree_delete(&tree, cur_key);
if(OPAL_SUCCESS != rc) {
test_failure("error deleting element in the tree");
}
}
for(i = 1; i < NUM_KEYS; i+=2) {
cur_key = &(keys[i]);
cur_val = (my_val_t*) opal_rb_tree_find(&tree, cur_key);
if(cur_val == NULL) {
test_failure("lookup returned NULL item");
}
else if(cur_val->val != i && (cur_val->key->base > cur_key->base ||
cur_val->key->bound < cur_key->base)) {
sprintf(buf, "lookup returned invalid item, returned %d, extected %d",
cur_val->val, i);
test_failure(buf);
}
}
}
void test1(void)
{
opal_rb_tree_t tree;
int rc;
void * result;
OBJ_CONSTRUCT(&tree, opal_rb_tree_t);
rc = opal_rb_tree_init(&tree, comp_fn);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly initialize the tree");
}
rc = opal_rb_tree_insert(&tree, &keys[0], &values[0]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[0]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[0], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[1], &values[1]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[1]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[1], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[2], &values[2]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[2]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[2], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[3], &values[3]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[3]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[3], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[4], &values[4]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[4]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[4], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[5], &values[5]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[5]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[5], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[6], &values[6]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[6]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[6], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_insert(&tree, &keys[7], &values[7]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly insert a new node");
}
result = opal_rb_tree_find(&tree, &keys[7]);
if(NULL == result) {
test_failure("lookup returned null!");
}
if(!test_verify_int(values[7], *((int *) result))) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_size(&tree);
if(!test_verify_int(8, rc)) {
test_failure("failed to properly insert a new node");
}
rc = opal_rb_tree_delete(&tree, &keys[0]);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly delete a node");
}
result = opal_rb_tree_find(&tree, &keys[0]);
if(NULL != result) {
test_failure("lookup returned a value instead of null!");
} else {
test_success();
}
OBJ_DESTRUCT(&tree);
}
/* the following test is based on memory lookups in the mpool */
int mem_node_compare(void * key1, void * key2);
void test2(void);
/* the maximum number of memory pools a piece of memory can be registered with */
#define MAX_REGISTRATIONS 10
/* the number of memory segments to allocate */
#define NUM_ALLOCATIONS 500
struct opal_test_rb_key_t
{
void * bottom; /* the bottom of the memory range */
void * top; /* the top of the memory range */
};
typedef struct opal_test_rb_key_t opal_test_rb_key_t;
struct opal_test_rb_value_t
{
opal_free_list_item_t super; /* the parent class */
opal_test_rb_key_t key; /* the key which holds the memory pointers */
mca_mpool_base_module_t* registered_mpools[MAX_REGISTRATIONS];
/* the mpools the memory is registered with */
};
typedef struct opal_test_rb_value_t opal_test_rb_value_t;
OBJ_CLASS_INSTANCE(opal_test_rb_value_t, opal_free_list_item_t, NULL, NULL);
int mem_node_compare(void * key1, void * key2)
{
if(((opal_test_rb_key_t *) key1)->bottom <
((opal_test_rb_key_t *) key2)->bottom)
{
return -1;
}
else if(((opal_test_rb_key_t *) key1)->bottom >
((opal_test_rb_key_t *) key2)->top)
{
return 1;
}
return 0;
}
void test2(void)
{
opal_free_list_t key_list;
opal_free_list_item_t * new_value;
opal_rb_tree_t tree;
int rc, i, size;
void * result, * lookup;
void * mem[NUM_ALLOCATIONS];
opal_free_list_item_t * key_array[NUM_ALLOCATIONS];
struct timeval start, end;
OBJ_CONSTRUCT(&key_list, opal_free_list_t);
opal_free_list_init (&key_list, sizeof(opal_test_rb_value_t),
opal_cache_line_size,
OBJ_CLASS(opal_test_rb_value_t),
0,opal_cache_line_size,
0, -1 , 128, NULL, 0, NULL, NULL, NULL);
OBJ_CONSTRUCT(&tree, opal_rb_tree_t);
rc = opal_rb_tree_init(&tree, mem_node_compare);
if(!test_verify_int(OPAL_SUCCESS, rc)) {
test_failure("failed to properly initialize the tree");
}
size = 1;
for(i = 0; i < NUM_ALLOCATIONS; i++)
{
mem[i] = malloc(size);
if(NULL == mem[i])
{
test_failure("system out of memory");
return;
}
new_value = opal_free_list_get (&key_list);
if(NULL == new_value)
{
test_failure("failed to get memory from free list");
}
key_array[i] = new_value;
((opal_test_rb_value_t *) new_value)->key.bottom = mem[i];
((opal_test_rb_value_t *) new_value)->key.top =
(void *) ((size_t) mem[i] + size - 1);
((opal_test_rb_value_t *) new_value)->registered_mpools[0] = (void *)(intptr_t) i;
rc = opal_rb_tree_insert(&tree, &((opal_test_rb_value_t *)new_value)->key,
new_value);
if(OPAL_SUCCESS != rc)
{
test_failure("failed to properly insert a new node");
}
size += 1;
}
gettimeofday(&start, NULL);
for(i = 0; i < NUM_ALLOCATIONS; i++)
{
lookup = (void *) ((size_t) mem[i] + i);
result = opal_rb_tree_find(&tree, &lookup);
if(NULL == result)
{
test_failure("lookup returned null!");
} else if(i != ((int)(intptr_t) ((opal_test_rb_value_t *) result)->registered_mpools[0]))
{
test_failure("lookup returned wrong node!");
}
result = opal_rb_tree_find(&tree, &lookup);
if(NULL == result)
{
test_failure("lookup returned null!");
} else if(i != ((int)(intptr_t) ((opal_test_rb_value_t *) result)->registered_mpools[0]))
{
test_failure("lookup returned wrong node!");
}
}
gettimeofday(&end, NULL);
#if 0
i = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
printf("In a %d node tree, %d lookups took %f microseconds each\n",
NUM_ALLOCATIONS, NUM_ALLOCATIONS * 2,
(float) i / (float) (NUM_ALLOCATIONS * 2));
#endif
for(i = 0; i < NUM_ALLOCATIONS; i++)
{
if(NULL != mem[i])
{
free(mem[i]);
}
opal_free_list_return (&(key_list), key_array[i]);
}
OBJ_DESTRUCT(&tree);
OBJ_DESTRUCT(&key_list);
}
int main(int argc, char **argv)
{
test_init("opal_rb_tree_t");
test1();
test2();
/* test_keys(); */
return test_finalize();
}
|