2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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/arrayref.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/flags.h"
52 #include "gromacs/utility/gmxregex.h"
53 #include "gromacs/utility/stringutil.h"
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testfilemanager.h"
58 #include "testutils/testoptions.h"
65 /********************************************************************
66 * Test fixture for selection testing
69 class SelectionCollectionTest : public ::testing::Test
72 static int s_debugLevel;
74 SelectionCollectionTest();
75 ~SelectionCollectionTest();
77 void setAtomCount(int natoms)
79 ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, natoms));
81 void loadTopology(const char *filename);
83 void loadIndexGroups(const char *filename);
85 gmx::test::TopologyManager topManager_;
86 gmx::SelectionCollection sc_;
87 gmx::SelectionList sel_;
90 gmx_ana_indexgrps_t *grps_;
93 int SelectionCollectionTest::s_debugLevel = 0;
95 // \cond/\endcond do not seem to work here with Doxygen 1.8.5 parser.
97 GMX_TEST_OPTIONS(SelectionCollectionTestOptions, options)
99 options->addOption(gmx::IntegerOption("seldebug")
100 .store(&SelectionCollectionTest::s_debugLevel)
101 .description("Set selection debug level"));
105 SelectionCollectionTest::SelectionCollectionTest()
106 : top_(NULL), frame_(NULL), grps_(NULL)
108 topManager_.requestFrame();
109 sc_.setDebugLevel(s_debugLevel);
110 sc_.setReferencePosType("atom");
111 sc_.setOutputPosType("atom");
114 SelectionCollectionTest::~SelectionCollectionTest()
118 gmx_ana_indexgrps_free(grps_);
123 SelectionCollectionTest::loadTopology(const char *filename)
125 topManager_.loadTopology(filename);
130 SelectionCollectionTest::setTopology()
132 top_ = topManager_.topology();
133 frame_ = topManager_.frame();
135 ASSERT_NO_THROW_GMX(sc_.setTopology(top_, -1));
139 SelectionCollectionTest::loadIndexGroups(const char *filename)
141 GMX_RELEASE_ASSERT(grps_ == NULL,
142 "External groups can only be loaded once");
143 std::string fullpath =
144 gmx::test::TestFileManager::getInputFilePath(filename);
145 gmx_ana_indexgrps_init(&grps_, NULL, fullpath.c_str());
146 sc_.setIndexGroups(grps_);
150 /********************************************************************
151 * Test fixture for selection testing with reference data
154 class SelectionCollectionDataTest : public SelectionCollectionTest
159 efTestEvaluation = 1<<0,
160 efTestPositionAtoms = 1<<1,
161 efTestPositionCoordinates = 1<<2,
162 efTestPositionMapping = 1<<3,
163 efTestPositionMasses = 1<<4,
164 efTestPositionCharges = 1<<5,
165 efTestSelectionNames = 1<<6,
166 efDontTestCompiledAtoms = 1<<8
168 typedef gmx::FlagsTemplate<TestFlag> TestFlags;
170 SelectionCollectionDataTest()
171 : checker_(data_.rootChecker()), count_(0), framenr_(0)
175 void setFlags(TestFlags flags) { flags_ = flags; }
177 void runParser(const gmx::ConstArrayRef<const char *> &selections);
180 void runEvaluateFinal();
182 void runTest(int natoms,
183 const gmx::ConstArrayRef<const char *> &selections);
184 void runTest(const char *filename,
185 const gmx::ConstArrayRef<const char *> &selections);
188 static void checkSelection(gmx::test::TestReferenceChecker *checker,
189 const gmx::Selection &sel, TestFlags flags);
191 void checkCompiled();
193 gmx::test::TestReferenceData data_;
194 gmx::test::TestReferenceChecker checker_;
202 SelectionCollectionDataTest::checkSelection(
203 gmx::test::TestReferenceChecker *checker,
204 const gmx::Selection &sel, TestFlags flags)
206 using gmx::test::TestReferenceChecker;
209 gmx::ConstArrayRef<int> atoms = sel.atomIndices();
210 checker->checkSequence(atoms.begin(), atoms.end(), "Atoms");
212 if (flags.test(efTestPositionAtoms)
213 || flags.test(efTestPositionCoordinates)
214 || flags.test(efTestPositionMapping)
215 || flags.test(efTestPositionMasses)
216 || flags.test(efTestPositionCharges))
218 TestReferenceChecker compound(
219 checker->checkSequenceCompound("Positions", sel.posCount()));
220 for (int i = 0; i < sel.posCount(); ++i)
222 TestReferenceChecker poscompound(compound.checkCompound("Position", NULL));
223 const gmx::SelectionPosition &p = sel.position(i);
224 if (flags.test(efTestPositionAtoms))
226 gmx::ConstArrayRef<int> atoms = p.atomIndices();
227 poscompound.checkSequence(atoms.begin(), atoms.end(), "Atoms");
229 if (flags.test(efTestPositionCoordinates))
231 poscompound.checkVector(p.x(), "Coordinates");
233 if (flags.test(efTestPositionMapping))
235 poscompound.checkInteger(p.refId(), "RefId");
236 poscompound.checkInteger(p.mappedId(), "MappedId");
238 if (flags.test(efTestPositionMasses))
240 poscompound.checkReal(p.mass(), "Mass");
242 if (flags.test(efTestPositionCharges))
244 poscompound.checkReal(p.charge(), "Charge");
252 SelectionCollectionDataTest::runParser(
253 const gmx::ConstArrayRef<const char *> &selections)
255 using gmx::test::TestReferenceChecker;
257 TestReferenceChecker compound(checker_.checkCompound("ParsedSelections", "Parsed"));
260 for (size_t i = 0; i < selections.size(); ++i)
262 SCOPED_TRACE(std::string("Parsing selection \"")
263 + selections[i] + "\"");
264 gmx::SelectionList result;
265 ASSERT_NO_THROW_GMX(result = sc_.parseFromString(selections[i]));
266 sel_.insert(sel_.end(), result.begin(), result.end());
267 if (sel_.size() == count_)
269 std::string id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
270 TestReferenceChecker varcompound(
271 compound.checkCompound("ParsedVariable", id.c_str()));
272 varcompound.checkString(selections[i], "Input");
277 std::string id = gmx::formatString("Selection%d", static_cast<int>(count_ + 1));
278 TestReferenceChecker selcompound(
279 compound.checkCompound("ParsedSelection", id.c_str()));
280 selcompound.checkString(selections[i], "Input");
281 if (flags_.test(efTestSelectionNames))
283 selcompound.checkString(sel_[count_].name(), "Name");
285 selcompound.checkString(sel_[count_].selectionText(), "Text");
286 selcompound.checkBoolean(sel_[count_].isDynamic(), "Dynamic");
294 SelectionCollectionDataTest::runCompiler()
296 ASSERT_NO_THROW_GMX(sc_.compile());
297 ASSERT_EQ(count_, sel_.size());
303 SelectionCollectionDataTest::checkCompiled()
305 using gmx::test::TestReferenceChecker;
306 const TestFlags mask = ~TestFlags(efTestPositionCoordinates);
308 TestReferenceChecker compound(checker_.checkCompound("CompiledSelections", "Compiled"));
309 for (size_t i = 0; i < count_; ++i)
311 SCOPED_TRACE(std::string("Checking selection \"") +
312 sel_[i].selectionText() + "\"");
313 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
314 TestReferenceChecker selcompound(
315 compound.checkCompound("Selection", id.c_str()));
316 if (flags_.test(efTestSelectionNames))
318 selcompound.checkString(sel_[i].name(), "Name");
320 if (!flags_.test(efDontTestCompiledAtoms))
322 checkSelection(&selcompound, sel_[i], flags_ & mask);
329 SelectionCollectionDataTest::runEvaluate()
331 using gmx::test::TestReferenceChecker;
334 ASSERT_NO_THROW_GMX(sc_.evaluate(frame_, NULL));
335 std::string frame = gmx::formatString("Frame%d", framenr_);
336 TestReferenceChecker compound(
337 checker_.checkCompound("EvaluatedSelections", frame.c_str()));
338 for (size_t i = 0; i < count_; ++i)
340 SCOPED_TRACE(std::string("Checking selection \"") +
341 sel_[i].selectionText() + "\"");
342 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
343 TestReferenceChecker selcompound(
344 compound.checkCompound("Selection", id.c_str()));
345 checkSelection(&selcompound, sel_[i], flags_);
351 SelectionCollectionDataTest::runEvaluateFinal()
353 ASSERT_NO_THROW_GMX(sc_.evaluateFinal(framenr_));
359 SelectionCollectionDataTest::runTest(
360 int natoms, const gmx::ConstArrayRef<const char *> &selections)
362 ASSERT_NO_FATAL_FAILURE(runParser(selections));
363 ASSERT_NO_FATAL_FAILURE(setAtomCount(natoms));
364 ASSERT_NO_FATAL_FAILURE(runCompiler());
369 SelectionCollectionDataTest::runTest(
370 const char *filename, const gmx::ConstArrayRef<const char *> &selections)
372 ASSERT_NO_FATAL_FAILURE(runParser(selections));
373 ASSERT_NO_FATAL_FAILURE(loadTopology(filename));
374 ASSERT_NO_FATAL_FAILURE(runCompiler());
375 if (flags_.test(efTestEvaluation))
377 ASSERT_NO_FATAL_FAILURE(runEvaluate());
378 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
383 /********************************************************************
384 * Tests for SelectionCollection functionality without reference data
387 TEST_F(SelectionCollectionTest, HandlesNoSelections)
389 EXPECT_FALSE(sc_.requiresTopology());
390 EXPECT_NO_THROW_GMX(sc_.compile());
393 TEST_F(SelectionCollectionTest, HandlesVelocityAndForceRequests)
395 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr 1 to 10; none"));
396 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
397 ASSERT_EQ(2U, sel_.size());
398 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateVelocities(true));
399 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateVelocities(true));
400 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
401 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateForces(true));
402 ASSERT_NO_THROW_GMX(sc_.compile());
403 EXPECT_TRUE(sel_[0].hasVelocities());
404 EXPECT_TRUE(sel_[1].hasVelocities());
405 EXPECT_TRUE(sel_[0].hasForces());
406 EXPECT_TRUE(sel_[1].hasForces());
409 TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
411 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromFile(
412 gmx::test::TestFileManager::getInputFilePath("selfile.dat")));
413 // These should match the contents of selfile.dat
414 ASSERT_EQ(2U, sel_.size());
415 EXPECT_STREQ("resname RA RB", sel_[0].selectionText());
416 EXPECT_STREQ("resname RB RC", sel_[1].selectionText());
419 TEST_F(SelectionCollectionTest, HandlesAtypicalWhitespace)
421 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr\n1\r\nto\t10;\vatomnr 3\f to 14\r"));
422 ASSERT_EQ(2U, sel_.size());
423 EXPECT_STREQ("atomnr 1 to 10", sel_[0].selectionText());
424 // TODO: Get rid of the trailing whitespace.
425 EXPECT_STREQ("atomnr 3 to 14 ", sel_[1].selectionText());
428 TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
430 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
432 sc_.parseFromString("resname ~ \"R[A\"");
434 }, gmx::InvalidInputError);
437 TEST_F(SelectionCollectionTest, HandlesUnsupportedRegularExpressions)
439 if (!gmx::Regex::isSupported())
441 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
443 sc_.parseFromString("resname \"R[AD]\"");
445 }, gmx::InvalidInputError);
449 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
451 EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"),
452 gmx::InvalidInputError);
455 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
457 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of"),
458 gmx::InvalidInputError);
461 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
463 EXPECT_THROW_GMX(sc_.parseFromString("within of atomnr 1"),
464 gmx::InvalidInputError);
467 // TODO: Tests for more parser errors
469 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser1)
471 ASSERT_NO_THROW_GMX(sc_.setIndexGroups(NULL));
472 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
473 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
476 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser2)
478 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
479 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
480 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
483 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed1)
485 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
486 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
487 EXPECT_THROW_GMX(sc_.setIndexGroups(NULL), gmx::InconsistentInputError);
488 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
491 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed2)
493 ASSERT_NO_THROW_GMX(sc_.parseFromString("group 4; group \"foo\""));
494 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
495 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
496 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
499 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReference)
501 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
502 EXPECT_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group \"GrpUnsorted\""),
503 gmx::InconsistentInputError);
504 EXPECT_THROW_GMX(sc_.parseFromString("group 2 or atomnr 2 to 5"),
505 gmx::InconsistentInputError);
506 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of group 2"),
507 gmx::InconsistentInputError);
510 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReferenceDelayed)
512 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group \"GrpUnsorted\""));
513 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group 2"));
514 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
515 // TODO: Add a separate check in the selection compiler for a safer API
516 // (makes sense in the future if the compiler needs the information for
517 // other purposes as well).
518 // EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
521 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
523 ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
524 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
525 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
528 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
530 ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
531 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
532 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
535 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
537 ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
538 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
539 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
542 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
544 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
545 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
546 EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
549 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
551 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
552 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
553 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
556 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
558 ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
559 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
560 ASSERT_NO_THROW_GMX(sc_.compile());
561 EXPECT_THROW_GMX(sc_.evaluate(frame_, NULL), gmx::InconsistentInputError);
564 // TODO: Tests for evaluation errors
567 /********************************************************************
568 * Tests for selection keywords
571 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
573 static const char * const selections[] = {
577 runTest(10, selections);
580 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
582 static const char * const selections[] = {
583 "atomnr 1 to 3 6 to 8",
587 runTest(10, selections);
590 TEST_F(SelectionCollectionDataTest, HandlesResnr)
592 static const char * const selections[] = {
596 runTest("simple.gro", selections);
599 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
601 static const char * const selections[] = {
605 runTest("simple.pdb", selections);
608 TEST_F(SelectionCollectionDataTest, HandlesMolIndex)
610 static const char * const selections[] = {
614 ASSERT_NO_FATAL_FAILURE(runParser(selections));
615 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
616 topManager_.initUniformMolecules(3);
617 ASSERT_NO_FATAL_FAILURE(runCompiler());
620 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
622 static const char * const selections[] = {
626 runTest("simple.gro", selections);
629 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
631 static const char * const selections[] = {
637 runTest("simple.pdb", selections);
641 TEST_F(SelectionCollectionDataTest, HandlesAtomtype)
643 static const char * const selections[] = {
646 ASSERT_NO_FATAL_FAILURE(runParser(selections));
647 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
648 const char *const types[] = { "CA", "SA", "SB" };
649 topManager_.initAtomTypes(types);
650 ASSERT_NO_FATAL_FAILURE(runCompiler());
653 TEST_F(SelectionCollectionDataTest, HandlesChain)
655 static const char * const selections[] = {
659 runTest("simple.pdb", selections);
662 TEST_F(SelectionCollectionDataTest, HandlesMass)
664 static const char * const selections[] = {
667 ASSERT_NO_FATAL_FAILURE(runParser(selections));
668 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
669 for (int i = 0; i < top_->atoms.nr; ++i)
671 top_->atoms.atom[i].m = 1.0 + i;
673 ASSERT_NO_FATAL_FAILURE(runCompiler());
676 TEST_F(SelectionCollectionDataTest, HandlesCharge)
678 static const char * const selections[] = {
681 ASSERT_NO_FATAL_FAILURE(runParser(selections));
682 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
683 for (int i = 0; i < top_->atoms.nr; ++i)
685 top_->atoms.atom[i].q = i / 10.0;
687 ASSERT_NO_FATAL_FAILURE(runCompiler());
690 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
692 static const char * const selections[] = {
696 runTest("simple.pdb", selections);
699 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
701 static const char * const selections[] = {
705 runTest("simple.pdb", selections);
708 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
710 static const char * const selections[] = {
714 runTest("simple.pdb", selections);
717 TEST_F(SelectionCollectionDataTest, HandlesBeta)
719 static const char * const selections[] = {
723 runTest("simple.pdb", selections);
726 TEST_F(SelectionCollectionDataTest, HandlesResname)
728 static const char * const selections[] = {
732 runTest("simple.gro", selections);
735 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
737 static const char * const selections[] = {
742 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
743 runTest("simple.gro", selections);
747 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
749 static const char * const selections[] = {
750 "same residue as atomnr 1 4 12"
752 runTest("simple.gro", selections);
756 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
758 static const char * const selections[] = {
759 "same resname as atomnr 1 14"
761 runTest("simple.gro", selections);
765 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
767 static const char * const selections[] = {
769 "res_cog of name CB and resnr 1 3",
770 "whole_res_cog of name CB and resnr 1 3",
771 "part_res_cog of x < 3",
772 "dyn_res_cog of x < 3"
774 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
775 | efTestPositionAtoms);
776 runTest("simple.gro", selections);
780 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
782 static const char * const selections[] = {
783 "distance from cog of resnr 1 < 2"
785 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
786 runTest("simple.gro", selections);
790 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
792 static const char * const selections[] = {
793 "mindistance from resnr 1 < 2"
795 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
796 runTest("simple.gro", selections);
800 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
802 static const char * const selections[] = {
803 "within 1 of resnr 2"
805 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
806 runTest("simple.gro", selections);
810 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
812 // Both of these should evaluate to empty on a correct implementation.
813 static const char * const selections[] = {
814 "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
815 "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
817 setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
818 runTest("sphere.gro", selections);
822 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
824 static const char * const selections[] = {
826 "res_cog of resnr 1 to 4 permute 2 1",
827 "name CB S1 and res_cog x < 3 permute 2 1"
829 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
830 | efTestPositionAtoms | efTestPositionMapping);
831 runTest("simple.gro", selections);
835 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
837 static const char * const selections[] = {
838 "name S2 plus name S1",
839 "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
840 "name S1 and y < 3 plus res_cog of x < 2.5"
842 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
843 | efTestPositionAtoms | efTestPositionMapping);
844 runTest("simple.gro", selections);
848 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
850 static const char * const selections[] = {
851 "name S2 merge name S1",
852 "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
853 "name S1 and x < 2.5 merge res_cog of x < 2.5"
855 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
856 | efTestPositionAtoms | efTestPositionMapping);
857 runTest("simple.gro", selections);
861 /********************************************************************
862 * Tests for generic selection evaluation
865 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
867 static const char * const selections[] = {
872 setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms
873 | efTestPositionMasses | efTestPositionCharges);
874 ASSERT_NO_FATAL_FAILURE(runParser(selections));
875 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
876 for (int i = 0; i < top_->atoms.nr; ++i)
878 top_->atoms.atom[i].m = 1.0 + i / 100.0;
879 top_->atoms.atom[i].q = -(1.0 + i / 100.0);
881 ASSERT_NO_FATAL_FAILURE(runCompiler());
882 ASSERT_NO_FATAL_FAILURE(runEvaluate());
883 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
886 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
888 static const char * const selections[] = {
889 "atomnr 1 to 3 8 to 9",
893 setFlags(TestFlags() | efTestPositionAtoms
894 | efTestPositionMasses | efTestPositionCharges);
895 runTest(10, selections);
899 /********************************************************************
900 * Tests for selection syntactic constructs
903 TEST_F(SelectionCollectionDataTest, HandlesSelectionNames)
905 static const char * const selections[] = {
906 "\"GroupSelection\" group \"GrpA\"",
907 "\"DynamicSelection\" x < 5",
910 setFlags(TestFlags() | efTestSelectionNames);
911 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
912 runTest(10, selections);
915 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelections)
917 static const char * const selections[] = {
921 "group \"GrpB\" and resname RB"
923 setFlags(TestFlags() | efTestSelectionNames);
924 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
925 runTest("simple.gro", selections);
928 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelectionsDelayed)
930 static const char * const selections[] = {
934 "group \"GrpB\" and resname RB"
936 setFlags(TestFlags() | efTestSelectionNames);
937 ASSERT_NO_FATAL_FAILURE(runParser(selections));
938 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
939 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
940 ASSERT_NO_FATAL_FAILURE(runCompiler());
943 TEST_F(SelectionCollectionDataTest, HandlesUnsortedIndexGroupsInSelections)
945 static const char * const selections[] = {
946 "foo = group \"GrpUnsorted\"",
947 "group \"GrpUnsorted\"",
950 "res_cog of group \"GrpUnsorted\"",
951 "group \"GrpUnsorted\" permute 2 1",
954 setFlags(TestFlags() | efTestPositionAtoms | efTestPositionMapping
955 | efTestSelectionNames);
956 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
957 runTest("simple.gro", selections);
960 TEST_F(SelectionCollectionDataTest, HandlesUnsortedIndexGroupsInSelectionsDelayed)
962 static const char * const selections[] = {
963 "foo = group \"GrpUnsorted\"",
964 "group \"GrpUnsorted\"",
967 "res_cog of group \"GrpUnsorted\"",
968 "group \"GrpUnsorted\" permute 2 1",
971 ASSERT_NO_FATAL_FAILURE(runParser(selections));
972 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
973 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
974 ASSERT_NO_FATAL_FAILURE(runCompiler());
977 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
979 static const char * const selections[] = {
982 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
983 runTest("simple.gro", selections);
987 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
989 static const char * const selections[] = {
990 "within 1 of [2, 1, 0]"
992 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
993 runTest("simple.gro", selections);
997 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
999 static const char * const selections[] = {
1003 runTest("simple.gro", selections);
1007 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
1009 static const char * const selections[] = {
1013 runTest("simple.gro", selections);
1017 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
1019 static const char * const selections[] = {
1020 "resname \"R[BD]\"",
1021 "resname ~ \"R[BD]\""
1023 if (gmx::Regex::isSupported())
1025 runTest("simple.gro", selections);
1030 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
1032 static const char * const selections[] = {
1033 "atomnr 1 to 5 and atomnr 2 to 7",
1034 "atomnr 1 to 5 or not atomnr 3 to 8",
1035 "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
1036 "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
1037 "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
1039 runTest(10, selections);
1043 TEST_F(SelectionCollectionDataTest, HandlesDynamicAtomValuedParameters)
1045 static const char * const selections[] = {
1046 "same residue as (atomnr 3 5 13 or y > 5)",
1047 "(resnr 1 3 5 or x > 10) and same residue as (atomnr 3 5 13 or z > 5)"
1049 setFlags(TestFlags() | efTestEvaluation);
1050 runTest("simple.gro", selections);
1054 TEST_F(SelectionCollectionDataTest, HandlesEmptySelectionWithUnevaluatedExpressions)
1056 static const char * const selections[] = {
1058 "none and same resname as resnr 2"
1060 runTest("simple.gro", selections);
1064 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
1066 static const char * const selections[] = {
1073 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1074 runTest("simple.gro", selections);
1078 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
1080 static const char * const selections[] = {
1086 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1087 runTest("simple.gro", selections);
1091 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
1093 static const char * const selections[] = {
1099 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1100 runTest("simple.gro", selections);
1104 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
1106 static const char * const selections[] = {
1108 "resname RA and value <= 4",
1109 "resname RA RB and x < 3 and value <= 4",
1111 "resname RA and index < 3",
1112 "resname RB and y < 3 and index < 6"
1114 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1115 runTest("simple.gro", selections);
1119 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
1121 static const char * const selections[] = {
1122 "foo = res_cog of resname RA",
1125 "bar = cog of resname RA",
1129 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1130 runTest("simple.gro", selections);
1134 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
1136 static const char * const selections[] = {
1137 "constpos = [1.0, 2.5, 0.5]",
1139 "within 2 of constpos"
1141 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
1142 | efTestPositionAtoms);
1143 runTest("simple.gro", selections);
1147 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1149 static const char * const selections[] = {
1154 "x + constreal1 < constreal2"
1156 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1157 runTest("simple.gro", selections);
1161 /********************************************************************
1162 * Tests for complex boolean syntax
1165 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1167 static const char * const selections[] = {
1168 "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1169 "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1170 "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1171 "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1172 "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1174 runTest(10, selections);
1178 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1180 static const char * const selections[] = {
1181 "foo = atomnr 4 to 7 or x < 2",
1182 "atomnr 1 to 4 and foo",
1183 "atomnr 2 to 6 and y < 3 and foo",
1184 "atomnr 6 to 10 and not foo"
1186 runTest(10, selections);
1190 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1192 static const char * const selections[] = {
1193 "foo = atomnr 4 to 7",
1194 "bar = foo and x < 2",
1195 "bar2 = foo and y < 2",
1196 "atomnr 1 to 4 and bar",
1197 "atomnr 2 to 6 and y < 3 and bar2",
1198 "atomnr 6 to 10 and not foo"
1200 runTest(10, selections);
1204 /********************************************************************
1205 * Tests for complex subexpression cases
1207 * These tests use some knowledge of the implementation to trigger different
1208 * paths in the code.
1211 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1213 static const char * const selections[] = {
1214 "unused1 = atomnr 1 to 3",
1215 "foo = atomnr 4 to 7",
1216 "atomnr 1 to 6 and foo",
1217 "unused2 = atomnr 3 to 5"
1219 runTest(10, selections);
1223 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1225 static const char * const selections[] = {
1226 "foo = atomnr 4 to 7 and x < 2",
1227 "atomnr 1 to 5 and foo",
1228 "atomnr 3 to 7 and foo"
1230 runTest(10, selections);
1234 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1236 static const char * const selections[] = {
1237 "foo = atomnr 4 to 7 and x < 2",
1238 "atomnr 1 to 6 and foo",
1242 runTest(10, selections);
1246 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups2)
1248 static const char * const selections[] = {
1249 "foo = atomnr 1 to 8 and x < 10",
1250 "atomnr 1 to 5 and y < 10 and foo",
1253 setFlags(TestFlags() | efTestEvaluation);
1254 runTest("simple.gro", selections);