Fix handling of real-valued 'to' selection ranges
authorTeemu Murtola <teemu.murtola@gmail.com>
Wed, 20 May 2015 18:22:25 +0000 (21:22 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Wed, 20 May 2015 19:35:45 +0000 (22:35 +0300)
Two adjacent ranges were incorrectly merged to a single range if the
difference between their nearest endpoints was less than one (the extra
+1 in the code is incorrectly copy-pasted from the integer case).

Add tests for handling overlapping and near-overlapping ranges.

Fixes #1743.

Change-Id: I3ac79c55dc3817e6325e43e27e0ccb4fe360e609

src/gromacs/selection/params.cpp
src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingIntegerRanges.xml [new file with mode: 0644]
src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml [new file with mode: 0644]
src/gromacs/selection/tests/selectioncollection.cpp

index e0305da77297c723957ef572e65a65e5ae269e96..fb462861d85d371d9654a87b30e535945570d12d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, 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.
@@ -388,7 +388,7 @@ parse_values_range(const SelectionParserValueList &values,
         qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
         for (i = j = 2; i < 2*n; i += 2)
         {
-            if (rdata[j-1]+1 >= rdata[i])
+            if (rdata[j-1] >= rdata[i])
             {
                 if (rdata[i+1] > rdata[j-1])
                 {
diff --git a/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingIntegerRanges.xml b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingIntegerRanges.xml
new file mode 100644 (file)
index 0000000..71949a4
--- /dev/null
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <ParsedSelections Name="Parsed">
+    <ParsedSelection Name="Selection1">
+      <String Name="Input">atomnr 2 to 4 5 to 8</String>
+      <String Name="Text">atomnr 2 to 4 5 to 8</String>
+      <Bool Name="Dynamic">false</Bool>
+    </ParsedSelection>
+    <ParsedSelection Name="Selection2">
+      <String Name="Input">atomnr 2 to 5 4 to 7</String>
+      <String Name="Text">atomnr 2 to 5 4 to 7</String>
+      <Bool Name="Dynamic">false</Bool>
+    </ParsedSelection>
+  </ParsedSelections>
+  <CompiledSelections Name="Compiled">
+    <Selection Name="Selection1">
+      <Sequence Name="Atoms">
+        <Int Name="Length">7</Int>
+        <Int>1</Int>
+        <Int>2</Int>
+        <Int>3</Int>
+        <Int>4</Int>
+        <Int>5</Int>
+        <Int>6</Int>
+        <Int>7</Int>
+      </Sequence>
+    </Selection>
+    <Selection Name="Selection2">
+      <Sequence Name="Atoms">
+        <Int Name="Length">6</Int>
+        <Int>1</Int>
+        <Int>2</Int>
+        <Int>3</Int>
+        <Int>4</Int>
+        <Int>5</Int>
+        <Int>6</Int>
+      </Sequence>
+    </Selection>
+  </CompiledSelections>
+</ReferenceData>
diff --git a/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml
new file mode 100644 (file)
index 0000000..c0f05ec
--- /dev/null
@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <ParsedSelections Name="Parsed">
+    <ParsedSelection Name="Selection1">
+      <String Name="Input">charge {-0.35 to -0.05 0.25 to 0.75}</String>
+      <String Name="Text">charge {-0.35 to -0.05 0.25 to 0.75}</String>
+      <Bool Name="Dynamic">false</Bool>
+    </ParsedSelection>
+    <ParsedSelection Name="Selection2">
+      <String Name="Input">charge {0.05 to -0.3 -0.05 to 0.55}</String>
+      <String Name="Text">charge {0.05 to -0.3 -0.05 to 0.55}</String>
+      <Bool Name="Dynamic">false</Bool>
+    </ParsedSelection>
+  </ParsedSelections>
+  <CompiledSelections Name="Compiled">
+    <Selection Name="Selection1">
+      <Sequence Name="Atoms">
+        <Int Name="Length">8</Int>
+        <Int>2</Int>
+        <Int>3</Int>
+        <Int>4</Int>
+        <Int>8</Int>
+        <Int>9</Int>
+        <Int>10</Int>
+        <Int>11</Int>
+        <Int>12</Int>
+      </Sequence>
+    </Selection>
+    <Selection Name="Selection2">
+      <Sequence Name="Atoms">
+        <Int Name="Length">9</Int>
+        <Int>2</Int>
+        <Int>3</Int>
+        <Int>4</Int>
+        <Int>5</Int>
+        <Int>6</Int>
+        <Int>7</Int>
+        <Int>8</Int>
+        <Int>9</Int>
+        <Int>10</Int>
+      </Sequence>
+    </Selection>
+  </CompiledSelections>
+</ReferenceData>
index d299a2b56c2e110b334c0c66bf0069be7a0b111e..66369c1a7d6293a5865c7bd7ffc311c4c0b74ab1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013,2014,2015, 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.
@@ -994,6 +994,32 @@ TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
 }
 
 
+TEST_F(SelectionCollectionDataTest, HandlesOverlappingIntegerRanges)
+{
+    static const char * const selections[] = {
+        "atomnr 2 to 4 5 to 8",
+        "atomnr 2 to 5 4 to 7"
+    };
+    ASSERT_NO_FATAL_FAILURE(runTest(10, selections));
+}
+
+
+TEST_F(SelectionCollectionDataTest, HandlesOverlappingRealRanges)
+{
+    static const char * const selections[] = {
+        "charge {-0.35 to -0.05 0.25 to 0.75}",
+        "charge {0.05 to -0.3 -0.05 to 0.55}"
+    };
+    ASSERT_NO_FATAL_FAILURE(runParser(selections));
+    ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
+    for (int i = 0; i < top_->atoms.nr; ++i)
+    {
+        top_->atoms.atom[i].q = i / 10.0 - 0.5;
+    }
+    ASSERT_NO_FATAL_FAILURE(runCompiler());
+}
+
+
 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
 {
     static const char * const selections[] = {