GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/context/cdhashmap_black.cpp Lines: 88 88 100.0 %
Date: 2021-03-22 Branches: 247 857 28.8 %

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