Branch data Line data Source code
1 : : /******************************************************************************
2 : : * This file is part of the cvc5 project.
3 : : *
4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 : : * in the top-level source directory and their institutional affiliations.
6 : : * All rights reserved. See the file COPYING in the top-level source
7 : : * directory for licensing information.
8 : : * ****************************************************************************
9 : : *
10 : : * Black box testing of map utility functions.
11 : : */
12 : :
13 : : #include <map>
14 : : #include <set>
15 : : #include <string>
16 : : #include <unordered_map>
17 : : #include <unordered_set>
18 : :
19 : : #include "base/map_util.h"
20 : : #include "context/cdhashmap.h"
21 : : #include "context/cdhashset.h"
22 : : #include "context/cdinsert_hashmap.h"
23 : : #include "context/context.h"
24 : : #include "test.h"
25 : :
26 : : namespace cvc5::internal {
27 : :
28 : : using cvc5::context::CDHashMap;
29 : : using cvc5::context::CDInsertHashMap;
30 : : using cvc5::context::Context;
31 : :
32 : : namespace test {
33 : :
34 : : class TestBaseBlackMap : public TestInternal
35 : : {
36 : : protected:
37 : : /** Returns a map containing {"key"->"value", "other"->"entry"}. */
38 : 10 : static const std::map<std::string, std::string>& default_map()
39 : : {
40 : : static const std::map<std::string, std::string> static_stored_map{
41 : 12 : {"key", "value"}, {"other", "entry"}};
42 : 10 : return static_stored_map;
43 : : }
44 : :
45 : : /**
46 : : * For each <key, value> pair in source, inserts mapping from key to value
47 : : * using insert into dest.
48 : : */
49 : : template <class M>
50 : 4 : void insert_all(const std::map<std::string, std::string>& source, M* dest)
51 : : {
52 [ + + ]: 12 : for (const auto& key_value : source)
53 : : {
54 : 8 : dest->insert(key_value.first, key_value.second);
55 : : }
56 : 4 : }
57 : : };
58 : :
59 : 4 : TEST_F(TestBaseBlackMap, map)
60 : : {
61 : 1 : std::map<std::string, std::string> map = default_map();
62 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
63 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
64 : :
65 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
66 [ + - ]: 1 : if (std::string* found_value = FindOrNull(map, "other"))
67 : : {
68 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
69 : 1 : *found_value = "new value";
70 : : }
71 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "new value");
72 [ + - ]: 1 : }
73 : :
74 : 4 : TEST_F(TestBaseBlackMap, constant_map)
75 : : {
76 : 1 : const std::map<std::string, std::string> map = default_map();
77 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
78 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
79 : :
80 [ + - ]: 1 : if (const std::string* found_value = FindOrNull(map, "other"))
81 : : {
82 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
83 : : }
84 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
85 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "entry");
86 : 1 : ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key.");
87 [ + - ]: 1 : }
88 : :
89 : 4 : TEST_F(TestBaseBlackMap, unordered_map)
90 : : {
91 : 1 : std::unordered_map<std::string, std::string> map(default_map().begin(),
92 : 2 : default_map().end());
93 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
94 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
95 : :
96 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
97 [ + - ]: 1 : if (std::string* found_value = FindOrNull(map, "other"))
98 : : {
99 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
100 : 1 : *found_value = "new value";
101 : : }
102 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "new value");
103 [ + - ]: 1 : }
104 : :
105 : 4 : TEST_F(TestBaseBlackMap, const_unordered_map)
106 : : {
107 : 1 : const std::unordered_map<std::string, std::string> map(default_map().begin(),
108 : 2 : default_map().end());
109 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
110 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
111 : :
112 [ + - ]: 1 : if (const std::string* found_value = FindOrNull(map, "other"))
113 : : {
114 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
115 : : }
116 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
117 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "entry");
118 : 1 : ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key.");
119 [ + - ]: 1 : }
120 : :
121 : 4 : TEST_F(TestBaseBlackMap, set)
122 : : {
123 : 5 : std::set<std::string> set{"entry", "other"};
124 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(set, "entry"));
125 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(set, "non member"));
126 : :
127 : 5 : const std::set<std::string> const_set{"entry", "other"};
128 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(const_set, "entry"));
129 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(const_set, "non member"));
130 [ + - ]: 1 : }
131 : :
132 : 4 : TEST_F(TestBaseBlackMap, unordered_set)
133 : : {
134 : 5 : std::unordered_set<std::string> set{"entry", "other"};
135 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(set, "entry"));
136 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(set, "non member"));
137 : :
138 : 5 : const std::unordered_set<std::string> const_set{"entry", "other"};
139 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(const_set, "entry"));
140 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(const_set, "non member"));
141 [ + - ]: 1 : }
142 : :
143 : 4 : TEST_F(TestBaseBlackMap, CDHashMap)
144 : : {
145 : 1 : Context context;
146 : 1 : CDHashMap<std::string, std::string> map(&context);
147 : 1 : insert_all(default_map(), &map);
148 : :
149 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
150 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
151 : :
152 [ + - ]: 1 : if (const std::string* found_value = FindOrNull(map, "other"))
153 : : {
154 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
155 : : }
156 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
157 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "entry");
158 [ + - ][ + - ]: 1 : }
159 : :
160 : 4 : TEST_F(TestBaseBlackMap, const_CDHashMap)
161 : : {
162 : 1 : Context context;
163 : 1 : CDHashMap<std::string, std::string> store(&context);
164 : 1 : insert_all(default_map(), &store);
165 : 1 : const auto& map = store;
166 : :
167 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
168 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
169 : :
170 [ + - ]: 1 : if (const std::string* found_value = FindOrNull(map, "other"))
171 : : {
172 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
173 : : }
174 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
175 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "entry");
176 [ + - ][ + - ]: 1 : }
177 : :
178 : 4 : TEST_F(TestBaseBlackMap, CDInsertHashMap)
179 : : {
180 : 1 : Context context;
181 : 1 : CDInsertHashMap<std::string, std::string> map(&context);
182 : 1 : insert_all(default_map(), &map);
183 : :
184 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
185 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
186 : :
187 [ + - ]: 1 : if (const std::string* found_value = FindOrNull(map, "other"))
188 : : {
189 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
190 : : }
191 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
192 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "entry");
193 [ + - ][ + - ]: 1 : }
194 : :
195 : 4 : TEST_F(TestBaseBlackMap, const_CDInsertHashMap)
196 : : {
197 : 1 : Context context;
198 : 1 : CDInsertHashMap<std::string, std::string> store(&context);
199 : 1 : insert_all(default_map(), &store);
200 : 1 : const auto& map = store;
201 : :
202 [ - + ][ + - ]: 1 : ASSERT_TRUE(ContainsKey(map, "key"));
203 [ - + ][ + - ]: 1 : ASSERT_FALSE(ContainsKey(map, "non key"));
204 [ + - ]: 1 : if (const std::string* found_value = FindOrNull(map, "other"))
205 : : {
206 [ - + ][ + - ]: 1 : ASSERT_EQ(*found_value, "entry");
207 : : }
208 [ - + ][ + - ]: 1 : ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
209 [ - + ][ + - ]: 2 : ASSERT_EQ(FindOrDie(map, "other"), "entry");
210 [ + - ][ + - ]: 1 : }
211 : : } // namespace test
212 : : } // namespace cvc5::internal
|