• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if !defined(_POSIX_C_SOURCE)
16 #define _POSIX_C_SOURCE 201410L
17 #endif
18 
19 #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 
32 #include <gtest/gtest.h>
33 
34 
RandString(void)35 static 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 
47 struct FreeLHASH {
operator ()FreeLHASH48   void operator()(_LHASH *lh) { lh_free(lh); }
49 };
50 
Lookup(std::map<std::string,std::unique_ptr<char[]>> * dummy_lh,const char * key)51 static 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 
TEST(LHashTest,Basic)61 TEST(LHashTest, Basic) {
62   std::unique_ptr<_LHASH, FreeLHASH> lh(
63       lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
64   ASSERT_TRUE(lh);
65 
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++) {
72     EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get()));
73 
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 
95       EXPECT_EQ(expected, actual);
96     }
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());
109         EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
110         break;
111       }
112 
113       case kInsert: {
114         std::unique_ptr<char[]> key = RandString();
115         void *previous;
116         ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get()));
117         EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
118         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());
125         EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
126         dummy_lh.erase(key.get());
127         break;
128       }
129     }
130   }
131 }
132