ec110109a9e34ff0fec27b1d641bb317d1d8d612
[platform/upstream/libzypp.git] / tests / zypp / RWPtr_test.cc
1 #include <boost/test/unit_test.hpp>
2
3 #include <zypp/base/PtrTypes.h>
4 #include <string>
5 #include <iostream>
6
7 #define BOOST_TEST_MODULE RWPtr_test
8
9 struct Foo
10 {
11   int _foo;
12
13   Foo(int foo=0): _foo(foo)
14   {
15     std::cerr << "created Foo(" << _foo << ")" << std::endl;
16   }
17   ~Foo()
18   {
19     std::cerr << "destroy Foo(" << _foo << ")" << std::endl;
20   }
21 };
22
23 #define REF_TEST(ref,msg,exp,res) \
24 do { \
25   bool unique = exp; \
26   std::cerr << msg << std::endl; \
27   if( ref) { \
28     std::cerr << "ref contains object" << std::endl; \
29   } else { \
30     std::cerr << "ref contains no object" << std::endl; \
31   } \
32   std::cerr << "ref counter is " << ref.use_count() << std::endl; \
33   if( ref.unique()) { \
34     std::cerr << "ref is unique" << std::endl; \
35     if( unique) { \
36       std::cerr << "EXPECTED" << std::endl; \
37     } else { \
38       std::cerr << "NOT EXPECTED" << std::endl; \
39       res = 1; \
40     } \
41   } else { \
42     std::cerr << "ref is shared" << std::endl; \
43     if( !unique) { \
44       std::cerr << "EXPECTED" << std::endl; \
45     } else { \
46       std::cerr << "NOT EXPECTED" << std::endl; \
47       res = 1;  \
48     } \
49   } \
50   std::cerr << std::endl; \
51 } while(0);
52
53 BOOST_AUTO_TEST_CASE(basic_test)
54 {
55   bool skip_reset = false;
56   int  result = 0;
57
58   typedef zypp::RW_pointer<Foo> FooRef;
59
60   FooRef ref;
61   REF_TEST(ref,"=== REF(nil)", true, result);
62
63   ref.reset(new Foo(42));
64   REF_TEST(ref,"=== REF(object)", true, result);
65
66   {
67     FooRef ref2(ref);
68     REF_TEST(ref,"=== REF2(REF)", false, result);
69   }
70
71   REF_TEST(ref,"=== REF(object), REF2 out of scope now", true, result);
72
73   if( !skip_reset)
74   {
75     ref.reset();
76     REF_TEST(ref,"=== REF(nil), reset()", true, result);
77   }
78
79   std::cerr << "RESULT: "
80             << (result == 0 ? "PASSED" : "FAILED")
81             << std::endl;
82 }
83