29 #include "Teuchos_UnitTestHarness.hpp" 30 #include "Teuchos_TestingHelpers.hpp" 31 #include "Teuchos_UnitTestRepository.hpp" 32 #include "Teuchos_GlobalMPISession.hpp" 34 #include "Teuchos_Array.hpp" 42 using Teuchos::ValueTypeSerializer;
44 template <
typename TayType>
46 const std::string& tag,
47 Teuchos::FancyOStream& out) {
50 typedef Teuchos::SerializationTraits<Ordinal,TayType> SerT;
54 Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
55 char *charBuffer =
new char[bytes];
56 SerT::serialize(count, &x[0], bytes, charBuffer);
59 Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
60 Teuchos::Array<TayType> x2(count2);
61 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
66 bool success = (count == count2);
67 out << tag <<
" serialize/deserialize count test";
72 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 76 for (
Ordinal i=0; i<count; i++) {
78 out << tag <<
" serialize/deserialize taylor test " << i;
83 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
85 success = success && success2;
91 template <
typename TayType,
typename Serializer>
93 Teuchos::Array<TayType>& x,
94 const std::string& tag,
95 Teuchos::FancyOStream& out) {
101 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
102 char *charBuffer =
new char[bytes];
103 serializer.serialize(count, &x[0], bytes, charBuffer);
106 Ordinal sz = serializer.getSerializerSize();
107 for (
Ordinal i=0; i<count; i++)
108 x[i].resize(sz-1,
true);
111 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
112 Teuchos::Array<TayType> x2(count2);
113 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
115 delete [] charBuffer;
118 bool success = (count == count2);
119 out << tag <<
" serialize/deserialize count test";
124 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 128 for (
Ordinal i=0; i<count; i++) {
130 out << tag <<
" serialize/deserialize taylor test " << i;
135 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
137 success = success && success2;
143 template <
typename TayType,
typename Serializer>
145 Teuchos::Array<TayType>& x,
146 const std::string& tag,
147 Teuchos::FancyOStream& out) {
153 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
154 char *charBuffer =
new char[bytes];
155 serializer.serialize(count, &x[0], bytes, charBuffer);
158 Ordinal sz = serializer.getSerializerSize();
159 typedef typename Serializer::value_serializer_type VST;
160 RCP<const VST> vs = serializer.getValueSerializer();
161 Ordinal sz_inner = vs->getSerializerSize();
162 for (
Ordinal i=0; i<count; i++) {
163 x[i].resize(sz-1,
true);
165 x[i].fastAccessCoeff(j).resize(sz_inner-1,
true);
166 x[i].val().resize(sz_inner-1,
true);
170 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
171 Teuchos::Array<TayType> x2(count2);
172 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
174 delete [] charBuffer;
177 bool success = (count == count2);
178 out << tag <<
" serialize/deserialize count test";
183 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 187 for (
Ordinal i=0; i<count; i++) {
189 out << tag <<
" serialize/deserialize taylor test " << i;
194 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
196 success = success && success2;
202 #define TAY_SERIALIZATION_TESTS(TayType, TAY) \ 203 TEUCHOS_UNIT_TEST( TAY##_Serialization, Uniform ) { \ 206 ValueTypeSerializer<int,TayType> tts( \ 207 rcp(new ValueTypeSerializer<int,double>), p+1); \ 208 Teuchos::Array<TayType> x(n); \ 209 for (int i=0; i<n; i++) { \ 210 x[i] = TayType(p, rnd.number()); \ 211 for (int j=0; j<=p; j++) \ 212 x[i].fastAccessCoeff(j) = rnd.number(); \ 214 bool success1 = testSerialization( \ 215 x, std::string(#TAY) + " Uniform", out); \ 216 bool success2 = testSerializationObject( \ 217 tts, x, std::string(#TAY) + " Uniform TTS", out); \ 218 success = success1 && success2; \ 221 TEUCHOS_UNIT_TEST( TAY##_Serialization, Empty ) { \ 223 ValueTypeSerializer<int,TayType> tts( \ 224 rcp(new ValueTypeSerializer<int,double>), 7); \ 225 Teuchos::Array<TayType> x(n); \ 226 for (int i=0; i<n; i++) { \ 227 x[i] = TayType(rnd.number()); \ 229 bool success1 = testSerialization( \ 230 x, std::string(#TAY) + " Empty", out); \ 231 bool success2 = testSerializationObject( \ 232 tts, x, std::string(#TAY) + " Empty TTS", out); \ 233 success = success1 && success2; \ 236 TEUCHOS_UNIT_TEST( TAY##_Serialization, Mixed ) { \ 238 int p[] = { 5, 0, 8, 8, 3, 0 }; \ 239 ValueTypeSerializer<int,TayType> tts( \ 240 rcp(new ValueTypeSerializer<int,double>), 9); \ 241 Teuchos::Array<TayType> x(n); \ 242 for (int i=0; i<n; i++) { \ 243 x[i] = TayType(p[i], rnd.number()); \ 244 for (int j=0; j<=p[i]; j++) \ 245 x[i].fastAccessCoeff(j) = rnd.number(); \ 247 bool success1 = testSerialization( \ 248 x, std::string(#TAY) + " Mixed", out); \ 249 bool success2 = testSerializationObject( \ 250 tts, x, std::string(#TAY) + " Mixed TTS", out); \ 251 success = success1 && success2; \ 254 TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedUniform ) { \ 255 typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \ 259 RCP< ValueTypeSerializer<int,TayType> > tts = \ 260 rcp(new ValueTypeSerializer<int,TayType>( \ 261 rcp(new ValueTypeSerializer<int,double>), p1+1)); \ 262 ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \ 263 Teuchos::Array<TayTayType> x(n); \ 264 for (int i=0; i<n; i++) { \ 265 TayType f(p1, rnd.number()); \ 266 for (int k=0; k<=p1; k++) \ 267 f.fastAccessCoeff(k) = rnd.number(); \ 268 x[i] = TayTayType(p2, f); \ 269 for (int j=0; j<=p2; j++) { \ 270 TayType g(p1, rnd.number()); \ 271 for (int k=0; k<=p1; k++) \ 272 g.fastAccessCoeff(k) = rnd.number(); \ 273 x[i].fastAccessCoeff(j) = g; \ 276 bool success1 = testSerialization( \ 277 x, std::string(#TAY) + " Nested Uniform", out); \ 278 bool success2 = testNestedSerializationObject( \ 279 ttts, x, std::string(#TAY) + " Nested Uniform TTS", out); \ 280 success = success1 && success2; \ 283 TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyInner ) { \ 284 typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \ 288 RCP< ValueTypeSerializer<int,TayType> > tts = \ 289 rcp(new ValueTypeSerializer<int,TayType>( \ 290 rcp(new ValueTypeSerializer<int,double>), p1+1)); \ 291 ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \ 292 Teuchos::Array<TayTayType> x(n); \ 293 for (int i=0; i<n; i++) { \ 294 TayType f(p1, rnd.number()); \ 295 for (int k=0; k<=p1; k++) \ 296 f.fastAccessCoeff(k) = rnd.number(); \ 297 x[i] = TayTayType(p2, f); \ 298 for (int j=0; j<=p2; j++) \ 299 x[i].fastAccessCoeff(j) = rnd.number(); \ 301 bool success1 = testSerialization( \ 302 x, std::string(#TAY) + " Nested Empty Inner", out); \ 303 bool success2 = testNestedSerializationObject( \ 304 ttts, x, std::string(#TAY) + " Nested Empty Inner TTS", out); \ 305 success = success1 && success2; \ 308 TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyOuter ) { \ 309 typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \ 312 RCP< ValueTypeSerializer<int,TayType> > tts = \ 313 rcp(new ValueTypeSerializer<int,TayType>( \ 314 rcp(new ValueTypeSerializer<int,double>), p1+1)); \ 315 ValueTypeSerializer<int,TayTayType> ttts(tts, 5); \ 316 Teuchos::Array<TayTayType> x(n); \ 317 for (int i=0; i<n; i++) { \ 318 TayType f(p1, rnd.number()); \ 319 for (int k=0; k<=p1; k++) \ 320 f.fastAccessCoeff(k) = rnd.number(); \ 321 x[i] = TayTayType(f); \ 323 bool success1 = testSerialization( \ 324 x, std::string(#TAY) + " Nested Empty Outer", out); \ 325 bool success2 = testNestedSerializationObject( \ 326 ttts, x, std::string(#TAY) + " Nested Empty Outer TTS", out); \ 327 success = success1 && success2; \ 330 TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyAll ) { \ 331 typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \ 333 RCP< ValueTypeSerializer<int,TayType> > tts = \ 334 rcp(new ValueTypeSerializer<int,TayType>( \ 335 rcp(new ValueTypeSerializer<int,double>), 5)); \ 336 ValueTypeSerializer<int,TayTayType> ttts(tts, 5); \ 337 Teuchos::Array<TayTayType> x(n); \ 338 for (int i=0; i<n; i++) { \ 339 x[i] = rnd.number(); \ 341 bool success1 = testSerialization( \ 342 x, std::string(#TAY) + " Nested Empty All", out); \ 343 bool success2 = testNestedSerializationObject( \ 344 ttts, x, std::string(#TAY) + " Nested Empty All TTS", out); \ 345 success = success1 && success2; \ 352 int
main(
int argc,
char* argv[] ) {
353 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
354 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
bool testSerializationObject(const Serializer &serializer, Teuchos::Array< TayType > &x, const std::string &tag, Teuchos::FancyOStream &out)
int main(int argc, char *argv[])
static KOKKOS_INLINE_FUNCTION bool eval(const T &x, const T &y)
Forward-mode AD class using dynamic memory allocation.
#define TAY_SERIALIZATION_TESTS(TayType, TAY)
bool testNestedSerializationObject(const Serializer &serializer, Teuchos::Array< TayType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Sacado::Random< double > rnd
bool testSerialization(const Teuchos::Array< TayType > &x, const std::string &tag, Teuchos::FancyOStream &out)