Add comparison operator to BasicVector
authorBerk Hess <hess@kth.se>
Tue, 23 Mar 2021 14:47:26 +0000 (14:47 +0000)
committerPaul Bauer <paul.bauer.q@gmail.com>
Tue, 23 Mar 2021 14:47:26 +0000 (14:47 +0000)
The default comparison seemed to compare the memory addresses
and not the elements.

api/legacy/include/gromacs/math/vectypes.h
src/gromacs/math/tests/vectypes.cpp

index d4d5211d11040df051f044d22918d33a4caf5588..55f28e2a4de3961446838ab56fc8d861bb97c687 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2016,2017,2018 by the GROMACS development team.
- * Copyright (c) 2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -123,6 +123,16 @@ public:
     ValueType& operator[](int i) { return x_[i]; }
     //! Indexing operator to make the class work as the raw array.
     ValueType operator[](int i) const { return x_[i]; }
+    //! Return whether all elements compare equal
+    bool operator==(const BasicVector<ValueType>& right)
+    {
+        return x_[0] == right[0] && x_[1] == right[1] && x_[2] == right[2];
+    }
+    //! Return whether any elements compare unequal
+    bool operator!=(const BasicVector<ValueType>& right)
+    {
+        return x_[0] != right[0] || x_[1] != right[1] || x_[2] != right[2];
+    }
     //! Allow inplace addition for BasicVector
     BasicVector<ValueType>& operator+=(const BasicVector<ValueType>& right)
     {
index 3cbaab7b72c75fa4cb919bf4cb5cf64b13354b3a..7049e97433da18558145017b15125ed69c86f4d4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2015,2016,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015,2016,2018,2019,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -118,6 +118,24 @@ TEST(RVecTest, WorksAs_rvec_Array)
     EXPECT_EQ(4, r[1][ZZ]);
 }
 
+TEST(RVecTest, ComparesEqual)
+{
+    RVec a(1, 2, 3);
+    RVec b(1, 2, 3);
+    RVec c(1, 2, 2);
+    EXPECT_EQ(a == b, true);
+    EXPECT_EQ(a == c, false);
+}
+
+TEST(RVecTest, ComparesUnequal)
+{
+    RVec a(1, 2, 3);
+    RVec b(1, 2, 3);
+    RVec c(1, 2, 2);
+    EXPECT_EQ(a != b, false);
+    EXPECT_EQ(a != c, true);
+}
+
 /*! \brief
  * Test overloaded math operations
  */