lhash_test.cc
Go to the documentation of this file.
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 <openssl/mem.h>
29 
30 #include <gtest/gtest.h>
31 
32 #include "internal.h"
33 
34 
35 DEFINE_LHASH_OF(char)
36 
37 static std::unique_ptr<char[]> RandString(void) {
38  unsigned len = 1 + (rand() % 3);
39  std::unique_ptr<char[]> ret(new char[len + 1]);
40 
41  for (unsigned i = 0; i < len; i++) {
42  ret[i] = '0' + (rand() & 7);
43  }
44  ret[len] = 0;
45 
46  return ret;
47 }
48 
50  void operator()(LHASH_OF(char) *lh) { lh_char_free(lh); }
51 };
52 
53 static const char *Lookup(
54  std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
55  // Using operator[] implicitly inserts into the map.
56  auto iter = dummy_lh->find(key);
57  if (iter == dummy_lh->end()) {
58  return nullptr;
59  }
60  return iter->second.get();
61 }
62 
63 TEST(LHashTest, Basic) {
64  std::unique_ptr<LHASH_OF(char), FreeLHASH_OF_char> lh(
65  lh_char_new(OPENSSL_strhash, strcmp));
66  ASSERT_TRUE(lh);
67 
68  // lh is expected to store a canonical instance of each string. dummy_lh
69  // mirrors what it stores for comparison. It also manages ownership of the
70  // pointers.
71  std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
72 
73  for (unsigned i = 0; i < 100000; i++) {
74  EXPECT_EQ(dummy_lh.size(), lh_char_num_items(lh.get()));
75 
76  // Check the entire contents and test |lh_*_doall_arg|. This takes O(N)
77  // time, so only do it every few iterations.
78  //
79  // TODO(davidben): |lh_*_doall_arg| also supports modifying the hash in the
80  // callback. Test this.
81  if (i % 1000 == 0) {
82  using ValueList = std::vector<const char *>;
83  ValueList expected, actual;
84  for (const auto &pair : dummy_lh) {
85  expected.push_back(pair.second.get());
86  }
87  std::sort(expected.begin(), expected.end());
88 
89  lh_char_doall_arg(lh.get(),
90  [](char *ptr, void *arg) {
91  ValueList *out = reinterpret_cast<ValueList *>(arg);
92  out->push_back(ptr);
93  },
94  &actual);
95  std::sort(actual.begin(), actual.end());
96  EXPECT_EQ(expected, actual);
97  }
98 
99  enum Action {
100  kRetrieve = 0,
101  kInsert,
102  kDelete,
103  };
104 
105  Action action = static_cast<Action>(rand() % 3);
106  switch (action) {
107  case kRetrieve: {
108  std::unique_ptr<char[]> key = RandString();
109  char *value = lh_char_retrieve(lh.get(), key.get());
110  EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
111 
112  // Do the same lookup with |lh_char_retrieve_key|.
113  value = lh_char_retrieve_key(
114  lh.get(), &key, OPENSSL_strhash(key.get()),
115  [](const void *key_ptr, const char *data) -> int {
116  const char *key_data =
117  reinterpret_cast<const std::unique_ptr<char[]> *>(key_ptr)
118  ->get();
119  return strcmp(key_data, data);
120  });
121  EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
122  break;
123  }
124 
125  case kInsert: {
126  std::unique_ptr<char[]> key = RandString();
127  char *previous;
128  ASSERT_TRUE(lh_char_insert(lh.get(), &previous, key.get()));
129  EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
130  dummy_lh[key.get()] = std::move(key);
131  break;
132  }
133 
134  case kDelete: {
135  std::unique_ptr<char[]> key = RandString();
136  char *value = lh_char_delete(lh.get(), key.get());
137  EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
138  dummy_lh.erase(key.get());
139  break;
140  }
141  }
142  }
143 }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
LHASH_OF
#define LHASH_OF(type)
Definition: lhash.h:73
generate_changelog.previous
previous
Definition: bloaty/third_party/protobuf/generate_changelog.py:55
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
FreeLHASH_OF_char
Definition: lhash_test.cc:49
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
FreeLHASH_OF_char::operator()
void operator()(LHASH_OF(char) *lh)
Definition: lhash_test.cc:50
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
RandString
static std::unique_ptr< char[]> RandString(void)
Definition: lhash_test.cc:37
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
OPENSSL_strhash
#define OPENSSL_strhash
Definition: boringssl_prefix_symbols.h:1892
TEST
TEST(LHashTest, Basic)
Definition: lhash_test.cc:63
arg
Definition: cmdline.cc:40
lhash.h
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
value
const char * value
Definition: hpack_parser_table.cc:165
internal.h
key
const char * key
Definition: hpack_parser_table.cc:164
client.action
action
Definition: examples/python/xds/client.py:49
DEFINE_LHASH_OF
#define DEFINE_LHASH_OF(type)
Definition: third_party/boringssl-with-bazel/src/crypto/lhash/internal.h:159
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
iter
Definition: test_winkernel.cpp:47
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
Lookup
static const char * Lookup(std::map< std::string, std::unique_ptr< char[]>> *dummy_lh, const char *key)
Definition: lhash_test.cc:53
pair
std::pair< std::string, std::string > pair
Definition: abseil-cpp/absl/container/internal/raw_hash_set_benchmark.cc:78
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:15