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 "cppunit/TestAssert.h" 26 #include "cppunit/TestFixture.h" 27 #include "cppunit/extensions/HelperMacros.h" 28 #include "postextstl.h" 29 30 #include <o3tl/heap_ptr.hxx> 31 32 33 34 35 using o3tl::heap_ptr; 36 37 38 class Help 39 { 40 public: 41 explicit Help( 42 int i_n ) 43 : n(i_n) { ++nInstanceCount_; } 44 ~Help() { --nInstanceCount_; } 45 int Value() const { return n; } 46 static int InstanceCount_() { return nInstanceCount_; } 47 48 private: 49 int n; 50 static int nInstanceCount_; 51 }; 52 int Help::nInstanceCount_ = 0; 53 54 55 class heap_ptr_test : public CppUnit::TestFixture 56 { 57 public: 58 void 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 CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is()); 73 CPPUNIT_ASSERT_MESSAGE("ctor2", t0.is()); 74 CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0).Value() == 7000 ); 75 CPPUNIT_ASSERT_MESSAGE("ctor4", t0->Value() == 7000 ); 76 CPPUNIT_ASSERT_MESSAGE("ctor5", t0.get() == t0.operator->() ); 77 CPPUNIT_ASSERT_MESSAGE("ctor6", t0.get() == &(*t0) ); 78 79 CPPUNIT_ASSERT_MESSAGE("ctor7", t1.is()); 80 CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1).Value() == 10 ); 81 CPPUNIT_ASSERT_MESSAGE("ctor9", t1->Value() == 10 ); 82 CPPUNIT_ASSERT_MESSAGE("ctor10", t1.get() == t1.operator->() ); 83 CPPUNIT_ASSERT_MESSAGE("ctor11", t1.get() == &(*t1) ); 84 85 CPPUNIT_ASSERT_MESSAGE("ctor12", t2.operator*().Value() == 20); 86 CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount); 87 88 89 // Operator safe_bool() / bool() 90 CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty); 91 CPPUNIT_ASSERT_MESSAGE("bool2", t0); 92 CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1)); 93 94 95 // Assignment, reset() and release() 96 // Release 97 Help * hp = t1.release(); 98 CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() ); 99 CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 ); 100 CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 ); 101 CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount); 102 103 // operator=() 104 t_empty = hp; 105 CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() ); 106 CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount); 107 108 t1 = t_empty.release(); 109 CPPUNIT_ASSERT_MESSAGE("assign3", t1.is() ); 110 CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty.is() ); 111 CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount); 112 113 // reset() 114 hp = new Help(30); 115 ++nHelpCount; 116 117 t_empty.reset(hp); 118 CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount); 119 CPPUNIT_ASSERT_MESSAGE("reset2", t_empty.is() ); 120 CPPUNIT_ASSERT_MESSAGE("reset3", t_empty.get() == hp ); 121 122 // Ignore second assignment 123 t_empty = hp; 124 CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount); 125 CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty.is() ); 126 CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty.get() == hp ); 127 128 t_empty.reset(0); 129 hp = 0; 130 --nHelpCount; 131 CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty.is() ); 132 CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount); 133 134 135 // swap 136 t1.swap(t2); 137 CPPUNIT_ASSERT_MESSAGE("swap1", t1->Value() == 20 ); 138 CPPUNIT_ASSERT_MESSAGE("swap2", t2->Value() == 10 ); 139 CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount); 140 141 o3tl::swap(t1,t2); 142 CPPUNIT_ASSERT_MESSAGE("swap4", t1->Value() == 10 ); 143 CPPUNIT_ASSERT_MESSAGE("swap5", t2->Value() == 20 ); 144 CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount); 145 146 // RAII 147 { 148 heap_ptr<Help> 149 t_raii( new Help(55) ); 150 CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1); 151 } 152 CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount); 153 } 154 155 156 // These macros are needed by auto register mechanism. 157 CPPUNIT_TEST_SUITE(heap_ptr_test); 158 CPPUNIT_TEST(global); 159 CPPUNIT_TEST_SUITE_END(); 160 }; // class heap_ptr_test 161 162 // ----------------------------------------------------------------------------- 163 CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test); 164