GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/context/cdlist_black.cpp Lines: 94 94 100.0 %
Date: 2021-08-06 Branches: 146 474 30.8 %

Line Exec Source
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::CDList<>.
14
 */
15
16
#include <limits.h>
17
18
#include <iostream>
19
#include <vector>
20
21
#include "base/exception.h"
22
#include "context/cdlist.h"
23
#include "memory.h"
24
#include "test_context.h"
25
26
namespace cvc5 {
27
28
using namespace context;
29
30
namespace test {
31
32
struct DtorSensitiveObject
33
{
34
  bool& d_dtorCalled;
35
10
  DtorSensitiveObject(bool& dtorCalled) : d_dtorCalled(dtorCalled) {}
36
16
  ~DtorSensitiveObject() { d_dtorCalled = true; }
37
};
38
39
44
class TestContextBlackCDList : public TestContext
40
{
41
 protected:
42
12
  void list_test(int n)
43
  {
44
12
    list_test(n, true);
45
12
    list_test(n, false);
46
12
  }
47
48
24
  void list_test(int32_t n, bool callDestructor)
49
  {
50
48
    CDList<int32_t> list(d_context.get(), callDestructor);
51
52
24
    ASSERT_TRUE(list.empty());
53
940
    for (int32_t i = 0; i < n; ++i)
54
    {
55
916
      ASSERT_EQ(list.size(), (uint32_t)i);
56
916
      list.push_back(i);
57
916
      ASSERT_FALSE(list.empty());
58
916
      ASSERT_EQ(list.back(), i);
59
916
      int32_t i2 = 0;
60
29876
      for (CDList<int32_t>::const_iterator j = list.begin(); j != list.end();
61
           ++j)
62
      {
63
28960
        ASSERT_EQ(*j, i2++);
64
      }
65
    }
66
24
    ASSERT_EQ(list.size(), (uint32_t)n);
67
68
940
    for (int32_t i = 0; i < n; ++i)
69
    {
70
916
      ASSERT_EQ(list[i], i);
71
    }
72
  }
73
};
74
75
20
TEST_F(TestContextBlackCDList, CDList10) { list_test(10); }
76
77
20
TEST_F(TestContextBlackCDList, CDList15) { list_test(15); }
78
79
20
TEST_F(TestContextBlackCDList, CDList20) { list_test(20); }
80
81
20
TEST_F(TestContextBlackCDList, CDList35) { list_test(35); }
82
83
20
TEST_F(TestContextBlackCDList, CDList50) { list_test(50); }
84
85
20
TEST_F(TestContextBlackCDList, CDList99) { list_test(99); }
86
87
20
TEST_F(TestContextBlackCDList, destructor_called)
88
{
89
2
  bool shouldRemainFalse = false;
90
2
  bool shouldFlipToTrue = false;
91
2
  bool alsoFlipToTrue = false;
92
2
  bool shouldAlsoRemainFalse = false;
93
2
  bool aThirdFalse = false;
94
95
4
  CDList<DtorSensitiveObject> listT(d_context.get(), true);
96
4
  CDList<DtorSensitiveObject> listF(d_context.get(), false);
97
98
4
  DtorSensitiveObject shouldRemainFalseDSO(shouldRemainFalse);
99
4
  DtorSensitiveObject shouldFlipToTrueDSO(shouldFlipToTrue);
100
4
  DtorSensitiveObject alsoFlipToTrueDSO(alsoFlipToTrue);
101
4
  DtorSensitiveObject shouldAlsoRemainFalseDSO(shouldAlsoRemainFalse);
102
4
  DtorSensitiveObject aThirdFalseDSO(aThirdFalse);
103
104
2
  listT.push_back(shouldAlsoRemainFalseDSO);
105
2
  listF.push_back(shouldAlsoRemainFalseDSO);
106
107
2
  d_context->push();
108
109
2
  listT.push_back(shouldFlipToTrueDSO);
110
2
  listT.push_back(alsoFlipToTrueDSO);
111
112
2
  listF.push_back(shouldRemainFalseDSO);
113
2
  listF.push_back(shouldAlsoRemainFalseDSO);
114
2
  listF.push_back(aThirdFalseDSO);
115
116
2
  ASSERT_EQ(shouldRemainFalse, false);
117
2
  ASSERT_EQ(shouldFlipToTrue, false);
118
2
  ASSERT_EQ(alsoFlipToTrue, false);
119
2
  ASSERT_EQ(shouldAlsoRemainFalse, false);
120
2
  ASSERT_EQ(aThirdFalse, false);
121
122
2
  d_context->pop();
123
124
2
  ASSERT_EQ(shouldRemainFalse, false);
125
2
  ASSERT_EQ(shouldFlipToTrue, true);
126
2
  ASSERT_EQ(alsoFlipToTrue, true);
127
2
  ASSERT_EQ(shouldAlsoRemainFalse, false);
128
2
  ASSERT_EQ(aThirdFalse, false);
129
}
130
131
20
TEST_F(TestContextBlackCDList, empty_iterator)
132
{
133
2
  CDList<int>* list = new (true) CDList<int>(d_context.get());
134
2
  ASSERT_EQ(list->begin(), list->end());
135
2
  list->deleteSelf();
136
}
137
138
20
TEST_F(TestContextBlackCDList, out_of_memory)
139
{
140
#ifndef CVC5_MEMORY_LIMITING_DISABLED
141
4
  CDList<uint32_t> list(d_context.get());
142
4
  test::WithLimitedMemory wlm(1);
143
144
4
  ASSERT_THROW(
145
      {
146
        // We cap it at UINT32_MAX, preferring to terminate with a
147
        // failure than run indefinitely.
148
        for (uint32_t i = 0; i < UINT32_MAX; ++i)
149
        {
150
          list.push_back(i);
151
        }
152
      },
153
2
      std::bad_alloc);
154
#endif
155
}
156
157
20
TEST_F(TestContextBlackCDList, pop_below_level_created)
158
{
159
2
  d_context->push();
160
4
  CDList<int32_t> list(d_context.get());
161
2
  d_context->popto(0);
162
2
  list.push_back(42);
163
2
}
164
165
20
TEST_F(TestContextBlackCDList, emplace_back)
166
{
167
2
  int32_t n = 10;
168
2
  int32_t start = 42;
169
4
  CDList<std::unique_ptr<int32_t>> list(d_context.get());
170
171
22
  for (int32_t i = 0; i < n; i++)
172
  {
173
20
    list.emplace_back(new int32_t(start + i));
174
  }
175
22
  for (int32_t i = 0; i < n; i++)
176
  {
177
20
    ASSERT_EQ(*list[i], start + i);
178
  }
179
2
  ASSERT_EQ(list.size(), n);
180
181
2
  d_context->push();
182
22
  for (int32_t i = 0; i < n; i++)
183
  {
184
20
    list.emplace_back(new int32_t(start + n + i));
185
  }
186
42
  for (int32_t i = 0; i < n * 2; i++)
187
  {
188
40
    ASSERT_EQ(*list[i], start + i);
189
  }
190
2
  ASSERT_EQ(list.size(), n * 2);
191
2
  d_context->pop();
192
193
22
  for (int32_t i = 0; i < n; i++)
194
  {
195
20
    ASSERT_EQ(*list[i], start + i);
196
  }
197
2
  ASSERT_EQ(list.size(), n);
198
}
199
200
}  // namespace test
201
36
}  // namespace cvc5