torture_heap.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2007 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25
/**@internal
26 27 28 29 30 31
 * @file torture_heap.c
 * @brief Test heap
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 */

32 33
#include "config.h"

34 35
#include <sofia-sip/heap.h>

36
#include <unistd.h>
37 38 39 40 41 42 43 44 45 46 47
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

typedef struct {
  unsigned key, value;
  size_t index;
48
} type1, *type2;
49

50
static type1 const null = { 0, 0, 0 };
51

52 53 54 55 56
static inline
int less1(type1 a, type1 b)
{
  return a.key < b.key;
}
57

58 59 60 61 62 63
static inline
void set1(type1 *heap, size_t index, type1 e)
{
  e.index = index;
  heap[index] = e;
}
64

65
#define alloc(a, o, size) realloc((o), (size))
66 67

static inline
68
int less2(type2 a, type2 b)
69
{
70
  return a->key < b->key;
71 72 73
}

static inline
74
void set2(type2 *heap, size_t index, type2 e)
75
{
76 77 78
  e->index = index;
  heap[index] = e;
}
79

80 81 82
#define scope static

/* Define heap having structs as its elements */
83

84 85 86 87 88 89 90 91 92 93 94
typedef HEAP_TYPE Heap1;

HEAP_DECLARE(static, Heap1, heap1_, type1);
HEAP_BODIES(static, Heap1, heap1_, type1, less1, set1, alloc, null);

/* Define heap having pointers as its elements */

typedef HEAP_TYPE Heap2;

HEAP_DECLARE(static, Heap2, heap2_, type1 *);
HEAP_BODIES(static, Heap2, heap2_, type1 *, less2, set2, alloc, NULL);
95 96 97 98 99 100 101 102 103 104 105

/* ====================================================================== */

int tstflags;

#define TSTFLAGS tstflags

#include <sofia-sip/tstdef.h>

char name[] = "torture_heap";

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
size_t _set, _cmp;

static int int_less(void *_array, size_t a, size_t b)
{
  int *array = _array;
  _cmp++;
  return array[a] < array[b];
}

static void int_swap(void *_array, size_t a, size_t b)
{
  int *array = _array;
  int swap = array[a];
  array[a] = array[b];
  array[b] = swap;

  _set++;
}

void test_sort(int *array, size_t r, size_t N)
{
  su_smoothsort(array, r, N, int_less, int_swap);
}

int test_smooth_sort()
{
  BEGIN();

  size_t i, n, N = 300000;
  int array[N];

  size_t v1 = 3, v2 = 1;

  for (n = v1; n <= N; n = v1 + v2 + 1, v2 = v1, v1 = n) {
    for (i = 0; i < n; i++) {
      array[i] = (int)(n - i - 1);
    }

    _set = 0;

146
    /* write(1, ".", 1); */
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    test_sort(array, 0, n);

    TEST_1(_set > 0); _set = 0;

    test_sort(array, 0, n);

    TEST(_set, 0);

    for (i = 0; i < n; i++) {
      TEST(array[i], i);
    }
  }

  for (n = 4; n <= N; n *= 2) {
    for (i = 0; i < n; i++) {
      array[i] = (int)(n - i - 1);
    }

166
    /* write(1, "/", 1); */
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

    test_sort(array, 0, n / 2);
    test_sort(array, n / 2, n / 2);

    for (i = 0; i < n / 2; i++) {
      TEST(array[i], i + n / 2);
    }
    for (; i < n; i++) {
      TEST(array[i], i - n / 2);
    }
  }
  
  END();
}

