Extend key-value tree comparison and tests
authorTeemu Murtola <teemu.murtola@gmail.com>
Mon, 27 Mar 2017 19:11:59 +0000 (22:11 +0300)
committerMark Abraham <mark.j.abraham@gmail.com>
Fri, 30 Jun 2017 11:20:59 +0000 (13:20 +0200)
Cover comparison support for key-value trees for all basic option types
with tests, and implement support for missing types.

With this, all the basic option types should now be supported through
the whole chain of t_inputrec handling.

Change-Id: Id8f821e2bb7485eecea0922872d86bddcc69ef6f

src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsBooleanOption.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsDoubleOption.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsEnumIntOption.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsEnumOption.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsFloatOption.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsInt64Option.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsIntegerOption.xml
src/gromacs/options/tests/refdata/TreeValueSupportTest_SupportsStringOption.xml
src/gromacs/options/tests/treesupport.cpp
src/gromacs/utility/keyvaluetree.cpp

index a3dce1faa00d40864ca79046a36f18bdfe8640ec..7e3ba5a424db31666d0433df86453c926332ea3e 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = true
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (true - missing)
 ]]></String>
 </ReferenceData>
index 5f80ef757a99fa7daa1e37f41aa4a4d88932c1d5..bd8173832e280bb9a481d6a5f84230c7749e7736 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = 1.5
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (1.5 - missing)
 ]]></String>
 </ReferenceData>
index 69986bb018b67cd57df1fe239fca10d729d55b9c..c81c4a03f4e1c5a58829cc20f3ff585d4ddc499f 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = foo
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (foo - missing)
 ]]></String>
 </ReferenceData>
index 69986bb018b67cd57df1fe239fca10d729d55b9c..c81c4a03f4e1c5a58829cc20f3ff585d4ddc499f 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = foo
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (foo - missing)
 ]]></String>
 </ReferenceData>
index 5f80ef757a99fa7daa1e37f41aa4a4d88932c1d5..bd8173832e280bb9a481d6a5f84230c7749e7736 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = 1.5
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (1.5 - missing)
 ]]></String>
 </ReferenceData>
index af8c78c1492509cabb057f99c93c8b8f76da6151..4891b7e72ef473028f6114044beed4ff79feb9e3 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = 2
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (2 - missing)
 ]]></String>
 </ReferenceData>
index 4d4d9dc5aa3de005d37b1c7b4e2d3d02acd749bc..e6d4ce3d465673ab2b498b9746d56151d2c80a5b 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = 2
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (2 - missing)
 ]]></String>
 </ReferenceData>
index 5ca950c426cd8f2db3a95fe45ed7339e7f6fa889..fd1b4a797e553dc5b58e22c1a4269c5a0fb47087 100644 (file)
@@ -7,5 +7,10 @@
   </Object>
   <String Name="Dumped"><![CDATA[
 a                                 = s
+]]></String>
+  <String Name="Compared"><![CDATA[
+]]></String>
+  <String Name="ComparedAgainstEmpty"><![CDATA[
+/a (s - missing)
 ]]></String>
 </ReferenceData>
index f075e377c4087b28553b4b120b434c3e1bee426a..fcc0aa280a850749bc817cb984aa8eb2782ea2d8 100644 (file)
@@ -255,6 +255,21 @@ class TreeValueSupportTest : public ::testing::Test
                 ASSERT_NO_THROW_GMX(gmx::dumpKeyValueTree(&writer, tree));
                 checker.checkTextBlock(stream.toString(), "Dumped");
             }
+            // Check that comparison works.
+            {
+                gmx::StringOutputStream stream;
+                gmx::TextWriter         writer(&stream);
+                ASSERT_NO_THROW_GMX(gmx::compareKeyValueTrees(&writer, tree, tree, 0.0, 0.0));
+                checker.checkTextBlock(stream.toString(), "Compared");
+            }
+            // Check that comparison works against an empty tree.
+            {
+                gmx::StringOutputStream stream;
+                gmx::TextWriter         writer(&stream);
+                gmx::KeyValueTreeObject empty;
+                ASSERT_NO_THROW_GMX(gmx::compareKeyValueTrees(&writer, tree, empty, 0.0, 0.0));
+                checker.checkTextBlock(stream.toString(), "ComparedAgainstEmpty");
+            }
         }
 
         gmx::Options              options_;
index 942e60d53320b4cca6ec266efa2366aac40b4c60..039e73dcb57773c03933db2640872ef0e6731b5a 100644 (file)
@@ -204,29 +204,10 @@ class CompareHelper
                 {
                     GMX_RELEASE_ASSERT(false, "Array comparison not implemented");
                 }
-                else if (value1.isType<double>())
+                else if (!areSimpleValuesOfSameTypeEqual(value1, value2))
                 {
-                    const double v1 = value1.cast<double>();
-                    const double v2 = value2.cast<double>();
-                    if (!equal_double(v1, v2, ftol_, abstol_))
-                    {
-                        writer_->writeString(currentPath_.toString());
-                        writer_->writeLine(formatString(" (%e - %e)", v1, v2));
-                    }
-                }
-                else if (value1.isType<float>())
-                {
-                    const float v1 = value1.cast<float>();
-                    const float v2 = value2.cast<float>();
-                    if (!equal_float(v1, v2, ftol_, abstol_))
-                    {
-                        writer_->writeString(currentPath_.toString());
-                        writer_->writeLine(formatString(" (%e - %e)", v1, v2));
-                    }
-                }
-                else
-                {
-                    GMX_RELEASE_ASSERT(false, "Unknown value type");
+                    writer_->writeString(currentPath_.toString());
+                    writer_->writeLine(formatString(" (%s - %s)", simpleValueToString(value1).c_str(), simpleValueToString(value2).c_str()));
                 }
             }
             else if ((value1.isType<double>() && value2.isType<float>())
@@ -248,6 +229,43 @@ class CompareHelper
             }
         }
 
+        bool areSimpleValuesOfSameTypeEqual(
+            const KeyValueTreeValue &value1,
+            const KeyValueTreeValue &value2)
+        {
+            GMX_ASSERT(value1.type() == value2.type(),
+                       "Caller should ensure that types are equal");
+            if (value1.isType<bool>())
+            {
+                return value1.cast<bool>() == value2.cast<bool>();
+            }
+            else if (value1.isType<int>())
+            {
+                return value1.cast<int>() == value2.cast<int>();
+            }
+            else if (value1.isType<gmx_int64_t>())
+            {
+                return value1.cast<gmx_int64_t>() == value2.cast<gmx_int64_t>();
+            }
+            else if (value1.isType<double>())
+            {
+                return equal_double(value1.cast<double>(), value2.cast<double>(), ftol_, abstol_);
+            }
+            else if (value1.isType<float>())
+            {
+                return equal_float(value1.cast<float>(), value2.cast<float>(), ftol_, abstol_);
+            }
+            else if (value1.isType<std::string>())
+            {
+                return value1.cast<std::string>() == value2.cast<std::string>();
+            }
+            else
+            {
+                GMX_RELEASE_ASSERT(false, "Unknown value type");
+                return false;
+            }
+        }
+
         void handleMismatchingTypes(const KeyValueTreeValue & /* value1 */,
                                     const KeyValueTreeValue & /* value2 */)
         {