Sacado Package Browser (Single Doxygen Collection) Version of the Day
Tay_SerializationTests.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                           Sacado Package
00005 //                 Copyright (2006) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
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 David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00025 // (etphipp@sandia.gov).
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 #include "Teuchos_UnitTestHarness.hpp"
00030 #include "Teuchos_TestingHelpers.hpp"
00031 #include "Teuchos_UnitTestRepository.hpp"
00032 #include "Teuchos_GlobalMPISession.hpp"
00033 
00034 #include "Teuchos_Array.hpp"
00035 #include "Sacado.hpp"
00036 #include "Sacado_Tay_CacheTaylor.hpp"
00037 #include "Sacado_mpl_apply.hpp"
00038 #include "Sacado_Random.hpp"
00039 
00040 using Teuchos::RCP;
00041 using Teuchos::rcp;
00042 using Teuchos::ValueTypeSerializer;
00043 
00044 template <typename TayType>
00045 bool testSerialization(const Teuchos::Array<TayType>& x, 
00046            const std::string& tag,
00047            Teuchos::FancyOStream& out) {
00048   
00049   typedef int Ordinal;
00050   typedef Teuchos::SerializationTraits<Ordinal,TayType> SerT;
00051   
00052   // Serialize
00053   Ordinal count = x.size();
00054   Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
00055   char *charBuffer = new char[bytes];
00056   SerT::serialize(count, &x[0], bytes, charBuffer);
00057   
00058   // Deserialize
00059   Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
00060   Teuchos::Array<TayType> x2(count2);
00061   SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
00062 
00063   delete [] charBuffer;
00064   
00065   // Check counts match
00066   bool success = (count == count2);
00067   out << tag << " serialize/deserialize count test";
00068   if (success)
00069     out << " passed";
00070   else
00071     out << " failed";
00072   out << ":  \n\tExpected:  " << count << ", \n\tGot:       " << count2 << "." 
00073       << std::endl;
00074   
00075   // Check coefficients match
00076   for (Ordinal i=0; i<count; i++) {
00077     bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
00078     out << tag << " serialize/deserialize taylor test " << i;
00079     if (success2)
00080       out << " passed";
00081     else
00082   out << " failed";
00083     out << ":  \n\tExpected:  " << x[i] << ", \n\tGot:       " << x2[i] 
00084   << "." << std::endl;
00085     success = success && success2;
00086   }
00087 
00088   return success;
00089 }
00090 
00091 template <typename TayType, typename Serializer>
00092 bool testSerializationObject(const Serializer& serializer,
00093            Teuchos::Array<TayType>& x, 
00094            const std::string& tag,
00095            Teuchos::FancyOStream& out) {
00096   
00097   typedef int Ordinal;
00098   
00099   // Serialize
00100   Ordinal count = x.size();
00101   Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
00102   char *charBuffer = new char[bytes];
00103   serializer.serialize(count, &x[0], bytes, charBuffer);
00104 
00105   // Expand x to serializer size
00106   Ordinal sz = serializer.getSerializerSize();
00107   for (Ordinal i=0; i<count; i++)
00108     x[i].resize(sz-1, true);
00109   
00110   // Deserialize
00111   Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
00112   Teuchos::Array<TayType> x2(count2);
00113   serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
00114 
00115   delete [] charBuffer;
00116   
00117   // Check counts match
00118   bool success = (count == count2);
00119   out << tag << " serialize/deserialize count test";
00120   if (success)
00121     out << " passed";
00122   else
00123     out << " failed";
00124   out << ":  \n\tExpected:  " << count << ", \n\tGot:       " << count2 << "." 
00125       << std::endl;
00126   
00127   // Check coefficients match
00128   for (Ordinal i=0; i<count; i++) {
00129     bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
00130     out << tag << " serialize/deserialize taylor test " << i;
00131     if (success2)
00132       out << " passed";
00133     else
00134   out << " failed";
00135     out << ":  \n\tExpected:  " << x[i] << ", \n\tGot:       " << x2[i] 
00136   << "." << std::endl;
00137     success = success && success2;
00138   }
00139 
00140   return success;
00141 }
00142 
00143 template <typename TayType, typename Serializer>
00144 bool testNestedSerializationObject(const Serializer& serializer,
00145            Teuchos::Array<TayType>& x, 
00146            const std::string& tag,
00147            Teuchos::FancyOStream& out) {
00148   
00149   typedef int Ordinal;
00150   
00151   // Serialize
00152   Ordinal count = x.size();
00153   Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
00154   char *charBuffer = new char[bytes];
00155   serializer.serialize(count, &x[0], bytes, charBuffer);
00156 
00157   // Expand x to serializer size
00158   Ordinal sz = serializer.getSerializerSize();
00159   typedef typename Serializer::value_serializer_type VST;
00160   RCP<const VST> vs = serializer.getValueSerializer();
00161   Ordinal sz_inner = vs->getSerializerSize();
00162   for (Ordinal i=0; i<count; i++) {
00163     x[i].resize(sz-1, true);
00164     for (Ordinal j=0; j<sz; j++)
00165       x[i].fastAccessCoeff(j).resize(sz_inner-1, true);
00166     x[i].val().resize(sz_inner-1, true);
00167   }
00168   
00169   // Deserialize
00170   Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
00171   Teuchos::Array<TayType> x2(count2);
00172   serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
00173 
00174   delete [] charBuffer;
00175   
00176   // Check counts match
00177   bool success = (count == count2);
00178   out << tag << " serialize/deserialize count test";
00179   if (success)
00180     out << " passed";
00181   else
00182     out << " failed";
00183   out << ":  \n\tExpected:  " << count << ", \n\tGot:       " << count2 << "." 
00184       << std::endl;
00185   
00186   // Check coefficients match
00187   for (Ordinal i=0; i<count; i++) {
00188     bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
00189     out << tag << " serialize/deserialize taylor test " << i;
00190     if (success2)
00191       out << " passed";
00192     else
00193   out << " failed";
00194     out << ":  \n\tExpected:  " << x[i] << ", \n\tGot:       " << x2[i] 
00195   << "." << std::endl;
00196     success = success && success2;
00197   }
00198 
00199   return success;
00200 }
00201 
00202 #define TAY_SERIALIZATION_TESTS(TayType, TAY)       \
00203   TEUCHOS_UNIT_TEST( TAY##_Serialization, Uniform ) {     \
00204     int n = 7;                \
00205     int p = 5;                \
00206     ValueTypeSerializer<int,TayType> tts(       \
00207       rcp(new ValueTypeSerializer<int,double>), p+1);     \
00208     Teuchos::Array<TayType> x(n);         \
00209     for (int i=0; i<n; i++) {           \
00210       x[i] = TayType(p, rnd.number());          \
00211       for (int j=0; j<=p; j++)            \
00212   x[i].fastAccessCoeff(j) = rnd.number();       \
00213     }                 \
00214     bool success1 = testSerialization(          \
00215       x, std::string(#TAY) + " Uniform", out);        \
00216     bool success2 = testSerializationObject(        \
00217       tts, x, std::string(#TAY) + " Uniform TTS", out);     \
00218     success = success1 && success2;         \
00219   }                 \
00220                   \
00221   TEUCHOS_UNIT_TEST( TAY##_Serialization, Empty ) {     \
00222     int n = 7;                \
00223     ValueTypeSerializer<int,TayType> tts(       \
00224       rcp(new ValueTypeSerializer<int,double>), 7);     \
00225     Teuchos::Array<TayType> x(n);         \
00226     for (int i=0; i<n; i++) {           \
00227       x[i] = TayType(rnd.number());         \
00228     }                 \
00229     bool success1 = testSerialization(          \
00230       x, std::string(#TAY) + " Empty", out);        \
00231     bool success2 = testSerializationObject(        \
00232       tts, x, std::string(#TAY) + " Empty TTS", out);     \
00233     success = success1 && success2;         \
00234   }                 \
00235                   \
00236   TEUCHOS_UNIT_TEST( TAY##_Serialization, Mixed ) {     \
00237     int n = 6;                \
00238     int p[] = { 5, 0, 8, 8, 3, 0 };         \
00239     ValueTypeSerializer<int,TayType> tts(       \
00240       rcp(new ValueTypeSerializer<int,double>), 9);     \
00241     Teuchos::Array<TayType> x(n);         \
00242     for (int i=0; i<n; i++) {           \
00243       x[i] = TayType(p[i], rnd.number());       \
00244       for (int j=0; j<=p[i]; j++)         \
00245   x[i].fastAccessCoeff(j) = rnd.number();       \
00246     }                 \
00247     bool success1 = testSerialization(          \
00248       x, std::string(#TAY) + " Mixed", out);        \
00249     bool success2 = testSerializationObject(        \
00250       tts, x, std::string(#TAY) + " Mixed TTS", out);     \
00251     success = success1 && success2;         \
00252   }                 \
00253                   \
00254   TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedUniform ) {   \
00255     typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
00256     int n = 7;                \
00257     int p1 = 5;               \
00258     int p2 = 3;               \
00259     RCP< ValueTypeSerializer<int,TayType> > tts =     \
00260       rcp(new ValueTypeSerializer<int,TayType>(       \
00261       rcp(new ValueTypeSerializer<int,double>), p1+1));   \
00262      ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1);   \
00263     Teuchos::Array<TayTayType> x(n);          \
00264     for (int i=0; i<n; i++) {           \
00265       TayType f(p1, rnd.number());          \
00266       for (int k=0; k<=p1; k++)           \
00267   f.fastAccessCoeff(k) = rnd.number();        \
00268       x[i] = TayTayType(p2, f);           \
00269       for (int j=0; j<=p2; j++) {         \
00270   TayType g(p1, rnd.number());          \
00271   for (int k=0; k<=p1; k++)         \
00272     g.fastAccessCoeff(k) = rnd.number();        \
00273   x[i].fastAccessCoeff(j) = g;          \
00274       }                 \
00275     }                 \
00276     bool success1 = testSerialization(          \
00277       x, std::string(#TAY) + " Nested Uniform", out);     \
00278     bool success2 = testNestedSerializationObject(      \
00279       ttts, x, std::string(#TAY) + " Nested Uniform TTS", out);   \
00280     success = success1 && success2;         \
00281   }                 \
00282                   \
00283   TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyInner ) {    \
00284     typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
00285     int n = 7;                \
00286     int p1 = 5;               \
00287     int p2 = 3;               \
00288     RCP< ValueTypeSerializer<int,TayType> > tts =     \
00289       rcp(new ValueTypeSerializer<int,TayType>(       \
00290       rcp(new ValueTypeSerializer<int,double>), p1+1));   \
00291      ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1);   \
00292     Teuchos::Array<TayTayType> x(n);          \
00293     for (int i=0; i<n; i++) {           \
00294       TayType f(p1, rnd.number());          \
00295       for (int k=0; k<=p1; k++)           \
00296   f.fastAccessCoeff(k) = rnd.number();        \
00297       x[i] = TayTayType(p2, f);           \
00298       for (int j=0; j<=p2; j++)           \
00299   x[i].fastAccessCoeff(j) =  rnd.number();      \
00300     }                 \
00301     bool success1 = testSerialization(          \
00302       x, std::string(#TAY) + " Nested Empty Inner", out);   \
00303     bool success2 = testNestedSerializationObject(      \
00304       ttts, x, std::string(#TAY) + " Nested Empty Inner TTS", out); \
00305     success = success1 && success2;         \
00306   }                 \
00307                   \
00308   TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyOuter ) {    \
00309     typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
00310     int n = 7;                \
00311     int p1 = 5;               \
00312     RCP< ValueTypeSerializer<int,TayType> > tts =     \
00313       rcp(new ValueTypeSerializer<int,TayType>(       \
00314       rcp(new ValueTypeSerializer<int,double>), p1+1));   \
00315     ValueTypeSerializer<int,TayTayType> ttts(tts, 5);     \
00316     Teuchos::Array<TayTayType> x(n);          \
00317     for (int i=0; i<n; i++) {           \
00318       TayType f(p1, rnd.number());          \
00319       for (int k=0; k<=p1; k++)           \
00320   f.fastAccessCoeff(k) = rnd.number();        \
00321       x[i] = TayTayType(f);           \
00322     }                 \
00323     bool success1 = testSerialization(          \
00324       x, std::string(#TAY) + " Nested Empty Outer", out);   \
00325     bool success2 = testNestedSerializationObject(      \
00326       ttts, x, std::string(#TAY) + " Nested Empty Outer TTS", out); \
00327     success = success1 && success2;         \
00328   }                 \
00329                   \
00330   TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyAll ) {    \
00331     typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
00332     int n = 7;                \
00333     RCP< ValueTypeSerializer<int,TayType> > tts =     \
00334       rcp(new ValueTypeSerializer<int,TayType>(       \
00335       rcp(new ValueTypeSerializer<int,double>), 5));    \
00336     ValueTypeSerializer<int,TayTayType> ttts(tts, 5);     \
00337     Teuchos::Array<TayTayType> x(n);          \
00338     for (int i=0; i<n; i++) {           \
00339       x[i] = rnd.number();            \
00340     }                 \
00341     bool success1 = testSerialization(          \
00342       x, std::string(#TAY) + " Nested Empty All", out);     \
00343     bool success2 = testNestedSerializationObject(      \
00344       ttts, x, std::string(#TAY) + " Nested Empty All TTS", out); \
00345     success = success1 && success2;         \
00346   }                 \
00347 
00348 Sacado::Random<double> rnd;
00349 TAY_SERIALIZATION_TESTS(Sacado::Tay::Taylor<double>, Taylor)
00350 TAY_SERIALIZATION_TESTS(Sacado::Tay::CacheTaylor<double>, CacheTaylor)
00351 
00352 int main( int argc, char* argv[] ) {
00353   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00354   return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
00355 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines