aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/stllike
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@broadpark.no>2018-05-26 12:30:21 +0200
committerTor Egge <Tor.Egge@broadpark.no>2018-05-27 17:26:01 +0200
commit8db8e842c9653a0ce9c98e6bd055ead125517b93 (patch)
treef8a4898b86bc7e96f3499fccdb6e53af6b7ad7cd /vespalib/src/tests/stllike
parent2b33daa972f399b7734215ac03d8102cb68d7c1b (diff)
Handle insertion of non-copyable keys and values in hash map.
Handle insertion of non-copyable keys in hash set.
Diffstat (limited to 'vespalib/src/tests/stllike')
-rw-r--r--vespalib/src/tests/stllike/hash_test.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/vespalib/src/tests/stllike/hash_test.cpp b/vespalib/src/tests/stllike/hash_test.cpp
index 94e214e9fb9..366111cad0d 100644
--- a/vespalib/src/tests/stllike/hash_test.cpp
+++ b/vespalib/src/tests/stllike/hash_test.cpp
@@ -434,6 +434,45 @@ TEST("test that for_each member works as std::for_each") {
TEST_DO(verify_sum(m, expected_sum));
}
+namespace {
+
+class WrappedKey
+{
+ std::unique_ptr<const int> _key;
+public:
+ WrappedKey() : _key() { }
+ WrappedKey(int key) : _key(std::make_unique<const int>(key)) { }
+ size_t hash() const { return vespalib::hash<int>()(*_key); }
+ bool operator==(const WrappedKey &rhs) const { return *_key == *rhs._key; }
+};
+
+}
+
+TEST("test that hash map can have non-copyable key")
+{
+ hash_map<WrappedKey, int> m;
+ EXPECT_TRUE(m.insert(std::make_pair(WrappedKey(4), 5)).second);
+ WrappedKey testKey(4);
+ ASSERT_TRUE(m.find(testKey) != m.end());
+ EXPECT_EQUAL(5, m.find(testKey)->second);
+}
+
+TEST("test that hash map can have non-copyable value")
+{
+ hash_map<int, std::unique_ptr<int>> m;
+ EXPECT_TRUE(m.insert(std::make_pair(4, std::make_unique<int>(5))).second);
+ EXPECT_TRUE(m[4]);
+ EXPECT_EQUAL(5, *m[4]);
+}
+
+TEST("test that hash set can have non-copyable key")
+{
+ hash_set<WrappedKey> m;
+ EXPECT_TRUE(m.insert(WrappedKey(4)).second);
+ WrappedKey testKey(4);
+ ASSERT_TRUE(m.find(testKey) != m.end());
+}
+
using IntHashSet = hash_set<int>;
TEST("test hash set initializer list - empty")