1 /********************* */
2 /*! \file map_util_black.h
4 ** Top contributors (to current version):
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
12 ** \brief Black box testing of map utility functions.
14 ** Black box testing of map utility functions.
17 #include <cxxtest/TestSuite.h>
21 #include <unordered_map>
22 #include <unordered_set>
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"
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
;
38 class MapUtilBlack
: public CxxTest::TestSuite
41 void setUp() override
{}
42 void tearDown() override
{}
44 // Returns a map containing {"key"->"value", "other"->"entry"}.
45 static const std::map
<string
, string
>& DefaultMap()
47 static const std::map
<string
, string
> static_stored_map
{{"key", "value"},
49 return static_stored_map
;
54 std::map
<string
, string
> map
= DefaultMap();
55 TS_ASSERT(ContainsKey(map
, "key"));
56 TS_ASSERT(!ContainsKey(map
, "non key"));
58 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
59 if (string
* found_value
= FindOrNull(map
, "other"))
61 TS_ASSERT_EQUALS(*found_value
, "entry");
62 *found_value
= "new value";
64 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "new value");
67 void testConstantMap()
69 const std::map
<string
, string
> map
= DefaultMap();
70 TS_ASSERT(ContainsKey(map
, "key"));
71 TS_ASSERT(!ContainsKey(map
, "non key"));
73 if (const string
* found_value
= FindOrNull(map
, "other"))
75 TS_ASSERT_EQUALS(*found_value
, "entry");
77 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
78 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "entry");
79 // Cannot do death tests for FindOrDie.
82 void testUnorderedMap()
84 std::unordered_map
<string
, string
> map(DefaultMap().begin(),
86 TS_ASSERT(ContainsKey(map
, "key"));
87 TS_ASSERT(!ContainsKey(map
, "non key"));
89 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
90 if (string
* found_value
= FindOrNull(map
, "other"))
92 TS_ASSERT_EQUALS(*found_value
, "entry");
93 *found_value
= "new value";
95 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "new value");
98 void testConstUnorderedMap()
100 const std::unordered_map
<string
, string
> map(DefaultMap().begin(),
102 TS_ASSERT(ContainsKey(map
, "key"));
103 TS_ASSERT(!ContainsKey(map
, "non key"));
105 if (const string
* found_value
= FindOrNull(map
, "other"))
107 TS_ASSERT_EQUALS(*found_value
, "entry");
109 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
110 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "entry");
111 // Cannot do death tests for FindOrDie.
116 std::set
<string
> set
{"entry", "other"};
117 TS_ASSERT(ContainsKey(set
, "entry"));
118 TS_ASSERT(!ContainsKey(set
, "non member"));
120 const std::set
<string
> const_set
{"entry", "other"};
121 TS_ASSERT(ContainsKey(const_set
, "entry"));
122 TS_ASSERT(!ContainsKey(const_set
, "non member"));
125 void testUnorderedSet()
127 std::unordered_set
<string
> set
{"entry", "other"};
128 TS_ASSERT(ContainsKey(set
, "entry"));
129 TS_ASSERT(!ContainsKey(set
, "non member"));
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"));
136 // For each <key, value> pair in source, inserts mapping from key to value
137 // using insert into dest.
139 void InsertAll(const std::map
<string
, string
>& source
, M
* dest
)
141 for (const auto& key_value
: source
)
143 dest
->insert(key_value
.first
, key_value
.second
);
150 CDHashMap
<string
, string
> map(&context
);
151 InsertAll(DefaultMap(), &map
);
153 TS_ASSERT(ContainsKey(map
, "key"));
154 TS_ASSERT(!ContainsKey(map
, "non key"));
156 if (const string
* found_value
= FindOrNull(map
, "other"))
158 TS_ASSERT_EQUALS(*found_value
, "entry");
160 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
161 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "entry");
164 void testConstCDHashMap()
167 CDHashMap
<string
, string
> store(&context
);
168 InsertAll(DefaultMap(), &store
);
169 const auto& map
= store
;
171 TS_ASSERT(ContainsKey(map
, "key"));
172 TS_ASSERT(!ContainsKey(map
, "non key"));
174 if (const string
* found_value
= FindOrNull(map
, "other"))
176 TS_ASSERT_EQUALS(*found_value
, "entry");
178 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
179 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "entry");
182 void testCDInsertHashMap()
185 CDInsertHashMap
<string
, string
> map(&context
);
186 InsertAll(DefaultMap(), &map
);
188 TS_ASSERT(ContainsKey(map
, "key"));
189 TS_ASSERT(!ContainsKey(map
, "non key"));
191 if (const string
* found_value
= FindOrNull(map
, "other"))
193 TS_ASSERT_EQUALS(*found_value
, "entry");
195 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
196 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "entry");
199 void testConstCDInsertHashMap()
202 CDInsertHashMap
<string
, string
> store(&context
);
203 InsertAll(DefaultMap(), &store
);
204 const auto& map
= store
;
206 TS_ASSERT(ContainsKey(map
, "key"));
207 TS_ASSERT(!ContainsKey(map
, "non key"));
208 if (const string
* found_value
= FindOrNull(map
, "other"))
210 TS_ASSERT_EQUALS(*found_value
, "entry");
212 TS_ASSERT(FindOrNull(map
, "non key") == nullptr);
213 TS_ASSERT_EQUALS(FindOrDie(map
, "other"), "entry");
216 }; /* class MapUtilBlack */