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/topology/topology.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/flags.h"
53 #include "gromacs/utility/gmxregex.h"
54 #include "gromacs/utility/stringutil.h"
56 #include "testutils/refdata.h"
57 #include "testutils/testasserts.h"
58 #include "testutils/testfilemanager.h"
59 #include "testutils/testoptions.h"
66 /********************************************************************
67 * Test fixture for selection testing
70 class SelectionCollectionTest : public ::testing::Test
73 static int s_debugLevel;
75 SelectionCollectionTest();
76 ~SelectionCollectionTest();
78 void setAtomCount(int natoms)
80 ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, natoms));
82 void loadTopology(const char *filename);
84 void loadIndexGroups(const char *filename);
86 gmx::test::TopologyManager topManager_;
87 gmx::SelectionCollection sc_;
88 gmx::SelectionList sel_;
91 gmx_ana_indexgrps_t *grps_;
94 int SelectionCollectionTest::s_debugLevel = 0;
96 // \cond/\endcond do not seem to work here with Doxygen 1.8.5 parser.
98 GMX_TEST_OPTIONS(SelectionCollectionTestOptions, options)
100 options->addOption(gmx::IntegerOption("seldebug")
101 .store(&SelectionCollectionTest::s_debugLevel)
102 .description("Set selection debug level"));
106 SelectionCollectionTest::SelectionCollectionTest()
107 : top_(NULL), frame_(NULL), grps_(NULL)
109 topManager_.requestFrame();
110 sc_.setDebugLevel(s_debugLevel);
111 sc_.setReferencePosType("atom");
112 sc_.setOutputPosType("atom");
115 SelectionCollectionTest::~SelectionCollectionTest()
119 gmx_ana_indexgrps_free(grps_);
124 SelectionCollectionTest::loadTopology(const char *filename)
126 topManager_.loadTopology(filename);
131 SelectionCollectionTest::setTopology()
133 top_ = topManager_.topology();
134 frame_ = topManager_.frame();
136 ASSERT_NO_THROW_GMX(sc_.setTopology(top_, -1));
140 SelectionCollectionTest::loadIndexGroups(const char *filename)
142 GMX_RELEASE_ASSERT(grps_ == NULL,
143 "External groups can only be loaded once");
144 std::string fullpath =
145 gmx::test::TestFileManager::getInputFilePath(filename);
146 gmx_ana_indexgrps_init(&grps_, NULL, fullpath.c_str());
147 sc_.setIndexGroups(grps_);
151 /********************************************************************
152 * Test fixture for selection testing with reference data
155 class SelectionCollectionDataTest : public SelectionCollectionTest
160 efTestEvaluation = 1<<0,
161 efTestPositionAtoms = 1<<1,
162 efTestPositionCoordinates = 1<<2,
163 efTestPositionMapping = 1<<3,
164 efTestPositionMasses = 1<<4,
165 efTestPositionCharges = 1<<5,
166 efTestSelectionNames = 1<<6,
167 efDontTestCompiledAtoms = 1<<8
169 typedef gmx::FlagsTemplate<TestFlag> TestFlags;
171 SelectionCollectionDataTest()
172 : checker_(data_.rootChecker()), count_(0), framenr_(0)
176 void setFlags(TestFlags flags) { flags_ = flags; }
178 void runParser(const gmx::ConstArrayRef<const char *> &selections);
181 void runEvaluateFinal();
183 void runTest(int natoms,
184 const gmx::ConstArrayRef<const char *> &selections);
185 void runTest(const char *filename,
186 const gmx::ConstArrayRef<const char *> &selections);
189 static void checkSelection(gmx::test::TestReferenceChecker *checker,
190 const gmx::Selection &sel, TestFlags flags);
192 void checkCompiled();
194 gmx::test::TestReferenceData data_;
195 gmx::test::TestReferenceChecker checker_;
203 SelectionCollectionDataTest::checkSelection(
204 gmx::test::TestReferenceChecker *checker,
205 const gmx::Selection &sel, TestFlags flags)
207 using gmx::test::TestReferenceChecker;
210 gmx::ConstArrayRef<int> atoms = sel.atomIndices();
211 checker->checkSequence(atoms.begin(), atoms.end(), "Atoms");
213 if (flags.test(efTestPositionAtoms)
214 || flags.test(efTestPositionCoordinates)
215 || flags.test(efTestPositionMapping)
216 || flags.test(efTestPositionMasses)
217 || flags.test(efTestPositionCharges))
219 TestReferenceChecker compound(
220 checker->checkSequenceCompound("Positions", sel.posCount()));
221 for (int i = 0; i < sel.posCount(); ++i)
223 TestReferenceChecker poscompound(compound.checkCompound("Position", NULL));
224 const gmx::SelectionPosition &p = sel.position(i);
225 if (flags.test(efTestPositionAtoms))
227 gmx::ConstArrayRef<int> atoms = p.atomIndices();
228 poscompound.checkSequence(atoms.begin(), atoms.end(), "Atoms");
230 if (flags.test(efTestPositionCoordinates))
232 poscompound.checkVector(p.x(), "Coordinates");
234 if (flags.test(efTestPositionMapping))
236 poscompound.checkInteger(p.refId(), "RefId");
237 poscompound.checkInteger(p.mappedId(), "MappedId");
239 if (flags.test(efTestPositionMasses))
241 poscompound.checkReal(p.mass(), "Mass");
243 if (flags.test(efTestPositionCharges))
245 poscompound.checkReal(p.charge(), "Charge");
253 SelectionCollectionDataTest::runParser(
254 const gmx::ConstArrayRef<const char *> &selections)
256 using gmx::test::TestReferenceChecker;
258 TestReferenceChecker compound(checker_.checkCompound("ParsedSelections", "Parsed"));
261 for (size_t i = 0; i < selections.size(); ++i)
263 SCOPED_TRACE(std::string("Parsing selection \"")
264 + selections[i] + "\"");
265 gmx::SelectionList result;
266 ASSERT_NO_THROW_GMX(result = sc_.parseFromString(selections[i]));
267 sel_.insert(sel_.end(), result.begin(), result.end());
268 if (sel_.size() == count_)
270 std::string id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
271 TestReferenceChecker varcompound(
272 compound.checkCompound("ParsedVariable", id.c_str()));
273 varcompound.checkString(selections[i], "Input");
278 std::string id = gmx::formatString("Selection%d", static_cast<int>(count_ + 1));
279 TestReferenceChecker selcompound(
280 compound.checkCompound("ParsedSelection", id.c_str()));
281 selcompound.checkString(selections[i], "Input");
282 if (flags_.test(efTestSelectionNames))
284 selcompound.checkString(sel_[count_].name(), "Name");
286 selcompound.checkString(sel_[count_].selectionText(), "Text");
287 selcompound.checkBoolean(sel_[count_].isDynamic(), "Dynamic");
295 SelectionCollectionDataTest::runCompiler()
297 ASSERT_NO_THROW_GMX(sc_.compile());
298 ASSERT_EQ(count_, sel_.size());
304 SelectionCollectionDataTest::checkCompiled()
306 using gmx::test::TestReferenceChecker;
307 const TestFlags mask = ~TestFlags(efTestPositionCoordinates);
309 TestReferenceChecker compound(checker_.checkCompound("CompiledSelections", "Compiled"));
310 for (size_t i = 0; i < count_; ++i)
312 SCOPED_TRACE(std::string("Checking selection \"") +
313 sel_[i].selectionText() + "\"");
314 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
315 TestReferenceChecker selcompound(
316 compound.checkCompound("Selection", id.c_str()));
317 if (flags_.test(efTestSelectionNames))
319 selcompound.checkString(sel_[i].name(), "Name");
321 if (!flags_.test(efDontTestCompiledAtoms))
323 checkSelection(&selcompound, sel_[i], flags_ & mask);
330 SelectionCollectionDataTest::runEvaluate()
332 using gmx::test::TestReferenceChecker;
335 ASSERT_NO_THROW_GMX(sc_.evaluate(frame_, NULL));
336 std::string frame = gmx::formatString("Frame%d", framenr_);
337 TestReferenceChecker compound(
338 checker_.checkCompound("EvaluatedSelections", frame.c_str()));
339 for (size_t i = 0; i < count_; ++i)
341 SCOPED_TRACE(std::string("Checking selection \"") +
342 sel_[i].selectionText() + "\"");
343 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
344 TestReferenceChecker selcompound(
345 compound.checkCompound("Selection", id.c_str()));
346 checkSelection(&selcompound, sel_[i], flags_);
352 SelectionCollectionDataTest::runEvaluateFinal()
354 ASSERT_NO_THROW_GMX(sc_.evaluateFinal(framenr_));
360 SelectionCollectionDataTest::runTest(
361 int natoms, const gmx::ConstArrayRef<const char *> &selections)
363 ASSERT_NO_FATAL_FAILURE(runParser(selections));
364 ASSERT_NO_FATAL_FAILURE(setAtomCount(natoms));
365 ASSERT_NO_FATAL_FAILURE(runCompiler());
370 SelectionCollectionDataTest::runTest(
371 const char *filename, const gmx::ConstArrayRef<const char *> &selections)
373 ASSERT_NO_FATAL_FAILURE(runParser(selections));
374 ASSERT_NO_FATAL_FAILURE(loadTopology(filename));
375 ASSERT_NO_FATAL_FAILURE(runCompiler());
376 if (flags_.test(efTestEvaluation))
378 ASSERT_NO_FATAL_FAILURE(runEvaluate());
379 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
384 /********************************************************************
385 * Tests for SelectionCollection functionality without reference data
388 TEST_F(SelectionCollectionTest, HandlesNoSelections)
390 EXPECT_FALSE(sc_.requiresTopology());
391 EXPECT_NO_THROW_GMX(sc_.compile());
394 TEST_F(SelectionCollectionTest, HandlesVelocityAndForceRequests)
396 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr 1 to 10; none"));
397 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
398 ASSERT_EQ(2U, sel_.size());
399 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateVelocities(true));
400 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateVelocities(true));
401 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
402 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateForces(true));
403 ASSERT_NO_THROW_GMX(sc_.compile());
404 EXPECT_TRUE(sel_[0].hasVelocities());
405 EXPECT_TRUE(sel_[1].hasVelocities());
406 EXPECT_TRUE(sel_[0].hasForces());
407 EXPECT_TRUE(sel_[1].hasForces());
410 TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
412 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromFile(
413 gmx::test::TestFileManager::getInputFilePath("selfile.dat")));
414 // These should match the contents of selfile.dat
415 ASSERT_EQ(2U, sel_.size());
416 EXPECT_STREQ("resname RA RB", sel_[0].selectionText());
417 EXPECT_STREQ("resname RB RC", sel_[1].selectionText());
420 TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
422 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
424 sc_.parseFromString("resname ~ \"R[A\"");
426 }, gmx::InvalidInputError);
429 TEST_F(SelectionCollectionTest, HandlesUnsupportedRegularExpressions)
431 if (!gmx::Regex::isSupported())
433 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
435 sc_.parseFromString("resname \"R[AD]\"");
437 }, gmx::InvalidInputError);
441 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
443 EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"),
444 gmx::InvalidInputError);
447 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
449 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of"),
450 gmx::InvalidInputError);
453 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
455 EXPECT_THROW_GMX(sc_.parseFromString("within of atomnr 1"),
456 gmx::InvalidInputError);
459 // TODO: Tests for more parser errors
461 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser1)
463 ASSERT_NO_THROW_GMX(sc_.setIndexGroups(NULL));
464 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
465 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
468 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser2)
470 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
471 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
472 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
475 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed1)
477 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
478 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
479 EXPECT_THROW_GMX(sc_.setIndexGroups(NULL), gmx::InconsistentInputError);
480 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
483 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed2)
485 ASSERT_NO_THROW_GMX(sc_.parseFromString("group 4; group \"foo\""));
486 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
487 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
488 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
491 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReference)
493 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
494 EXPECT_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group \"GrpUnsorted\""),
495 gmx::InconsistentInputError);
496 EXPECT_THROW_GMX(sc_.parseFromString("group 2 or atomnr 2 to 5"),
497 gmx::InconsistentInputError);
498 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of group 2"),
499 gmx::InconsistentInputError);
502 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReferenceDelayed)
504 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group \"GrpUnsorted\""));
505 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group 2"));
506 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
507 // TODO: Add a separate check in the selection compiler for a safer API
508 // (makes sense in the future if the compiler needs the information for
509 // other purposes as well).
510 // EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
513 TEST_F(SelectionCollectionTest, HandlesOutOfRangeAtomIndexInGroup)
515 ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, 5));
516 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
517 EXPECT_THROW_GMX(sc_.parseFromString("group \"GrpB\""), gmx::InconsistentInputError);
520 TEST_F(SelectionCollectionTest, HandlesOutOfRangeAtomIndexInGroupDelayed)
522 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
523 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"GrpB\""));
524 EXPECT_THROW_GMX(sc_.setTopology(NULL, 5), gmx::InconsistentInputError);
527 TEST_F(SelectionCollectionTest, HandlesOutOfRangeAtomIndexInGroupDelayed2)
529 ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, 5));
530 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"GrpB\""));
531 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
534 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
536 ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
537 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
538 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
541 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
543 ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
544 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
545 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
548 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
550 ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
551 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
552 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
555 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
557 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
558 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
559 EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
562 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
564 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
565 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
566 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
569 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
571 ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
572 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
573 ASSERT_NO_THROW_GMX(sc_.compile());
574 EXPECT_THROW_GMX(sc_.evaluate(frame_, NULL), gmx::InconsistentInputError);
577 // TODO: Tests for evaluation errors
580 /********************************************************************
581 * Tests for selection keywords
584 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
586 static const char * const selections[] = {
590 runTest(10, selections);
593 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
595 static const char * const selections[] = {
596 "atomnr 1 to 3 6 to 8",
600 runTest(10, selections);
603 TEST_F(SelectionCollectionDataTest, HandlesResnr)
605 static const char * const selections[] = {
609 runTest("simple.gro", selections);
612 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
614 static const char * const selections[] = {
618 runTest("simple.pdb", selections);
621 TEST_F(SelectionCollectionDataTest, HandlesMolIndex)
623 static const char * const selections[] = {
627 ASSERT_NO_FATAL_FAILURE(runParser(selections));
628 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
629 topManager_.initUniformMolecules(3);
630 ASSERT_NO_FATAL_FAILURE(runCompiler());
633 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
635 static const char * const selections[] = {
639 runTest("simple.gro", selections);
642 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
644 static const char * const selections[] = {
650 runTest("simple.pdb", selections);
654 TEST_F(SelectionCollectionDataTest, HandlesAtomtype)
656 static const char * const selections[] = {
659 ASSERT_NO_FATAL_FAILURE(runParser(selections));
660 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
661 const char *const types[] = { "CA", "SA", "SB" };
662 topManager_.initAtomTypes(types);
663 ASSERT_NO_FATAL_FAILURE(runCompiler());
666 TEST_F(SelectionCollectionDataTest, HandlesChain)
668 static const char * const selections[] = {
672 runTest("simple.pdb", selections);
675 TEST_F(SelectionCollectionDataTest, HandlesMass)
677 static const char * const selections[] = {
680 ASSERT_NO_FATAL_FAILURE(runParser(selections));
681 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
682 for (int i = 0; i < top_->atoms.nr; ++i)
684 top_->atoms.atom[i].m = 1.0 + i;
686 ASSERT_NO_FATAL_FAILURE(runCompiler());
689 TEST_F(SelectionCollectionDataTest, HandlesCharge)
691 static const char * const selections[] = {
694 ASSERT_NO_FATAL_FAILURE(runParser(selections));
695 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
696 for (int i = 0; i < top_->atoms.nr; ++i)
698 top_->atoms.atom[i].q = i / 10.0;
700 ASSERT_NO_FATAL_FAILURE(runCompiler());
703 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
705 static const char * const selections[] = {
709 runTest("simple.pdb", selections);
712 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
714 static const char * const selections[] = {
718 runTest("simple.pdb", selections);
721 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
723 static const char * const selections[] = {
727 runTest("simple.pdb", selections);
730 TEST_F(SelectionCollectionDataTest, HandlesBeta)
732 static const char * const selections[] = {
736 runTest("simple.pdb", selections);
739 TEST_F(SelectionCollectionDataTest, HandlesResname)
741 static const char * const selections[] = {
745 runTest("simple.gro", selections);
748 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
750 static const char * const selections[] = {
755 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
756 runTest("simple.gro", selections);
760 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
762 static const char * const selections[] = {
763 "same residue as atomnr 1 4 12"
765 runTest("simple.gro", selections);
769 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
771 static const char * const selections[] = {
772 "same resname as atomnr 1 14"
774 runTest("simple.gro", selections);
778 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
780 static const char * const selections[] = {
782 "res_cog of name CB and resnr 1 3",
783 "whole_res_cog of name CB and resnr 1 3",
784 "part_res_cog of x < 3",
785 "dyn_res_cog of x < 3"
787 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
788 | efTestPositionAtoms);
789 runTest("simple.gro", selections);
793 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
795 static const char * const selections[] = {
796 "distance from cog of resnr 1 < 2"
798 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
799 runTest("simple.gro", selections);
803 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
805 static const char * const selections[] = {
806 "mindistance from resnr 1 < 2"
808 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
809 runTest("simple.gro", selections);
813 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
815 static const char * const selections[] = {
816 "within 1 of resnr 2"
818 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
819 runTest("simple.gro", selections);
823 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
825 // Both of these should evaluate to empty on a correct implementation.
826 static const char * const selections[] = {
827 "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
828 "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
830 setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
831 runTest("sphere.gro", selections);
835 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
837 static const char * const selections[] = {
839 "res_cog of resnr 1 to 4 permute 2 1",
840 "name CB S1 and res_cog x < 3 permute 2 1"
842 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
843 | efTestPositionAtoms | efTestPositionMapping);
844 runTest("simple.gro", selections);
848 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
850 static const char * const selections[] = {
851 "name S2 plus name S1",
852 "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
853 "name S1 and y < 3 plus res_cog of x < 2.5"
855 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
856 | efTestPositionAtoms | efTestPositionMapping);
857 runTest("simple.gro", selections);
861 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
863 static const char * const selections[] = {
864 "name S2 merge name S1",
865 "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
866 "name S1 and x < 2.5 merge res_cog of x < 2.5"
868 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
869 | efTestPositionAtoms | efTestPositionMapping);
870 runTest("simple.gro", selections);
874 /********************************************************************
875 * Tests for generic selection evaluation
878 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
880 static const char * const selections[] = {
885 setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms
886 | efTestPositionMasses | efTestPositionCharges);
887 ASSERT_NO_FATAL_FAILURE(runParser(selections));
888 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
889 for (int i = 0; i < top_->atoms.nr; ++i)
891 top_->atoms.atom[i].m = 1.0 + i / 100.0;
892 top_->atoms.atom[i].q = -(1.0 + i / 100.0);
894 ASSERT_NO_FATAL_FAILURE(runCompiler());
895 ASSERT_NO_FATAL_FAILURE(runEvaluate());
896 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
899 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
901 static const char * const selections[] = {
902 "atomnr 1 to 3 8 to 9",
906 setFlags(TestFlags() | efTestPositionAtoms
907 | efTestPositionMasses | efTestPositionCharges);
908 runTest(10, selections);
912 /********************************************************************
913 * Tests for selection syntactic constructs
916 TEST_F(SelectionCollectionDataTest, HandlesSelectionNames)
918 static const char * const selections[] = {
919 "\"GroupSelection\" group \"GrpA\"",
920 "\"DynamicSelection\" x < 5",
923 setFlags(TestFlags() | efTestSelectionNames);
924 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
925 runTest(10, selections);
928 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelections)
930 static const char * const selections[] = {
934 "group \"GrpB\" and resname RB"
936 setFlags(TestFlags() | efTestSelectionNames);
937 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
938 runTest("simple.gro", selections);
941 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelectionsDelayed)
943 static const char * const selections[] = {
947 "group \"GrpB\" and resname RB"
949 setFlags(TestFlags() | efTestSelectionNames);
950 ASSERT_NO_FATAL_FAILURE(runParser(selections));
951 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
952 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
953 ASSERT_NO_FATAL_FAILURE(runCompiler());
956 TEST_F(SelectionCollectionDataTest, HandlesUnsortedIndexGroupsInSelections)
958 static const char * const selections[] = {
959 "foo = group \"GrpUnsorted\"",
960 "group \"GrpUnsorted\"",
963 "res_cog of group \"GrpUnsorted\"",
964 "group \"GrpUnsorted\" permute 2 1",
967 setFlags(TestFlags() | efTestPositionAtoms | efTestPositionMapping
968 | efTestSelectionNames);
969 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
970 runTest("simple.gro", selections);
973 TEST_F(SelectionCollectionDataTest, HandlesUnsortedIndexGroupsInSelectionsDelayed)
975 static const char * const selections[] = {
976 "foo = group \"GrpUnsorted\"",
977 "group \"GrpUnsorted\"",
980 "res_cog of group \"GrpUnsorted\"",
981 "group \"GrpUnsorted\" permute 2 1",
984 ASSERT_NO_FATAL_FAILURE(runParser(selections));
985 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
986 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
987 ASSERT_NO_FATAL_FAILURE(runCompiler());
990 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
992 static const char * const selections[] = {
995 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
996 runTest("simple.gro", selections);
1000 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
1002 static const char * const selections[] = {
1003 "within 1 of [2, 1, 0]"
1005 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1006 runTest("simple.gro", selections);
1010 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
1012 static const char * const selections[] = {
1016 runTest("simple.gro", selections);
1020 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
1022 static const char * const selections[] = {
1026 runTest("simple.gro", selections);
1030 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
1032 static const char * const selections[] = {
1033 "resname \"R[BD]\"",
1034 "resname ~ \"R[BD]\""
1036 if (gmx::Regex::isSupported())
1038 runTest("simple.gro", selections);
1043 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
1045 static const char * const selections[] = {
1046 "atomnr 1 to 5 and atomnr 2 to 7",
1047 "atomnr 1 to 5 or not atomnr 3 to 8",
1048 "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
1049 "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
1050 "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
1052 runTest(10, selections);
1056 TEST_F(SelectionCollectionDataTest, HandlesDynamicAtomValuedParameters)
1058 static const char * const selections[] = {
1059 "same residue as (atomnr 3 5 13 or y > 5)",
1060 "(resnr 1 3 5 or x > 10) and same residue as (atomnr 3 5 13 or z > 5)"
1062 setFlags(TestFlags() | efTestEvaluation);
1063 runTest("simple.gro", selections);
1067 TEST_F(SelectionCollectionDataTest, HandlesEmptySelectionWithUnevaluatedExpressions)
1069 static const char * const selections[] = {
1071 "none and same resname as resnr 2"
1073 runTest("simple.gro", selections);
1077 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
1079 static const char * const selections[] = {
1086 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1087 runTest("simple.gro", selections);
1091 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
1093 static const char * const selections[] = {
1099 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1100 runTest("simple.gro", selections);
1104 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
1106 static const char * const selections[] = {
1112 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1113 runTest("simple.gro", selections);
1117 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
1119 static const char * const selections[] = {
1121 "resname RA and value <= 4",
1122 "resname RA RB and x < 3 and value <= 4",
1124 "resname RA and index < 3",
1125 "resname RB and y < 3 and index < 6"
1127 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1128 runTest("simple.gro", selections);
1132 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
1134 static const char * const selections[] = {
1135 "foo = res_cog of resname RA",
1138 "bar = cog of resname RA",
1142 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1143 runTest("simple.gro", selections);
1147 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
1149 static const char * const selections[] = {
1150 "constpos = [1.0, 2.5, 0.5]",
1152 "within 2 of constpos"
1154 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
1155 | efTestPositionAtoms);
1156 runTest("simple.gro", selections);
1160 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1162 static const char * const selections[] = {
1167 "x + constreal1 < constreal2"
1169 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1170 runTest("simple.gro", selections);
1174 /********************************************************************
1175 * Tests for complex boolean syntax
1178 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1180 static const char * const selections[] = {
1181 "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1182 "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1183 "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1184 "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1185 "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1187 runTest(10, selections);
1191 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1193 static const char * const selections[] = {
1194 "foo = atomnr 4 to 7 or x < 2",
1195 "atomnr 1 to 4 and foo",
1196 "atomnr 2 to 6 and y < 3 and foo",
1197 "atomnr 6 to 10 and not foo"
1199 runTest(10, selections);
1203 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1205 static const char * const selections[] = {
1206 "foo = atomnr 4 to 7",
1207 "bar = foo and x < 2",
1208 "bar2 = foo and y < 2",
1209 "atomnr 1 to 4 and bar",
1210 "atomnr 2 to 6 and y < 3 and bar2",
1211 "atomnr 6 to 10 and not foo"
1213 runTest(10, selections);
1217 /********************************************************************
1218 * Tests for complex subexpression cases
1220 * These tests use some knowledge of the implementation to trigger different
1221 * paths in the code.
1224 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1226 static const char * const selections[] = {
1227 "unused1 = atomnr 1 to 3",
1228 "foo = atomnr 4 to 7",
1229 "atomnr 1 to 6 and foo",
1230 "unused2 = atomnr 3 to 5"
1232 runTest(10, selections);
1236 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1238 static const char * const selections[] = {
1239 "foo = atomnr 4 to 7 and x < 2",
1240 "atomnr 1 to 5 and foo",
1241 "atomnr 3 to 7 and foo"
1243 runTest(10, selections);
1247 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1249 static const char * const selections[] = {
1250 "foo = atomnr 4 to 7 and x < 2",
1251 "atomnr 1 to 6 and foo",
1255 runTest(10, selections);
1259 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups2)
1261 static const char * const selections[] = {
1262 "foo = atomnr 1 to 8 and x < 10",
1263 "atomnr 1 to 5 and y < 10 and foo",
1266 setFlags(TestFlags() | efTestEvaluation);
1267 runTest("simple.gro", selections);