File: u-reftable-tree.c

package info (click to toggle)
git 1%3A2.50.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 61,696 kB
  • sloc: ansic: 302,907; sh: 260,696; perl: 27,874; tcl: 22,303; makefile: 4,280; python: 3,442; javascript: 772; csh: 45; lisp: 12
file content (78 lines) | stat: -rw-r--r-- 1,702 bytes parent folder | download
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
/*
Copyright 2020 Google LLC

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/

#include "unit-test.h"
#include "reftable/tree.h"

static int t_compare(const void *a, const void *b)
{
	return (char *)a - (char *)b;
}

struct curry {
	void **arr;
	size_t len;
};

static void store(void *arg, void *key)
{
	struct curry *c = arg;
	c->arr[c->len++] = key;
}

void test_reftable_tree__tree_search(void)
{
	struct tree_node *root = NULL;
	void *values[11] = { 0 };
	struct tree_node *nodes[11] = { 0 };
	size_t i = 1;

	/*
	 * Pseudo-randomly insert the pointers for elements between
	 * values[1] and values[10] (inclusive) in the tree.
	 */
	do {
		nodes[i] = tree_insert(&root, &values[i], &t_compare);
		cl_assert(nodes[i] != NULL);
		i = (i * 7) % 11;
	} while (i != 1);

	for (i = 1; i < ARRAY_SIZE(nodes); i++) {
		cl_assert_equal_p(&values[i], nodes[i]->key);
		cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare));
	}

	cl_assert(tree_search(root, values, t_compare) == NULL);
	tree_free(root);
}

void test_reftable_tree__infix_walk(void)
{
	struct tree_node *root = NULL;
	void *values[11] = { 0 };
	void *out[11] = { 0 };
	struct curry c = {
		.arr = (void **) &out,
	};
	size_t i = 1;
	size_t count = 0;

	do {
		struct tree_node *node = tree_insert(&root, &values[i], t_compare);
		cl_assert(node != NULL);
		i = (i * 7) % 11;
		count++;
	} while (i != 1);

	infix_walk(root, &store, &c);
	for (i = 1; i < ARRAY_SIZE(values); i++)
		cl_assert_equal_p(&values[i], out[i - 1]);
	cl_assert(out[i - 1] == NULL);
	cl_assert_equal_i(c.len, count);
	tree_free(root);
}