summaryrefslogtreecommitdiff
path: root/test/unit/context/cdmap_black.h
blob: d0db0cc0f28cd7c96a391640605d6336efd6c475 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*********************                                                        */
/*! \file cdmap_black.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Morgan Deters, Dejan Jovanovic, Paul Meng
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Black box testing of CVC4::context::CDMap<>.
 **
 ** Black box testing of CVC4::context::CDMap<>.
 **/

#include <cxxtest/TestSuite.h>

#include <map>

#include "base/cvc4_assert.h"
#include "context/context.h"
#include "context/cdhashmap.h"
#include "context/cdlist.h"

using CVC4::AssertionException;
using CVC4::context::Context;
using CVC4::context::CDHashMap;

class CDMapBlack : public CxxTest::TestSuite {
  Context* d_context;

 public:
  void setUp() {
    d_context = new Context;
    // Debug.on("context");
    // Debug.on("gc");
    // Debug.on("pushpop");
  }

  void tearDown() { delete d_context; }

  // Returns the elements in a CDHashMap.
  static std::map<int, int> GetElements(const CDHashMap<int, int>& map) {
    return std::map<int, int>{map.begin(), map.end()};
  }

  // Returns true if the elements in map are the same as expected.
  // NOTE: This is mostly to help the type checker for matching expected within
  // a TS_ASSERT.
  static bool ElementsAre(const CDHashMap<int, int>& map,
                          const std::map<int, int>& expected) {
    return GetElements(map) == expected;
  }

  void testSimpleSequence() {
    CDHashMap<int, int> map(d_context);
    TS_ASSERT(ElementsAre(map, {}));

    map.insert(3, 4);
    TS_ASSERT(ElementsAre(map, {{3, 4}}));

    {
      d_context->push();
      TS_ASSERT(ElementsAre(map, {{3, 4}}));

      map.insert(5, 6);
      map.insert(9, 8);
      TS_ASSERT(ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}}));

      {
        d_context->push();
        TS_ASSERT(ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}}));

        map.insert(1, 2);
        TS_ASSERT(ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}}));

        {
          d_context->push();
          TS_ASSERT(ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}}));

          map.insertAtContextLevelZero(23, 317);
          map.insert(1, 45);

          TS_ASSERT(
              ElementsAre(map, {{1, 45}, {3, 4}, {5, 6}, {9, 8}, {23, 317}}));
          map.insert(23, 324);

          TS_ASSERT(
              ElementsAre(map, {{1, 45}, {3, 4}, {5, 6}, {9, 8}, {23, 324}}));
          d_context->pop();
        }

        TS_ASSERT(
            ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 317}}));
        d_context->pop();
      }

      TS_ASSERT(ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}, {23, 317}}));
      d_context->pop();
    }

    TS_ASSERT(ElementsAre(map, {{3, 4}, {23, 317}}));
  }

  // no intervening find() in this one
  // (under the theory that this could trigger a bug)
  void testSimpleSequenceFewerFinds() {
    CDHashMap<int, int> map(d_context);
    map.insert(3, 4);

    {
      d_context->push();

      map.insert(5, 6);
      map.insert(9, 8);

      {
        d_context->push();

        map.insert(1, 2);

        TS_ASSERT(ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}}));
        {
          d_context->push();
          d_context->pop();
        }

        d_context->pop();
      }

      d_context->pop();
    }
  }

  void testInsertAtContextLevelZero() {
    CDHashMap<int, int> map(d_context);

    map.insert(3, 4);

    TS_ASSERT(ElementsAre(map, {{3, 4}}));
    {
      d_context->push();
      TS_ASSERT(ElementsAre(map, {{3, 4}}));

      map.insert(5, 6);
      map.insert(9, 8);

      TS_ASSERT(ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}}));

      {
        d_context->push();

        TS_ASSERT(ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}}));

        map.insert(1, 2);

        TS_ASSERT(ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}}));

        map.insertAtContextLevelZero(23, 317);

        TS_ASSERT(
            ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 317}}));

        TS_ASSERT_THROWS(map.insertAtContextLevelZero(23, 317),
                         AssertionException);
        TS_ASSERT_THROWS(map.insertAtContextLevelZero(23, 472),
                         AssertionException);
        map.insert(23, 472);

        TS_ASSERT(
            ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 472}}));
        {
          d_context->push();

          TS_ASSERT(
            ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 472}}));

          TS_ASSERT_THROWS(map.insertAtContextLevelZero(23, 0),
                           AssertionException);
          map.insert(23, 1024);

          TS_ASSERT(
            ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 1024}}));
          d_context->pop();
        }
        TS_ASSERT(
            ElementsAre(map, {{1, 2}, {3, 4}, {5, 6}, {9, 8}, {23, 472}}));
        d_context->pop();
      }


      TS_ASSERT(
          ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}, {23, 317}}));

      TS_ASSERT_THROWS(map.insertAtContextLevelZero(23, 0), AssertionException);
      map.insert(23, 477);

      TS_ASSERT(
          ElementsAre(map, {{3, 4}, {5, 6}, {9, 8}, {23, 477}}));
      d_context->pop();
    }

    TS_ASSERT_THROWS(map.insertAtContextLevelZero(23, 0), AssertionException);

    TS_ASSERT(
        ElementsAre(map, {{3, 4}, {23, 317}}));
  }
};
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback