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