GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/context/cdlist_black.cpp Lines: 75 75 100.0 %
Date: 2021-03-22 Branches: 107 358 29.9 %

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