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