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"
42using Teuchos::ValueTypeSerializer;
44template <
typename FadType>
46 const std::string& tag,
47 Teuchos::FancyOStream& out) {
50 typedef Teuchos::SerializationTraits<Ordinal,FadType> SerT;
55 char *charBuffer =
new char[bytes];
56 SerT::serialize(
count, &
x[0], bytes, charBuffer);
59 Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
60 Teuchos::Array<FadType> 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 <<
"."
78 out << tag <<
" serialize/deserialize fad test " <<
i;
83 out <<
": \n\tExpected: " <<
x[
i] <<
", \n\tGot: " << x2[
i]
85 success = success && success2;
91template <
typename FadType,
typename Serializer>
93 Teuchos::Array<FadType>&
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();
111 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
112 Teuchos::Array<FadType> 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 <<
"."
130 out << tag <<
" serialize/deserialize fad test " <<
i;
135 out <<
": \n\tExpected: " <<
x[
i] <<
", \n\tGot: " << x2[
i]
137 success = success && success2;
143template <
typename FadType,
typename Serializer>
145 Teuchos::Array<FadType>&
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();
166 x[
i].val().expand(sz_inner);
170 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
171 Teuchos::Array<FadType> 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 <<
"."
189 out << tag <<
" serialize/deserialize fad test " <<
i;
194 out <<
": \n\tExpected: " <<
x[
i] <<
", \n\tGot: " << x2[
i]
196 success = success && success2;
202#define FAD_SERIALIZATION_TESTS(FadType, FAD) \
203 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \
206 ValueTypeSerializer<int,FadType> fts( \
207 rcp(new ValueTypeSerializer<int,double>), p); \
208 Teuchos::Array<FadType> x(n); \
209 for (int i=0; i<n; i++) { \
210 x[i] = FadType(p, rnd.number()); \
211 for (int j=0; j<p; j++) \
212 x[i].fastAccessDx(j) = rnd.number(); \
214 bool success1 = testSerialization( \
215 x, std::string(#FAD) + " Uniform", out); \
216 bool success2 = testSerializationObject( \
217 fts, x, std::string(#FAD) + " Uniform FTS", out); \
218 success = success1 && success2; \
221 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadEmpty ) { \
223 ValueTypeSerializer<int,FadType> fts( \
224 rcp(new ValueTypeSerializer<int,double>), 5); \
225 Teuchos::Array<FadType> x(n); \
226 for (int i=0; i<n; i++) { \
227 x[i] = FadType(rnd.number()); \
229 bool success1 = testSerialization(x, std::string( \
230 #FAD) + " Empty", out); \
231 bool success2 = testSerializationObject( \
232 fts, x, std::string(#FAD) + " Empty FTS", out); \
233 success = success1 && success2; \
236 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadMixed ) { \
238 int p[] = { 5, 0, 8, 8, 3, 0 }; \
239 ValueTypeSerializer<int,FadType> fts( \
240 rcp(new ValueTypeSerializer<int,double>), 8); \
241 Teuchos::Array<FadType> x(n); \
242 for (int i=0; i<n; i++) { \
243 x[i] = FadType(p[i], rnd.number()); \
244 for (int j=0; j<p[i]; j++) \
245 x[i].fastAccessDx(j) = rnd.number(); \
247 bool success1 = testSerialization( \
248 x, std::string(#FAD) + " Mixed", out); \
249 bool success2 = testSerializationObject( \
250 fts, x, std::string(#FAD) + " Mixed FTS", out); \
251 success = success1 && success2; \
254 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \
255 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
259 RCP< ValueTypeSerializer<int,FadType> > fts = \
260 rcp(new ValueTypeSerializer<int,FadType>( \
261 rcp(new ValueTypeSerializer<int,double>), p1)); \
262 ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
263 Teuchos::Array<FadFadType> x(n); \
264 for (int i=0; i<n; i++) { \
265 FadType f(p1, rnd.number()); \
266 for (int k=0; k<p1; k++) \
267 f.fastAccessDx(k) = rnd.number(); \
268 x[i] = FadFadType(p2, f); \
269 for (int j=0; j<p2; j++) { \
270 FadType g(p1, rnd.number()); \
271 for (int k=0; k<p1; k++) \
272 g.fastAccessDx(k) = rnd.number(); \
273 x[i].fastAccessDx(j) = g; \
276 bool success1 = testSerialization( \
277 x, std::string(#FAD) + " Nested Uniform", out); \
279 testNestedSerializationObject( \
280 ffts, x, std::string(#FAD) + " Nested Uniform", out); \
281 success = success1 && success2; \
284 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyInner ) { \
285 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
289 RCP< ValueTypeSerializer<int,FadType> > fts = \
290 rcp(new ValueTypeSerializer<int,FadType>( \
291 rcp(new ValueTypeSerializer<int,double>), p1)); \
292 ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
293 Teuchos::Array<FadFadType> x(n); \
294 for (int i=0; i<n; i++) { \
295 FadType f(p1, rnd.number()); \
296 for (int k=0; k<p1; k++) \
297 f.fastAccessDx(k) = rnd.number(); \
298 x[i] = FadFadType(p2, f); \
299 for (int j=0; j<p2; j++) \
300 x[i].fastAccessDx(j) = rnd.number(); \
302 bool success1 = testSerialization( \
303 x, std::string(#FAD) + " Nested Empty Inner", out); \
304 bool success2 = testNestedSerializationObject( \
305 ffts, x, std::string(#FAD) + " Nested Empty Inner FTS", out); \
306 success = success1 && success2; \
309 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyOuter ) { \
310 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
313 RCP< ValueTypeSerializer<int,FadType> > fts = \
314 rcp(new ValueTypeSerializer<int,FadType>( \
315 rcp(new ValueTypeSerializer<int,double>), p1)); \
316 ValueTypeSerializer<int,FadFadType> ffts(fts, 5); \
317 Teuchos::Array<FadFadType> x(n); \
318 for (int i=0; i<n; i++) { \
319 FadType f(p1, rnd.number()); \
320 for (int k=0; k<p1; k++) \
321 f.fastAccessDx(k) = rnd.number(); \
322 x[i] = FadFadType(f); \
324 bool success1 =testSerialization( \
325 x, std::string(#FAD) + " Nested Empty Outer", out); \
326 bool success2 =testNestedSerializationObject( \
327 ffts, x, std::string(#FAD) + " Nested Empty Outer FTS", out); \
328 success = success1 && success2; \
331 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyAll ) { \
332 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
334 RCP< ValueTypeSerializer<int,FadType> > fts = \
335 rcp(new ValueTypeSerializer<int,FadType>( \
336 rcp(new ValueTypeSerializer<int,double>), 5)); \
337 ValueTypeSerializer<int,FadFadType> ffts(fts, 5); \
338 Teuchos::Array<FadFadType> x(n); \
339 for (int i=0; i<n; i++) { \
340 x[i] = rnd.number(); \
342 bool success1 = testSerialization( \
343 x, std::string(#FAD) + " Nested Empty All", out); \
344 bool success2 = testNestedSerializationObject( \
345 ffts, x, std::string(#FAD) + " Nested Empty All FTS", out); \
346 success = success1 && success2; \
349#define SFAD_SERIALIZATION_TESTS(FadType, FAD) \
350 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \
353 ValueTypeSerializer<int,FadType> fts( \
354 rcp(new ValueTypeSerializer<int,double>), p); \
355 Teuchos::Array<FadType> x(n); \
356 for (int i=0; i<n; i++) { \
357 x[i] = FadType(p, rnd.number()); \
358 for (int j=0; j<p; j++) \
359 x[i].fastAccessDx(j) = rnd.number(); \
361 bool success1 = testSerialization( \
362 x, std::string(#FAD) + " Uniform", out); \
363 bool success2 = testSerializationObject( \
364 fts, x, std::string(#FAD) + " Uniform FTS", out); \
365 success = success1 && success2; \
368 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \
369 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
373 RCP< ValueTypeSerializer<int,FadType> > fts = \
374 rcp(new ValueTypeSerializer<int,FadType>( \
375 rcp(new ValueTypeSerializer<int,double>), p1)); \
376 ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
377 Teuchos::Array<FadFadType> x(n); \
378 for (int i=0; i<n; i++) { \
379 FadType f(p1, rnd.number()); \
380 for (int k=0; k<p1; k++) \
381 f.fastAccessDx(k) = rnd.number(); \
382 x[i] = FadFadType(p2, f); \
383 for (int j=0; j<p2; j++) { \
384 FadType g(p1, rnd.number()); \
385 for (int k=0; k<p1; k++) \
386 g.fastAccessDx(k) = rnd.number(); \
387 x[i].fastAccessDx(j) = g; \
390 bool success1 = testSerialization( \
391 x, std::string(#FAD) + " Nested Uniform", out); \
392 bool success2 = testNestedSerializationObject( \
393 ffts, x, std::string(#FAD) + " Nested Uniform FTS", out); \
394 success = success1 && success2; \
427int main(
int argc,
char* argv[] ) {
428 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
430 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
bool testSerialization(const Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
bool testSerializationObject(const Serializer &serializer, Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Sacado::Random< double > rnd
#define SFAD_SERIALIZATION_TESTS(FadType, FAD)
bool testNestedSerializationObject(const Serializer &serializer, Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
#define FAD_SERIALIZATION_TESTS(FadType, FAD)
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
A random number generator that generates random numbers uniformly distributed in the interval (a,...
static SACADO_INLINE_FUNCTION bool eval(const T &x, const T &y)