Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Ptr_test.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "TestClasses.hpp"
11 #include "Teuchos_Ptr.hpp"
16 #include "Teuchos_Version.hpp"
17 #include "Teuchos_Assert.hpp"
18 #include "Teuchos_getConst.hpp"
19 #include "Teuchos_as.hpp"
20 
21 
22 int main( int argc, char* argv[] ) {
23 
24  using Teuchos::Ptr;
25  using Teuchos::ptr;
26  using Teuchos::ptrFromRef;
27  using Teuchos::constPtr;
28  using Teuchos::outArg;
29  using Teuchos::inOutArg;
30  using Teuchos::inoutArg;
31  using Teuchos::optInArg;
32  using Teuchos::constOptInArg;
34 
35  bool success = true;
36 
37  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
38  //const int procRank = Teuchos::GlobalMPISession::getRank();
39 
42 
43  try {
44 
45  //
46  // Read options from the commandline
47  //
48 
49  CommandLineProcessor clp(false); // Don't throw exceptions
50 
51  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
52 
53  if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
54  *out << "\nEnd Result: TEST FAILED" << std::endl;
55  return parse_return;
56  }
57 
58  *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
59 
60  *out << "\nTesting Teuchos::Ptr class ...\n";
61 
62  {
63  // Test null construction
64  Ptr<A> a_ptr;
65  *out << "\nNull a_ptr = " << a_ptr << "\n";
66  TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.get() );
67  TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.getRawPtr() );
68 #ifdef TEUCHOS_DEBUG
69  try {
70  A &a = *a_ptr; // Should throw!
71  a.A_g();
72  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
73  "Error, Ptr::operator*() on null Ptr should have thrown exception!" );
74  }
75  catch( const Teuchos::NullReferenceError &except ) {
76  // Caught expected exception!
77  }
78 #endif
79 #ifdef TEUCHOS_DEBUG
80  try {
81  a_ptr->A_g(); // Should throw!
82  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
83  "Error, Ptr::operator->() on null Ptr should have thrown exception!" );
84  }
85  catch( const Teuchos::NullReferenceError &except ) {
86  // Caught expected exception!
87  }
88 #endif
89  }
90 
91  {
92  // Test basic construction of Ptr
93  A a;
94  Ptr<A> a_ptr(&a);
95  *out << "\nNon-null a_ptr = " << a_ptr << "\n";
96  TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
97  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
98  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.getRawPtr() );
99  }
100 
101  {
102  // Test copy constructor for Ptr
103  A a;
104  Ptr<A> a_ptr1(&a);
105  Ptr<A> a_ptr2(a_ptr1);
106  TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
107  }
108 
109  {
110  // Test implicit copy conversion
111  C c;
112  Ptr<C> c_ptr(&c);
113  Ptr<A> a_ptr(c_ptr);
114  TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
115  }
116 
117  {
118  // Test assignment operator
119  C c;
120  Ptr<C> c_ptr(&c);
121  Ptr<A> a_ptr;
122  a_ptr = c_ptr;
123  TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
124  }
125 
126  {
127  // Test construction of Ptr from ptr()
128  A a;
129  Ptr<A> a_ptr = ptr(&a);
130  TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
131  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
132  }
133 
134  {
135  // Test construction of Ptr from ptrFromRef()
136  A a;
137  Ptr<A> a_ptr = ptrFromRef(a);
138  TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
139  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
140  }
141 
142  {
143  // Test construction of Ptr from constPtr()
144  A a;
145  Ptr<const A> a_ptr = constPtr(a);
146  TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
147  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
148  }
149 
150  {
151  // Test construction of Ptr from outArg()
152  A a;
153  Ptr<A> a_ptr = outArg(a);
154  TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
155  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
156  }
157 
158  {
159  // Test construction of Ptr from inOutArg()
160  A a;
161  Ptr<A> a_ptr = inOutArg(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 inOutArg()
168  A a;
169  Ptr<A> a_ptr = inoutArg(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 optInArg()
176  A a;
177  Ptr<const A> a_ptr = optInArg(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 optInArg()
184  A a;
185  Ptr<const A> a_ptr = constOptInArg(a);
186  TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
187  TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
188  }
189 
190  {
191  // Test ptr_implicit_cast()
192  C c;
193  Ptr<C> c_ptr(&c);
194  Ptr<A> a_ptr1 = c_ptr;
195  Ptr<A> a_ptr2 = Teuchos::ptr_implicit_cast<A>(c_ptr);
196  TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
197  }
198 
199  {
200  // Test ptr_static_cast()
201  E e;
202  Ptr<D> d_ptr(&e);
203  Ptr<E> e_ptr = Teuchos::ptr_static_cast<E>(d_ptr);
204  TEUCHOS_ASSERT_EQUALITY( &*e_ptr, &e );
205  }
206 
207  {
208  // Test ptr_const_cast()
209  C c;
210  Ptr<const C> c_ptr1(&c);
211  Ptr<C> c_ptr2 = Teuchos::ptr_const_cast<C>(c_ptr1);
212  TEUCHOS_ASSERT_EQUALITY( &*c_ptr2, &*c_ptr1 );
213  }
214 
215  {
216  // Test null ptr_dynamic_cast()
217  Ptr<A> a_ptr;
218  Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
219  TEUCHOS_ASSERT_EQUALITY( c_ptr.get(), 0 );
220  }
221 
222  {
223  // Test non-throw non-null ptr_dynamic_cast()
224  C c;
225  Ptr<A> a_ptr(&c);
226  Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
227  TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
228  }
229 
230  {
231  // Test good throwing non-null ptr_dynamic_cast()
232  C c;
233  Ptr<A> a_ptr(&c);
234  Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
235  TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
236  }
237 
238  {
239  // Test bad throwing non-null ptr_dynamic_cast()
240  B1 b1;
241  Ptr<A> a_ptr(&b1);
242  try {
243  Ptr<C> b2_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
244  (void) b2_ptr; // Silence "set but not used" compiler warning.
245  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
246  "If you get here then the test failed!" );
247  }
248  catch ( const Teuchos::m_bad_cast &except ) {
249  // Test passed!
250  }
251  }
252 
253  }
254  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
255 
256  if (success)
257  *out << "\nEnd Result: TEST PASSED" << std::endl;
258  else
259  *out << "\nEnd Result: TEST FAILED" << std::endl;
260 
261  return ( success ? 0 : 1 );
262 
263 }
Null reference error exception class.
Ptr< T > inOutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
virtual int A_g()
Definition: TestClasses.hpp:67
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Ptr< T > optInArg(T &arg)
create a non-persisting non-const optional input argument for a function call.
Initialize, finalize, and query the global MPI session.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
Ptr< T > ptrFromRef(T &arg)
Create a pointer to a object from an object reference.
Ptr< T > inoutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
std::string Teuchos_Version()
Exception class for bad cast.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
int main(int argc, char *argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
Smart reference counting pointer class for automatic garbage collection.
Ptr< const T > constPtr(T &arg)
Create a pointer from a const object given a non-const object reference.
#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 ...
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Ptr< const T > constOptInArg(T &arg)
create a non-persisting const optional input argument for a function call.
Definition of Teuchos::as, for conversions between types.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...