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/legacyheaders/smalloc.h"
45 #include "gromacs/legacyheaders/statutil.h"
46 #include "gromacs/legacyheaders/tpxio.h"
47 #include "gromacs/legacyheaders/vec.h"
49 #include "gromacs/options/basicoptions.h"
50 #include "gromacs/options/options.h"
51 #include "gromacs/selection/selectioncollection.h"
52 #include "gromacs/selection/selection.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/flags.h"
55 #include "gromacs/utility/gmxregex.h"
56 #include "gromacs/utility/stringutil.h"
58 #include "testutils/refdata.h"
59 #include "testutils/testasserts.h"
60 #include "testutils/testfilemanager.h"
61 #include "testutils/testoptions.h"
66 /********************************************************************
67 * Test fixture for selection testing
70 class SelectionCollectionTest : public ::testing::Test
73 static void SetUpTestCase();
75 static int s_debugLevel;
77 SelectionCollectionTest();
78 ~SelectionCollectionTest();
80 void setAtomCount(int natoms)
82 ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, natoms));
84 void loadTopology(const char *filename);
86 gmx::SelectionCollection sc_;
87 gmx::SelectionList sel_;
92 int SelectionCollectionTest::s_debugLevel = 0;
94 void SelectionCollectionTest::SetUpTestCase()
96 gmx::Options options(NULL, NULL);
97 options.addOption(gmx::IntegerOption("seldebug").store(&s_debugLevel));
98 gmx::test::parseTestOptions(&options);
102 SelectionCollectionTest::SelectionCollectionTest()
103 : top_(NULL), frame_(NULL)
105 sc_.setDebugLevel(s_debugLevel);
106 sc_.setReferencePosType("atom");
107 sc_.setOutputPosType("atom");
111 SelectionCollectionTest::~SelectionCollectionTest()
115 free_t_atoms(&top_->atoms, TRUE);
129 SelectionCollectionTest::loadTopology(const char *filename)
137 read_tps_conf(gmx::test::TestFileManager::getInputFilePath(filename).c_str(),
138 title, top_, &ePBC, &xtop, NULL, box, FALSE);
141 frame_->flags = TRX_NEED_X;
142 frame_->natoms = top_->atoms.nr;
144 snew(frame_->x, frame_->natoms);
145 memcpy(frame_->x, xtop, sizeof(*frame_->x) * frame_->natoms);
147 copy_mat(box, frame_->box);
151 ASSERT_NO_THROW_GMX(sc_.setTopology(top_, -1));
155 /********************************************************************
156 * Test fixture for selection testing with reference data
159 class SelectionCollectionDataTest : public SelectionCollectionTest
164 efTestEvaluation = 1<<0,
165 efTestPositionAtoms = 1<<1,
166 efTestPositionCoordinates = 1<<2,
167 efTestPositionMapping = 1<<3,
168 efTestPositionMasses = 1<<4,
169 efTestPositionCharges = 1<<5,
170 efDontTestCompiledAtoms = 1<<8
172 typedef gmx::FlagsTemplate<TestFlag> TestFlags;
174 SelectionCollectionDataTest()
175 : checker_(data_.rootChecker()), count_(0), framenr_(0)
179 void setFlags(TestFlags flags) { flags_ = flags; }
181 template <size_t count>
182 void runTest(int natoms, const char *const (&selections)[count])
184 runTest(natoms, selections, count);
186 template <size_t count>
187 void runTest(const char *filename, const char *const (&selections)[count])
189 runTest(filename, selections, count);
192 template <size_t count>
193 void runParser(const char *const (&selections)[count])
195 runParser(selections, count);
200 void runEvaluateFinal();
203 static void checkSelection(gmx::test::TestReferenceChecker *checker,
204 const gmx::Selection &sel, TestFlags flags);
206 void runTest(int natoms, const char *const *selections, size_t count);
207 void runTest(const char *filename, const char *const *selections,
209 void runParser(const char *const *selections, size_t count);
211 void checkCompiled();
213 gmx::test::TestReferenceData data_;
214 gmx::test::TestReferenceChecker checker_;
222 SelectionCollectionDataTest::checkSelection(
223 gmx::test::TestReferenceChecker *checker,
224 const gmx::Selection &sel, TestFlags flags)
226 using gmx::test::TestReferenceChecker;
229 gmx::ConstArrayRef<int> atoms = sel.atomIndices();
230 checker->checkSequence(atoms.begin(), atoms.end(), "Atoms");
232 if (flags.test(efTestPositionAtoms)
233 || flags.test(efTestPositionCoordinates)
234 || flags.test(efTestPositionMapping)
235 || flags.test(efTestPositionMasses)
236 || flags.test(efTestPositionCharges))
238 TestReferenceChecker compound(
239 checker->checkSequenceCompound("Positions", sel.posCount()));
240 for (int i = 0; i < sel.posCount(); ++i)
242 TestReferenceChecker poscompound(compound.checkCompound("Position", NULL));
243 const gmx::SelectionPosition &p = sel.position(i);
244 if (flags.test(efTestPositionAtoms))
246 gmx::ConstArrayRef<int> atoms = p.atomIndices();
247 poscompound.checkSequence(atoms.begin(), atoms.end(), "Atoms");
249 if (flags.test(efTestPositionCoordinates))
251 poscompound.checkVector(p.x(), "Coordinates");
253 if (flags.test(efTestPositionMapping))
255 poscompound.checkInteger(p.refId(), "RefId");
256 poscompound.checkInteger(p.mappedId(), "MappedId");
258 if (flags.test(efTestPositionMasses))
260 poscompound.checkReal(p.mass(), "Mass");
262 if (flags.test(efTestPositionCharges))
264 poscompound.checkReal(p.charge(), "Charge");
272 SelectionCollectionDataTest::runParser(const char *const *selections,
275 using gmx::test::TestReferenceChecker;
277 TestReferenceChecker compound(checker_.checkCompound("ParsedSelections", "Parsed"));
280 for (size_t i = 0; i < count; ++i)
282 SCOPED_TRACE(std::string("Parsing selection \"")
283 + selections[i] + "\"");
284 gmx::SelectionList result;
285 ASSERT_NO_THROW_GMX(result = sc_.parseFromString(selections[i]));
286 sel_.insert(sel_.end(), result.begin(), result.end());
287 if (sel_.size() == count_)
289 std::string id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
290 TestReferenceChecker varcompound(
291 compound.checkCompound("ParsedVariable", id.c_str()));
292 varcompound.checkString(selections[i], "Input");
297 std::string id = gmx::formatString("Selection%d", static_cast<int>(count_ + 1));
298 TestReferenceChecker selcompound(
299 compound.checkCompound("ParsedSelection", id.c_str()));
300 selcompound.checkString(selections[i], "Input");
301 selcompound.checkString(sel_[count_].name(), "Name");
302 selcompound.checkString(sel_[count_].selectionText(), "Text");
303 selcompound.checkBoolean(sel_[count_].isDynamic(), "Dynamic");
311 SelectionCollectionDataTest::runCompiler()
313 ASSERT_NO_THROW_GMX(sc_.compile());
314 ASSERT_EQ(count_, sel_.size());
320 SelectionCollectionDataTest::checkCompiled()
322 using gmx::test::TestReferenceChecker;
323 const TestFlags mask = ~TestFlags(efTestPositionCoordinates);
325 TestReferenceChecker compound(checker_.checkCompound("CompiledSelections", "Compiled"));
326 for (size_t i = 0; i < count_; ++i)
328 SCOPED_TRACE(std::string("Checking selection \"") +
329 sel_[i].selectionText() + "\"");
330 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
331 TestReferenceChecker selcompound(
332 compound.checkCompound("Selection", id.c_str()));
333 if (!flags_.test(efDontTestCompiledAtoms))
335 checkSelection(&selcompound, sel_[i], flags_ & mask);
342 SelectionCollectionDataTest::runEvaluate()
344 using gmx::test::TestReferenceChecker;
347 ASSERT_NO_THROW_GMX(sc_.evaluate(frame_, NULL));
348 std::string frame = gmx::formatString("Frame%d", framenr_);
349 TestReferenceChecker compound(
350 checker_.checkCompound("EvaluatedSelections", frame.c_str()));
351 for (size_t i = 0; i < count_; ++i)
353 SCOPED_TRACE(std::string("Checking selection \"") +
354 sel_[i].selectionText() + "\"");
355 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
356 TestReferenceChecker selcompound(
357 compound.checkCompound("Selection", id.c_str()));
358 checkSelection(&selcompound, sel_[i], flags_);
364 SelectionCollectionDataTest::runEvaluateFinal()
366 ASSERT_NO_THROW_GMX(sc_.evaluateFinal(framenr_));
367 if (!checker_.isWriteMode())
375 SelectionCollectionDataTest::runTest(int natoms, const char * const *selections,
378 ASSERT_NO_FATAL_FAILURE(runParser(selections, count));
379 ASSERT_NO_FATAL_FAILURE(setAtomCount(natoms));
380 ASSERT_NO_FATAL_FAILURE(runCompiler());
385 SelectionCollectionDataTest::runTest(const char *filename,
386 const char * const *selections,
389 ASSERT_NO_FATAL_FAILURE(runParser(selections, count));
390 ASSERT_NO_FATAL_FAILURE(loadTopology(filename));
391 ASSERT_NO_FATAL_FAILURE(runCompiler());
392 if (flags_.test(efTestEvaluation))
394 ASSERT_NO_FATAL_FAILURE(runEvaluate());
395 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
400 /********************************************************************
401 * Tests for SelectionCollection functionality without reference data
404 TEST_F(SelectionCollectionTest, HandlesNoSelections)
406 EXPECT_FALSE(sc_.requiresTopology());
407 EXPECT_NO_THROW_GMX(sc_.compile());
410 TEST_F(SelectionCollectionTest, HandlesVelocityAndForceRequests)
412 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr 1 to 10; none"));
413 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
414 ASSERT_EQ(2U, sel_.size());
415 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateVelocities(true));
416 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateVelocities(true));
417 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
418 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateForces(true));
419 ASSERT_NO_THROW_GMX(sc_.compile());
420 EXPECT_TRUE(sel_[0].hasVelocities());
421 EXPECT_TRUE(sel_[1].hasVelocities());
422 EXPECT_TRUE(sel_[0].hasForces());
423 EXPECT_TRUE(sel_[1].hasForces());
426 TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
428 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromFile(
429 gmx::test::TestFileManager::getInputFilePath("selfile.dat")));
430 // These should match the contents of selfile.dat
431 ASSERT_EQ(2U, sel_.size());
432 EXPECT_STREQ("resname RA RB", sel_[0].selectionText());
433 EXPECT_STREQ("resname RB RC", sel_[1].selectionText());
436 TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
438 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
440 sc_.parseFromString("resname ~ \"R[A\"");
442 }, gmx::InvalidInputError);
445 TEST_F(SelectionCollectionTest, HandlesUnsupportedRegularExpressions)
447 if (!gmx::Regex::isSupported())
449 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
451 sc_.parseFromString("resname \"R[AD]\"");
453 }, gmx::InvalidInputError);
457 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
459 EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"),
460 gmx::InvalidInputError);
463 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
465 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of"),
466 gmx::InvalidInputError);
469 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
471 EXPECT_THROW_GMX(sc_.parseFromString("within of atomnr 1"),
472 gmx::InvalidInputError);
475 TEST_F(SelectionCollectionTest, HandlesHelpKeywordInInvalidContext)
477 EXPECT_THROW_GMX(sc_.parseFromString("resname help"),
478 gmx::InvalidInputError);
481 // TODO: Tests for more parser errors
483 TEST_F(SelectionCollectionTest, RecoversFromUnknownGroupReference)
485 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
486 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
487 EXPECT_THROW_GMX(sc_.setIndexGroups(NULL), gmx::InvalidInputError);
488 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
491 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
493 ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
494 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
495 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
498 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
500 ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
501 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
502 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
505 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
507 ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
508 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
509 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
512 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
514 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
515 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
516 EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
519 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
521 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
522 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
523 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
526 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
528 ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
529 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
530 ASSERT_NO_THROW_GMX(sc_.compile());
531 EXPECT_THROW_GMX(sc_.evaluate(frame_, NULL), gmx::InconsistentInputError);
534 // TODO: Tests for evaluation errors
537 /********************************************************************
538 * Tests for selection keywords
541 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
543 static const char * const selections[] = {
547 runTest(10, selections);
550 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
552 static const char * const selections[] = {
553 "atomnr 1 to 3 6 to 8",
557 runTest(10, selections);
560 TEST_F(SelectionCollectionDataTest, HandlesResnr)
562 static const char * const selections[] = {
566 runTest("simple.gro", selections);
569 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
571 static const char * const selections[] = {
575 runTest("simple.pdb", selections);
578 // TODO: Add test for "molindex"
580 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
582 static const char * const selections[] = {
586 runTest("simple.gro", selections);
589 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
591 static const char * const selections[] = {
597 runTest("simple.pdb", selections);
600 // TODO: Add test for atomtype
602 TEST_F(SelectionCollectionDataTest, HandlesChain)
604 static const char * const selections[] = {
608 runTest("simple.pdb", selections);
611 TEST_F(SelectionCollectionDataTest, HandlesMass)
613 static const char * const selections[] = {
616 ASSERT_NO_FATAL_FAILURE(runParser(selections));
617 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
618 for (int i = 0; i < top_->atoms.nr; ++i)
620 top_->atoms.atom[i].m = 1.0 + i;
622 ASSERT_NO_FATAL_FAILURE(runCompiler());
625 TEST_F(SelectionCollectionDataTest, HandlesCharge)
627 static const char * const selections[] = {
630 ASSERT_NO_FATAL_FAILURE(runParser(selections));
631 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
632 for (int i = 0; i < top_->atoms.nr; ++i)
634 top_->atoms.atom[i].q = i / 10.0;
636 ASSERT_NO_FATAL_FAILURE(runCompiler());
639 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
641 static const char * const selections[] = {
645 runTest("simple.pdb", selections);
648 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
650 static const char * const selections[] = {
654 runTest("simple.pdb", selections);
657 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
659 static const char * const selections[] = {
663 runTest("simple.pdb", selections);
666 TEST_F(SelectionCollectionDataTest, HandlesBeta)
668 static const char * const selections[] = {
672 runTest("simple.pdb", selections);
675 TEST_F(SelectionCollectionDataTest, HandlesResname)
677 static const char * const selections[] = {
681 runTest("simple.gro", selections);
684 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
686 static const char * const selections[] = {
691 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
692 runTest("simple.gro", selections);
696 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
698 static const char * const selections[] = {
699 "same residue as atomnr 1 4 12"
701 runTest("simple.gro", selections);
705 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
707 static const char * const selections[] = {
708 "same resname as atomnr 1 14"
710 runTest("simple.gro", selections);
714 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
716 static const char * const selections[] = {
718 "res_cog of name CB and resnr 1 3",
719 "whole_res_cog of name CB and resnr 1 3",
720 "part_res_cog of x < 3",
721 "dyn_res_cog of x < 3"
723 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
724 | efTestPositionAtoms);
725 runTest("simple.gro", selections);
729 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
731 static const char * const selections[] = {
732 "distance from cog of resnr 1 < 2"
734 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
735 runTest("simple.gro", selections);
739 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
741 static const char * const selections[] = {
742 "mindistance from resnr 1 < 2"
744 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
745 runTest("simple.gro", selections);
749 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
751 static const char * const selections[] = {
752 "within 1 of resnr 2"
754 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
755 runTest("simple.gro", selections);
759 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
761 // Both of these should evaluate to empty on a correct implementation.
762 static const char * const selections[] = {
763 "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
764 "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
766 setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
767 runTest("sphere.gro", selections);
771 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
773 static const char * const selections[] = {
775 "res_cog of resnr 1 to 4 permute 2 1",
776 "name CB S1 and res_cog x < 3 permute 2 1"
778 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
779 | efTestPositionAtoms | efTestPositionMapping);
780 runTest("simple.gro", selections);
784 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
786 static const char * const selections[] = {
787 "name S2 plus name S1",
788 "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
789 "name S1 and y < 3 plus res_cog of x < 2.5"
791 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
792 | efTestPositionAtoms | efTestPositionMapping);
793 runTest("simple.gro", selections);
797 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
799 static const char * const selections[] = {
800 "name S2 merge name S1",
801 "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
802 "name S1 and x < 2.5 merge res_cog of x < 2.5"
804 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
805 | efTestPositionAtoms | efTestPositionMapping);
806 runTest("simple.gro", selections);
810 /********************************************************************
811 * Tests for generic selection evaluation
814 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
816 static const char * const selections[] = {
821 setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms
822 | efTestPositionMasses | efTestPositionCharges);
823 ASSERT_NO_FATAL_FAILURE(runParser(selections));
824 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
825 for (int i = 0; i < top_->atoms.nr; ++i)
827 top_->atoms.atom[i].m = 1.0 + i / 100.0;
828 top_->atoms.atom[i].q = -(1.0 + i / 100.0);
830 ASSERT_NO_FATAL_FAILURE(runCompiler());
831 ASSERT_NO_FATAL_FAILURE(runEvaluate());
832 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
835 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
837 static const char * const selections[] = {
838 "atomnr 1 to 3 8 to 9",
842 setFlags(TestFlags() | efTestPositionAtoms
843 | efTestPositionMasses | efTestPositionCharges);
844 runTest(10, selections);
848 /********************************************************************
849 * Tests for selection syntactic constructs
852 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
854 static const char * const selections[] = {
857 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
858 runTest("simple.gro", selections);
862 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
864 static const char * const selections[] = {
865 "within 1 of [2, 1, 0]"
867 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
868 runTest("simple.gro", selections);
872 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
874 static const char * const selections[] = {
878 runTest("simple.gro", selections);
882 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
884 static const char * const selections[] = {
888 runTest("simple.gro", selections);
892 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
894 static const char * const selections[] = {
896 "resname ~ \"R[BD]\""
898 if (gmx::Regex::isSupported())
900 runTest("simple.gro", selections);
905 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
907 static const char * const selections[] = {
908 "atomnr 1 to 5 and atomnr 2 to 7",
909 "atomnr 1 to 5 or not atomnr 3 to 8",
910 "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
911 "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
912 "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
914 runTest(10, selections);
918 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
920 static const char * const selections[] = {
927 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
928 runTest("simple.gro", selections);
932 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
934 static const char * const selections[] = {
940 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
941 runTest("simple.gro", selections);
945 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
947 static const char * const selections[] = {
953 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
954 runTest("simple.gro", selections);
958 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
960 static const char * const selections[] = {
962 "resname RA and value <= 4",
963 "resname RA RB and x < 3 and value <= 4",
965 "resname RA and index < 3",
966 "resname RB and y < 3 and index < 6"
968 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
969 runTest("simple.gro", selections);
973 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
975 static const char * const selections[] = {
976 "foo = res_cog of resname RA",
979 "bar = cog of resname RA",
983 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
984 runTest("simple.gro", selections);
988 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
990 static const char * const selections[] = {
991 "constpos = [1.0, 2.5, 0.5]",
993 "within 2 of constpos"
995 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
996 | efTestPositionAtoms);
997 runTest("simple.gro", selections);
1001 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1003 static const char * const selections[] = {
1008 "x + constreal1 < constreal2"
1010 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1011 runTest("simple.gro", selections);
1015 /********************************************************************
1016 * Tests for complex boolean syntax
1019 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1021 static const char * const selections[] = {
1022 "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1023 "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1024 "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1025 "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1026 "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1028 runTest(10, selections);
1032 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1034 static const char * const selections[] = {
1035 "foo = atomnr 4 to 7 or x < 2",
1036 "atomnr 1 to 4 and foo",
1037 "atomnr 2 to 6 and y < 3 and foo",
1038 "atomnr 6 to 10 and not foo"
1040 runTest(10, selections);
1044 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1046 static const char * const selections[] = {
1047 "foo = atomnr 4 to 7",
1048 "bar = foo and x < 2",
1049 "bar2 = foo and y < 2",
1050 "atomnr 1 to 4 and bar",
1051 "atomnr 2 to 6 and y < 3 and bar2",
1052 "atomnr 6 to 10 and not foo"
1054 runTest(10, selections);
1058 /********************************************************************
1059 * Tests for complex subexpression cases
1061 * These tests use some knowledge of the implementation to trigger different
1062 * paths in the code.
1065 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1067 static const char * const selections[] = {
1068 "unused1 = atomnr 1 to 3",
1069 "foo = atomnr 4 to 7",
1070 "atomnr 1 to 6 and foo",
1071 "unused2 = atomnr 3 to 5"
1073 runTest(10, selections);
1077 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1079 static const char * const selections[] = {
1080 "foo = atomnr 4 to 7 and x < 2",
1081 "atomnr 1 to 5 and foo",
1082 "atomnr 3 to 7 and foo"
1084 runTest(10, selections);
1088 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1090 static const char * const selections[] = {
1091 "foo = atomnr 4 to 7 and x < 2",
1092 "atomnr 1 to 6 and foo",
1096 runTest(10, selections);