From 93216374efbabbf040eb2b52958acb8774c1110c Mon Sep 17 00:00:00 2001 From: LeonineKing1199 Date: Mon, 22 Nov 2021 13:30:10 -0800 Subject: [PATCH] Flesh out test suite to cover all permutations of transparent/non-transparent Hash & KeyEqual pairs --- test/unordered/transparent_tests.cpp | 186 +++++++++++++++++++++++---- 1 file changed, 163 insertions(+), 23 deletions(-) diff --git a/test/unordered/transparent_tests.cpp b/test/unordered/transparent_tests.cpp index b5cc5bfc..13779bd7 100644 --- a/test/unordered/transparent_tests.cpp +++ b/test/unordered/transparent_tests.cpp @@ -59,37 +59,177 @@ struct transparent_key_equal bool transparent_key_equal::was_called_; +struct hasher +{ + std::size_t operator()(key const& k) const + { + return boost::hash()(k.x_); + } +}; + +struct key_equal +{ + static bool was_called_; + + bool operator()(key const& k1, key const& k2) const { return k1.x_ == k2.x_; } + bool operator()(int const x, key const& k1) const + { + was_called_ = true; + return k1.x_ == x; + } +}; + +bool key_equal::was_called_; + UNORDERED_AUTO_TEST (unordered_map_transparent_count) { - key::count_ = 0; - transparent_key_equal::was_called_ = false; + { + // transparent Hash, transparent KeyEqual + // - boost::unordered_map map; + key::count_ = 0; + transparent_key_equal::was_called_ = false; + key_equal::was_called_ = false; - // initial `key(0)` expression increases the count - // then copying into the `unordered_map` increments the count again thus we - // have 2 - // - map[key(0)] = 1337; - BOOST_TEST(key::count_ == 2); + boost::unordered_map + map; - // now the number of `key` objects created should be a constant and never - // touched again - // - std::size_t count = 0; - count = map.count(0); + // initial `key(0)` expression increases the count + // then copying into the `unordered_map` increments the count again thus we + // have 2 + // + map[key(0)] = 1337; + BOOST_TEST(key::count_ == 2); - BOOST_TEST(count == 1); - BOOST_TEST(key::count_ == 2); - BOOST_TEST(map.key_eq().was_called_); + // now the number of `key` objects created should be a constant and never + // touched again + // + std::size_t count = 0; + count = map.count(0); - count = map.count(1); + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 2); + BOOST_TEST(map.key_eq().was_called_); - BOOST_TEST(count == 0); - BOOST_TEST(key::count_ == 2); + count = map.count(1); - count = map.count(key(0)); - BOOST_TEST(count == 1); - BOOST_TEST(key::count_ > 2); + BOOST_TEST(count == 0); + BOOST_TEST(key::count_ == 2); + + count = map.count(key(0)); + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ > 2); + } + + { + // non-transparent Hash, non-transparent KeyEqual + // + + key::count_ = 0; + transparent_key_equal::was_called_ = false; + key_equal::was_called_ = false; + + boost::unordered_map map; + + // initial `key(0)` expression increases the count + // then copying into the `unordered_map` increments the count again thus we + // have 2 + // + map[key(0)] = 1337; + BOOST_TEST(key::count_ == 2); + + // rely on the implicit constructor here to spawn a new object which + // increases the count + // + std::size_t count = 0; + count = map.count(0); + + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 3); + BOOST_TEST(!map.key_eq().was_called_); + + count = map.count(1); + + BOOST_TEST(count == 0); + BOOST_TEST(key::count_ == 4); + + count = map.count(key(0)); + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 5); + } + + { + // transparent Hash, non-transparent KeyEqual + // + + key::count_ = 0; + transparent_key_equal::was_called_ = false; + key_equal::was_called_ = false; + + boost::unordered_map map; + + // initial `key(0)` expression increases the count + // then copying into the `unordered_map` increments the count again thus we + // have 2 + // + map[key(0)] = 1337; + BOOST_TEST(key::count_ == 2); + + // rely on the implicit constructor here to spawn a new object which + // increases the count + // + std::size_t count = 0; + count = map.count(0); + + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 3); + BOOST_TEST(!map.key_eq().was_called_); + + count = map.count(1); + + BOOST_TEST(count == 0); + BOOST_TEST(key::count_ == 4); + + count = map.count(key(0)); + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 5); + } + + { + // non-transparent Hash, transparent KeyEqual + // + + key::count_ = 0; + transparent_key_equal::was_called_ = false; + key_equal::was_called_ = false; + + boost::unordered_map map; + + // initial `key(0)` expression increases the count + // then copying into the `unordered_map` increments the count again thus we + // have 2 + // + map[key(0)] = 1337; + BOOST_TEST(key::count_ == 2); + + // rely on the implicit constructor here to spawn a new object which + // increases the count + // + std::size_t count = 0; + count = map.count(0); + + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 3); + BOOST_TEST(!map.key_eq().was_called_); + + count = map.count(1); + + BOOST_TEST(count == 0); + BOOST_TEST(key::count_ == 4); + + count = map.count(key(0)); + BOOST_TEST(count == 1); + BOOST_TEST(key::count_ == 5); + } } RUN_TESTS()