blob: cc146e876c39a1b9498f9cd870bd8f10ee244e4b [file] [log] [blame]
Robert Sloan69939df2017-01-09 10:53:07 -08001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#if !defined(_POSIX_C_SOURCE)
16#define _POSIX_C_SOURCE 201410L
17#endif
18
Robert Sloan69939df2017-01-09 10:53:07 -080019#include <openssl/lhash.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <algorithm>
26#include <memory>
27#include <map>
28#include <string>
29#include <utility>
30#include <vector>
31
Robert Sloan8ff03552017-06-14 12:40:58 -070032#include <gtest/gtest.h>
33
Robert Sloan69939df2017-01-09 10:53:07 -080034
35static std::unique_ptr<char[]> RandString(void) {
36 unsigned len = 1 + (rand() % 3);
37 std::unique_ptr<char[]> ret(new char[len + 1]);
38
39 for (unsigned i = 0; i < len; i++) {
40 ret[i] = '0' + (rand() & 7);
41 }
42 ret[len] = 0;
43
44 return ret;
45}
46
47struct FreeLHASH {
48 void operator()(_LHASH *lh) { lh_free(lh); }
49};
50
51static const char *Lookup(
52 std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
53 // Using operator[] implicitly inserts into the map.
54 auto iter = dummy_lh->find(key);
55 if (iter == dummy_lh->end()) {
56 return nullptr;
57 }
58 return iter->second.get();
59}
60
Robert Sloan8ff03552017-06-14 12:40:58 -070061TEST(LHashTest, Basic) {
Robert Sloan69939df2017-01-09 10:53:07 -080062 std::unique_ptr<_LHASH, FreeLHASH> lh(
63 lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
Robert Sloan8ff03552017-06-14 12:40:58 -070064 ASSERT_TRUE(lh);
Robert Sloan69939df2017-01-09 10:53:07 -080065
66 // lh is expected to store a canonical instance of each string. dummy_lh
67 // mirrors what it stores for comparison. It also manages ownership of the
68 // pointers.
69 std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
70
71 for (unsigned i = 0; i < 100000; i++) {
Robert Sloan8ff03552017-06-14 12:40:58 -070072 EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get()));
Robert Sloan69939df2017-01-09 10:53:07 -080073
74 // Check the entire contents and test |lh_doall_arg|. This takes O(N) time,
75 // so only do it every few iterations.
76 //
77 // TODO(davidben): |lh_doall_arg| also supports modifying the hash in the
78 // callback. Test this.
79 if (i % 1000 == 0) {
80 using ValueList = std::vector<const char *>;
81 ValueList expected, actual;
82 for (const auto &pair : dummy_lh) {
83 expected.push_back(pair.second.get());
84 }
85 std::sort(expected.begin(), expected.end());
86
87 lh_doall_arg(lh.get(),
88 [](void *ptr, void *arg) {
89 ValueList *out = reinterpret_cast<ValueList *>(arg);
90 out->push_back(reinterpret_cast<char *>(ptr));
91 },
92 &actual);
93 std::sort(actual.begin(), actual.end());
94
Robert Sloan8ff03552017-06-14 12:40:58 -070095 EXPECT_EQ(expected, actual);
Robert Sloan69939df2017-01-09 10:53:07 -080096 }
97
98 enum Action {
99 kRetrieve = 0,
100 kInsert,
101 kDelete,
102 };
103
104 Action action = static_cast<Action>(rand() % 3);
105 switch (action) {
106 case kRetrieve: {
107 std::unique_ptr<char[]> key = RandString();
108 void *value = lh_retrieve(lh.get(), key.get());
Robert Sloan8ff03552017-06-14 12:40:58 -0700109 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
Robert Sloan69939df2017-01-09 10:53:07 -0800110 break;
111 }
112
113 case kInsert: {
114 std::unique_ptr<char[]> key = RandString();
115 void *previous;
Robert Sloan8ff03552017-06-14 12:40:58 -0700116 ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get()));
117 EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
Robert Sloan69939df2017-01-09 10:53:07 -0800118 dummy_lh[key.get()] = std::move(key);
119 break;
120 }
121
122 case kDelete: {
123 std::unique_ptr<char[]> key = RandString();
124 void *value = lh_delete(lh.get(), key.get());
Robert Sloan8ff03552017-06-14 12:40:58 -0700125 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
Robert Sloan69939df2017-01-09 10:53:07 -0800126 dummy_lh.erase(key.get());
127 break;
128 }
Robert Sloan69939df2017-01-09 10:53:07 -0800129 }
130 }
Robert Sloan69939df2017-01-09 10:53:07 -0800131}