1 /* 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 #include <openssl/lhash.h>
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <algorithm>
22 #include <memory>
23 #include <map>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include <gtest/gtest.h>
29
30
DEFINE_LHASH_OF(char)31 DEFINE_LHASH_OF(char)
32
33 static std::unique_ptr<char[]> RandString(void) {
34 unsigned len = 1 + (rand() % 3);
35 std::unique_ptr<char[]> ret(new char[len + 1]);
36
37 for (unsigned i = 0; i < len; i++) {
38 ret[i] = '0' + (rand() & 7);
39 }
40 ret[len] = 0;
41
42 return ret;
43 }
44
45 struct FreeLHASH_OF_char {
operator ()FreeLHASH_OF_char46 void operator()(LHASH_OF(char) *lh) { lh_char_free(lh); }
47 };
48
Lookup(std::map<std::string,std::unique_ptr<char[]>> * dummy_lh,const char * key)49 static const char *Lookup(
50 std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
51 // Using operator[] implicitly inserts into the map.
52 auto iter = dummy_lh->find(key);
53 if (iter == dummy_lh->end()) {
54 return nullptr;
55 }
56 return iter->second.get();
57 }
58
TEST(LHashTest,Basic)59 TEST(LHashTest, Basic) {
60 std::unique_ptr<LHASH_OF(char), FreeLHASH_OF_char> lh(
61 lh_char_new(lh_strhash, strcmp));
62 ASSERT_TRUE(lh);
63
64 // lh is expected to store a canonical instance of each string. dummy_lh
65 // mirrors what it stores for comparison. It also manages ownership of the
66 // pointers.
67 std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
68
69 for (unsigned i = 0; i < 100000; i++) {
70 EXPECT_EQ(dummy_lh.size(), lh_char_num_items(lh.get()));
71
72 // Check the entire contents and test |lh_*_doall_arg|. This takes O(N)
73 // time, so only do it every few iterations.
74 //
75 // TODO(davidben): |lh_*_doall_arg| also supports modifying the hash in the
76 // callback. Test this.
77 if (i % 1000 == 0) {
78 using ValueList = std::vector<const char *>;
79 ValueList expected, actual;
80 for (const auto &pair : dummy_lh) {
81 expected.push_back(pair.second.get());
82 }
83 std::sort(expected.begin(), expected.end());
84
85 lh_char_doall_arg(lh.get(),
86 [](char *ptr, void *arg) {
87 ValueList *out = reinterpret_cast<ValueList *>(arg);
88 out->push_back(ptr);
89 },
90 &actual);
91 std::sort(actual.begin(), actual.end());
92 EXPECT_EQ(expected, actual);
93
94 // Also test |lh_*_doall|.
95 actual.clear();
96 static ValueList *global_actual_list;
97 global_actual_list = &actual;
98 lh_char_doall(lh.get(),
99 [](char *ptr) { global_actual_list->push_back(ptr); });
100 std::sort(actual.begin(), actual.end());
101 EXPECT_EQ(expected, actual);
102 }
103
104 enum Action {
105 kRetrieve = 0,
106 kInsert,
107 kDelete,
108 };
109
110 Action action = static_cast<Action>(rand() % 3);
111 switch (action) {
112 case kRetrieve: {
113 std::unique_ptr<char[]> key = RandString();
114 char *value = lh_char_retrieve(lh.get(), key.get());
115 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
116
117 // Do the same lookup with |lh_char_retrieve_key|.
118 value = lh_char_retrieve_key(
119 lh.get(), &key, lh_strhash(key.get()),
120 [](const void *key_ptr, const char *data) -> int {
121 const char *key_data =
122 reinterpret_cast<const std::unique_ptr<char[]> *>(key_ptr)
123 ->get();
124 return strcmp(key_data, data);
125 });
126 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
127 break;
128 }
129
130 case kInsert: {
131 std::unique_ptr<char[]> key = RandString();
132 char *previous;
133 ASSERT_TRUE(lh_char_insert(lh.get(), &previous, key.get()));
134 EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
135 dummy_lh[key.get()] = std::move(key);
136 break;
137 }
138
139 case kDelete: {
140 std::unique_ptr<char[]> key = RandString();
141 char *value = lh_char_delete(lh.get(), key.get());
142 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
143 dummy_lh.erase(key.get());
144 break;
145 }
146 }
147 }
148 }
149