2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Tests various corners of gmx::RVec implementation.
39 * The main point of these tests is to check that all different constructs
40 * using gmx::RVec compile, and that some of the non-trivial conversions
41 * to/from rvec work as intended.
43 * \author Teemu Murtola <teemu.murtola@gmail.com>
44 * \ingroup module_math
48 #include "gromacs/math/vectypes.h"
52 #include <gtest/gtest.h>
54 #include "gromacs/math/vec.h"
56 #include "testutils/testasserts.h"
65 TEST(RVecTest
, CanBeStoredInVector
)
68 v
.emplace_back(1, 2, 3);
70 EXPECT_EQ(1, v
[0][XX
]);
71 EXPECT_EQ(2, v
[0][YY
]);
72 EXPECT_EQ(3, v
[0][ZZ
]);
75 TEST(RVecTest
, ConvertsImplicitlyFrom_rvec
)
80 EXPECT_EQ(1, v
[0][XX
]);
81 EXPECT_EQ(2, v
[0][YY
]);
82 EXPECT_EQ(3, v
[0][ZZ
]);
85 TEST(RVecTest
, ConvertsImplicitlyTo_rvec
)
88 v
.emplace_back(1, 2, 3);
96 TEST(RVecTest
, WorksAsMutable_rvec
)
99 v
.emplace_back(1, 2, 3);
102 EXPECT_EQ(2, v
[0][XX
]);
103 EXPECT_EQ(3, v
[0][YY
]);
104 EXPECT_EQ(4, v
[0][ZZ
]);
107 TEST(RVecTest
, WorksAs_rvec_Array
)
110 v
.emplace_back(1, 2, 3);
111 v
.emplace_back(2, 3, 4);
112 const rvec
*r
= as_rvec_array(v
.data());
113 EXPECT_EQ(1, r
[0][XX
]);
114 EXPECT_EQ(2, r
[0][YY
]);
115 EXPECT_EQ(3, r
[0][ZZ
]);
116 EXPECT_EQ(2, r
[1][XX
]);
117 EXPECT_EQ(3, r
[1][YY
]);
118 EXPECT_EQ(4, r
[1][ZZ
]);
122 * Test overloaded math operations
125 TEST(RVecTest
, CanAddRVecToRvec
)
136 TEST(RVecTest
, CanAddAssignRVecToRvec
)
147 TEST(RVecTest
, CanSubtractRVecFromRvec
)
155 EXPECT_EQ(-2, c
[ZZ
]);
158 TEST(RVecTest
, CanSubtractAssignRVecFromRvec
)
166 EXPECT_EQ(-2, b
[ZZ
]);
169 TEST(RVecTest
, CanDotProductRVecByRvec
)
178 TEST(RVecTest
, CanCrossProductRVecByRvec
)
184 EXPECT_EQ(-4, c
[XX
]);
186 EXPECT_EQ(-4, c
[ZZ
]);
190 * Test for inplace operations imported from vec.h
193 TEST(RVecTest
, CanDivideRVecInplace
)
204 TEST(RVecTest
, CanScaleRVec
)
214 TEST(RVecTest
, CanDivideRVec
)
224 TEST(RVecTest
, CanDoUnitvFromRVec
)
229 EXPECT_REAL_EQ(1, b
[XX
]);
230 EXPECT_REAL_EQ(0, b
[YY
]);
231 EXPECT_REAL_EQ(0, b
[ZZ
]);
234 TEST(RVecTest
, CanSqLengthOfRVec
)
239 EXPECT_REAL_EQ(9, b
);
242 TEST(RVecTest
, CanLengthOfRVec
)
247 EXPECT_REAL_EQ(3, b
);
250 TEST(RVecTest
, CanCastToRVec
)
260 TEST(RVecTest
, CanCastToDVec
)
272 * Tests for out of class functions
274 TEST(RVecTest
, CanLeftScalarMultiply
)
285 TEST(RVecTest
, CanRightScalarMultiply
)
296 TEST(RVecTest
, CanGetUnitvFromRVec
)
300 b
= gmx::unitVector(a
);
301 EXPECT_REAL_EQ(1, b
[XX
]);
302 EXPECT_REAL_EQ(0, b
[YY
]);
303 EXPECT_REAL_EQ(0, b
[ZZ
]);
306 TEST(RVecTest
, CanGetSqLengthOfRVec
)
311 EXPECT_REAL_EQ(9, b
);
314 TEST(RVecTest
, CanGetLengthOfRVec
)
319 EXPECT_REAL_EQ(3, b
);
322 TEST(RVecTest
, CanDoCrossProductOfRVec
)
327 c
= gmx::cross(a
, b
);
328 EXPECT_EQ(-4, c
[XX
]);
330 EXPECT_EQ(-4, c
[ZZ
]);
333 TEST(RVecTest
, CanDoDotProductOfRVec
)
342 TEST(RVecTest
, CanScaleByVector
)
346 RVec scaled
= scaleByVector(a
, b
);
347 EXPECT_REAL_EQ(3, scaled
[XX
]);
348 EXPECT_REAL_EQ(4, scaled
[YY
]);
349 EXPECT_REAL_EQ(3, scaled
[ZZ
]);
352 TEST(RVecTest
, asIVec
)
354 RVec
a(1.2, 2.7, -3e3
);
355 auto asIvec
= a
.toIVec();
357 EXPECT_REAL_EQ(1, asIvec
[XX
]);
358 EXPECT_REAL_EQ(2, asIvec
[YY
]);
359 EXPECT_REAL_EQ(-3000, asIvec
[ZZ
]);
362 TEST(RVecTest
, elementWiseMin
)
366 auto minAB
= elementWiseMin(a
, b
);
368 EXPECT_REAL_EQ(1, minAB
[XX
]);
369 EXPECT_REAL_EQ(2, minAB
[YY
]);
370 EXPECT_REAL_EQ(1, minAB
[ZZ
]);
373 TEST(RVecTest
, elementWiseMax
)
377 auto maxAB
= elementWiseMax(a
, b
);
379 EXPECT_REAL_EQ(3, maxAB
[XX
]);
380 EXPECT_REAL_EQ(2, maxAB
[YY
]);
381 EXPECT_REAL_EQ(3, maxAB
[ZZ
]);
385 * Helper function for testing DVec to dvec conversions.
387 const dvec
*testFunction(const dvec
&x
)
392 TEST(RVecTest
, WorksAs_dvec_Reference
)
395 const dvec
*r
= testFunction(v
.as_vec());
396 EXPECT_EQ(1, r
[0][XX
]);
397 EXPECT_EQ(2, r
[0][YY
]);
398 EXPECT_EQ(3, r
[0][ZZ
]);
402 * Helper function for testing IVec to ivec conversions.
404 const ivec
*testFunction(const ivec
&x
)
409 TEST(RVecTest
, WorksAs_ivec_Reference
)
412 const ivec
*r
= testFunction(v
.as_vec());
413 EXPECT_EQ(1, r
[0][XX
]);
414 EXPECT_EQ(2, r
[0][YY
]);
415 EXPECT_EQ(3, r
[0][ZZ
]);
419 * Helper function for testing RVec to rvec conversions.
421 #if !GMX_DOUBLE //otherwise rvec==dvec
422 const rvec
*testFunction(const rvec
&x
)
428 TEST(RVecTest
, WorksAs_rvec_Reference
)
431 const rvec
*r
= testFunction(v
);
432 EXPECT_EQ(1, r
[0][XX
]);
433 EXPECT_EQ(2, r
[0][YY
]);
434 EXPECT_EQ(3, r
[0][ZZ
]);
437 TEST(RVecTest
, CopyConstructorWorks
)
441 EXPECT_EQ(1, copy
[XX
]);
442 EXPECT_EQ(2, copy
[YY
]);
443 EXPECT_EQ(3, copy
[ZZ
]);
446 TEST(RVecTest
, CopyAssignmentWorks
)
451 EXPECT_EQ(1, copy
[XX
]);
452 EXPECT_EQ(2, copy
[YY
]);
453 EXPECT_EQ(3, copy
[ZZ
]);
456 TEST(RVecTest
, MoveConstructorWorks
)
460 EXPECT_EQ(1, copy
[XX
]);
461 EXPECT_EQ(2, copy
[YY
]);
462 EXPECT_EQ(3, copy
[ZZ
]);
465 TEST(RVecTest
, MoveAssignmentWorks
)
470 EXPECT_EQ(1, copy
[XX
]);
471 EXPECT_EQ(2, copy
[YY
]);
472 EXPECT_EQ(3, copy
[ZZ
]);