/* $OpenBSD: rb-test.c,v 1.4 2008/04/13 00:22:17 djm Exp $ */ /* * Copyright 2002 Niels Provos * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. */ #define RB_AUGMENT(_n) rb_augment(_n) #include #include #include #include #include #include #include struct node { RB_ENTRY(node) rb; RBT_ENTRY(node) rbt; unsigned int num; unsigned int rbt_sum; unsigned int rb_sum; int key; }; RB_HEAD(rb, node) rb_root; RBT_HEAD(rbt, node) rbt_root; static inline int compare(const struct node *a, const struct node *b) { if (a->key < b->key) return (-1); else if (a->key > b->key) return (1); return (0); } RB_PROTOTYPE(rb, node, rb, compare); RBT_PROTOTYPE(rbt, node, rbt, compare); static inline void rb_augment(struct node *n) { struct node *child; unsigned int sum = n->num; child = RB_LEFT(n, rb); if (child != NULL) sum += child->rb_sum; child = RB_RIGHT(n, rb); if (child != NULL) sum += child->rb_sum; n->rb_sum = sum; } static inline void rbt_augment(struct node *n) { struct node *child; unsigned int sum = n->num; child = RBT_LEFT(rbt, n); if (child != NULL) sum += child->rbt_sum; child = RBT_RIGHT(rbt, n); if (child != NULL) sum += child->rbt_sum; n->rbt_sum = sum; } RB_GENERATE(rb, node, rb, compare); RBT_GENERATE_AUGMENT(rbt, node, rbt, compare, rbt_augment); #define ITER 1500 #define MIN 5 #define MAX 5000 int main(int argc, char **argv) { struct node *tmp, *ins; int i, max, min; unsigned int sum = 0; int rv = 0; RBT_INIT(rbt, &rbt_root); RB_INIT(&rb_root); for (i = 0; i < ITER; i++) { tmp = malloc(sizeof(struct node)); if (tmp == NULL) err(1, "malloc"); tmp->num = arc4random() & 0xffff; tmp->rb_sum = tmp->num; tmp->rbt_sum = tmp->num; sum += tmp->num; do { tmp->key = arc4random_uniform(MAX-MIN); tmp->key += MIN; } while (RBT_FIND(rbt, &rbt_root, tmp) != NULL); if (i == 0) max = min = tmp->key; else { if (tmp->key > max) max = tmp->key; if (tmp->key < min) min = tmp->key; } if (RBT_INSERT(rbt, &rbt_root, tmp) != NULL) errx(1, "RBT_INSERT failed"); if (RB_INSERT(rb, &rb_root, tmp) != NULL) errx(1, "RB_INSERT failed"); } tmp = RB_ROOT(&rb_root); if (tmp->rb_sum != sum) { warnx("rb augment sum failure: %u != %u", tmp->rb_sum, sum); rv = 1; } tmp = RBT_ROOT(rbt, &rbt_root); if (tmp->rbt_sum != sum) { warnx("rbt augment sum failure: %u != %u", tmp->rbt_sum, sum); rv = 1; } ins = RBT_MIN(rbt, &rbt_root); if (ins->key != min) errx(1, "min does not match"); tmp = ins; ins = RBT_MAX(rbt, &rbt_root); if (ins->key != max) errx(1, "max does not match"); if (RBT_REMOVE(rbt, &rbt_root, tmp) != tmp) errx(1, "RB_REMOVE failed"); for (i = 0; i < ITER - 1; i++) { tmp = RBT_ROOT(rbt, &rbt_root); if (tmp == NULL) errx(1, "RB_ROOT error"); if (RBT_REMOVE(rbt, &rbt_root, tmp) != tmp) errx(1, "RB_REMOVE error"); free(tmp); } return (rv); }