xref: /trunk/main/o3tl/qa/test-heap_ptr.cxx (revision 18cf0442)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "preextstl.h"
25 #include "gtest/gtest.h"
26 #include "postextstl.h"
27 
28 #include <o3tl/heap_ptr.hxx>
29 
30 
31 
32 
33 using o3tl::heap_ptr;
34 
35 
36 class Help
37 {
38   public:
Help(int i_n)39     explicit            Help(
40                             int                 i_n )
41                                                 : n(i_n) { ++nInstanceCount_; }
~Help()42                         ~Help()                 { --nInstanceCount_; }
Value() const43     int                 Value() const           { return n; }
InstanceCount_()44     static int          InstanceCount_()        { return nInstanceCount_; }
45 
46   private:
47     int                 n;
48     static int          nInstanceCount_;
49 };
50 int Help::nInstanceCount_ = 0;
51 
52 
53 class heap_ptr_test : public ::testing::Test
54 {
55   public:
56 }; // class heap_ptr_test
57 
TEST_F(heap_ptr_test,global)58 TEST_F(heap_ptr_test, global)
59 {
60     // Construction
61     heap_ptr<Help>
62         t_empty;
63     const heap_ptr<Help>
64         t0( new Help(7000) );
65     heap_ptr<Help>
66         t1( new Help(10) );
67     heap_ptr<Help>
68         t2( new Help(20) );
69 
70     int nHelpCount = 3;
71 
72     ASSERT_TRUE(! t_empty.is()) << "ctor1";
73     ASSERT_TRUE(t0.is()) << "ctor2";
74     ASSERT_TRUE((*t0).Value() == 7000) << "ctor3";
75     ASSERT_TRUE(t0->Value() == 7000) << "ctor4";
76     ASSERT_TRUE(t0.get() == t0.operator->()) << "ctor5";
77     ASSERT_TRUE(t0.get() == &(*t0)) << "ctor6";
78 
79     ASSERT_TRUE(t1.is()) << "ctor7";
80     ASSERT_TRUE((*t1).Value() == 10) << "ctor8";
81     ASSERT_TRUE(t1->Value() == 10) << "ctor9";
82     ASSERT_TRUE(t1.get() == t1.operator->()) << "ctor10";
83     ASSERT_TRUE(t1.get() == &(*t1)) << "ctor11";
84 
85     ASSERT_TRUE(t2.operator*().Value() == 20) << "ctor12";
86     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "ctor13";
87 
88 
89     // Operator safe_bool() / bool()
90     ASSERT_TRUE(! t_empty) << "bool1";
91     ASSERT_TRUE(t0) << "bool2";
92     ASSERT_TRUE(t1.is() == static_cast<bool>(t1)) << "bool3";
93 
94 
95     // Assignment, reset() and release()
96         // Release
97     Help * hp = t1.release();
98     ASSERT_TRUE(! t1.is()) << "release1";
99     ASSERT_TRUE(t1.get() == 0) << "release2";
100     ASSERT_TRUE(t1.operator->() == 0) << "release3";
101     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "release4";
102 
103         // operator=()
104     t_empty = hp;
105     ASSERT_TRUE(t_empty.is()) << "assign1";
106     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "assign2";
107 
108     t1 = t_empty.release();
109     ASSERT_TRUE(t1.is()) << "assign3";
110     ASSERT_TRUE(! t_empty.is()) << "assign4";
111     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "assign5";
112 
113         // reset()
114     hp = new Help(30);
115     ++nHelpCount;
116 
117     t_empty.reset(hp);
118     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "reset1";
119     ASSERT_TRUE(t_empty.is()) << "reset2";
120     ASSERT_TRUE(t_empty.get() == hp) << "reset3";
121 
122         // Ignore second assignment
123     t_empty = hp;
124     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "selfassign1";
125     ASSERT_TRUE(t_empty.is()) << "selfassign2";
126     ASSERT_TRUE(t_empty.get() == hp) << "selfassign3";
127 
128     t_empty.reset(0);
129     hp = 0;
130     --nHelpCount;
131     ASSERT_TRUE(! t_empty.is()) << "reset4";
132     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "reset5";
133 
134 
135     // swap
136     t1.swap(t2);
137     ASSERT_TRUE(t1->Value() == 20) << "swap1";
138     ASSERT_TRUE(t2->Value() == 10) << "swap2";
139     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "swap3";
140 
141     o3tl::swap(t1,t2);
142     ASSERT_TRUE(t1->Value() == 10) << "swap4";
143     ASSERT_TRUE(t2->Value() == 20) << "swap5";
144     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "swap6";
145 
146     // RAII
147     {
148         heap_ptr<Help>
149             t_raii( new Help(55) );
150         ASSERT_TRUE(Help::InstanceCount_() == nHelpCount + 1) << "raii1";
151     }
152     ASSERT_TRUE(Help::InstanceCount_() == nHelpCount) << "raii2";
153 }
154 
155 
156