| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | * | 
|  | 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 | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 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. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <gtest/gtest.h> | 
|  | 18 |  | 
|  | 19 | #include <search.h> | 
|  | 20 |  | 
|  | 21 | static int int_cmp(const void* lhs, const void* rhs) { | 
|  | 22 | return *reinterpret_cast<const int*>(rhs) - *reinterpret_cast<const int*>(lhs); | 
|  | 23 | } | 
|  | 24 |  | 
|  | 25 | TEST(search, lfind_lsearch) { | 
|  | 26 | int xs[10]; | 
|  | 27 | memset(xs, 0, sizeof(xs)); | 
|  | 28 | size_t x_size = 0; | 
|  | 29 |  | 
|  | 30 | int needle; | 
|  | 31 |  | 
|  | 32 | // lfind(3) can't find '2' in the empty table. | 
|  | 33 | needle = 2; | 
|  | 34 | ASSERT_EQ(nullptr, lfind(&needle, xs, &x_size, sizeof(xs[0]), int_cmp)); | 
|  | 35 | ASSERT_EQ(0U, x_size); | 
|  | 36 |  | 
|  | 37 | // lsearch(3) will add it. | 
|  | 38 | ASSERT_EQ(&xs[0], lsearch(&needle, xs, &x_size, sizeof(xs[0]), int_cmp)); | 
|  | 39 | ASSERT_EQ(2, xs[0]); | 
|  | 40 | ASSERT_EQ(1U, x_size); | 
|  | 41 |  | 
|  | 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); | 
|  | 45 |  | 
|  | 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); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | struct node { | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 52 | explicit node(const char* s) : s(strdup(s)) {} | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 53 |  | 
|  | 54 | char* s; | 
|  | 55 | }; | 
|  | 56 |  | 
|  | 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); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | static std::vector<std::string> g_nodes; | 
|  | 62 |  | 
|  | 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); | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | static size_t g_free_calls; | 
|  | 71 |  | 
|  | 72 | static void node_free(void* p) { | 
|  | 73 | node* n = reinterpret_cast<node*>(p); | 
|  | 74 | free(n->s); | 
|  | 75 | ++g_free_calls; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | TEST(search, tfind_tsearch_twalk_tdestroy) { | 
|  | 79 | void* root = nullptr; | 
|  | 80 |  | 
|  | 81 | node n1("z"); | 
|  | 82 | node n2("a"); | 
|  | 83 | node n3("m"); | 
|  | 84 |  | 
|  | 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)); | 
|  | 89 |  | 
|  | 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); | 
|  | 93 |  | 
|  | 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)); | 
|  | 98 |  | 
|  | 99 | // Add the other nodes. | 
|  | 100 | ASSERT_NE(nullptr, tsearch(&n2, &root, node_cmp)); | 
|  | 101 | ASSERT_NE(nullptr, tsearch(&n3, &root, node_cmp)); | 
|  | 102 |  | 
|  | 103 | // Use twalk(3) to iterate over the nodes. | 
|  | 104 | g_nodes.clear(); | 
|  | 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]); | 
|  | 110 |  | 
|  | 111 | // tdestroy(3) removes nodes under a node, calling our callback to destroy each one. | 
|  | 112 | g_free_calls = 0; | 
|  | 113 | tdestroy(root, node_free); | 
|  | 114 | ASSERT_EQ(3U, g_free_calls); | 
|  | 115 | } | 
|  | 116 |  | 
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 117 | TEST(search, tdestroy_null) { | 
|  | 118 | // It's okay to pass a null node, and your callback will not be called. | 
|  | 119 | tdestroy(nullptr, nullptr); | 
|  | 120 | } | 
|  | 121 |  | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 122 | struct pod_node { | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 123 | explicit pod_node(int i) : i(i) {} | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 124 | int i; | 
|  | 125 | }; | 
|  | 126 |  | 
|  | 127 | static int pod_node_cmp(const void* lhs, const void* rhs) { | 
|  | 128 | return reinterpret_cast<const pod_node*>(rhs)->i - reinterpret_cast<const pod_node*>(lhs)->i; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | TEST(search, tdelete) { | 
|  | 132 | void* root = nullptr; | 
|  | 133 |  | 
|  | 134 | pod_node n1(123); | 
|  | 135 | ASSERT_NE(nullptr, tsearch(&n1, &root, pod_node_cmp)); | 
|  | 136 |  | 
|  | 137 | // tdelete(3) leaks n1. | 
|  | 138 | pod_node not_there(456); | 
|  | 139 | ASSERT_EQ(nullptr, tdelete(¬_there, &root, pod_node_cmp)); | 
|  | 140 | ASSERT_NE(nullptr, tdelete(&n1, &root, pod_node_cmp)); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | struct q_node { | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 144 | explicit q_node(int i) : i(i) {} | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 145 |  | 
|  | 146 | q_node* next; | 
|  | 147 | q_node* prev; | 
|  | 148 |  | 
|  | 149 | int i; | 
|  | 150 | }; | 
|  | 151 |  | 
|  | 152 | TEST(search, insque_remque) { | 
|  | 153 | q_node zero(0); | 
|  | 154 | q_node one(1); | 
|  | 155 | q_node two(2); | 
|  | 156 |  | 
|  | 157 | // Linear (not circular). | 
|  | 158 |  | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 159 | insque(&zero, nullptr); | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 160 | insque(&one, &zero); | 
|  | 161 | insque(&two, &one); | 
|  | 162 |  | 
|  | 163 | int expected = 0; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 164 | for (q_node* q = &zero; q != nullptr; q = q->next) { | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 165 | ASSERT_EQ(expected, q->i); | 
|  | 166 | ++expected; | 
|  | 167 | } | 
|  | 168 | ASSERT_EQ(3, expected); | 
|  | 169 |  | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 170 | for (q_node* q = &two; q != nullptr; q = q->prev) { | 
| Elliott Hughes | b902641 | 2014-07-23 16:02:26 -0700 | [diff] [blame] | 171 | --expected; | 
|  | 172 | ASSERT_EQ(expected, q->i); | 
|  | 173 | } | 
|  | 174 | ASSERT_EQ(0, expected); | 
|  | 175 |  | 
|  | 176 | q_node* head = &zero; | 
|  | 177 |  | 
|  | 178 | remque(&one); | 
|  | 179 | ASSERT_EQ(0, head->i); | 
|  | 180 | ASSERT_EQ(2, head->next->i); | 
|  | 181 | ASSERT_EQ(nullptr, head->next->next); | 
|  | 182 |  | 
|  | 183 | remque(&two); | 
|  | 184 | ASSERT_EQ(0, head->i); | 
|  | 185 | ASSERT_EQ(nullptr, head->next); | 
|  | 186 |  | 
|  | 187 | remque(&zero); | 
|  | 188 |  | 
|  | 189 | // Circular. | 
|  | 190 |  | 
|  | 191 | zero.next = &zero; | 
|  | 192 | zero.prev = &zero; | 
|  | 193 |  | 
|  | 194 | insque(&one, &zero); | 
|  | 195 | insque(&two, &one); | 
|  | 196 |  | 
|  | 197 | ASSERT_EQ(0, head->i); | 
|  | 198 | ASSERT_EQ(1, head->next->i); | 
|  | 199 | ASSERT_EQ(2, head->next->next->i); | 
|  | 200 | ASSERT_EQ(0, head->next->next->next->i); | 
|  | 201 | ASSERT_EQ(1, head->next->next->next->next->i); | 
|  | 202 | ASSERT_EQ(2, head->next->next->next->next->next->i); | 
|  | 203 |  | 
|  | 204 | remque(&one); | 
|  | 205 | ASSERT_EQ(0, head->i); | 
|  | 206 | ASSERT_EQ(2, head->next->i); | 
|  | 207 | ASSERT_EQ(0, head->next->next->i); | 
|  | 208 | ASSERT_EQ(2, head->next->next->next->i); | 
|  | 209 |  | 
|  | 210 | remque(&two); | 
|  | 211 | ASSERT_EQ(0, head->i); | 
|  | 212 | ASSERT_EQ(0, head->next->i); | 
|  | 213 |  | 
|  | 214 | remque(&zero); | 
|  | 215 | } | 
| Elliott Hughes | 5702c6f | 2017-08-31 17:27:05 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | static void AssertEntry(ENTRY* e, const char* expected_key, const char* expected_data) { | 
|  | 218 | ASSERT_TRUE(e != nullptr); | 
|  | 219 | ASSERT_STREQ(expected_key, reinterpret_cast<char*>(e->key)); | 
|  | 220 | ASSERT_STREQ(expected_data, reinterpret_cast<char*>(e->data)); | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | TEST(search, hcreate_hsearch_hdestroy) { | 
|  | 224 | ASSERT_NE(0, hcreate(13)); | 
|  | 225 |  | 
|  | 226 | // Add some initial entries. | 
|  | 227 | ENTRY* e; | 
|  | 228 | e = hsearch(ENTRY{.key = const_cast<char*>("a"), .data = const_cast<char*>("A")}, ENTER); | 
|  | 229 | AssertEntry(e, "a", "A"); | 
|  | 230 | e = hsearch(ENTRY{.key = const_cast<char*>("aa"), .data = const_cast<char*>("B")}, ENTER); | 
|  | 231 | AssertEntry(e, "aa", "B"); | 
|  | 232 | e = hsearch(ENTRY{.key = const_cast<char*>("aaa"), .data = const_cast<char*>("C")}, ENTER); | 
|  | 233 | AssertEntry(e, "aaa", "C"); | 
|  | 234 |  | 
|  | 235 | // Check missing. | 
|  | 236 | e = hsearch(ENTRY{.key = const_cast<char*>("aaaa"), .data = nullptr}, FIND); | 
|  | 237 | ASSERT_FALSE(e != nullptr); | 
|  | 238 |  | 
|  | 239 | // Check present. | 
|  | 240 | e = hsearch(ENTRY{.key = const_cast<char*>("aa"), .data = nullptr}, FIND); | 
|  | 241 | AssertEntry(e, "aa", "B"); | 
|  | 242 |  | 
|  | 243 | // ENTER with an existing key just returns the existing ENTRY. | 
|  | 244 | e = hsearch(ENTRY{.key = const_cast<char*>("aa"), .data = const_cast<char*>("X")}, ENTER); | 
|  | 245 | AssertEntry(e, "aa", "B"); | 
|  | 246 | e->data = const_cast<char*>("X"); | 
|  | 247 |  | 
|  | 248 | // Check present and updated. | 
|  | 249 | e = hsearch(ENTRY{.key = const_cast<char*>("aa"), .data = nullptr}, FIND); | 
|  | 250 | AssertEntry(e, "aa", "X"); | 
|  | 251 | // But other entries stayed the same. | 
|  | 252 | e = hsearch(ENTRY{.key = const_cast<char*>("a"), .data = nullptr}, FIND); | 
|  | 253 | AssertEntry(e, "a", "A"); | 
|  | 254 | e = hsearch(ENTRY{.key = const_cast<char*>("aaa"), .data = nullptr}, FIND); | 
|  | 255 | AssertEntry(e, "aaa", "C"); | 
|  | 256 |  | 
|  | 257 | hdestroy(); | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | TEST(search, hcreate_r_hsearch_r_hdestroy_r) { | 
|  | 261 | hsearch_data h1 = {}; | 
|  | 262 | ASSERT_EQ(1, hcreate_r(13, &h1)); | 
|  | 263 |  | 
|  | 264 | hsearch_data h2 = {}; | 
|  | 265 | ASSERT_EQ(1, hcreate_r(128, &h2)); | 
|  | 266 |  | 
|  | 267 | // Add some initial entries. | 
|  | 268 | ENTRY* e; | 
|  | 269 | ASSERT_EQ(1, hsearch_r(ENTRY{.key = const_cast<char*>("a"), .data = const_cast<char*>("A")}, | 
|  | 270 | ENTER, &e, &h1)); | 
|  | 271 | AssertEntry(e, "a", "A"); | 
|  | 272 | ASSERT_EQ(1, hsearch_r(ENTRY{.key = const_cast<char*>("a"), .data = const_cast<char*>("B")}, | 
|  | 273 | ENTER, &e, &h2)); | 
|  | 274 | AssertEntry(e, "a", "B"); | 
|  | 275 |  | 
|  | 276 | // Check missing. | 
|  | 277 | errno = 0; | 
|  | 278 | ASSERT_EQ(0, hsearch_r(ENTRY{.key = const_cast<char*>("b"), .data = nullptr}, FIND, &e, &h1)); | 
|  | 279 | ASSERT_EQ(ESRCH, errno); | 
|  | 280 |  | 
|  | 281 | // Check present. | 
|  | 282 | ASSERT_EQ(1, hsearch_r(ENTRY{.key = const_cast<char*>("a"), .data = nullptr}, FIND, &e, &h1)); | 
|  | 283 | AssertEntry(e, "a", "A"); | 
|  | 284 | ASSERT_EQ(1, hsearch_r(ENTRY{.key = const_cast<char*>("a"), .data = nullptr}, FIND, &e, &h2)); | 
|  | 285 | AssertEntry(e, "a", "B"); | 
|  | 286 |  | 
|  | 287 | // Destroying one doesn't affect the other. | 
|  | 288 | hdestroy_r(&h1); | 
|  | 289 | ASSERT_EQ(1, hsearch_r(ENTRY{.key = const_cast<char*>("a"), .data = nullptr}, FIND, &e, &h2)); | 
|  | 290 | AssertEntry(e, "a", "B"); | 
|  | 291 | hdestroy_r(&h2); | 
|  | 292 | } | 
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 293 |  | 
|  | 294 | TEST(search, hsearch_resizing) { | 
|  | 295 | ASSERT_NE(0, hcreate(1)); | 
|  | 296 |  | 
|  | 297 | std::vector<char*> entries; | 
|  | 298 | // Add enough entries to ensure that we've had to resize. | 
|  | 299 | for (char ch = ' '; ch <= '~'; ++ch) { | 
|  | 300 | char* p; | 
|  | 301 | asprintf(&p, "%c", ch); | 
|  | 302 | ENTRY e; | 
|  | 303 | e.data = e.key = p; | 
|  | 304 | ASSERT_TRUE(hsearch(e, ENTER) != nullptr); | 
|  | 305 | entries.push_back(p); | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | // Check they're all there. | 
|  | 309 | for (auto& p : entries) { | 
|  | 310 | ENTRY* e = hsearch(ENTRY{.key = p, .data = nullptr}, FIND); | 
|  | 311 | AssertEntry(e, p, p); | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | for (auto& p : entries) free(p); | 
|  | 315 | } |