Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 28 | #include <gtest/gtest.h> |
| 29 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 30 | |
| 31 | static 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 | |
| 43 | struct FreeLHASH { |
| 44 | void operator()(_LHASH *lh) { lh_free(lh); } |
| 45 | }; |
| 46 | |
| 47 | static 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 Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 57 | TEST(LHashTest, Basic) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 58 | std::unique_ptr<_LHASH, FreeLHASH> lh( |
| 59 | lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp)); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 60 | ASSERT_TRUE(lh); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 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++) { |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 68 | EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get())); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 91 | EXPECT_EQ(expected, actual); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 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()); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 105 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), value); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | |
| 109 | case kInsert: { |
| 110 | std::unique_ptr<char[]> key = RandString(); |
| 111 | void *previous; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get())); |
| 113 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 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()); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 121 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), value); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 122 | dummy_lh.erase(key.get()); |
| 123 | break; |
| 124 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 127 | } |