ConstNonconstObjectContainer.cpp

Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //                           Teuchos Package
00005 //                 Copyright (2009) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 #include "Teuchos_ConstNonconstObjectContainer.hpp"
00030 
00031 #include "Teuchos_UnitTestHarness.hpp"
00032 
00033 namespace Teuchos {
00034 
00035 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, create ) {
00036   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00037   RCP<std::vector<double> > vec = rcp(new std::vector<double> );
00038   vectorObj.initialize(vec); // nonconst
00039   TEST_ASSERT( vectorObj.isConst() == false );
00040 }
00041 
00042 
00043 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, DefaultConstruct ) {
00044   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00045   TEST_ASSERT( vectorObj.isConst() == true );
00046   TEST_ASSERT( vectorObj.getConstObj() == null );
00047   // This does not throw an exception because the pointer is null
00048   TEST_ASSERT( vectorObj.getNonconstObj() == null );
00049 }
00050 
00051 
00052 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, NonconstConstruct ) {
00053   RCP<std::vector<double> > vec = rcp(new std::vector<double> );
00054   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00055   TEST_ASSERT( vectorObj.isConst() == false );
00056   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00057   TEST_ASSERT( vec == vec2 );
00058   RCP<std::vector<double> > vec3 = vectorObj.getNonconstObj();
00059   TEST_ASSERT( vec == vec3 );
00060 }
00061 
00062 
00063 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, ConstConstruct) {
00064   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00065   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00066   TEST_ASSERT( vectorObj.isConst() == true );
00067   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00068   TEST_ASSERT( vec == vec2 );
00069   RCP<std::vector<double> > vec3;
00070   TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
00071 }
00072 
00073 
00074 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, NonconstInitialize) {
00075   RCP<std::vector<double> > vec = rcp(new std::vector<double> );
00076   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00077   vectorObj.initialize(vec);
00078   TEST_ASSERT( vectorObj.isConst() == false );
00079   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00080   TEST_ASSERT( vec == vec2 );
00081   RCP<std::vector<double> > vec3 = vectorObj.getNonconstObj();
00082   TEST_ASSERT( vec == vec3 );
00083 }
00084 
00085 
00086 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, ConstInitialize) {
00087   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00088   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00089   vectorObj.initialize(vec);
00090   TEST_ASSERT( vectorObj.isConst() == true );
00091   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00092   TEST_ASSERT( vec == vec2 );
00093   RCP<std::vector<double> > vec3;
00094   TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
00095 }
00096 
00097 
00098 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, AssignmentFromRCP) {
00099   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00100   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00101   vectorObj = vec;
00102   TEST_ASSERT( vectorObj.isConst() == true );
00103   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00104   TEST_ASSERT( vec == vec2 );
00105   RCP<std::vector<double> > vec3;
00106   TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
00107 }
00108 
00109 
00110 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, uninitialize) {
00111   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00112   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00113   TEST_ASSERT( vectorObj.isConst() == true );
00114   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00115   TEST_ASSERT( vec == vec2 );
00116   vectorObj.uninitialize();
00117   TEST_ASSERT( vectorObj.isConst() == true );
00118   TEST_ASSERT( vectorObj.getConstObj() == null );
00119 }
00120 
00121 
00122 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, parens ) {
00123   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00124   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00125   vectorObj.initialize(vec);
00126   RCP<const std::vector<double> > vec2 = vectorObj();
00127   TEST_ASSERT( vec == vec2 );
00128 }
00129 
00130 
00131 // operator->
00132 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, arrow ) {
00133   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00134   vec->push_back(25.0);
00135   vec->push_back(32.0);
00136   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00137   TEST_ASSERT( vectorObj->size() == 2 );
00138 }
00139 
00140 
00141 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, arrowEmpty ) {
00142   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00143 #ifdef TEUCHOS_DEBUG
00144   TEST_THROW( vectorObj->size(), NullReferenceError );
00145 #endif
00146 }
00147 
00148 
00149 // operator*
00150 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, dereference ) {
00151   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00152   vec->push_back(25.0);
00153   vec->push_back(32.0);
00154   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00155   TEST_ASSERT( (*vectorObj)[0] == 25.0 );
00156   TEST_ASSERT( (*vectorObj)[1] == 32.0 );
00157 }
00158 
00159 
00160 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, dereferenceEmpty ) {
00161   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00162 #ifdef TEUCHOS_DEBUG
00163   TEST_THROW( (*vectorObj).size(), NullReferenceError );
00164 #endif
00165 }
00166 
00167 
00168 // implicit cast 
00169 //    RCP<const ObjType>  <- 
00170 //    ConstNonconstObjectContainer<const ObjType>
00171 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, castToRCP ) {
00172   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00173   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00174   RCP<const std::vector<double> > vec2(vectorObj);
00175   TEST_ASSERT( vec == vec2 );
00176 }
00177 
00178 
00179 // implicit cast 
00180 //    RCP<const ObjType>  -> 
00181 //    ConstNonconstObjectContainer<const ObjType>
00182 //    This is already done through the constructors on
00183 //    ConstNonconstObjectContainer.
00184 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, castFromRCP ) {
00185   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00186   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00187   TEST_ASSERT( vectorObj.getConstObj() == vec );
00188 }
00189 
00190 
00191 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, is_null ) {
00192   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00193   TEST_ASSERT( is_null(vectorObj) );
00194 }
00195 
00196 
00197 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, nonnull ) {
00198   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00199   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00200   TEST_ASSERT( nonnull(vectorObj) );
00201 }
00202 
00203 
00204 } // namespace Teuchos
00205 
00206 
00207 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:28 2011 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.3