diff options
author | 2015-08-27 09:04:18 -0700 | |
---|---|---|
committer | 2015-08-28 15:31:57 -0700 | |
commit | cf7792d4789b98e4a44674f1b0a96c5e63349a85 (patch) | |
tree | 5f4b2d8129871c6f0972708d395471115c6e24f1 | |
parent | ce209462cc1a7ce235e5ac0d0e6db6b402f73441 (diff) |
Test HashSet lookup by alternate key type.
The test case demonstrates and verifies looking up a HashSet element
by an alternate key type.
Change-Id: I833d572fb6105bf4d7c343ce50de873f27e4311f
-rw-r--r-- | runtime/base/hash_set_test.cc | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/runtime/base/hash_set_test.cc b/runtime/base/hash_set_test.cc index 4ef1f9e8c9..6d2c5e0f2c 100644 --- a/runtime/base/hash_set_test.cc +++ b/runtime/base/hash_set_test.cc @@ -17,9 +17,11 @@ #include "hash_set.h" #include <map> +#include <forward_list> #include <sstream> #include <string> #include <unordered_set> +#include <vector> #include <gtest/gtest.h> #include "hash_map.h" @@ -258,4 +260,59 @@ TEST_F(HashSetTest, TestHashMap) { ASSERT_EQ(it->second, 124); } +struct IsEmptyFnVectorInt { + void MakeEmpty(std::vector<int>& item) const { + item.clear(); + } + bool IsEmpty(const std::vector<int>& item) const { + return item.empty(); + } +}; + +template <typename T> +size_t HashIntSequence(T begin, T end) { + size_t hash = 0; + for (auto iter = begin; iter != end; ++iter) { + hash = hash * 2 + *iter; + } + return hash; +}; + +struct VectorIntHashEquals { + std::size_t operator()(const std::vector<int>& item) const { + return HashIntSequence(item.begin(), item.end()); + } + + std::size_t operator()(const std::forward_list<int>& item) const { + return HashIntSequence(item.begin(), item.end()); + } + + bool operator()(const std::vector<int>& a, const std::vector<int>& b) const { + return a == b; + } + + bool operator()(const std::vector<int>& a, const std::forward_list<int>& b) const { + auto aiter = a.begin(); + auto biter = b.begin(); + while (aiter != a.end() && biter != b.end()) { + if (*aiter != *biter) { + return false; + } + aiter++; + biter++; + } + return (aiter == a.end() && biter == b.end()); + } +}; + +TEST_F(HashSetTest, TestLookupByAlternateKeyType) { + HashSet<std::vector<int>, IsEmptyFnVectorInt, VectorIntHashEquals, VectorIntHashEquals> hash_set; + hash_set.Insert(std::vector<int>({1, 2, 3, 4})); + hash_set.Insert(std::vector<int>({4, 2})); + ASSERT_EQ(hash_set.end(), hash_set.Find(std::vector<int>({1, 1, 1, 1}))); + ASSERT_NE(hash_set.end(), hash_set.Find(std::vector<int>({1, 2, 3, 4}))); + ASSERT_EQ(hash_set.end(), hash_set.Find(std::forward_list<int>({1, 1, 1, 1}))); + ASSERT_NE(hash_set.end(), hash_set.Find(std::forward_list<int>({1, 2, 3, 4}))); +} + } // namespace art |