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
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
57TEST(LHashTest, Basic) {
58  std::unique_ptr<_LHASH, FreeLHASH> lh(
59      lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
60  ASSERT_TRUE(lh);
61
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++) {
68    EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get()));
69
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
91      EXPECT_EQ(expected, actual);
92    }
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());
105        EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
106        break;
107      }
108
109      case kInsert: {
110        std::unique_ptr<char[]> key = RandString();
111        void *previous;
112        ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get()));
113        EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
114        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());
121        EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
122        dummy_lh.erase(key.get());
123        break;
124      }
125    }
126  }
127}
128