2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gtest/gtest.h>
21 static int int_cmp(const void* lhs, const void* rhs) {
22 return *reinterpret_cast<const int*>(rhs) - *reinterpret_cast<const int*>(lhs);
25 TEST(search, lfind_lsearch) {
27 memset(xs, 0, sizeof(xs));
32 // lfind(3) can't find '2' in the empty table.
34 ASSERT_EQ(nullptr, lfind(&needle, xs, &x_size, sizeof(xs[0]), int_cmp));
35 ASSERT_EQ(0U, x_size);
37 // lsearch(3) will add it.
38 ASSERT_EQ(&xs[0], lsearch(&needle, xs, &x_size, sizeof(xs[0]), int_cmp));
40 ASSERT_EQ(1U, x_size);
42 // And then lfind(3) can find it.
43 ASSERT_EQ(&xs[0], lfind(&needle, xs, &x_size, sizeof(xs[0]), int_cmp));
44 ASSERT_EQ(1U, x_size);
46 // Inserting a duplicate does nothing (but returns the existing element).
47 ASSERT_EQ(&xs[0], lsearch(&needle, xs, &x_size, sizeof(xs[0]), int_cmp));
48 ASSERT_EQ(1U, x_size);
52 node(const char* s) : s(strdup(s)) {}
57 static int node_cmp(const void* lhs, const void* rhs) {
58 return strcmp(reinterpret_cast<const node*>(lhs)->s, reinterpret_cast<const node*>(rhs)->s);
61 static std::vector<std::string> g_nodes;
63 static void node_walk(const void* p, VISIT order, int) {
64 const node* n = *reinterpret_cast<const node* const*>(p);
65 if (order == postorder || order == leaf) {
66 g_nodes.push_back(n->s);
70 static size_t g_free_calls;
72 static void node_free(void* p) {
73 node* n = reinterpret_cast<node*>(p);
78 TEST(search, tfind_tsearch_twalk_tdestroy) {
85 // tfind(3) can't find anything in the empty tree.
86 ASSERT_EQ(nullptr, tfind(&n1, &root, node_cmp));
87 ASSERT_EQ(nullptr, tfind(&n2, &root, node_cmp));
88 ASSERT_EQ(nullptr, tfind(&n3, &root, node_cmp));
90 // tsearch(3) inserts and returns a pointer to a new node.
91 void* i1 = tsearch(&n1, &root, node_cmp);
92 ASSERT_NE(nullptr, i1);
94 // ...which tfind(3) will then return.
95 ASSERT_EQ(i1, tfind(&n1, &root, node_cmp));
96 ASSERT_EQ(nullptr, tfind(&n2, &root, node_cmp));
97 ASSERT_EQ(nullptr, tfind(&n3, &root, node_cmp));
99 // Add the other nodes.
100 ASSERT_NE(nullptr, tsearch(&n2, &root, node_cmp));
101 ASSERT_NE(nullptr, tsearch(&n3, &root, node_cmp));
103 // Use twalk(3) to iterate over the nodes.
105 twalk(root, node_walk);
106 ASSERT_EQ(3U, g_nodes.size());
107 ASSERT_EQ("a", g_nodes[0]);
108 ASSERT_EQ("m", g_nodes[1]);
109 ASSERT_EQ("z", g_nodes[2]);
111 // tdestroy(3) removes nodes under a node, calling our callback to destroy each one.
113 tdestroy(root, node_free);
114 ASSERT_EQ(3U, g_free_calls);
118 pod_node(int i) : i(i) {}
122 static int pod_node_cmp(const void* lhs, const void* rhs) {
123 return reinterpret_cast<const pod_node*>(rhs)->i - reinterpret_cast<const pod_node*>(lhs)->i;
126 TEST(search, tdelete) {
127 void* root = nullptr;
130 ASSERT_NE(nullptr, tsearch(&n1, &root, pod_node_cmp));
132 // tdelete(3) leaks n1.
133 pod_node not_there(456);
134 ASSERT_EQ(nullptr, tdelete(¬_there, &root, pod_node_cmp));
135 ASSERT_NE(nullptr, tdelete(&n1, &root, pod_node_cmp));
139 q_node(int i) : i(i) {}
147 TEST(search, insque_remque) {
152 // Linear (not circular).
159 for (q_node* q = &zero; q != NULL; q = q->next) {
160 ASSERT_EQ(expected, q->i);
163 ASSERT_EQ(3, expected);
165 for (q_node* q = &two; q != NULL; q = q->prev) {
167 ASSERT_EQ(expected, q->i);
169 ASSERT_EQ(0, expected);
171 q_node* head = &zero;
174 ASSERT_EQ(0, head->i);
175 ASSERT_EQ(2, head->next->i);
176 ASSERT_EQ(nullptr, head->next->next);
179 ASSERT_EQ(0, head->i);
180 ASSERT_EQ(nullptr, head->next);
192 ASSERT_EQ(0, head->i);
193 ASSERT_EQ(1, head->next->i);
194 ASSERT_EQ(2, head->next->next->i);
195 ASSERT_EQ(0, head->next->next->next->i);
196 ASSERT_EQ(1, head->next->next->next->next->i);
197 ASSERT_EQ(2, head->next->next->next->next->next->i);
200 ASSERT_EQ(0, head->i);
201 ASSERT_EQ(2, head->next->i);
202 ASSERT_EQ(0, head->next->next->i);
203 ASSERT_EQ(2, head->next->next->next->i);
206 ASSERT_EQ(0, head->i);
207 ASSERT_EQ(0, head->next->i);