about summary refs log tree commit diff homepage
path: root/unittests/Ref/RefTest.cpp
blob: bf313513a96a0248af038af93724f2102fb72549 (plain) (blame)
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
//===-- RefTest.cpp ---------------------------------------------*- C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

/* Regression test for a bug caused by assigning a ref to itself.
   More details at
   http://keeda.stanford.edu/pipermail/klee-commits/2012-February/000904.html */

#include "klee/ADT/Ref.h"
#include "gtest/gtest.h"
#include <iostream>

using klee::ref;

int finished = 0;
int finished_counter = 0;

struct Expr {
  /// @brief Required by klee::ref-managed objects
  class klee::ReferenceCounter _refCount;
  Expr() = default;

  Expr(const Expr &) = delete;
  Expr &operator=(const Expr &) = delete;

  ~Expr() {
    // std::cout << "Expr(" << this << ") destroyed\n";
    EXPECT_EQ(finished, 1);
    finished_counter++;
  }
};

struct SelfRefExpr {
  /// @brief Required by klee::ref-managed objects
  class klee::ReferenceCounter _refCount;
  ref<SelfRefExpr> next_;

  explicit SelfRefExpr(ref<SelfRefExpr> next) : next_(next) {}
  SelfRefExpr(const SelfRefExpr &) = delete;
  SelfRefExpr &operator=(const SelfRefExpr &) = delete;

  ~SelfRefExpr() { finished_counter++; }
};

struct ParentExpr {
  /// @brief Required by klee::ref-managed objects
  class klee::ReferenceCounter _refCount;

  enum ExprKind {
    EK_Parent,
    EK_Child,
  };

  const ExprKind Kind;
  ExprKind getKind() const { return Kind; }

  ParentExpr(ExprKind K) : Kind(K) {}

  virtual ~ParentExpr() {
    EXPECT_EQ(finished, 1);
    finished_counter++;
  }
};

struct ChildExpr : public ParentExpr {
  ChildExpr() : ParentExpr(EK_Child) {}

  static bool classof(const ParentExpr *S) { return S->getKind() == EK_Child; }
};

TEST(RefTest, SelfAssign) {
  finished = 0;
  finished_counter = 0;
  {

    struct Expr *r_e = new Expr();
    ref<Expr> r(r_e);
    EXPECT_EQ(r_e->_refCount.getCount(), 1u);
    r = r;
    EXPECT_EQ(r_e->_refCount.getCount(), 1u);
    finished = 1;
  }
  EXPECT_EQ(1, finished_counter);
}

TEST(RefTest, SelfMove) {
  finished = 0;
  finished_counter = 0;
  {
    struct Expr *r_e = new Expr();
    ref<Expr> r(r_e);

    // Check self move
    r = std::move(r);
    finished = 1;
  }
  EXPECT_EQ(1, finished_counter);
}

TEST(RefTest, MoveAssignment) {
  finished = 0;
  finished_counter = 0;
  {
    struct Expr *r_e = new Expr();
    ref<Expr> r(r_e);

    struct Expr *q_e = new Expr();
    ref<Expr> q(q_e);

    finished = 1;

    // Move the object
    q = std::move(r);

    // Re-assign new object
    r = new Expr();
  }
  EXPECT_EQ(3, finished_counter);
}

TEST(RefTest, MoveCastingAssignment) {
  finished = 0;
  finished_counter = 0;
  {
    ref<ParentExpr> r(new ChildExpr());

    struct ChildExpr *child = new ChildExpr();
    ref<ChildExpr> ce(child);

    finished = 1;

    // Move the object
    r = std::move(child);
  }
  EXPECT_EQ(2, finished_counter);
}

TEST(RefTest, MoveConstructor) {
  finished = 0;
  finished_counter = 0;
  {
    struct Expr *r_e = new Expr();
    ref<Expr> r(r_e);

    ref<Expr> q(std::move(r_e));

    finished = 1;
  }
  EXPECT_EQ(1, finished_counter);
}

TEST(RefTest, SelfRef) {
  struct SelfRefExpr *e_1 = new SelfRefExpr(nullptr);
  ref<SelfRefExpr> r_e_1(e_1);
  EXPECT_EQ(1u, r_e_1->_refCount.getCount());
  ref<SelfRefExpr> r_root = r_e_1;
  EXPECT_EQ(2u, r_e_1->_refCount.getCount());

  {
    ref<SelfRefExpr> r2(new SelfRefExpr(r_e_1));
    EXPECT_EQ(3u, r_e_1->_refCount.getCount());

    r_root = r2;
    EXPECT_EQ(2u, r_e_1->_refCount.getCount());
  }

  r_root = r_root->next_;
  EXPECT_EQ(2u, r_e_1->_refCount.getCount());
}