- Added testcase for zypp::RW_pointer unique()
authorMarius Tomaschewski <mt@suse.de>
Fri, 17 Feb 2006 16:28:36 +0000 (16:28 +0000)
committerMarius Tomaschewski <mt@suse.de>
Fri, 17 Feb 2006 16:28:36 +0000 (16:28 +0000)
testsuite/zypp/tests/Makefile.am
testsuite/zypp/tests/RWPtr.cc [new file with mode: 0644]
testsuite/zypp/zypp.test/RWPtr.exp [new file with mode: 0644]

index 228afb5..4c4ee0a 100644 (file)
@@ -8,6 +8,7 @@ SUBDIRS =
 ## ##################################################
 
 noinst_PROGRAMS = Arch Url1 Url2 Url3 Url4 Url5 \
+       RWPtr \
        Edition                 \
        Capabilities
 
@@ -23,6 +24,7 @@ Url2_SOURCES = Url2.cc
 Url3_SOURCES = Url3.cc
 Url4_SOURCES = Url4.cc
 Url5_SOURCES = Url5.cc
+RWPtr_SOURCES = RWPtr.cc
 Edition_SOURCES = Edition.cc
 Capabilities_SOURCES = Capabilities.cc
 
diff --git a/testsuite/zypp/tests/RWPtr.cc b/testsuite/zypp/tests/RWPtr.cc
new file mode 100644 (file)
index 0000000..6e1ddc6
--- /dev/null
@@ -0,0 +1,82 @@
+#include <zypp/base/PtrTypes.h>
+#include <string>
+#include <iostream>
+
+struct Foo
+{
+  int _foo;
+
+  Foo(int foo=0): _foo(foo)
+  {
+    std::cerr << "created Foo(" << _foo << ")" << std::endl;
+  }
+  ~Foo()
+  {
+    std::cerr << "destroy Foo(" << _foo << ")" << std::endl;
+  }
+};
+
+#define REF_TEST(ref,msg,exp,res) \
+do { \
+  bool unique = exp; \
+  std::cerr << msg << std::endl; \
+  if( ref) { \
+    std::cerr << "ref contains object" << std::endl; \
+  } else { \
+    std::cerr << "ref contains no object" << std::endl; \
+  } \
+  std::cerr << "ref counter is " << ref.use_count() << std::endl; \
+  if( ref.unique()) { \
+    std::cerr << "ref is unique" << std::endl; \
+    if( unique) { \
+      std::cerr << "EXPECTED" << std::endl; \
+    } else { \
+      std::cerr << "NOT EXPECTED" << std::endl; \
+      res = 1; \
+    } \
+  } else { \
+    std::cerr << "ref is shared" << std::endl; \
+    if( !unique) { \
+      std::cerr << "EXPECTED" << std::endl; \
+    } else { \
+      std::cerr << "NOT EXPECTED" << std::endl; \
+      res = 1;  \
+    } \
+  } \
+  std::cerr << std::endl; \
+} while(0);
+
+int main(int argc, char *argv[])
+{
+  (void)argv;
+
+  bool skip_reset = argc > 1;
+  int  result = 0;
+
+  typedef zypp::RW_pointer<Foo> FooRef;
+
+  FooRef ref;
+  REF_TEST(ref,"=== REF(nil)", true, result);
+
+  ref.reset(new Foo(42));
+  REF_TEST(ref,"=== REF(object)", true, result);
+
+  {
+    FooRef ref2(ref);
+    REF_TEST(ref,"=== REF2(REF)", false, result);
+  }
+
+  REF_TEST(ref,"=== REF(object), REF2 out of scope now", true, result);
+
+  if( !skip_reset)
+  {
+    ref.reset();
+    REF_TEST(ref,"=== REF(nil), reset()", true, result);
+  }
+
+  std::cerr << "RESULT: "
+            << (result == 0 ? "PASSED" : "FAILED")
+            << std::endl;
+  return result;
+}
+
diff --git a/testsuite/zypp/zypp.test/RWPtr.exp b/testsuite/zypp/zypp.test/RWPtr.exp
new file mode 100644 (file)
index 0000000..8d93e17
--- /dev/null
@@ -0,0 +1,4 @@
+# RWPtr.exp
+# run tests for RWPtr
+
+  shouldPass "RWPtr"