Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Ptr_test.cpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#include "TestClasses.hpp"
43#include "Teuchos_Ptr.hpp"
48#include "Teuchos_Version.hpp"
49#include "Teuchos_Assert.hpp"
50#include "Teuchos_getConst.hpp"
51#include "Teuchos_as.hpp"
52
53
54int main( int argc, char* argv[] ) {
55
56 using Teuchos::Ptr;
57 using Teuchos::ptr;
58 using Teuchos::ptrFromRef;
59 using Teuchos::constPtr;
60 using Teuchos::outArg;
61 using Teuchos::inOutArg;
62 using Teuchos::inoutArg;
63 using Teuchos::optInArg;
64 using Teuchos::constOptInArg;
66
67 bool success = true;
68
69 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
70 //const int procRank = Teuchos::GlobalMPISession::getRank();
71
74
75 try {
76
77 //
78 // Read options from the commandline
79 //
80
81 CommandLineProcessor clp(false); // Don't throw exceptions
82
83 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
84
85 if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
86 *out << "\nEnd Result: TEST FAILED" << std::endl;
87 return parse_return;
88 }
89
90 *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
91
92 *out << "\nTesting Teuchos::Ptr class ...\n";
93
94 {
95 // Test null construction
96 Ptr<A> a_ptr;
97 *out << "\nNull a_ptr = " << a_ptr << "\n";
98 TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.get() );
99 TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.getRawPtr() );
100#ifdef TEUCHOS_DEBUG
101 try {
102 A &a = *a_ptr; // Should throw!
103 a.A_g();
104 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
105 "Error, Ptr::operator*() on null Ptr should have thrown exception!" );
106 }
107 catch( const Teuchos::NullReferenceError &except ) {
108 // Caught expected exception!
109 }
110#endif
111#ifdef TEUCHOS_DEBUG
112 try {
113 a_ptr->A_g(); // Should throw!
114 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
115 "Error, Ptr::operator->() on null Ptr should have thrown exception!" );
116 }
117 catch( const Teuchos::NullReferenceError &except ) {
118 // Caught expected exception!
119 }
120#endif
121 }
122
123 {
124 // Test basic construction of Ptr
125 A a;
126 Ptr<A> a_ptr(&a);
127 *out << "\nNon-null a_ptr = " << a_ptr << "\n";
128 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
129 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
130 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.getRawPtr() );
131 }
132
133 {
134 // Test copy constructor for Ptr
135 A a;
136 Ptr<A> a_ptr1(&a);
137 Ptr<A> a_ptr2(a_ptr1);
138 TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
139 }
140
141 {
142 // Test implicit copy conversion
143 C c;
144 Ptr<C> c_ptr(&c);
145 Ptr<A> a_ptr(c_ptr);
146 TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
147 }
148
149 {
150 // Test assignment operator
151 C c;
152 Ptr<C> c_ptr(&c);
153 Ptr<A> a_ptr;
154 a_ptr = c_ptr;
155 TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
156 }
157
158 {
159 // Test construction of Ptr from ptr()
160 A a;
161 Ptr<A> a_ptr = ptr(&a);
162 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
163 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
164 }
165
166 {
167 // Test construction of Ptr from ptrFromRef()
168 A a;
169 Ptr<A> a_ptr = ptrFromRef(a);
170 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
171 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
172 }
173
174 {
175 // Test construction of Ptr from constPtr()
176 A a;
177 Ptr<const A> a_ptr = constPtr(a);
178 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
179 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
180 }
181
182 {
183 // Test construction of Ptr from outArg()
184 A a;
185 Ptr<A> a_ptr = outArg(a);
186 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
187 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
188 }
189
190 {
191 // Test construction of Ptr from inOutArg()
192 A a;
193 Ptr<A> a_ptr = inOutArg(a);
194 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
195 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
196 }
197
198 {
199 // Test construction of Ptr from inOutArg()
200 A a;
201 Ptr<A> a_ptr = inoutArg(a);
202 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
203 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
204 }
205
206 {
207 // Test construction of Ptr from optInArg()
208 A a;
209 Ptr<const A> a_ptr = optInArg(a);
210 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
211 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
212 }
213
214 {
215 // Test construction of Ptr from optInArg()
216 A a;
217 Ptr<const A> a_ptr = constOptInArg(a);
218 TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
219 TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
220 }
221
222 {
223 // Test ptr_implicit_cast()
224 C c;
225 Ptr<C> c_ptr(&c);
226 Ptr<A> a_ptr1 = c_ptr;
227 Ptr<A> a_ptr2 = Teuchos::ptr_implicit_cast<A>(c_ptr);
228 TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
229 }
230
231 {
232 // Test ptr_static_cast()
233 E e;
234 Ptr<D> d_ptr(&e);
235 Ptr<E> e_ptr = Teuchos::ptr_static_cast<E>(d_ptr);
236 TEUCHOS_ASSERT_EQUALITY( &*e_ptr, &e );
237 }
238
239 {
240 // Test ptr_const_cast()
241 C c;
242 Ptr<const C> c_ptr1(&c);
243 Ptr<C> c_ptr2 = Teuchos::ptr_const_cast<C>(c_ptr1);
244 TEUCHOS_ASSERT_EQUALITY( &*c_ptr2, &*c_ptr1 );
245 }
246
247 {
248 // Test null ptr_dynamic_cast()
249 Ptr<A> a_ptr;
250 Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
251 TEUCHOS_ASSERT_EQUALITY( c_ptr.get(), 0 );
252 }
253
254 {
255 // Test non-throw non-null ptr_dynamic_cast()
256 C c;
257 Ptr<A> a_ptr(&c);
258 Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
259 TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
260 }
261
262 {
263 // Test good throwing non-null ptr_dynamic_cast()
264 C c;
265 Ptr<A> a_ptr(&c);
266 Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
267 TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
268 }
269
270 {
271 // Test bad throwing non-null ptr_dynamic_cast()
272 B1 b1;
273 Ptr<A> a_ptr(&b1);
274 try {
275 Ptr<C> b2_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
276 (void) b2_ptr; // Silence "set but not used" compiler warning.
277 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
278 "If you get here then the test failed!" );
279 }
280 catch ( const Teuchos::m_bad_cast &except ) {
281 // Test passed!
282 }
283 }
284
285 }
286 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
287
288 if (success)
289 *out << "\nEnd Result: TEST PASSED" << std::endl;
290 else
291 *out << "\nEnd Result: TEST FAILED" << std::endl;
292
293 return ( success ? 0 : 1 );
294
295}
Basic command line parser for input from (argc,argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
Definition of Teuchos::as, for conversions between types.
virtual int A_g()
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Initialize, finalize, and query the global MPI session.
Null reference error exception class.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Concrete serial communicator subclass.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
Exception class for bad cast.
int main()
Definition evilMain.cpp:75
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
Definition PackageA.cpp:3
Definition PackageC.cpp:3
std::string Teuchos_Version()