blob: 0859eeb60d6c4a432f4dd4babf43f018a4e75beb [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
Robert Sloan69939df2017-01-09 10:53:07 -080015#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
Robert Sloan8ff03552017-06-14 12:40:58 -070028#include <gtest/gtest.h>
29
Robert Sloan69939df2017-01-09 10:53:07 -080030
31static std::unique_ptr<char[]> RandString(void) {
32 unsigned len = 1 + (rand() % 3);
33 std::unique_ptr<char[]> ret(new char[len + 1]);
34
35 for (unsigned i = 0; i < len; i++) {
36 ret[i] = '0' + (rand() & 7);
37 }
38 ret[len] = 0;
39
40 return ret;
41}
42
43struct FreeLHASH {
44 void operator()(_LHASH *lh) { lh_free(lh); }
45};
46
47static const char *Lookup(
48 std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
49 // Using operator[] implicitly inserts into the map.
50 auto iter = dummy_lh->find(key);
51 if (iter == dummy_lh->end()) {
52 return nullptr;
53 }
54 return iter->second.get();
55}
56
Robert Sloan8ff03552017-06-14 12:40:58 -070057TEST(LHashTest, Basic) {
Robert Sloan69939df2017-01-09 10:53:07 -080058 std::unique_ptr<_LHASH, FreeLHASH> lh(
59 lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
Robert Sloan8ff03552017-06-14 12:40:58 -070060 ASSERT_TRUE(lh);
Robert Sloan69939df2017-01-09 10:53:07 -080061
62 // lh is expected to store a canonical instance of each string. dummy_lh
63 // mirrors what it stores for comparison. It also manages ownership of the
64 // pointers.
65 std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
66
67 for (unsigned i = 0; i < 100000; i++) {
Robert Sloan8ff03552017-06-14 12:40:58 -070068 EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get()));
Robert Sloan69939df2017-01-09 10:53:07 -080069
70 // Check the entire contents and test |lh_doall_arg|. This takes O(N) time,
71 // so only do it every few iterations.
72 //
73 // TODO(davidben): |lh_doall_arg| also supports modifying the hash in the
74 // callback. Test this.
75 if (i % 1000 == 0) {
76 using ValueList = std::vector<const char *>;
77 ValueList expected, actual;
78 for (const auto &pair : dummy_lh) {
79 expected.push_back(pair.second.get());
80 }
81 std::sort(expected.begin(), expected.end());
82
83 lh_doall_arg(lh.get(),
84 [](void *ptr, void *arg) {
85 ValueList *out = reinterpret_cast<ValueList *>(arg);
86 out->push_back(reinterpret_cast<char *>(ptr));
87 },
88 &actual);
89 std::sort(actual.begin(), actual.end());
90
Robert Sloan8ff03552017-06-14 12:40:58 -070091 EXPECT_EQ(expected, actual);
Robert Sloan69939df2017-01-09 10:53:07 -080092 }
93
94 enum Action {
95 kRetrieve = 0,
96 kInsert,
97 kDelete,
98 };
99
100 Action action = static_cast<Action>(rand() % 3);
101 switch (action) {
102 case kRetrieve: {
103 std::unique_ptr<char[]> key = RandString();
104 void *value = lh_retrieve(lh.get(), key.get());
Robert Sloan8ff03552017-06-14 12:40:58 -0700105 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
Robert Sloan69939df2017-01-09 10:53:07 -0800106 break;
107 }
108
109 case kInsert: {
110 std::unique_ptr<char[]> key = RandString();
111 void *previous;
Robert Sloan8ff03552017-06-14 12:40:58 -0700112 ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get()));
113 EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
Robert Sloan69939df2017-01-09 10:53:07 -0800114 dummy_lh[key.get()] = std::move(key);
115 break;
116 }
117
118 case kDelete: {
119 std::unique_ptr<char[]> key = RandString();
120 void *value = lh_delete(lh.get(), key.get());
Robert Sloan8ff03552017-06-14 12:40:58 -0700121 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
Robert Sloan69939df2017-01-09 10:53:07 -0800122 dummy_lh.erase(key.get());
123 break;
124 }
Robert Sloan69939df2017-01-09 10:53:07 -0800125 }
126 }
Robert Sloan69939df2017-01-09 10:53:07 -0800127}