From 4124756a03b5d9e634d2384ae639a1732d76497a Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Wed, 20 May 2015 21:22:25 +0300 Subject: [PATCH] Fix handling of real-valued 'to' selection ranges 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 | 4 +- ...taTest_HandlesOverlappingIntegerRanges.xml | 41 +++++++++++++++++ ...nDataTest_HandlesOverlappingRealRanges.xml | 45 +++++++++++++++++++ .../selection/tests/selectioncollection.cpp | 28 +++++++++++- 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingIntegerRanges.xml create mode 100644 src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml diff --git a/src/gromacs/selection/params.cpp b/src/gromacs/selection/params.cpp index e0305da772..fb462861d8 100644 --- a/src/gromacs/selection/params.cpp +++ b/src/gromacs/selection/params.cpp @@ -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 index 0000000000..71949a4847 --- /dev/null +++ b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingIntegerRanges.xml @@ -0,0 +1,41 @@ + + + + + + atomnr 2 to 4 5 to 8 + atomnr 2 to 4 5 to 8 + false + + + atomnr 2 to 5 4 to 7 + atomnr 2 to 5 4 to 7 + false + + + + + + 7 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + + + + + 6 + 1 + 2 + 3 + 4 + 5 + 6 + + + + diff --git a/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml new file mode 100644 index 0000000000..c0f05ec941 --- /dev/null +++ b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesOverlappingRealRanges.xml @@ -0,0 +1,45 @@ + + + + + + charge {-0.35 to -0.05 0.25 to 0.75} + charge {-0.35 to -0.05 0.25 to 0.75} + false + + + charge {0.05 to -0.3 -0.05 to 0.55} + charge {0.05 to -0.3 -0.05 to 0.55} + false + + + + + + 8 + 2 + 3 + 4 + 8 + 9 + 10 + 11 + 12 + + + + + 9 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + + + + diff --git a/src/gromacs/selection/tests/selectioncollection.cpp b/src/gromacs/selection/tests/selectioncollection.cpp index d299a2b56c..66369c1a7d 100644 --- a/src/gromacs/selection/tests/selectioncollection.cpp +++ b/src/gromacs/selection/tests/selectioncollection.cpp @@ -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[] = { -- 2.22.0