1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Aina Niemetz, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 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. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Black box testing of cvc5::context::CDMap<>. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include <map> |
17 |
|
|
18 |
|
#include "base/check.h" |
19 |
|
#include "context/cdhashmap.h" |
20 |
|
#include "context/cdlist.h" |
21 |
|
#include "test_context.h" |
22 |
|
|
23 |
|
namespace cvc5 { |
24 |
|
namespace test { |
25 |
|
|
26 |
|
using cvc5::context::CDHashMap; |
27 |
|
using cvc5::context::Context; |
28 |
|
|
29 |
12 |
class TestContextBlackCDHashMap : public TestContext |
30 |
|
{ |
31 |
|
protected: |
32 |
|
/** Returns the elements in a CDHashMap. */ |
33 |
52 |
static std::map<int32_t, int32_t> get_elements( |
34 |
|
const CDHashMap<int32_t, int32_t>& map) |
35 |
|
{ |
36 |
52 |
return std::map<int32_t, int32_t>{map.begin(), map.end()}; |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
|
* Returns true if the elements in map are the same as expected. |
41 |
|
* NOTE: This is mostly to help the type checker for matching expected within |
42 |
|
* a ASSERT_*. |
43 |
|
*/ |
44 |
52 |
static bool elements_are(const CDHashMap<int32_t, int32_t>& map, |
45 |
|
const std::map<int32_t, int32_t>& expected) |
46 |
|
{ |
47 |
52 |
return get_elements(map) == expected; |
48 |
|
} |
49 |
|
}; |
50 |
|
|
51 |
12 |
TEST_F(TestContextBlackCDHashMap, simple_sequence) |
52 |
|
{ |
53 |
4 |
CDHashMap<int32_t, int32_t> map(d_context.get()); |
54 |
2 |
ASSERT_TRUE(elements_are(map, {})); |
55 |
|
|
56 |
2 |
map.insert(3, 4); |
57 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}})); |
58 |
|
|
59 |
|
{ |
60 |
2 |
d_context->push(); |
61 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}})); |
62 |
|
|
63 |
2 |
map.insert(5, 6); |
64 |
2 |
map.insert(9, 8); |
65 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}})); |
66 |
|
|
67 |
|
{ |
68 |
2 |
d_context->push(); |
69 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}})); |
70 |
|
|
71 |
2 |
map.insert(1, 2); |
72 |
2 |
ASSERT_TRUE(elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}})); |
73 |
|
|
74 |
|
{ |
75 |
2 |
d_context->push(); |
76 |
2 |
ASSERT_TRUE(elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}})); |
77 |
|
|
78 |
2 |
map.insertAtContextLevelZero(23, 317); |
79 |
2 |
map.insert(1, 45); |
80 |
|
|
81 |
2 |
ASSERT_TRUE( |
82 |
2 |
elements_are(map, {{1, 45}, {3, 4}, {5, 6}, {9, 8}, {23, 317}})); |
83 |
2 |
map.insert(23, 324); |
84 |
|
|
85 |
2 |
ASSERT_TRUE( |
86 |
2 |
elements_are(map, {{1, 45}, {3, 4}, {5, 6}, {9, 8}, {23, 324}})); |
87 |
2 |
d_context->pop(); |
88 |
|
} |
89 |
|
|
90 |
2 |
ASSERT_TRUE( |
91 |
2 |
elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 317}})); |
92 |
2 |
d_context->pop(); |
93 |
|
} |
94 |
|
|
95 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}, {23, 317}})); |
96 |
2 |
d_context->pop(); |
97 |
|
} |
98 |
|
|
99 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {23, 317}})); |
100 |
|
} |
101 |
|
|
102 |
12 |
TEST_F(TestContextBlackCDHashMap, simple_sequence_fewer_finds) |
103 |
|
{ |
104 |
|
// no intervening find() in this one (under the theory that this could trigger |
105 |
|
// a bug) |
106 |
4 |
CDHashMap<int, int> map(d_context.get()); |
107 |
2 |
map.insert(3, 4); |
108 |
|
|
109 |
|
{ |
110 |
2 |
d_context->push(); |
111 |
|
|
112 |
2 |
map.insert(5, 6); |
113 |
2 |
map.insert(9, 8); |
114 |
|
|
115 |
|
{ |
116 |
2 |
d_context->push(); |
117 |
|
|
118 |
2 |
map.insert(1, 2); |
119 |
|
|
120 |
2 |
ASSERT_TRUE(elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}})); |
121 |
|
{ |
122 |
2 |
d_context->push(); |
123 |
2 |
d_context->pop(); |
124 |
|
} |
125 |
|
|
126 |
2 |
d_context->pop(); |
127 |
|
} |
128 |
|
|
129 |
2 |
d_context->pop(); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
12 |
TEST_F(TestContextBlackCDHashMap, insert_at_context_level_zero) |
134 |
|
{ |
135 |
4 |
CDHashMap<int, int> map(d_context.get()); |
136 |
|
|
137 |
2 |
map.insert(3, 4); |
138 |
|
|
139 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}})); |
140 |
|
{ |
141 |
2 |
d_context->push(); |
142 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}})); |
143 |
|
|
144 |
2 |
map.insert(5, 6); |
145 |
2 |
map.insert(9, 8); |
146 |
|
|
147 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}})); |
148 |
|
|
149 |
|
{ |
150 |
2 |
d_context->push(); |
151 |
|
|
152 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}})); |
153 |
|
|
154 |
2 |
map.insert(1, 2); |
155 |
|
|
156 |
2 |
ASSERT_TRUE(elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}})); |
157 |
|
|
158 |
2 |
map.insertAtContextLevelZero(23, 317); |
159 |
|
|
160 |
2 |
ASSERT_TRUE( |
161 |
2 |
elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 317}})); |
162 |
|
|
163 |
2 |
ASSERT_DEATH(map.insertAtContextLevelZero(23, 317), |
164 |
|
"insertAtContextLevelZero"); |
165 |
2 |
ASSERT_DEATH(map.insertAtContextLevelZero(23, 472), |
166 |
|
"insertAtContextLevelZero"); |
167 |
2 |
map.insert(23, 472); |
168 |
|
|
169 |
2 |
ASSERT_TRUE( |
170 |
2 |
elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 472}})); |
171 |
|
{ |
172 |
2 |
d_context->push(); |
173 |
|
|
174 |
2 |
ASSERT_TRUE( |
175 |
2 |
elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 472}})); |
176 |
|
|
177 |
2 |
ASSERT_DEATH(map.insertAtContextLevelZero(23, 0), |
178 |
|
"insertAtContextLevelZero"); |
179 |
2 |
map.insert(23, 1024); |
180 |
|
|
181 |
2 |
ASSERT_TRUE( |
182 |
2 |
elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 1024}})); |
183 |
2 |
d_context->pop(); |
184 |
|
} |
185 |
2 |
ASSERT_TRUE( |
186 |
2 |
elements_are(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 472}})); |
187 |
2 |
d_context->pop(); |
188 |
|
} |
189 |
|
|
190 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}, {23, 317}})); |
191 |
|
|
192 |
2 |
ASSERT_DEATH(map.insertAtContextLevelZero(23, 0), |
193 |
|
"insertAtContextLevelZero"); |
194 |
2 |
map.insert(23, 477); |
195 |
|
|
196 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {5, 6}, {9, 8}, {23, 477}})); |
197 |
2 |
d_context->pop(); |
198 |
|
} |
199 |
|
|
200 |
2 |
ASSERT_DEATH(map.insertAtContextLevelZero(23, 0), "insertAtContextLevelZero"); |
201 |
|
|
202 |
2 |
ASSERT_TRUE(elements_are(map, {{3, 4}, {23, 317}})); |
203 |
|
} |
204 |
|
} // namespace test |
205 |
12 |
} // namespace cvc5 |