about summary refs log tree commit diff homepage
path: root/unittests/Ref
diff options
context:
space:
mode:
authorMartin Nowack <m.nowack@imperial.ac.uk>2019-04-03 16:01:36 +0100
committerCristian Cadar <c.cadar@imperial.ac.uk>2020-02-19 12:05:22 +0000
commit288e3110e5df232ab471db705371d818605b4ae4 (patch)
tree0d36b9dc49581b9b335cc962f0896c13e2756993 /unittests/Ref
parentb723470d00f80ad5620b27e81f2afa9efdd95135 (diff)
downloadklee-288e3110e5df232ab471db705371d818605b4ae4.tar.gz
Add `ReferenceCounter` struct utilized by ref<>
Using KLEE's `ref<>` shared ptr requires the referenced object
to contain a reference counter to be added and initialised to 0
as part of the constructor.

To support better reuse of the `ref<>` ptr add a `ReferenceCounter`
struct.

Just adding this struct to a new class/struct as member enables
reference counting with `ref<>` - no additional counter management
needed.
Diffstat (limited to 'unittests/Ref')
-rw-r--r--unittests/Ref/RefTest.cpp61
1 files changed, 36 insertions, 25 deletions
diff --git a/unittests/Ref/RefTest.cpp b/unittests/Ref/RefTest.cpp
index 7d587f69..3548da85 100644
--- a/unittests/Ref/RefTest.cpp
+++ b/unittests/Ref/RefTest.cpp
@@ -8,40 +8,35 @@
 //===----------------------------------------------------------------------===//
 
 /* 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 */
+   More details at
+   http://keeda.stanford.edu/pipermail/klee-commits/2012-February/000904.html */
 
+#include "klee/util/Ref.h"
 #include "gtest/gtest.h"
 #include <iostream>
-#include "klee/util/Ref.h"
 using klee::ref;
 
 int finished = 0;
 int finished_counter = 0;
 
-struct Expr
-{
-  int refCount;
-  Expr() : refCount(0) { 
-    //std::cout << "Expr(" << this << ") created\n"; 
-  }
-  ~Expr() { 
-    //std::cout << "Expr(" << this << ") destroyed\n"; 
+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++;
   }
 };
 
-TEST(RefTest, SelfAssign) 
-{
-  struct Expr *r_e = new Expr();
-  ref<Expr> r(r_e);
-  EXPECT_EQ(r_e->refCount, 1);
-  r = r;
-  EXPECT_EQ(r_e->refCount, 1);
-  finished = 1;
-}
 struct SelfRefExpr {
   /// @brief Required by klee::ref-managed objects
-  struct klee::ReferenceCounter __refCount;
+  class klee::ReferenceCounter _refCount;
   ref<SelfRefExpr> next_;
 
   explicit SelfRefExpr(ref<SelfRefExpr> next) : next_(next) {}
@@ -50,21 +45,37 @@ struct SelfRefExpr {
 
   ~SelfRefExpr() { finished_counter++; }
 };
+
+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, SelfRef) {
   struct SelfRefExpr *e_1 = new SelfRefExpr(nullptr);
   ref<SelfRefExpr> r_e_1(e_1);
-  EXPECT_EQ(1u, r_e_1->__refCount.refCount);
+  EXPECT_EQ(1u, r_e_1->_refCount.getCount());
   ref<SelfRefExpr> r_root = r_e_1;
-  EXPECT_EQ(2u, r_e_1->__refCount.refCount);
+  EXPECT_EQ(2u, r_e_1->_refCount.getCount());
 
   {
     ref<SelfRefExpr> r2(new SelfRefExpr(r_e_1));
-    EXPECT_EQ(3u, r_e_1->__refCount.refCount);
+    EXPECT_EQ(3u, r_e_1->_refCount.getCount());
 
     r_root = r2;
-    EXPECT_EQ(2u, r_e_1->__refCount.refCount);
+    EXPECT_EQ(2u, r_e_1->_refCount.getCount());
   }
 
   r_root = r_root->next_;
-  EXPECT_EQ(2u, r_e_1->__refCount.refCount);
+  EXPECT_EQ(2u, r_e_1->_refCount.getCount());
 }
\ No newline at end of file