2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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 selection parsing and compilation.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
42 #include <gtest/gtest.h>
44 #include "gromacs/options/basicoptions.h"
45 #include "gromacs/options/options.h"
46 #include "gromacs/selection/indexutil.h"
47 #include "gromacs/selection/selectioncollection.h"
48 #include "gromacs/selection/selection.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/flags.h"
51 #include "gromacs/utility/gmxregex.h"
52 #include "gromacs/utility/stringutil.h"
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/testfilemanager.h"
57 #include "testutils/testoptions.h"
64 /********************************************************************
65 * Test fixture for selection testing
68 class SelectionCollectionTest : public ::testing::Test
71 static int s_debugLevel;
73 SelectionCollectionTest();
74 ~SelectionCollectionTest();
76 void setAtomCount(int natoms)
78 ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, natoms));
80 void loadTopology(const char *filename);
82 void loadIndexGroups(const char *filename);
84 gmx::test::TopologyManager topManager_;
85 gmx::SelectionCollection sc_;
86 gmx::SelectionList sel_;
89 gmx_ana_indexgrps_t *grps_;
92 int SelectionCollectionTest::s_debugLevel = 0;
94 GMX_TEST_OPTIONS(SelectionCollectionTestOptions, options)
96 options->addOption(gmx::IntegerOption("seldebug")
97 .store(&SelectionCollectionTest::s_debugLevel)
98 .description("Set selection debug level"));
101 SelectionCollectionTest::SelectionCollectionTest()
102 : top_(NULL), frame_(NULL), grps_(NULL)
104 topManager_.requestFrame();
105 sc_.setDebugLevel(s_debugLevel);
106 sc_.setReferencePosType("atom");
107 sc_.setOutputPosType("atom");
110 SelectionCollectionTest::~SelectionCollectionTest()
114 gmx_ana_indexgrps_free(grps_);
119 SelectionCollectionTest::loadTopology(const char *filename)
121 topManager_.loadTopology(filename);
126 SelectionCollectionTest::setTopology()
128 top_ = topManager_.topology();
129 frame_ = topManager_.frame();
131 ASSERT_NO_THROW_GMX(sc_.setTopology(top_, -1));
135 SelectionCollectionTest::loadIndexGroups(const char *filename)
137 GMX_RELEASE_ASSERT(grps_ == NULL,
138 "External groups can only be loaded once");
139 std::string fullpath =
140 gmx::test::TestFileManager::getInputFilePath(filename);
141 gmx_ana_indexgrps_init(&grps_, NULL, fullpath.c_str());
142 sc_.setIndexGroups(grps_);
146 /********************************************************************
147 * Test fixture for selection testing with reference data
150 class SelectionCollectionDataTest : public SelectionCollectionTest
155 efTestEvaluation = 1<<0,
156 efTestPositionAtoms = 1<<1,
157 efTestPositionCoordinates = 1<<2,
158 efTestPositionMapping = 1<<3,
159 efTestPositionMasses = 1<<4,
160 efTestPositionCharges = 1<<5,
161 efTestSelectionNames = 1<<6,
162 efDontTestCompiledAtoms = 1<<8
164 typedef gmx::FlagsTemplate<TestFlag> TestFlags;
166 SelectionCollectionDataTest()
167 : checker_(data_.rootChecker()), count_(0), framenr_(0)
171 void setFlags(TestFlags flags) { flags_ = flags; }
173 template <size_t count>
174 void runTest(int natoms, const char *const (&selections)[count])
176 runTest(natoms, selections, count);
178 template <size_t count>
179 void runTest(const char *filename, const char *const (&selections)[count])
181 runTest(filename, selections, count);
184 template <size_t count>
185 void runParser(const char *const (&selections)[count])
187 runParser(selections, count);
192 void runEvaluateFinal();
195 static void checkSelection(gmx::test::TestReferenceChecker *checker,
196 const gmx::Selection &sel, TestFlags flags);
198 void runTest(int natoms, const char *const *selections, size_t count);
199 void runTest(const char *filename, const char *const *selections,
201 void runParser(const char *const *selections, size_t count);
203 void checkCompiled();
205 gmx::test::TestReferenceData data_;
206 gmx::test::TestReferenceChecker checker_;
214 SelectionCollectionDataTest::checkSelection(
215 gmx::test::TestReferenceChecker *checker,
216 const gmx::Selection &sel, TestFlags flags)
218 using gmx::test::TestReferenceChecker;
221 gmx::ConstArrayRef<int> atoms = sel.atomIndices();
222 checker->checkSequence(atoms.begin(), atoms.end(), "Atoms");
224 if (flags.test(efTestPositionAtoms)
225 || flags.test(efTestPositionCoordinates)
226 || flags.test(efTestPositionMapping)
227 || flags.test(efTestPositionMasses)
228 || flags.test(efTestPositionCharges))
230 TestReferenceChecker compound(
231 checker->checkSequenceCompound("Positions", sel.posCount()));
232 for (int i = 0; i < sel.posCount(); ++i)
234 TestReferenceChecker poscompound(compound.checkCompound("Position", NULL));
235 const gmx::SelectionPosition &p = sel.position(i);
236 if (flags.test(efTestPositionAtoms))
238 gmx::ConstArrayRef<int> atoms = p.atomIndices();
239 poscompound.checkSequence(atoms.begin(), atoms.end(), "Atoms");
241 if (flags.test(efTestPositionCoordinates))
243 poscompound.checkVector(p.x(), "Coordinates");
245 if (flags.test(efTestPositionMapping))
247 poscompound.checkInteger(p.refId(), "RefId");
248 poscompound.checkInteger(p.mappedId(), "MappedId");
250 if (flags.test(efTestPositionMasses))
252 poscompound.checkReal(p.mass(), "Mass");
254 if (flags.test(efTestPositionCharges))
256 poscompound.checkReal(p.charge(), "Charge");
264 SelectionCollectionDataTest::runParser(const char *const *selections,
267 using gmx::test::TestReferenceChecker;
269 TestReferenceChecker compound(checker_.checkCompound("ParsedSelections", "Parsed"));
272 for (size_t i = 0; i < count; ++i)
274 SCOPED_TRACE(std::string("Parsing selection \"")
275 + selections[i] + "\"");
276 gmx::SelectionList result;
277 ASSERT_NO_THROW_GMX(result = sc_.parseFromString(selections[i]));
278 sel_.insert(sel_.end(), result.begin(), result.end());
279 if (sel_.size() == count_)
281 std::string id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
282 TestReferenceChecker varcompound(
283 compound.checkCompound("ParsedVariable", id.c_str()));
284 varcompound.checkString(selections[i], "Input");
289 std::string id = gmx::formatString("Selection%d", static_cast<int>(count_ + 1));
290 TestReferenceChecker selcompound(
291 compound.checkCompound("ParsedSelection", id.c_str()));
292 selcompound.checkString(selections[i], "Input");
293 selcompound.checkString(sel_[count_].name(), "Name");
294 selcompound.checkString(sel_[count_].selectionText(), "Text");
295 selcompound.checkBoolean(sel_[count_].isDynamic(), "Dynamic");
303 SelectionCollectionDataTest::runCompiler()
305 ASSERT_NO_THROW_GMX(sc_.compile());
306 ASSERT_EQ(count_, sel_.size());
312 SelectionCollectionDataTest::checkCompiled()
314 using gmx::test::TestReferenceChecker;
315 const TestFlags mask = ~TestFlags(efTestPositionCoordinates);
317 TestReferenceChecker compound(checker_.checkCompound("CompiledSelections", "Compiled"));
318 for (size_t i = 0; i < count_; ++i)
320 SCOPED_TRACE(std::string("Checking selection \"") +
321 sel_[i].selectionText() + "\"");
322 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
323 TestReferenceChecker selcompound(
324 compound.checkCompound("Selection", id.c_str()));
325 if (flags_.test(efTestSelectionNames))
327 selcompound.checkString(sel_[i].name(), "Name");
329 if (!flags_.test(efDontTestCompiledAtoms))
331 checkSelection(&selcompound, sel_[i], flags_ & mask);
338 SelectionCollectionDataTest::runEvaluate()
340 using gmx::test::TestReferenceChecker;
343 ASSERT_NO_THROW_GMX(sc_.evaluate(frame_, NULL));
344 std::string frame = gmx::formatString("Frame%d", framenr_);
345 TestReferenceChecker compound(
346 checker_.checkCompound("EvaluatedSelections", frame.c_str()));
347 for (size_t i = 0; i < count_; ++i)
349 SCOPED_TRACE(std::string("Checking selection \"") +
350 sel_[i].selectionText() + "\"");
351 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
352 TestReferenceChecker selcompound(
353 compound.checkCompound("Selection", id.c_str()));
354 checkSelection(&selcompound, sel_[i], flags_);
360 SelectionCollectionDataTest::runEvaluateFinal()
362 ASSERT_NO_THROW_GMX(sc_.evaluateFinal(framenr_));
368 SelectionCollectionDataTest::runTest(int natoms, const char * const *selections,
371 ASSERT_NO_FATAL_FAILURE(runParser(selections, count));
372 ASSERT_NO_FATAL_FAILURE(setAtomCount(natoms));
373 ASSERT_NO_FATAL_FAILURE(runCompiler());
378 SelectionCollectionDataTest::runTest(const char *filename,
379 const char * const *selections,
382 ASSERT_NO_FATAL_FAILURE(runParser(selections, count));
383 ASSERT_NO_FATAL_FAILURE(loadTopology(filename));
384 ASSERT_NO_FATAL_FAILURE(runCompiler());
385 if (flags_.test(efTestEvaluation))
387 ASSERT_NO_FATAL_FAILURE(runEvaluate());
388 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
393 /********************************************************************
394 * Tests for SelectionCollection functionality without reference data
397 TEST_F(SelectionCollectionTest, HandlesNoSelections)
399 EXPECT_FALSE(sc_.requiresTopology());
400 EXPECT_NO_THROW_GMX(sc_.compile());
403 TEST_F(SelectionCollectionTest, HandlesVelocityAndForceRequests)
405 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr 1 to 10; none"));
406 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
407 ASSERT_EQ(2U, sel_.size());
408 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateVelocities(true));
409 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateVelocities(true));
410 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
411 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateForces(true));
412 ASSERT_NO_THROW_GMX(sc_.compile());
413 EXPECT_TRUE(sel_[0].hasVelocities());
414 EXPECT_TRUE(sel_[1].hasVelocities());
415 EXPECT_TRUE(sel_[0].hasForces());
416 EXPECT_TRUE(sel_[1].hasForces());
419 TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
421 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromFile(
422 gmx::test::TestFileManager::getInputFilePath("selfile.dat")));
423 // These should match the contents of selfile.dat
424 ASSERT_EQ(2U, sel_.size());
425 EXPECT_STREQ("resname RA RB", sel_[0].selectionText());
426 EXPECT_STREQ("resname RB RC", sel_[1].selectionText());
429 TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
431 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
433 sc_.parseFromString("resname ~ \"R[A\"");
435 }, gmx::InvalidInputError);
438 TEST_F(SelectionCollectionTest, HandlesUnsupportedRegularExpressions)
440 if (!gmx::Regex::isSupported())
442 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
444 sc_.parseFromString("resname \"R[AD]\"");
446 }, gmx::InvalidInputError);
450 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
452 EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"),
453 gmx::InvalidInputError);
456 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
458 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of"),
459 gmx::InvalidInputError);
462 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
464 EXPECT_THROW_GMX(sc_.parseFromString("within of atomnr 1"),
465 gmx::InvalidInputError);
468 TEST_F(SelectionCollectionTest, HandlesHelpKeywordInInvalidContext)
470 EXPECT_THROW_GMX(sc_.parseFromString("resname help"),
471 gmx::InvalidInputError);
474 // TODO: Tests for more parser errors
476 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser1)
478 ASSERT_NO_THROW_GMX(sc_.setIndexGroups(NULL));
479 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InvalidInputError);
480 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InvalidInputError);
483 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser2)
485 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
486 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InvalidInputError);
487 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InvalidInputError);
490 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed1)
492 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
493 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
494 EXPECT_THROW_GMX(sc_.setIndexGroups(NULL), gmx::InvalidInputError);
495 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
498 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed2)
500 ASSERT_NO_THROW_GMX(sc_.parseFromString("group 4; group \"foo\""));
501 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
502 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InvalidInputError);
503 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
506 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
508 ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
509 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
510 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
513 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
515 ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
516 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
517 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
520 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
522 ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
523 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
524 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
527 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
529 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
530 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
531 EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
534 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
536 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
537 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
538 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
541 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
543 ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
544 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
545 ASSERT_NO_THROW_GMX(sc_.compile());
546 EXPECT_THROW_GMX(sc_.evaluate(frame_, NULL), gmx::InconsistentInputError);
549 // TODO: Tests for evaluation errors
552 /********************************************************************
553 * Tests for selection keywords
556 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
558 static const char * const selections[] = {
562 runTest(10, selections);
565 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
567 static const char * const selections[] = {
568 "atomnr 1 to 3 6 to 8",
572 runTest(10, selections);
575 TEST_F(SelectionCollectionDataTest, HandlesResnr)
577 static const char * const selections[] = {
581 runTest("simple.gro", selections);
584 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
586 static const char * const selections[] = {
590 runTest("simple.pdb", selections);
593 TEST_F(SelectionCollectionDataTest, HandlesMolIndex)
595 static const char * const selections[] = {
599 ASSERT_NO_FATAL_FAILURE(runParser(selections));
600 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
601 topManager_.initUniformMolecules(3);
602 ASSERT_NO_FATAL_FAILURE(runCompiler());
605 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
607 static const char * const selections[] = {
611 runTest("simple.gro", selections);
614 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
616 static const char * const selections[] = {
622 runTest("simple.pdb", selections);
626 TEST_F(SelectionCollectionDataTest, HandlesAtomtype)
628 static const char * const selections[] = {
631 ASSERT_NO_FATAL_FAILURE(runParser(selections));
632 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
633 const char *const types[] = { "CA", "SA", "SB" };
634 topManager_.initAtomTypes(types);
635 ASSERT_NO_FATAL_FAILURE(runCompiler());
638 TEST_F(SelectionCollectionDataTest, HandlesChain)
640 static const char * const selections[] = {
644 runTest("simple.pdb", selections);
647 TEST_F(SelectionCollectionDataTest, HandlesMass)
649 static const char * const selections[] = {
652 ASSERT_NO_FATAL_FAILURE(runParser(selections));
653 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
654 for (int i = 0; i < top_->atoms.nr; ++i)
656 top_->atoms.atom[i].m = 1.0 + i;
658 ASSERT_NO_FATAL_FAILURE(runCompiler());
661 TEST_F(SelectionCollectionDataTest, HandlesCharge)
663 static const char * const selections[] = {
666 ASSERT_NO_FATAL_FAILURE(runParser(selections));
667 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
668 for (int i = 0; i < top_->atoms.nr; ++i)
670 top_->atoms.atom[i].q = i / 10.0;
672 ASSERT_NO_FATAL_FAILURE(runCompiler());
675 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
677 static const char * const selections[] = {
681 runTest("simple.pdb", selections);
684 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
686 static const char * const selections[] = {
690 runTest("simple.pdb", selections);
693 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
695 static const char * const selections[] = {
699 runTest("simple.pdb", selections);
702 TEST_F(SelectionCollectionDataTest, HandlesBeta)
704 static const char * const selections[] = {
708 runTest("simple.pdb", selections);
711 TEST_F(SelectionCollectionDataTest, HandlesResname)
713 static const char * const selections[] = {
717 runTest("simple.gro", selections);
720 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
722 static const char * const selections[] = {
727 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
728 runTest("simple.gro", selections);
732 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
734 static const char * const selections[] = {
735 "same residue as atomnr 1 4 12"
737 runTest("simple.gro", selections);
741 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
743 static const char * const selections[] = {
744 "same resname as atomnr 1 14"
746 runTest("simple.gro", selections);
750 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
752 static const char * const selections[] = {
754 "res_cog of name CB and resnr 1 3",
755 "whole_res_cog of name CB and resnr 1 3",
756 "part_res_cog of x < 3",
757 "dyn_res_cog of x < 3"
759 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
760 | efTestPositionAtoms);
761 runTest("simple.gro", selections);
765 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
767 static const char * const selections[] = {
768 "distance from cog of resnr 1 < 2"
770 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
771 runTest("simple.gro", selections);
775 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
777 static const char * const selections[] = {
778 "mindistance from resnr 1 < 2"
780 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
781 runTest("simple.gro", selections);
785 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
787 static const char * const selections[] = {
788 "within 1 of resnr 2"
790 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
791 runTest("simple.gro", selections);
795 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
797 // Both of these should evaluate to empty on a correct implementation.
798 static const char * const selections[] = {
799 "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
800 "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
802 setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
803 runTest("sphere.gro", selections);
807 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
809 static const char * const selections[] = {
811 "res_cog of resnr 1 to 4 permute 2 1",
812 "name CB S1 and res_cog x < 3 permute 2 1"
814 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
815 | efTestPositionAtoms | efTestPositionMapping);
816 runTest("simple.gro", selections);
820 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
822 static const char * const selections[] = {
823 "name S2 plus name S1",
824 "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
825 "name S1 and y < 3 plus res_cog of x < 2.5"
827 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
828 | efTestPositionAtoms | efTestPositionMapping);
829 runTest("simple.gro", selections);
833 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
835 static const char * const selections[] = {
836 "name S2 merge name S1",
837 "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
838 "name S1 and x < 2.5 merge res_cog of x < 2.5"
840 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
841 | efTestPositionAtoms | efTestPositionMapping);
842 runTest("simple.gro", selections);
846 /********************************************************************
847 * Tests for generic selection evaluation
850 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
852 static const char * const selections[] = {
857 setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms
858 | efTestPositionMasses | efTestPositionCharges);
859 ASSERT_NO_FATAL_FAILURE(runParser(selections));
860 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
861 for (int i = 0; i < top_->atoms.nr; ++i)
863 top_->atoms.atom[i].m = 1.0 + i / 100.0;
864 top_->atoms.atom[i].q = -(1.0 + i / 100.0);
866 ASSERT_NO_FATAL_FAILURE(runCompiler());
867 ASSERT_NO_FATAL_FAILURE(runEvaluate());
868 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
871 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
873 static const char * const selections[] = {
874 "atomnr 1 to 3 8 to 9",
878 setFlags(TestFlags() | efTestPositionAtoms
879 | efTestPositionMasses | efTestPositionCharges);
880 runTest(10, selections);
884 /********************************************************************
885 * Tests for selection syntactic constructs
888 TEST_F(SelectionCollectionDataTest, HandlesSelectionNames)
890 static const char * const selections[] = {
891 "\"GroupSelection\" group \"GrpA\"",
892 "\"DynamicSelection\" x < 5",
895 setFlags(TestFlags() | efTestSelectionNames);
896 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
897 runTest(10, selections);
900 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelections)
902 static const char * const selections[] = {
906 "group \"GrpB\" and resname RB"
908 setFlags(TestFlags() | efTestSelectionNames);
909 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
910 runTest("simple.gro", selections);
913 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelectionsDelayed)
915 static const char * const selections[] = {
919 "group \"GrpB\" and resname RB"
921 setFlags(TestFlags() | efTestSelectionNames);
922 ASSERT_NO_FATAL_FAILURE(runParser(selections));
923 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
924 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
925 ASSERT_NO_FATAL_FAILURE(runCompiler());
928 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
930 static const char * const selections[] = {
933 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
934 runTest("simple.gro", selections);
938 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
940 static const char * const selections[] = {
941 "within 1 of [2, 1, 0]"
943 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
944 runTest("simple.gro", selections);
948 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
950 static const char * const selections[] = {
954 runTest("simple.gro", selections);
958 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
960 static const char * const selections[] = {
964 runTest("simple.gro", selections);
968 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
970 static const char * const selections[] = {
972 "resname ~ \"R[BD]\""
974 if (gmx::Regex::isSupported())
976 runTest("simple.gro", selections);
981 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
983 static const char * const selections[] = {
984 "atomnr 1 to 5 and atomnr 2 to 7",
985 "atomnr 1 to 5 or not atomnr 3 to 8",
986 "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
987 "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
988 "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
990 runTest(10, selections);
994 TEST_F(SelectionCollectionDataTest, HandlesDynamicAtomValuedParameters)
996 static const char * const selections[] = {
997 "same residue as (atomnr 3 5 13 or y > 5)",
998 "(resnr 1 3 5 or x > 10) and same residue as (atomnr 3 5 13 or z > 5)"
1000 setFlags(TestFlags() | efTestEvaluation);
1001 runTest("simple.gro", selections);
1005 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
1007 static const char * const selections[] = {
1014 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1015 runTest("simple.gro", selections);
1019 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
1021 static const char * const selections[] = {
1027 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1028 runTest("simple.gro", selections);
1032 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
1034 static const char * const selections[] = {
1040 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1041 runTest("simple.gro", selections);
1045 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
1047 static const char * const selections[] = {
1049 "resname RA and value <= 4",
1050 "resname RA RB and x < 3 and value <= 4",
1052 "resname RA and index < 3",
1053 "resname RB and y < 3 and index < 6"
1055 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1056 runTest("simple.gro", selections);
1060 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
1062 static const char * const selections[] = {
1063 "foo = res_cog of resname RA",
1066 "bar = cog of resname RA",
1070 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1071 runTest("simple.gro", selections);
1075 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
1077 static const char * const selections[] = {
1078 "constpos = [1.0, 2.5, 0.5]",
1080 "within 2 of constpos"
1082 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
1083 | efTestPositionAtoms);
1084 runTest("simple.gro", selections);
1088 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1090 static const char * const selections[] = {
1095 "x + constreal1 < constreal2"
1097 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1098 runTest("simple.gro", selections);
1102 /********************************************************************
1103 * Tests for complex boolean syntax
1106 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1108 static const char * const selections[] = {
1109 "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1110 "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1111 "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1112 "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1113 "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1115 runTest(10, selections);
1119 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1121 static const char * const selections[] = {
1122 "foo = atomnr 4 to 7 or x < 2",
1123 "atomnr 1 to 4 and foo",
1124 "atomnr 2 to 6 and y < 3 and foo",
1125 "atomnr 6 to 10 and not foo"
1127 runTest(10, selections);
1131 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1133 static const char * const selections[] = {
1134 "foo = atomnr 4 to 7",
1135 "bar = foo and x < 2",
1136 "bar2 = foo and y < 2",
1137 "atomnr 1 to 4 and bar",
1138 "atomnr 2 to 6 and y < 3 and bar2",
1139 "atomnr 6 to 10 and not foo"
1141 runTest(10, selections);
1145 /********************************************************************
1146 * Tests for complex subexpression cases
1148 * These tests use some knowledge of the implementation to trigger different
1149 * paths in the code.
1152 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1154 static const char * const selections[] = {
1155 "unused1 = atomnr 1 to 3",
1156 "foo = atomnr 4 to 7",
1157 "atomnr 1 to 6 and foo",
1158 "unused2 = atomnr 3 to 5"
1160 runTest(10, selections);
1164 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1166 static const char * const selections[] = {
1167 "foo = atomnr 4 to 7 and x < 2",
1168 "atomnr 1 to 5 and foo",
1169 "atomnr 3 to 7 and foo"
1171 runTest(10, selections);
1175 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1177 static const char * const selections[] = {
1178 "foo = atomnr 4 to 7 and x < 2",
1179 "atomnr 1 to 6 and foo",
1183 runTest(10, selections);
1187 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups2)
1189 static const char * const selections[] = {
1190 "foo = atomnr 1 to 8 and x < 10",
1191 "atomnr 1 to 5 and y < 10 and foo",
1194 setFlags(TestFlags() | efTestEvaluation);
1195 runTest("simple.gro", selections);