Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
MoveTests.cpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Sacado Package
5// Copyright (2006) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// This library is free software; you can redistribute it and/or modify
11// it under the terms of the GNU Lesser General Public License as
12// published by the Free Software Foundation; either version 2.1 of the
13// License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23// USA
24// Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25// (etphipp@sandia.gov).
26//
27// ***********************************************************************
28// @HEADER
29
30#include "Teuchos_UnitTestHarness.hpp"
31#include "Teuchos_UnitTestRepository.hpp"
32#include "Teuchos_GlobalMPISession.hpp"
33#include "Teuchos_TestingHelpers.hpp"
34
35#include "Sacado_No_Kokkos.hpp"
36
37#if SACADO_ENABLE_NEW_DESIGN
40
41// Size used for all Fad types
42const int global_fad_size = 10;
43
44//
45// Move constructor tests
46//
47
48TEUCHOS_UNIT_TEST( MoveConstructorTests, SFad )
49{
50 typedef double value_type;
52 success = true;
53
54 // Initialize AD type
55 ad_type x(global_fad_size, value_type(1.5));
56 for (int i=0; i<global_fad_size; ++i)
57 x.fastAccessDx(i) = value_type(2.0+i);
58
59 // Move x into y
60 ad_type y = std::move(x);
61
62 // Check y is correct
63 TEST_EQUALITY_CONST( y.size(), global_fad_size );
64 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
65 for (int i=0; i<global_fad_size; ++i) {
66 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
67 }
68
69 // Check x is correct
70 TEST_EQUALITY_CONST( x.size(), global_fad_size );
71 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
72 TEST_INEQUALITY( x.dx(), y.dx() );
73 for (int i=0; i<global_fad_size; ++i) {
74 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
75 }
76}
77
78TEUCHOS_UNIT_TEST( MoveConstructorTests, SLFad )
79{
80 typedef double value_type;
82 success = true;
83
84 // Initialize AD type
85 ad_type x(global_fad_size, value_type(1.5));
86 for (int i=0; i<global_fad_size; ++i)
87 x.fastAccessDx(i) = value_type(2.0+i);
88
89 // Move x into y
90 ad_type y = std::move(x);
91
92 // Check y is correct
93 TEST_EQUALITY_CONST( y.size(), global_fad_size );
94 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
95 for (int i=0; i<global_fad_size; ++i) {
96 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
97 }
98
99 // Check x is correct
100 TEST_EQUALITY_CONST( x.size(), global_fad_size );
101 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
102 TEST_INEQUALITY( x.dx(), y.dx() );
103 for (int i=0; i<global_fad_size; ++i) {
104 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
105 }
106}
107
108TEUCHOS_UNIT_TEST( MoveConstructorTests, DFad )
109{
110 typedef double value_type;
112 success = true;
113
114 // Initialize AD type
115 ad_type x(global_fad_size, value_type(1.5));
116 for (int i=0; i<global_fad_size; ++i)
117 x.fastAccessDx(i) = value_type(2.0+i);
118
119 // Move x into y
120 ad_type y = std::move(x);
121
122 // Check y is correct
123 TEST_EQUALITY_CONST( y.size(), global_fad_size );
124 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
125 for (int i=0; i<global_fad_size; ++i) {
126 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
127 }
128
129 // Check x is correct
130 value_type *null_ptr = nullptr;
131 TEST_EQUALITY_CONST( x.size(), 0 );
132 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
133 TEST_EQUALITY( x.dx(), null_ptr );
134}
135
136TEUCHOS_UNIT_TEST( MoveConstructorTests, DVFad_Owned )
137{
138 typedef double value_type;
140 success = true;
141
142 // Initialize AD type
143 ad_type x(global_fad_size, value_type(1.5));
144 for (int i=0; i<global_fad_size; ++i)
145 x.fastAccessDx(i) = value_type(2.0+i);
146
147 // Move x into y
148 ad_type y = std::move(x);
149
150 // Check y is correct
151 TEST_EQUALITY_CONST( y.size(), global_fad_size );
152 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
153 for (int i=0; i<global_fad_size; ++i) {
154 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
155 }
156
157 // Check x is correct
158 TEST_EQUALITY_CONST( x.size(), global_fad_size );
159 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
160 TEST_INEQUALITY( x.dx(), y.dx() );
161 for (int i=0; i<global_fad_size; ++i) {
162 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
163 }
164}
165
166TEUCHOS_UNIT_TEST( MoveConstructorTests, DVFad_Unowned )
167{
168 typedef double value_type;
170 success = true;
171
172 // Initialize AD type
173 value_type x_val = value_type(1.5);
174 std::vector<value_type> x_dx(global_fad_size);
175 for (int i=0; i<global_fad_size; ++i)
176 x_dx[i] = value_type(2.0+i);
177 ad_type x(global_fad_size, &x_val, x_dx.data(), 1, false);
178
179 // Move x into y
180 ad_type y = std::move(x);
181
182 // Check y is correct
183 TEST_EQUALITY_CONST( y.size(), global_fad_size );
184 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
185 for (int i=0; i<global_fad_size; ++i) {
186 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
187 }
188
189 // Check x is correct
190 TEST_EQUALITY_CONST( x.size(), global_fad_size );
191 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
192 TEST_INEQUALITY( x.dx(), y.dx() );
193 for (int i=0; i<global_fad_size; ++i) {
194 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
195 }
196}
197
198TEUCHOS_UNIT_TEST( MoveConstructorTests, ViewFad )
199{
200 typedef double value_type;
201 typedef Sacado::Fad::Exp::DFad<value_type> dfad_type;
203 success = true;
204
205 // Initialize AD type
206 value_type x_val = value_type(1.5);
207 std::vector<value_type> x_dx(global_fad_size);
208 for (int i=0; i<global_fad_size; ++i)
209 x_dx[i] = value_type(2.0+i);
210 ad_type x(x_dx.data(), &x_val, global_fad_size, 1);
211
212 // Move x into y
213 ad_type y = std::move(x);
214
215 // Check y is correct
216 TEST_EQUALITY_CONST( y.size(), global_fad_size );
217 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
218 TEST_EQUALITY( y.dx(), x_dx.data() );
219 for (int i=0; i<global_fad_size; ++i) {
220 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
221 }
222
223 // Check x is correct
224 TEST_EQUALITY_CONST( x.size(), global_fad_size );
225 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
226 TEST_EQUALITY( x.dx(), x_dx.data() );
227 for (int i=0; i<global_fad_size; ++i) {
228 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
229 }
230}
231
232//
233// Move assignment tests
234//
235
236TEUCHOS_UNIT_TEST( MoveAssignmentTests, SFad )
237{
238 typedef double value_type;
240 success = true;
241
242 // Initialize AD type
243 ad_type x(global_fad_size, value_type(1.5));
244 for (int i=0; i<global_fad_size; ++i)
245 x.fastAccessDx(i) = value_type(2.0+i);
246
247 // Move x into y
248 ad_type y(0.5);
249 y = std::move(x);
250
251 // Check y is correct
252 TEST_EQUALITY_CONST( y.size(), global_fad_size );
253 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
254 for (int i=0; i<global_fad_size; ++i) {
255 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
256 }
257
258 // Check x is correct
259 TEST_EQUALITY_CONST( x.size(), global_fad_size );
260 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
261 TEST_INEQUALITY( x.dx(), y.dx() );
262 for (int i=0; i<global_fad_size; ++i) {
263 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
264 }
265}
266
267TEUCHOS_UNIT_TEST( MoveAssignmentTests, SLFad )
268{
269 typedef double value_type;
271 success = true;
272
273 // Initialize AD type
274 ad_type x(global_fad_size, value_type(1.5));
275 for (int i=0; i<global_fad_size; ++i)
276 x.fastAccessDx(i) = value_type(2.0+i);
277
278 // Move x into y
279 ad_type y(0.5);
280 y = std::move(x);
281
282 // Check y is correct
283 TEST_EQUALITY_CONST( y.size(), global_fad_size );
284 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
285 for (int i=0; i<global_fad_size; ++i) {
286 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
287 }
288
289 // Check x is correct
290 TEST_EQUALITY_CONST( x.size(), global_fad_size );
291 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
292 TEST_INEQUALITY( x.dx(), y.dx() );
293 for (int i=0; i<global_fad_size; ++i) {
294 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
295 }
296}
297
298TEUCHOS_UNIT_TEST( MoveAssignmentTests, DFad )
299{
300 typedef double value_type;
302 success = true;
303
304 // Initialize AD type
305 ad_type x(global_fad_size, value_type(1.5));
306 for (int i=0; i<global_fad_size; ++i)
307 x.fastAccessDx(i) = value_type(2.0+i);
308
309 // Move x into y
310 ad_type y(0.5);
311 y = std::move(x);
312
313 // Check y is correct
314 TEST_EQUALITY_CONST( y.size(), global_fad_size );
315 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
316 for (int i=0; i<global_fad_size; ++i) {
317 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
318 }
319
320 // Check x is correct
321 value_type *null_ptr = nullptr;
322 TEST_EQUALITY_CONST( x.size(), 0 );
323 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
324 TEST_EQUALITY( x.dx(), null_ptr );
325}
326
327TEUCHOS_UNIT_TEST( MoveAssignmentTests, DVFad_Owned )
328{
329 typedef double value_type;
331 success = true;
332
333 // Initialize AD type
334 ad_type x(global_fad_size, value_type(1.5));
335 for (int i=0; i<global_fad_size; ++i)
336 x.fastAccessDx(i) = value_type(2.0+i);
337
338 // Move x into y
339 ad_type y(0.5);
340 y = std::move(x);
341
342 // Check y is correct
343 TEST_EQUALITY_CONST( y.size(), global_fad_size );
344 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
345 for (int i=0; i<global_fad_size; ++i) {
346 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
347 }
348
349 // Check x is correct
350 TEST_EQUALITY_CONST( x.size(), global_fad_size );
351 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
352 TEST_INEQUALITY( x.dx(), y.dx() );
353 for (int i=0; i<global_fad_size; ++i) {
354 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
355 }
356}
357
358TEUCHOS_UNIT_TEST( MoveAssignmentTests, DVFad_Unowned )
359{
360 typedef double value_type;
362 success = true;
363
364 // Initialize AD type
365 value_type x_val = value_type(1.5);
366 std::vector<value_type> x_dx(global_fad_size);
367 for (int i=0; i<global_fad_size; ++i)
368 x_dx[i] = value_type(2.0+i);
369 ad_type x(global_fad_size, &x_val, x_dx.data(), 1, false);
370
371 // Move x into y
372 ad_type y(0.5);
373 y = std::move(x);
374
375 // Check y is correct
376 TEST_EQUALITY_CONST( y.size(), global_fad_size );
377 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
378 for (int i=0; i<global_fad_size; ++i) {
379 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
380 }
381
382 // Check x is correct
383 TEST_EQUALITY_CONST( x.size(), global_fad_size );
384 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
385 TEST_INEQUALITY( x.dx(), y.dx() );
386 for (int i=0; i<global_fad_size; ++i) {
387 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
388 }
389}
390
391TEUCHOS_UNIT_TEST( MoveAssignmentTests, ViewFad )
392{
393 typedef double value_type;
394 typedef Sacado::Fad::Exp::DFad<value_type> dfad_type;
396 success = true;
397
398 // Initialize AD type
399 value_type x_val = value_type(1.5);
400 std::vector<value_type> x_dx(global_fad_size);
401 for (int i=0; i<global_fad_size; ++i)
402 x_dx[i] = value_type(2.0+i);
403 ad_type x(x_dx.data(), &x_val, global_fad_size, 1);
404
405 // Move x into y
406 value_type y_val = value_type(0.5);
407 std::vector<value_type> y_dx(global_fad_size);
408 for (int i=0; i<global_fad_size; ++i)
409 y_dx[i] = value_type(20.0+i);
410 ad_type y(y_dx.data(), &y_val, global_fad_size, 1);
411 y = std::move(x);
412
413 // Check y is correct
414 TEST_EQUALITY_CONST( y.size(), global_fad_size );
415 TEST_EQUALITY_CONST( y.val(), value_type(1.5) );
416 TEST_EQUALITY( y.dx(), y_dx.data() );
417 for (int i=0; i<global_fad_size; ++i) {
418 TEST_EQUALITY_CONST( y.dx(i), value_type(2.0+i) );
419 }
420
421 // Check x is correct
422 TEST_EQUALITY_CONST( x.size(), global_fad_size );
423 TEST_EQUALITY_CONST( x.val(), value_type(1.5) );
424 TEST_EQUALITY( x.dx(), x_dx.data() );
425 for (int i=0; i<global_fad_size; ++i) {
426 TEST_EQUALITY_CONST( x.dx(i), value_type(2.0+i) );
427 }
428}
429
430#endif
431
432int main( int argc, char* argv[] ) {
433 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
434 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
435}
const int global_fad_size
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
int main()
Definition: ad_example.cpp:191
Forward-mode AD class templated on the storage for the derivative array.
const double y