mirror of https://github.com/grpc/grpc.git
parent
ceb7318d79
commit
fba79f213f
17 changed files with 795 additions and 1 deletions
@ -0,0 +1,67 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* 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 Google Inc. 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 |
||||||
|
* OWNER 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPC_SUPPORT_AVL_H |
||||||
|
#define GRPC_SUPPORT_AVL_H |
||||||
|
|
||||||
|
#include <grpc/support/sync.h> |
||||||
|
|
||||||
|
typedef struct gpr_avl_node { |
||||||
|
gpr_refcount refs; |
||||||
|
void *key; |
||||||
|
void *value; |
||||||
|
struct gpr_avl_node *left; |
||||||
|
struct gpr_avl_node *right; |
||||||
|
long height; |
||||||
|
} gpr_avl_node; |
||||||
|
|
||||||
|
typedef struct gpr_avl_vtable { |
||||||
|
void (*destroy_key)(void *key); |
||||||
|
void *(*copy_key)(void *key); |
||||||
|
long (*compare_keys)(void *key1, void *key2); |
||||||
|
void (*destroy_value)(void *value); |
||||||
|
void *(*copy_value)(void *value); |
||||||
|
} gpr_avl_vtable; |
||||||
|
|
||||||
|
typedef struct gpr_avl { |
||||||
|
const gpr_avl_vtable *vtable; |
||||||
|
gpr_avl_node *root; |
||||||
|
} gpr_avl; |
||||||
|
|
||||||
|
gpr_avl gpr_avl_create(const gpr_avl_vtable *vtable); |
||||||
|
void gpr_avl_destroy(gpr_avl avl); |
||||||
|
gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value); |
||||||
|
gpr_avl gpr_avl_remove(gpr_avl avl, void *key); |
||||||
|
void *gpr_avl_get(gpr_avl avl, void *key); |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,233 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* 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 Google Inc. 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 |
||||||
|
* OWNER 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 <grpc/support/avl.h> |
||||||
|
|
||||||
|
#include <assert.h> |
||||||
|
#include <stdlib.h> |
||||||
|
|
||||||
|
#include <grpc/support/alloc.h> |
||||||
|
#include <grpc/support/string_util.h> |
||||||
|
#include <grpc/support/useful.h> |
||||||
|
|
||||||
|
gpr_avl gpr_avl_create(const gpr_avl_vtable *vtable) { |
||||||
|
gpr_avl out; |
||||||
|
out.vtable = vtable; |
||||||
|
out.root = NULL; |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *ref_node(gpr_avl_node *node) { |
||||||
|
if (node) { |
||||||
|
gpr_ref(&node->refs); |
||||||
|
} |
||||||
|
return node; |
||||||
|
} |
||||||
|
|
||||||
|
static void unref_node(const gpr_avl_vtable *vtable, gpr_avl_node *node) { |
||||||
|
if (node == NULL) { |
||||||
|
return; |
||||||
|
} |
||||||
|
if (gpr_unref(&node->refs)) { |
||||||
|
vtable->destroy_key(node->key); |
||||||
|
vtable->destroy_value(node->value); |
||||||
|
unref_node(vtable, node->left); |
||||||
|
unref_node(vtable, node->right); |
||||||
|
gpr_free(node); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static long node_height(gpr_avl_node *node) { |
||||||
|
return node == NULL ? 0 : node->height; |
||||||
|
} |
||||||
|
|
||||||
|
static long calculate_height(gpr_avl_node *node) { |
||||||
|
return node == NULL ? 0 : 1 + GPR_MAX(calculate_height(node->left), |
||||||
|
calculate_height(node->right)); |
||||||
|
} |
||||||
|
|
||||||
|
static void assert_invariants(gpr_avl_node *n) { |
||||||
|
if (n == NULL) return; |
||||||
|
assert_invariants(n->left); |
||||||
|
assert_invariants(n->right); |
||||||
|
assert(calculate_height(n) == n->height); |
||||||
|
assert(labs(node_height(n->left) - node_height(n->right)) <= 1); |
||||||
|
} |
||||||
|
|
||||||
|
gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left, |
||||||
|
gpr_avl_node *right) { |
||||||
|
gpr_avl_node *node = gpr_malloc(sizeof(*node)); |
||||||
|
gpr_ref_init(&node->refs, 1); |
||||||
|
node->key = key; |
||||||
|
node->value = value; |
||||||
|
node->left = left; |
||||||
|
node->right = right; |
||||||
|
node->height = 1 + GPR_MAX(node_height(left), node_height(right)); |
||||||
|
return node; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *get(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
||||||
|
void *key) { |
||||||
|
long cmp; |
||||||
|
|
||||||
|
if (node == NULL) { |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
cmp = vtable->compare_keys(node->key, key); |
||||||
|
if (cmp == 0) { |
||||||
|
return node; |
||||||
|
} else if (cmp > 0) { |
||||||
|
return get(vtable, node->left, key); |
||||||
|
} else { |
||||||
|
return get(vtable, node->right, key); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void *gpr_avl_get(gpr_avl avl, void *key) { |
||||||
|
gpr_avl_node *node = get(avl.vtable, avl.root, key); |
||||||
|
return node ? node->value : NULL; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *rotate_left(const gpr_avl_vtable *vtable, void *key, |
||||||
|
void *value, gpr_avl_node *left, |
||||||
|
gpr_avl_node *right) { |
||||||
|
gpr_avl_node *n = |
||||||
|
new_node(vtable->copy_key(right->key), vtable->copy_value(right->value), |
||||||
|
new_node(key, value, left, ref_node(right->left)), |
||||||
|
ref_node(right->right)); |
||||||
|
unref_node(vtable, right); |
||||||
|
return n; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *rotate_right(const gpr_avl_vtable *vtable, void *key, |
||||||
|
void *value, gpr_avl_node *left, |
||||||
|
gpr_avl_node *right) { |
||||||
|
gpr_avl_node *n = new_node( |
||||||
|
vtable->copy_key(left->key), vtable->copy_value(left->value), |
||||||
|
ref_node(left->left), new_node(key, value, ref_node(left->right), right)); |
||||||
|
unref_node(vtable, left); |
||||||
|
return n; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *rotate_left_right(const gpr_avl_vtable *vtable, void *key, |
||||||
|
void *value, gpr_avl_node *left, |
||||||
|
gpr_avl_node *right) { |
||||||
|
/* rotate_right(..., rotate_left(left), right) */ |
||||||
|
/* TODO(ctiller): elide first allocation */ |
||||||
|
gpr_avl_node *leftp = new_node( |
||||||
|
vtable->copy_key(left->right->key), |
||||||
|
vtable->copy_value(left->right->value), |
||||||
|
new_node(vtable->copy_key(left->key), vtable->copy_value(left->value), |
||||||
|
ref_node(left->left), ref_node(left->right->left)), |
||||||
|
ref_node(left->right->right)); |
||||||
|
gpr_avl_node *n = |
||||||
|
new_node(vtable->copy_key(leftp->key), vtable->copy_value(leftp->value), |
||||||
|
ref_node(leftp->left), |
||||||
|
new_node(key, value, ref_node(leftp->right), right)); |
||||||
|
unref_node(vtable, left); |
||||||
|
unref_node(vtable, leftp); |
||||||
|
return n; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *rotate_right_left(const gpr_avl_vtable *vtable, void *key, |
||||||
|
void *value, gpr_avl_node *left, |
||||||
|
gpr_avl_node *right) { |
||||||
|
/* rotate_left(..., left, rotate_right(right)) */ |
||||||
|
/* TODO(ctiller): elide first allocation */ |
||||||
|
gpr_avl_node *rightp = new_node( |
||||||
|
vtable->copy_key(right->left->key), |
||||||
|
vtable->copy_value(right->left->value), ref_node(right->left->left), |
||||||
|
new_node(vtable->copy_key(right->key), vtable->copy_key(right->value), |
||||||
|
ref_node(right->left->right), ref_node(right->right))); |
||||||
|
gpr_avl_node *n = |
||||||
|
new_node(vtable->copy_key(rightp->key), vtable->copy_value(rightp->value), |
||||||
|
new_node(key, value, left, ref_node(rightp->left)), |
||||||
|
ref_node(rightp->right)); |
||||||
|
unref_node(vtable, right); |
||||||
|
unref_node(vtable, rightp); |
||||||
|
return n; |
||||||
|
} |
||||||
|
|
||||||
|
static gpr_avl_node *add(const gpr_avl_vtable *vtable, gpr_avl_node *node, |
||||||
|
void *key, void *value) { |
||||||
|
long cmp; |
||||||
|
gpr_avl_node *l; |
||||||
|
gpr_avl_node *r; |
||||||
|
if (node == NULL) { |
||||||
|
return new_node(key, value, NULL, NULL); |
||||||
|
} |
||||||
|
cmp = vtable->compare_keys(node->key, key); |
||||||
|
if (cmp == 0) { |
||||||
|
return new_node(key, value, NULL, NULL); |
||||||
|
} |
||||||
|
l = node->left; |
||||||
|
r = node->right; |
||||||
|
if (cmp > 0) { |
||||||
|
l = add(vtable, l, key, value); |
||||||
|
ref_node(r); |
||||||
|
} else { |
||||||
|
r = add(vtable, r, key, value); |
||||||
|
ref_node(l); |
||||||
|
} |
||||||
|
|
||||||
|
key = vtable->copy_key(node->key); |
||||||
|
value = vtable->copy_value(node->value); |
||||||
|
|
||||||
|
switch (node_height(l) - node_height(r)) { |
||||||
|
case 2: |
||||||
|
if (node_height(l->left) - node_height(l->right) == 1) { |
||||||
|
return rotate_right(vtable, key, value, l, r); |
||||||
|
} else { |
||||||
|
return rotate_left_right(vtable, key, value, l, r); |
||||||
|
} |
||||||
|
case -2: |
||||||
|
if (node_height(r->left) - node_height(r->right) == 1) { |
||||||
|
return rotate_right_left(vtable, key, value, l, r); |
||||||
|
} else { |
||||||
|
return rotate_left(vtable, key, value, l, r); |
||||||
|
} |
||||||
|
default: |
||||||
|
return new_node(key, value, l, r); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value) { |
||||||
|
gpr_avl_node *old_root = avl.root; |
||||||
|
avl.root = add(avl.vtable, avl.root, key, value); |
||||||
|
assert_invariants(avl.root); |
||||||
|
unref_node(avl.vtable, old_root); |
||||||
|
return avl; |
||||||
|
} |
||||||
|
|
||||||
|
void gpr_avl_destroy(gpr_avl avl) { unref_node(avl.vtable, avl.root); } |
@ -0,0 +1,174 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* 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 Google Inc. 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 |
||||||
|
* OWNER 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 <grpc/support/avl.h> |
||||||
|
#include <grpc/support/alloc.h> |
||||||
|
#include <grpc/support/log.h> |
||||||
|
#include "test/core/util/test_config.h" |
||||||
|
|
||||||
|
static int *box(int x) { |
||||||
|
int *b = gpr_malloc(sizeof(*b)); |
||||||
|
*b = x; |
||||||
|
return b; |
||||||
|
} |
||||||
|
|
||||||
|
static long int_compare(void *int1, void *int2) { |
||||||
|
return (*(int *)int1) - (*(int *)int2); |
||||||
|
} |
||||||
|
static void *int_copy(void *p) { return box(*(int *)p); } |
||||||
|
|
||||||
|
static const gpr_avl_vtable int_int_vtable = {gpr_free, int_copy, int_compare, |
||||||
|
gpr_free, int_copy}; |
||||||
|
|
||||||
|
static void check_get(gpr_avl avl, int key, int value) { |
||||||
|
int *k = box(key); |
||||||
|
gpr_log(GPR_DEBUG, "check avl[%d] == %d", key, value); |
||||||
|
GPR_ASSERT(*(int *)gpr_avl_get(avl, k) == value); |
||||||
|
gpr_free(k); |
||||||
|
} |
||||||
|
|
||||||
|
static void check_negget(gpr_avl avl, int key) { |
||||||
|
int *k = box(key); |
||||||
|
gpr_log(GPR_DEBUG, "check avl[%d] == nil", key); |
||||||
|
GPR_ASSERT(gpr_avl_get(avl, k) == NULL); |
||||||
|
gpr_free(k); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_get(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_get"); |
||||||
|
avl = gpr_avl_add( |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), box(1), box(11)), |
||||||
|
box(2), box(22)), |
||||||
|
box(3), box(33)); |
||||||
|
check_get(avl, 1, 11); |
||||||
|
check_get(avl, 2, 22); |
||||||
|
check_get(avl, 3, 33); |
||||||
|
check_negget(avl, 4); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_ll(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_ll"); |
||||||
|
avl = gpr_avl_add( |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), box(5), box(1)), |
||||||
|
box(4), box(2)), |
||||||
|
box(3), box(3)); |
||||||
|
GPR_ASSERT(*(int *)avl.root->key == 4); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->key == 3); |
||||||
|
GPR_ASSERT(*(int *)avl.root->right->key == 5); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_lr(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_lr"); |
||||||
|
avl = gpr_avl_add( |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), box(5), box(1)), |
||||||
|
box(3), box(2)), |
||||||
|
box(4), box(3)); |
||||||
|
GPR_ASSERT(*(int *)avl.root->key == 4); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->key == 3); |
||||||
|
GPR_ASSERT(*(int *)avl.root->right->key == 5); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_rr(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_rr"); |
||||||
|
avl = gpr_avl_add( |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), box(3), box(1)), |
||||||
|
box(4), box(2)), |
||||||
|
box(5), box(3)); |
||||||
|
GPR_ASSERT(*(int *)avl.root->key == 4); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->key == 3); |
||||||
|
GPR_ASSERT(*(int *)avl.root->right->key == 5); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_rl(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_rl"); |
||||||
|
avl = gpr_avl_add( |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), box(3), box(1)), |
||||||
|
box(5), box(2)), |
||||||
|
box(4), box(3)); |
||||||
|
GPR_ASSERT(*(int *)avl.root->key == 4); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->key == 3); |
||||||
|
GPR_ASSERT(*(int *)avl.root->right->key == 5); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_unbalanced(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_unbalanced"); |
||||||
|
avl = gpr_avl_add( |
||||||
|
gpr_avl_add( |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), |
||||||
|
box(5), box(1)), |
||||||
|
box(4), box(2)), |
||||||
|
box(3), box(3)), |
||||||
|
box(2), box(4)), |
||||||
|
box(1), box(5)); |
||||||
|
GPR_ASSERT(*(int *)avl.root->key == 4); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->key == 2); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->left->key == 1); |
||||||
|
GPR_ASSERT(*(int *)avl.root->left->right->key == 3); |
||||||
|
GPR_ASSERT(*(int *)avl.root->right->key == 5); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
static void test_replace(void) { |
||||||
|
gpr_avl avl; |
||||||
|
gpr_log(GPR_DEBUG, "test_replace"); |
||||||
|
avl = |
||||||
|
gpr_avl_add(gpr_avl_add(gpr_avl_create(&int_int_vtable), box(1), box(1)), |
||||||
|
box(1), box(2)); |
||||||
|
check_get(avl, 1, 2); |
||||||
|
gpr_avl_destroy(avl); |
||||||
|
} |
||||||
|
|
||||||
|
int main(int argc, char *argv[]) { |
||||||
|
grpc_test_init(argc, argv); |
||||||
|
|
||||||
|
test_get(); |
||||||
|
test_ll(); |
||||||
|
test_lr(); |
||||||
|
test_rr(); |
||||||
|
test_rl(); |
||||||
|
test_unbalanced(); |
||||||
|
test_replace(); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,178 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||||
|
<Import Project="..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies.openssl.props" Condition="Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\1.0.2.3.props')" /> |
||||||
|
<ItemGroup Label="ProjectConfigurations"> |
||||||
|
<ProjectConfiguration Include="Debug|Win32"> |
||||||
|
<Configuration>Debug</Configuration> |
||||||
|
<Platform>Win32</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
<ProjectConfiguration Include="Debug|x64"> |
||||||
|
<Configuration>Debug</Configuration> |
||||||
|
<Platform>x64</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
<ProjectConfiguration Include="Release|Win32"> |
||||||
|
<Configuration>Release</Configuration> |
||||||
|
<Platform>Win32</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
<ProjectConfiguration Include="Release|x64"> |
||||||
|
<Configuration>Release</Configuration> |
||||||
|
<Platform>x64</Platform> |
||||||
|
</ProjectConfiguration> |
||||||
|
</ItemGroup> |
||||||
|
<PropertyGroup Label="Globals"> |
||||||
|
<ProjectGuid>{144D8CFF-2737-A18A-DCFD-01603533D63F}</ProjectGuid> |
||||||
|
</PropertyGroup> |
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> |
||||||
|
<PropertyGroup Condition="'$(VisualStudioVersion)' == '10.0'" Label="Configuration"> |
||||||
|
<PlatformToolset>v100</PlatformToolset> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(VisualStudioVersion)' == '11.0'" Label="Configuration"> |
||||||
|
<PlatformToolset>v110</PlatformToolset> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(VisualStudioVersion)' == '12.0'" Label="Configuration"> |
||||||
|
<PlatformToolset>v120</PlatformToolset> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration"> |
||||||
|
<ConfigurationType>Application</ConfigurationType> |
||||||
|
<UseDebugLibraries>true</UseDebugLibraries> |
||||||
|
<CharacterSet>Unicode</CharacterSet> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration"> |
||||||
|
<ConfigurationType>Application</ConfigurationType> |
||||||
|
<UseDebugLibraries>false</UseDebugLibraries> |
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization> |
||||||
|
<CharacterSet>Unicode</CharacterSet> |
||||||
|
</PropertyGroup> |
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> |
||||||
|
<ImportGroup Label="ExtensionSettings"> |
||||||
|
</ImportGroup> |
||||||
|
<ImportGroup Label="PropertySheets"> |
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\global.props" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\openssl.props" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\winsock.props" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\zlib.props" /> |
||||||
|
</ImportGroup> |
||||||
|
<PropertyGroup Label="UserMacros" /> |
||||||
|
<PropertyGroup Condition="'$(Configuration)'=='Debug'"> |
||||||
|
<TargetName>gpr_avl_test</TargetName> |
||||||
|
<Linkage-grpc_dependencies_zlib>static</Linkage-grpc_dependencies_zlib> |
||||||
|
<Configuration-grpc_dependencies_zlib>Debug</Configuration-grpc_dependencies_zlib> |
||||||
|
<Configuration-grpc_dependencies_openssl>Debug</Configuration-grpc_dependencies_openssl> |
||||||
|
</PropertyGroup> |
||||||
|
<PropertyGroup Condition="'$(Configuration)'=='Release'"> |
||||||
|
<TargetName>gpr_avl_test</TargetName> |
||||||
|
<Linkage-grpc_dependencies_zlib>static</Linkage-grpc_dependencies_zlib> |
||||||
|
<Configuration-grpc_dependencies_zlib>Debug</Configuration-grpc_dependencies_zlib> |
||||||
|
<Configuration-grpc_dependencies_openssl>Debug</Configuration-grpc_dependencies_openssl> |
||||||
|
</PropertyGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
||||||
|
<ClCompile> |
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<Optimization>Disabled</Optimization> |
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;_USE_32BIT_TIME_T;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> |
||||||
|
<TreatWarningAsError>true</TreatWarningAsError> |
||||||
|
<DebugInformationFormat Condition="$(Jenkins)">None</DebugInformationFormat> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<GenerateDebugInformation Condition="!$(Jenkins)">true</GenerateDebugInformation> |
||||||
|
<GenerateDebugInformation Condition="$(Jenkins)">false</GenerateDebugInformation> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
||||||
|
<ClCompile> |
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<Optimization>Disabled</Optimization> |
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> |
||||||
|
<TreatWarningAsError>true</TreatWarningAsError> |
||||||
|
<DebugInformationFormat Condition="$(Jenkins)">None</DebugInformationFormat> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<GenerateDebugInformation Condition="!$(Jenkins)">true</GenerateDebugInformation> |
||||||
|
<GenerateDebugInformation Condition="$(Jenkins)">false</GenerateDebugInformation> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
||||||
|
<ClCompile> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader> |
||||||
|
<Optimization>MaxSpeed</Optimization> |
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking> |
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions> |
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;_USE_32BIT_TIME_T;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> |
||||||
|
<TreatWarningAsError>true</TreatWarningAsError> |
||||||
|
<DebugInformationFormat Condition="$(Jenkins)">None</DebugInformationFormat> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<GenerateDebugInformation Condition="!$(Jenkins)">true</GenerateDebugInformation> |
||||||
|
<GenerateDebugInformation Condition="$(Jenkins)">false</GenerateDebugInformation> |
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
||||||
|
<OptimizeReferences>true</OptimizeReferences> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
||||||
|
<ClCompile> |
||||||
|
<WarningLevel>Level3</WarningLevel> |
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader> |
||||||
|
<Optimization>MaxSpeed</Optimization> |
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking> |
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions> |
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
||||||
|
<SDLCheck>true</SDLCheck> |
||||||
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> |
||||||
|
<TreatWarningAsError>true</TreatWarningAsError> |
||||||
|
<DebugInformationFormat Condition="$(Jenkins)">None</DebugInformationFormat> |
||||||
|
</ClCompile> |
||||||
|
<Link> |
||||||
|
<SubSystem>Console</SubSystem> |
||||||
|
<GenerateDebugInformation Condition="!$(Jenkins)">true</GenerateDebugInformation> |
||||||
|
<GenerateDebugInformation Condition="$(Jenkins)">false</GenerateDebugInformation> |
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
||||||
|
<OptimizeReferences>true</OptimizeReferences> |
||||||
|
</Link> |
||||||
|
</ItemDefinitionGroup> |
||||||
|
<ItemGroup> |
||||||
|
<ClCompile Include="..\..\..\..\test\core\support\avl_test.c"> |
||||||
|
</ClCompile> |
||||||
|
</ItemGroup> |
||||||
|
<ItemGroup> |
||||||
|
<ProjectReference Include="..\..\..\..\vsprojects\vcxproj\.\gpr_test_util\gpr_test_util.vcxproj"> |
||||||
|
<Project>{EAB0A629-17A9-44DB-B5FF-E91A721FE037}</Project> |
||||||
|
</ProjectReference> |
||||||
|
<ProjectReference Include="..\..\..\..\vsprojects\vcxproj\.\gpr\gpr.vcxproj"> |
||||||
|
<Project>{B23D3D1A-9438-4EDA-BEB6-9A0A03D17792}</Project> |
||||||
|
</ProjectReference> |
||||||
|
</ItemGroup> |
||||||
|
<ItemGroup> |
||||||
|
<None Include="packages.config" /> |
||||||
|
</ItemGroup> |
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> |
||||||
|
<ImportGroup Label="ExtensionTargets"> |
||||||
|
<Import Project="..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\native\grpc.dependencies.zlib.redist.targets" Condition="Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\native\grpc.dependencies\grpc.dependencies.zlib.targets')" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.1.2.8.9\build\native\grpc.dependencies.zlib.targets" Condition="Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.1.2.8.9\build\native\grpc.dependencies\grpc.dependencies.zlib.targets')" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.redist.1.0.2.3\build\native\grpc.dependencies.openssl.redist.targets" Condition="Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.redist.1.0.2.3\build\native\grpc.dependencies\grpc.dependencies.openssl.targets')" /> |
||||||
|
<Import Project="..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies.openssl.targets" Condition="Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies\grpc.dependencies.openssl.targets')" /> |
||||||
|
</ImportGroup> |
||||||
|
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> |
||||||
|
<PropertyGroup> |
||||||
|
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> |
||||||
|
</PropertyGroup> |
||||||
|
<Error Condition="!Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\native\grpc.dependencies.zlib.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\native\grpc.dependencies.zlib.redist.targets')" /> |
||||||
|
<Error Condition="!Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.1.2.8.9\build\native\grpc.dependencies.zlib.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\vsprojects\packages\grpc.dependencies.zlib.1.2.8.9\build\native\grpc.dependencies.zlib.targets')" /> |
||||||
|
<Error Condition="!Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.redist.1.0.2.3\build\native\grpc.dependencies.openssl.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.redist.1.0.2.3\build\native\grpc.dependencies.openssl.redist.targets')" /> |
||||||
|
<Error Condition="!Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies.openssl.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies.openssl.props')" /> |
||||||
|
<Error Condition="!Exists('..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies.openssl.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\vsprojects\packages\grpc.dependencies.openssl.1.0.2.3\build\native\grpc.dependencies.openssl.targets')" /> |
||||||
|
</Target> |
||||||
|
</Project> |
||||||
|
|
@ -0,0 +1,21 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||||
|
<ItemGroup> |
||||||
|
<ClCompile Include="..\..\..\..\test\core\support\avl_test.c"> |
||||||
|
<Filter>test\core\support</Filter> |
||||||
|
</ClCompile> |
||||||
|
</ItemGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<Filter Include="test"> |
||||||
|
<UniqueIdentifier>{36d067be-341d-9b6e-8ebc-bc48318fa916}</UniqueIdentifier> |
||||||
|
</Filter> |
||||||
|
<Filter Include="test\core"> |
||||||
|
<UniqueIdentifier>{4f3292f9-bb3f-e90c-324d-120ff96d394a}</UniqueIdentifier> |
||||||
|
</Filter> |
||||||
|
<Filter Include="test\core\support"> |
||||||
|
<UniqueIdentifier>{d02b711f-14e2-086b-d3a5-48e2d82145ee}</UniqueIdentifier> |
||||||
|
</Filter> |
||||||
|
</ItemGroup> |
||||||
|
</Project> |
||||||
|
|
Loading…
Reference in new issue