/********************* */ /*! \file map_util_black.cpp ** \verbatim ** Top contributors (to current version): ** Aina Niemetz, Tim King ** This file is part of the CVC4 project. ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS ** in the top-level source directory and their institutional affiliations. ** All rights reserved. See the file COPYING in the top-level source ** directory for licensing information.\endverbatim ** ** \brief Black box testing of map utility functions. ** ** Black box testing of map utility functions. **/ #include #include #include #include #include #include "base/map_util.h" #include "context/cdhashmap.h" #include "context/cdhashset.h" #include "context/cdinsert_hashmap.h" #include "context/context.h" #include "test.h" namespace CVC5 { using context::CDHashMap; using context::CDInsertHashMap; using context::Context; namespace test { class TestBaseBlackMap : public TestInternal { protected: /** Returns a map containing {"key"->"value", "other"->"entry"}. */ static const std::map& default_map() { static const std::map static_stored_map{ {"key", "value"}, {"other", "entry"}}; return static_stored_map; } /** * For each pair in source, inserts mapping from key to value * using insert into dest. */ template void insert_all(const std::map& source, M* dest) { for (const auto& key_value : source) { dest->insert(key_value.first, key_value.second); } } }; TEST_F(TestBaseBlackMap, map) { std::map map = default_map(); ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); ASSERT_EQ(FindOrNull(map, "non key"), nullptr); if (std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); *found_value = "new value"; } ASSERT_EQ(FindOrDie(map, "other"), "new value"); } TEST_F(TestBaseBlackMap, constant_map) { const std::map map = default_map(); ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); if (const std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); } ASSERT_EQ(FindOrNull(map, "non key"), nullptr); ASSERT_EQ(FindOrDie(map, "other"), "entry"); ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key."); } TEST_F(TestBaseBlackMap, unordered_map) { std::unordered_map map(default_map().begin(), default_map().end()); ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); ASSERT_EQ(FindOrNull(map, "non key"), nullptr); if (std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); *found_value = "new value"; } ASSERT_EQ(FindOrDie(map, "other"), "new value"); } TEST_F(TestBaseBlackMap, const_unordered_map) { const std::unordered_map map(default_map().begin(), default_map().end()); ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); if (const std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); } ASSERT_EQ(FindOrNull(map, "non key"), nullptr); ASSERT_EQ(FindOrDie(map, "other"), "entry"); ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key."); } TEST_F(TestBaseBlackMap, set) { std::set set{"entry", "other"}; ASSERT_TRUE(CVC5::ContainsKey(set, "entry")); ASSERT_FALSE(CVC5::ContainsKey(set, "non member")); const std::set const_set{"entry", "other"}; ASSERT_TRUE(CVC5::ContainsKey(const_set, "entry")); ASSERT_FALSE(CVC5::ContainsKey(const_set, "non member")); } TEST_F(TestBaseBlackMap, unordered_set) { std::unordered_set set{"entry", "other"}; ASSERT_TRUE(CVC5::ContainsKey(set, "entry")); ASSERT_FALSE(CVC5::ContainsKey(set, "non member")); const std::unordered_set const_set{"entry", "other"}; ASSERT_TRUE(CVC5::ContainsKey(const_set, "entry")); ASSERT_FALSE(CVC5::ContainsKey(const_set, "non member")); } TEST_F(TestBaseBlackMap, CDHashMap) { Context context; CDHashMap map(&context); insert_all(default_map(), &map); ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); if (const std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); } ASSERT_EQ(FindOrNull(map, "non key"), nullptr); ASSERT_EQ(FindOrDie(map, "other"), "entry"); } TEST_F(TestBaseBlackMap, const_CDHashMap) { Context context; CDHashMap store(&context); insert_all(default_map(), &store); const auto& map = store; ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); if (const std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); } ASSERT_EQ(FindOrNull(map, "non key"), nullptr); ASSERT_EQ(FindOrDie(map, "other"), "entry"); } TEST_F(TestBaseBlackMap, CDInsertHashMap) { Context context; CDInsertHashMap map(&context); insert_all(default_map(), &map); ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); if (const std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); } ASSERT_EQ(FindOrNull(map, "non key"), nullptr); ASSERT_EQ(FindOrDie(map, "other"), "entry"); } TEST_F(TestBaseBlackMap, const_CDInsertHashMap) { Context context; CDInsertHashMap store(&context); insert_all(default_map(), &store); const auto& map = store; ASSERT_TRUE(CVC5::ContainsKey(map, "key")); ASSERT_FALSE(CVC5::ContainsKey(map, "non key")); if (const std::string* found_value = FindOrNull(map, "other")) { ASSERT_EQ(*found_value, "entry"); } ASSERT_EQ(FindOrNull(map, "non key"), nullptr); ASSERT_EQ(FindOrDie(map, "other"), "entry"); } } // namespace test } // namespace CVC5