182
int test_value()
183 184
{
  BEGIN();
185 186

  Heap1 heap = { NULL };
187
  unsigned i, previous, n, N;
188 189
  unsigned char *tests;

190
  N = 3000;
191

192
  TEST_1(tests = calloc(sizeof (unsigned char), N + 1));
193

194
  TEST(heap1_resize(NULL, &heap, 0), 0);
195 196 197

  /* Add N entries in reverse order */
  for (i = N; i > 0; i--) {
198 199 200 201 202
    type1 e = { i / 10, i, 0 };
    if (heap1_is_full(heap))
      TEST(heap1_resize(NULL, &heap, 0), 0);
    TEST(heap1_is_full(heap), 0);
    TEST(heap1_add(heap, e), 0);
203
    tests[i] |= 1;
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
  TEST(heap1_used(heap), N);

  for (i = 1; i <= N; i++) {
    type1 const e = heap1_get(heap, i);

    TEST(e.index, i);
    TEST(tests[e.value] & 2, 0);
    tests[e.value] |= 2;

    if (2 * i <= N) {
      type1 const left = heap1_get(heap, 2 * i);
      TEST_1(e.key <= left.key);
    }

    if (2 * i + 1 <= N) {
      type1 const right = heap1_get(heap, 2 * i + 1);
      TEST_1(e.key <= right.key);
    }
  }

  /* Remove N entries */
  previous = 0;

  for (n = 0; heap1_used(heap) > 0; n++) {
    type1 const e = heap1_get(heap, 1);
    TEST_1(previous <= e.key);

    TEST(tests[e.value] & 4, 0);
    tests[e.value] |= 4;

    previous = e.key;
237 238
    TEST(heap1_get(heap, 1).index, 1);
    TEST(heap1_remove(heap, 1).index, 0);
239 240 241 242 243 244 245 246 247 248 249 250 251
  }
  TEST(n, N);

  /* Add N entries in reverse order */
  for (i = N; i > 0; i--) {
    type1 e = { i / 10, i, 0 };
    if (heap1_is_full(heap))
      TEST(heap1_resize(NULL, &heap, 0), 0);
    TEST(heap1_is_full(heap), 0);
    TEST(heap1_add(heap, e), 0);
  }

  TEST(heap1_used(heap), N);
252

253 254 255 256 257 258
  heap1_sort(heap);
  for (i = 1; i <= N; i++) {
    type1 e = heap1_get(heap, i);
    TEST(e.index, i);
  }

259 260 261 262 263 264 265 266 267
  /* Remove 1000 entries from random places */
  previous = 0;

  for (i = 0; i < 1000 && heap1_used(heap) > 0; i++) {
    type1 e;
    n = i * 397651 % heap1_used(heap) + 1;
    e = heap1_get(heap, n);
    TEST(e.index, n);
    TEST(tests[e.value] & 8, 0); tests[e.value] |= 8;
268 269
    TEST(heap1_get(heap, n).index, n);
    TEST(heap1_remove(heap, n).index, 0);
270 271
  }

272 273 274 275 276 277
  for (i = 1; i <= heap1_used(heap); i++) {
    type1 e = heap1_get(heap, i);
    type1 left = heap1_get(heap, 2 * i);
    type1 right = heap1_get(heap, 2 * i + 1);
    TEST_1(left.index == 0 || e.key <= left.key);
    TEST_1(right.index == 0 || e.key <= right.key);
278 279
  }

280 281 282 283 284 285 286 287
  /* Remove rest */
  for (n = 0, previous = 0; heap1_used(heap) > 0; n++) {
    type1 e = heap1_get(heap, 1);
    TEST(e.index, 1);
    TEST(tests[e.value] & 8, 0);
    tests[e.value] |= 8;
    TEST_1(previous <= e.key);
    previous = e.key;
288 289
    TEST(heap1_get(heap, 1).index, 1);
    TEST(heap1_remove(heap, 1).index, 0);
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
  }

  for (i = 1; i <= N; i++) {
    TEST(tests[i], 8 | 4 | 2 | 1);
  }

  TEST(heap1_resize(NULL, &heap, 63), 0);
  TEST(heap1_size(heap), 63);

  TEST(heap1_free(NULL, &heap), 0);
  free(tests);

  END();
}

int test_ref()
{
  BEGIN();

  Heap2 heap = { NULL };
  unsigned i, previous, n, N;
  unsigned char *tests;
  type1 *items;

  N = 300000;

  TEST_1(tests = calloc(sizeof (unsigned char), N + 1));
  TEST_1(items = calloc((sizeof *items), N + 1));

  TEST(heap2_resize(NULL, &heap, 0), 0);

  /* Add N entries in reverse order */
  for (i = N; i > 0; i--) {
    type2 e = items + i;

    e->key = i / 10, e->value = i;

    if (heap2_is_full(heap))
      TEST(heap2_resize(NULL, &heap, 0), 0);
    TEST(heap2_is_full(heap), 0);
    TEST(heap2_add(heap, e), 0);
    tests[i] |= 1;
  }

  TEST(heap2_used(heap), N);

  for (i = 1; i <= N; i++) {
    type2 const e = heap2_get(heap, i);

    TEST_1(e);
    TEST(e->index, i);
    TEST(tests[e->value] & 2, 0);
    tests[e->value] |= 2;

    if (2 * i <= N) {
      type2 const left = heap2_get(heap, 2 * i);
      TEST_1(e->key <= left->key);
    }

    if (2 * i + 1 <= N) {
      type2 const right = heap2_get(heap, 2 * i + 1);
      TEST_1(e->key <= right->key);
    }
353 354 355 356 357
  }

  /* Remove N entries */
  previous = 0;

358 359 360 361 362
  for (n = 0; heap2_used(heap) > 0; n++) {
    type2 const e = heap2_get(heap, 1);

    TEST_1(e);
    TEST_1(previous <= e->key);
363

364 365
    TEST(tests[e->value] & 4, 0);
    tests[e->value] |= 4;
366

367 368
    previous = e->key;
    TEST(heap2_remove(heap, 1), e);
369 370 371
  }
  TEST(n, N);

372 373
  /* Add N entries in reverse order */
  for (i = N; i > 0; i--) {
374 375 376 377 378 379
    type2 e = items + i;

    if (heap2_is_full(heap))
      TEST(heap2_resize(NULL, &heap, 0), 0);
    TEST(heap2_is_full(heap), 0);
    TEST(heap2_add(heap, e), 0);
380 381
  }

382
  TEST(heap2_used(heap), N);
383

384 385 386 387 388 389 390 391 392 393 394
  heap2_sort(heap);
  for (i = 1; i <= N; i++) {
    type2 const e = heap2_get(heap, i);
    TEST_1(e); TEST(e->index, i);
  }

  heap2_sort(heap);
  for (i = 1; i <= N; i++) {
    type2 const e = heap2_get(heap, i);
    TEST_1(e); TEST(e->index, i);
  }
395

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  /* Remove all odd entries */
  for (i = heap2_used(heap), n = 0; i > 0; i--) {
    type2 e = heap2_get(heap, i);
    TEST_1(e); TEST(e->index, i);

    n++;

    tests[e->value] |= 16;

    if (e->value & 1) {
      TEST(tests[e->value] & 8, 0); tests[e->value] |= 8;
      heap2_remove(heap, i);
    }
  }

  for (i = 1; i <= N; i++) {
    if (i & 1)
      TEST(tests[i] & 8, 8);
    else
      TEST(tests[i] & 8, 0);
  }

  /* Remove 1000 entries from random places */
419 420 421 422 423 424 425
  for (i = 0; i < 1000 && heap2_used(heap) > 0; i++) {
    type2 e;
    n = i * 397651 % heap2_used(heap) + 1;
    e = heap2_get(heap, n);
    TEST_1(e); TEST(e->index, n);
    TEST(tests[e->value] & 8, 0); tests[e->value] |= 8;
    TEST(heap2_remove(heap, n), e);
426 427
  }

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  for (i = 1; i <= heap2_used(heap); i++) {
    type2 e = heap2_get(heap, i);

    TEST_1(e); TEST(e->index, i);

    if (2 * i <= heap2_used(heap)) {
      type2 const left = heap2_get(heap, 2 * i);
      TEST_1(left);
      TEST_1(e->key <= left->key);
    }

    if (2 * i + 1 <= heap2_used(heap)) {
      type2 const right = heap2_get(heap, 2 * i + 1);
      TEST_1(right);
      TEST_1(e->key <= right->key);
    }
444 445 446
  }

  /* Remove rest */
447 448
  for (n = 0, previous = 0; heap2_used(heap) > 0; n++) {
    type2 e = heap2_remove(heap, 1);
449
    TEST_1(e); TEST(e->index, 0);
450 451 452 453
    TEST(tests[e->value] & 8, 0);
    tests[e->value] |= 8;
    TEST_1(previous <= e->key);
    previous = e->key;
454 455 456
  }

  for (i = 1; i <= N; i++) {
457
    TEST(tests[i], 16 | 8 | 4 | 2 | 1);
458 459
  }

460 461 462 463 464 465
  TEST(heap2_resize(NULL, &heap, 63), 0);
  TEST(heap2_size(heap), 63);

  TEST(heap2_free(NULL, &heap), 0);
  free(tests);
  free(items);
466 467 468 469

  END();
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
int test_triplet()
{
  BEGIN();

  Heap2 heap = { NULL };
  
  unsigned i, N;
  type1 *items;

  N = 3;

  TEST_1(items = calloc((sizeof *items), N + 1));

  TEST(heap2_resize(NULL, &heap, 0), 0);

  for (i = 1; i <= N; i++) {
    type2 e = items + i;

    e->key = i, e->value = i;

    if (heap2_is_full(heap))
      TEST(heap2_resize(NULL, &heap, 0), 0);
    TEST(heap2_is_full(heap), 0);
    TEST(heap2_add(heap, e), 0);
  }

  for (i = 1; i <= N; i++) {
    type2 e = heap2_get(heap, i);
    TEST(e->key, i);
    TEST(e->value, i);
  }
  
  for (i = 1; i <= N; i++) {
    type2 e = heap2_remove(heap, 1);
    TEST(e->key, i);
    TEST(e->value, i);
  }

  TEST(heap2_free(NULL, &heap), 0);
  free(items);

  END();
}


515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
void usage(int exitcode)
{
  fprintf(stderr,
	  "usage: %s [-v] [-a]\n",
	  name);
  exit(exitcode);
}

int main(int argc, char *argv[])
{
  int retval = 0;
  int i;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
    else if (strcmp(argv[i], "-a") == 0)
      tstflags |= tst_abort;
    else
      usage(1);
  }

537
  retval |= test_triplet(); fflush(stdout);
538
  retval |= test_smooth_sort(); fflush(stdout);
539 540
  retval |= test_value(); fflush(stdout);
  retval |= test_ref(); fflush(stdout);
541 542 543

  return retval;
}