Teuchos Package Browser (Single Doxygen Collection) Version of the Day
ConstNonconstObjectContainer.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_ConstNonconstObjectContainer.hpp"
00043 
00044 #include "Teuchos_UnitTestHarness.hpp"
00045 
00046 namespace Teuchos {
00047 
00048 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, create ) {
00049   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00050   RCP<std::vector<double> > vec = rcp(new std::vector<double> );
00051   vectorObj.initialize(vec); // nonconst
00052   TEST_ASSERT( vectorObj.isConst() == false );
00053 }
00054 
00055 
00056 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, DefaultConstruct ) {
00057   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00058   TEST_ASSERT( vectorObj.isConst() == true );
00059   TEST_ASSERT( vectorObj.getConstObj() == null );
00060   // This does not throw an exception because the pointer is null
00061   TEST_ASSERT( vectorObj.getNonconstObj() == null );
00062 }
00063 
00064 
00065 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, NonconstConstruct ) {
00066   RCP<std::vector<double> > vec = rcp(new std::vector<double> );
00067   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00068   TEST_ASSERT( vectorObj.isConst() == false );
00069   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00070   TEST_ASSERT( vec == vec2 );
00071   RCP<std::vector<double> > vec3 = vectorObj.getNonconstObj();
00072   TEST_ASSERT( vec == vec3 );
00073 }
00074 
00075 
00076 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, ConstConstruct) {
00077   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00078   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00079   TEST_ASSERT( vectorObj.isConst() == true );
00080   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00081   TEST_ASSERT( vec == vec2 );
00082   RCP<std::vector<double> > vec3;
00083   TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
00084 }
00085 
00086 
00087 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, NonconstInitialize) {
00088   RCP<std::vector<double> > vec = rcp(new std::vector<double> );
00089   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00090   vectorObj.initialize(vec);
00091   TEST_ASSERT( vectorObj.isConst() == false );
00092   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00093   TEST_ASSERT( vec == vec2 );
00094   RCP<std::vector<double> > vec3 = vectorObj.getNonconstObj();
00095   TEST_ASSERT( vec == vec3 );
00096 }
00097 
00098 
00099 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, ConstInitialize) {
00100   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00101   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00102   vectorObj.initialize(vec);
00103   TEST_ASSERT( vectorObj.isConst() == true );
00104   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00105   TEST_ASSERT( vec == vec2 );
00106   RCP<std::vector<double> > vec3;
00107   TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
00108 }
00109 
00110 
00111 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, AssignmentFromRCP) {
00112   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00113   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00114   vectorObj = vec;
00115   TEST_ASSERT( vectorObj.isConst() == true );
00116   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00117   TEST_ASSERT( vec == vec2 );
00118   RCP<std::vector<double> > vec3;
00119   TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
00120 }
00121 
00122 
00123 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, uninitialize) {
00124   RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
00125   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00126   TEST_ASSERT( vectorObj.isConst() == true );
00127   RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
00128   TEST_ASSERT( vec == vec2 );
00129   vectorObj.uninitialize();
00130   TEST_ASSERT( vectorObj.isConst() == true );
00131   TEST_ASSERT( vectorObj.getConstObj() == null );
00132 }
00133 
00134 
00135 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, parens ) {
00136   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00137   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00138   vectorObj.initialize(vec);
00139   RCP<const std::vector<double> > vec2 = vectorObj();
00140   TEST_ASSERT( vec == vec2 );
00141 }
00142 
00143 
00144 // operator->
00145 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, arrow ) {
00146   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00147   vec->push_back(25.0);
00148   vec->push_back(32.0);
00149   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00150   TEST_ASSERT( vectorObj->size() == 2 );
00151 }
00152 
00153 
00154 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, arrowEmpty ) {
00155   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00156 #ifdef TEUCHOS_DEBUG
00157   TEST_THROW( vectorObj->size(), NullReferenceError );
00158 #endif
00159 }
00160 
00161 
00162 // operator*
00163 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, dereference ) {
00164   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00165   vec->push_back(25.0);
00166   vec->push_back(32.0);
00167   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00168   TEST_ASSERT( (*vectorObj)[0] == 25.0 );
00169   TEST_ASSERT( (*vectorObj)[1] == 32.0 );
00170 }
00171 
00172 
00173 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, dereferenceEmpty ) {
00174   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00175 #ifdef TEUCHOS_DEBUG
00176   TEST_THROW( (*vectorObj).size(), NullReferenceError );
00177 #endif
00178 }
00179 
00180 
00181 // implicit cast 
00182 //    RCP<const ObjType>  <- 
00183 //    ConstNonconstObjectContainer<const ObjType>
00184 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, castToRCP ) {
00185   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00186   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00187   RCP<const std::vector<double> > vec2(vectorObj);
00188   TEST_ASSERT( vec == vec2 );
00189 }
00190 
00191 
00192 // implicit cast 
00193 //    RCP<const ObjType>  -> 
00194 //    ConstNonconstObjectContainer<const ObjType>
00195 //    This is already done through the constructors on
00196 //    ConstNonconstObjectContainer.
00197 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, castFromRCP ) {
00198   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00199   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00200   TEST_ASSERT( vectorObj.getConstObj() == vec );
00201 }
00202 
00203 
00204 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, is_null ) {
00205   ConstNonconstObjectContainer<std::vector<double> > vectorObj;
00206   TEST_ASSERT( is_null(vectorObj) );
00207 }
00208 
00209 
00210 TEUCHOS_UNIT_TEST( ConstNonconstObjectContainer, nonnull ) {
00211   RCP<std::vector<double> > vec = rcp(new std::vector<double>);
00212   ConstNonconstObjectContainer<std::vector<double> > vectorObj(vec);
00213   TEST_ASSERT( nonnull(vectorObj) );
00214 }
00215 
00216 
00217 } // namespace Teuchos
00218 
00219 
00220 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines