1 // $Header$
2 //
3 // Copyright (C) 2000 - 2003, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 #include "sys.h"
14 #include <iostream>
15 #include "alloctag_debug.h"
16 #include "libcwd/lockable_auto_ptr.h"
17 
18 using namespace libcwd;
19 
20 class Bl {};
21 class Al : public Bl {};
22 
23 MAIN_FUNCTION
24 { PREFIX_CODE
25   Debug( check_configuration() );
26 
27 #if CWDEBUG_ALLOC && !defined(THREADTEST)
28   // Don't show allocations that are allocated before main()
29   make_all_allocations_invisible_except(NULL);
30 #endif
31 
32   // Select channels
33   Debug( dc::notice.on() );
34 #ifndef THREADTEST
35   // Write debug output to cout
36   Debug( libcw_do.set_ostream(&std::cout) );
37 #endif
38   // Turn debug object on
39   Debug( libcw_do.on() );
40 
41   {
42 
43   Al* a;
44 
45   a = new Al;
46   AllocTag(a, "A1");
47   lockable_auto_ptr<Al> ap1(a);
48 
49   LIBCWD_ASSERT(ap1.get() == a);
50   LIBCWD_ASSERT(ap1.is_owner() && !ap1.strict_owner());
51 
52   lockable_auto_ptr<Al> ap2(ap1);
53 
54   LIBCWD_ASSERT(ap1.get() == a);
55   LIBCWD_ASSERT(ap2.get() == a);
56   LIBCWD_ASSERT(!ap1.is_owner());
57   LIBCWD_ASSERT(ap2.is_owner() && !ap2.strict_owner());
58 
59   lockable_auto_ptr<Bl> bp1(ap2);
60 
61   LIBCWD_ASSERT(ap2.get() == a);
62   LIBCWD_ASSERT(bp1.get() == a);
63   LIBCWD_ASSERT(!ap2.is_owner());
64   LIBCWD_ASSERT(bp1.is_owner() && !bp1.strict_owner());
65 
66   a = new Al;
67   AllocTag(a, "A2");
68   lockable_auto_ptr<Al> ap3(a);
69   lockable_auto_ptr<Bl> bp2;
70   bp2 = ap3;
71 
72   LIBCWD_ASSERT(ap3.get() == a);
73   LIBCWD_ASSERT(bp2.get() == a);
74   LIBCWD_ASSERT(!ap3.is_owner());
75   LIBCWD_ASSERT(bp2.is_owner() && !bp2.strict_owner());
76 
77   a = new Al;
78   AllocTag(a, "A3");
79   lockable_auto_ptr<Al> ap4(a);
80   ap4.lock();
81 
82   LIBCWD_ASSERT(ap4.get() == a);
83   LIBCWD_ASSERT(ap4.is_owner() && ap4.strict_owner());
84 
85   lockable_auto_ptr<Al> ap5(ap4);
86 
87   LIBCWD_ASSERT(ap4.get() == a);
88   LIBCWD_ASSERT(ap5.get() == a);
89   LIBCWD_ASSERT(ap4.is_owner() && ap4.strict_owner());
90   LIBCWD_ASSERT(!ap5.is_owner());
91 
92   lockable_auto_ptr<Bl> bp3(ap5);
93 
94   LIBCWD_ASSERT(ap5.get() == a);
95   LIBCWD_ASSERT(bp3.get() == a);
96   LIBCWD_ASSERT(!ap5.is_owner());
97   LIBCWD_ASSERT(!bp3.is_owner());
98 
99   a = new Al;
100   AllocTag(a, "A4");
101   lockable_auto_ptr<Al> ap6(a);
102   ap6.lock();
103   lockable_auto_ptr<Bl> bp4;
104   bp4 = ap6;
105 
106   LIBCWD_ASSERT(ap6.get() == a);
107   LIBCWD_ASSERT(bp4.get() == a);
108   LIBCWD_ASSERT(ap6.is_owner() && ap6.strict_owner());
109   LIBCWD_ASSERT(!bp4.is_owner());
110 
111   } // Destruct all lockable_auto_ptr<>'s (exit() doesn't do that!)
112 
113   Dout(dc::notice, "Test successful");
114 
115   EXIT(0);
116 }
117