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