2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Tests selection parsing and compilation.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_selection
45 #include "gromacs/selection/selectioncollection.h"
47 #include <gtest/gtest.h>
49 #include "gromacs/options/basicoptions.h"
50 #include "gromacs/options/ioptionscontainer.h"
51 #include "gromacs/selection/indexutil.h"
52 #include "gromacs/selection/selection.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/trajectory/trajectoryframe.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/flags.h"
58 #include "gromacs/utility/stringutil.h"
60 #include "testutils/interactivetest.h"
61 #include "testutils/refdata.h"
62 #include "testutils/testasserts.h"
63 #include "testutils/testfilemanager.h"
64 #include "testutils/testoptions.h"
71 /********************************************************************
72 * Test fixture for selection testing
75 class SelectionCollectionTest : public ::testing::Test
78 static int s_debugLevel;
80 SelectionCollectionTest();
81 ~SelectionCollectionTest() override;
83 void setAtomCount(int natoms) { ASSERT_NO_THROW_GMX(sc_.setTopology(nullptr, natoms)); }
84 void loadTopology(const char* filename);
86 void loadIndexGroups(const char* filename);
88 gmx::test::TopologyManager topManager_;
89 gmx::SelectionCollection sc_;
90 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() : grps_(nullptr)
108 topManager_.requestFrame();
109 sc_.setDebugLevel(s_debugLevel);
110 sc_.setReferencePosType("atom");
111 sc_.setOutputPosType("atom");
114 SelectionCollectionTest::~SelectionCollectionTest()
116 if (grps_ != nullptr)
118 gmx_ana_indexgrps_free(grps_);
122 void SelectionCollectionTest::loadTopology(const char* filename)
124 topManager_.loadTopology(filename);
128 void SelectionCollectionTest::setTopology()
130 ASSERT_NO_THROW_GMX(sc_.setTopology(topManager_.topology(), -1));
133 void SelectionCollectionTest::loadIndexGroups(const char* filename)
135 GMX_RELEASE_ASSERT(grps_ == nullptr, "External groups can only be loaded once");
136 std::string fullpath = gmx::test::TestFileManager::getInputFilePath(filename);
137 gmx_ana_indexgrps_init(&grps_, nullptr, fullpath.c_str());
138 sc_.setIndexGroups(grps_);
142 /********************************************************************
143 * Test fixture for interactive SelectionCollection tests
146 class SelectionCollectionInteractiveTest : public SelectionCollectionTest
149 SelectionCollectionInteractiveTest() : helper_(data_.rootChecker()) {}
151 void runTest(int count, bool bInteractive, const gmx::ArrayRef<const char* const>& input);
153 gmx::test::TestReferenceData data_;
154 gmx::test::InteractiveTestHelper helper_;
157 void SelectionCollectionInteractiveTest::runTest(int count,
159 const gmx::ArrayRef<const char* const>& inputLines)
161 helper_.setInputLines(inputLines);
162 // TODO: Check something about the returned selections as well.
163 ASSERT_NO_THROW_GMX(sc_.parseInteractive(count,
164 &helper_.inputStream(),
165 bInteractive ? &helper_.outputStream() : nullptr,
166 "for test context"));
167 helper_.checkSession();
171 /********************************************************************
172 * Test fixture for selection testing with reference data
175 class SelectionCollectionDataTest : public SelectionCollectionTest
178 enum TestFlag : uint64_t
180 efTestEvaluation = 1 << 0,
181 efTestPositionAtoms = 1 << 1,
182 efTestPositionCoordinates = 1 << 2,
183 efTestPositionMapping = 1 << 3,
184 efTestPositionMasses = 1 << 4,
185 efTestPositionCharges = 1 << 5,
186 efTestSelectionNames = 1 << 6,
187 efDontTestCompiledAtoms = 1 << 8
189 typedef gmx::FlagsTemplate<TestFlag> TestFlags;
191 SelectionCollectionDataTest() : checker_(data_.rootChecker()), count_(0), framenr_(0) {}
193 void setFlags(TestFlags flags) { flags_ = flags; }
195 void runParser(const gmx::ArrayRef<const char* const>& selections);
198 void runEvaluateFinal();
200 void runTest(int natoms, const gmx::ArrayRef<const char* const>& selections);
201 void runTest(const char* filename, const gmx::ArrayRef<const char* const>& selections);
204 static void checkSelection(gmx::test::TestReferenceChecker* checker,
205 const gmx::Selection& sel,
208 void checkCompiled();
210 gmx::test::TestReferenceData data_;
211 gmx::test::TestReferenceChecker checker_;
218 void SelectionCollectionDataTest::checkSelection(gmx::test::TestReferenceChecker* checker,
219 const gmx::Selection& sel,
222 using gmx::test::TestReferenceChecker;
225 gmx::ArrayRef<const int> atoms = sel.atomIndices();
226 checker->checkSequence(atoms.begin(), atoms.end(), "Atoms");
228 if (flags.test(efTestPositionAtoms) || flags.test(efTestPositionCoordinates)
229 || flags.test(efTestPositionMapping) || flags.test(efTestPositionMasses)
230 || flags.test(efTestPositionCharges))
232 TestReferenceChecker compound(checker->checkSequenceCompound("Positions", sel.posCount()));
233 for (int i = 0; i < sel.posCount(); ++i)
235 TestReferenceChecker poscompound(compound.checkCompound("Position", nullptr));
236 const gmx::SelectionPosition& p = sel.position(i);
237 if (flags.test(efTestPositionAtoms))
239 gmx::ArrayRef<const int> atoms = p.atomIndices();
240 poscompound.checkSequence(atoms.begin(), atoms.end(), "Atoms");
242 if (flags.test(efTestPositionCoordinates))
244 poscompound.checkVector(p.x(), "Coordinates");
246 if (flags.test(efTestPositionMapping))
248 poscompound.checkInteger(p.refId(), "RefId");
249 poscompound.checkInteger(p.mappedId(), "MappedId");
251 if (flags.test(efTestPositionMasses))
253 poscompound.checkReal(p.mass(), "Mass");
255 if (flags.test(efTestPositionCharges))
257 poscompound.checkReal(p.charge(), "Charge");
264 void SelectionCollectionDataTest::runParser(const gmx::ArrayRef<const char* const>& selections)
266 using gmx::test::TestReferenceChecker;
268 TestReferenceChecker compound(checker_.checkCompound("ParsedSelections", "Parsed"));
271 for (gmx::index i = 0; i < selections.ssize(); ++i)
273 SCOPED_TRACE(std::string("Parsing selection \"") + selections[i] + "\"");
274 gmx::SelectionList result;
275 ASSERT_NO_THROW_GMX(result = sc_.parseFromString(selections[i]));
276 sel_.insert(sel_.end(), result.begin(), result.end());
277 if (sel_.size() == count_)
279 std::string id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
280 TestReferenceChecker varcompound(compound.checkCompound("ParsedVariable", id.c_str()));
281 varcompound.checkString(selections[i], "Input");
286 std::string id = gmx::formatString("Selection%d", static_cast<int>(count_ + 1));
287 TestReferenceChecker selcompound(compound.checkCompound("ParsedSelection", id.c_str()));
288 selcompound.checkString(selections[i], "Input");
289 if (flags_.test(efTestSelectionNames))
291 selcompound.checkString(sel_[count_].name(), "Name");
293 selcompound.checkString(sel_[count_].selectionText(), "Text");
294 selcompound.checkBoolean(sel_[count_].isDynamic(), "Dynamic");
301 void SelectionCollectionDataTest::runCompiler()
303 ASSERT_NO_THROW_GMX(sc_.compile());
304 ASSERT_EQ(count_, sel_.size());
309 void SelectionCollectionDataTest::checkCompiled()
311 using gmx::test::TestReferenceChecker;
312 const TestFlags mask = ~TestFlags(efTestPositionCoordinates);
314 TestReferenceChecker compound(checker_.checkCompound("CompiledSelections", "Compiled"));
315 for (size_t i = 0; i < count_; ++i)
317 SCOPED_TRACE(std::string("Checking selection \"") + sel_[i].selectionText() + "\"");
318 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
319 TestReferenceChecker selcompound(compound.checkCompound("Selection", id.c_str()));
320 if (flags_.test(efTestSelectionNames))
322 selcompound.checkString(sel_[i].name(), "Name");
324 if (!flags_.test(efDontTestCompiledAtoms))
326 checkSelection(&selcompound, sel_[i], flags_ & mask);
332 void SelectionCollectionDataTest::runEvaluate()
334 using gmx::test::TestReferenceChecker;
337 ASSERT_NO_THROW_GMX(sc_.evaluate(topManager_.frame(), nullptr));
338 std::string frame = gmx::formatString("Frame%d", framenr_);
339 TestReferenceChecker compound(checker_.checkCompound("EvaluatedSelections", frame.c_str()));
340 for (size_t i = 0; i < count_; ++i)
342 SCOPED_TRACE(std::string("Checking selection \"") + sel_[i].selectionText() + "\"");
343 std::string id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
344 TestReferenceChecker selcompound(compound.checkCompound("Selection", id.c_str()));
345 checkSelection(&selcompound, sel_[i], flags_);
350 void SelectionCollectionDataTest::runEvaluateFinal()
352 ASSERT_NO_THROW_GMX(sc_.evaluateFinal(framenr_));
357 void SelectionCollectionDataTest::runTest(int natoms, const gmx::ArrayRef<const char* const>& selections)
359 ASSERT_NO_FATAL_FAILURE(runParser(selections));
360 ASSERT_NO_FATAL_FAILURE(setAtomCount(natoms));
361 ASSERT_NO_FATAL_FAILURE(runCompiler());
365 void SelectionCollectionDataTest::runTest(const char* filename,
366 const gmx::ArrayRef<const char* const>& selections)
368 ASSERT_NO_FATAL_FAILURE(runParser(selections));
369 ASSERT_NO_FATAL_FAILURE(loadTopology(filename));
370 ASSERT_NO_FATAL_FAILURE(runCompiler());
371 if (flags_.test(efTestEvaluation))
373 ASSERT_NO_FATAL_FAILURE(runEvaluate());
374 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
379 /********************************************************************
380 * Tests for SelectionCollection functionality without reference data
383 TEST_F(SelectionCollectionTest, HandlesNoSelections)
385 EXPECT_FALSE(sc_.requiredTopologyProperties().hasAny());
386 EXPECT_NO_THROW_GMX(sc_.compile());
387 EXPECT_FALSE(sc_.requiredTopologyProperties().hasAny());
390 TEST_F(SelectionCollectionTest, HandlesNoSelectionsWithDefaultPositionType)
392 EXPECT_NO_THROW_GMX(sc_.setOutputPosType("res_com"));
393 EXPECT_TRUE(sc_.requiredTopologyProperties().needsTopology);
394 EXPECT_TRUE(sc_.requiredTopologyProperties().needsMasses);
395 EXPECT_NO_THROW_GMX(sc_.setOutputPosType("res_cog"));
396 EXPECT_TRUE(sc_.requiredTopologyProperties().needsTopology);
397 EXPECT_FALSE(sc_.requiredTopologyProperties().needsMasses);
398 ASSERT_NO_THROW_GMX(sc_.parseFromString("atom of atomnr 1 to 10"));
399 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
400 ASSERT_NO_THROW_GMX(sc_.compile());
401 EXPECT_FALSE(sc_.requiredTopologyProperties().hasAny());
404 TEST_F(SelectionCollectionTest, HandlesVelocityAndForceRequests)
406 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr 1 to 10; none"));
407 EXPECT_FALSE(sc_.requiredTopologyProperties().hasAny());
408 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
409 ASSERT_EQ(2U, sel_.size());
410 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateVelocities(true));
411 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateVelocities(true));
412 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
413 ASSERT_NO_THROW_GMX(sel_[1].setEvaluateForces(true));
414 EXPECT_FALSE(sc_.requiredTopologyProperties().hasAny());
415 ASSERT_NO_THROW_GMX(sc_.compile());
416 EXPECT_FALSE(sc_.requiredTopologyProperties().hasAny());
417 EXPECT_TRUE(sel_[0].hasVelocities());
418 EXPECT_TRUE(sel_[1].hasVelocities());
419 EXPECT_TRUE(sel_[0].hasForces());
420 EXPECT_TRUE(sel_[1].hasForces());
423 TEST_F(SelectionCollectionTest, HandlesForceRequestForCenterOfGeometry)
425 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("res_cog of atomnr 1 to 10"));
426 EXPECT_TRUE(sc_.requiredTopologyProperties().needsTopology);
427 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
428 ASSERT_EQ(1U, sel_.size());
429 ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
430 // In principle, the code could know here that the masses are required, but
431 // currently it only knows this after compilation.
432 ASSERT_NO_THROW_GMX(sc_.compile());
433 EXPECT_TRUE(sc_.requiredTopologyProperties().needsMasses);
434 EXPECT_TRUE(sel_[0].hasForces());
437 TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
440 sel_ = sc_.parseFromFile(gmx::test::TestFileManager::getInputFilePath("selfile.dat")));
441 // These should match the contents of selfile.dat
442 ASSERT_EQ(2U, sel_.size());
443 EXPECT_STREQ("resname RA RB", sel_[0].selectionText());
444 EXPECT_STREQ("resname RB RC", sel_[1].selectionText());
447 TEST_F(SelectionCollectionTest, HandlesAtypicalWhitespace)
449 ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr\n1\r\nto\t10;\vatomnr 3\f to 14\r"));
450 ASSERT_EQ(2U, sel_.size());
451 EXPECT_STREQ("atomnr 1 to 10", sel_[0].selectionText());
452 // TODO: Get rid of the trailing whitespace.
453 EXPECT_STREQ("atomnr 3 to 14 ", sel_[1].selectionText());
456 TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
458 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
461 sc_.parseFromString("resname ~ \"R[A\"");
464 gmx::InvalidInputError);
467 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
469 EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"), gmx::InvalidInputError);
472 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
474 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of"), gmx::InvalidInputError);
477 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
479 EXPECT_THROW_GMX(sc_.parseFromString("within of atomnr 1"), gmx::InvalidInputError);
482 // TODO: Tests for more parser errors
484 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser1)
486 ASSERT_NO_THROW_GMX(sc_.setIndexGroups(nullptr));
487 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
488 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
491 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser2)
493 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
494 EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
495 EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
498 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed1)
500 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
501 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
502 EXPECT_THROW_GMX(sc_.setIndexGroups(nullptr), gmx::InconsistentInputError);
503 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
506 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed2)
508 ASSERT_NO_THROW_GMX(sc_.parseFromString("group 4; group \"foo\""));
509 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
510 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
511 EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
514 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReference)
516 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
517 EXPECT_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group \"GrpUnsorted\""),
518 gmx::InconsistentInputError);
519 EXPECT_THROW_GMX(sc_.parseFromString("group 2 or atomnr 2 to 5"), gmx::InconsistentInputError);
520 EXPECT_THROW_GMX(sc_.parseFromString("within 1 of group 2"), gmx::InconsistentInputError);
523 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReferenceDelayed)
525 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group \"GrpUnsorted\""));
526 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 1 to 3 and group 2"));
527 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
528 // TODO: Add a separate check in the selection compiler for a safer API
529 // (makes sense in the future if the compiler needs the information for
530 // other purposes as well).
531 // EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
534 TEST_F(SelectionCollectionTest, HandlesOutOfRangeAtomIndexInGroup)
536 ASSERT_NO_THROW_GMX(sc_.setTopology(nullptr, 5));
537 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
538 EXPECT_THROW_GMX(sc_.parseFromString("group \"GrpB\""), gmx::InconsistentInputError);
541 TEST_F(SelectionCollectionTest, HandlesOutOfRangeAtomIndexInGroupDelayed)
543 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
544 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"GrpB\""));
545 EXPECT_THROW_GMX(sc_.setTopology(nullptr, 5), gmx::InconsistentInputError);
548 TEST_F(SelectionCollectionTest, HandlesOutOfRangeAtomIndexInGroupDelayed2)
550 ASSERT_NO_THROW_GMX(sc_.setTopology(nullptr, 5));
551 ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"GrpB\""));
552 EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
555 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
557 ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
558 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
559 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
562 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
564 ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
565 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
566 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
569 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
571 ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
572 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
573 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
576 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
578 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
579 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
580 EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
583 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
585 ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
586 ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
587 EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
590 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
592 ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
593 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
594 ASSERT_NO_THROW_GMX(sc_.compile());
595 EXPECT_THROW_GMX(sc_.evaluate(topManager_.frame(), nullptr), gmx::InconsistentInputError);
598 TEST_F(SelectionCollectionTest, HandlesFramesWithTooSmallAtomSubsets)
600 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 3 to 10"));
601 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
602 ASSERT_NO_THROW_GMX(sc_.compile());
603 topManager_.frame()->natoms = 8;
604 EXPECT_THROW_GMX(sc_.evaluate(topManager_.frame(), nullptr), gmx::InconsistentInputError);
607 TEST_F(SelectionCollectionTest, HandlesFramesWithTooSmallAtomSubsets2)
609 const int index[] = { 1, 2, 3, 9 };
610 ASSERT_NO_THROW_GMX(sc_.parseFromString("atomnr 3 4 7 10"));
611 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
612 ASSERT_NO_THROW_GMX(sc_.compile());
613 topManager_.initFrameIndices(index);
614 EXPECT_THROW_GMX(sc_.evaluate(topManager_.frame(), nullptr), gmx::InconsistentInputError);
617 TEST_F(SelectionCollectionTest, HandlesFramesWithTooSmallAtomSubsets3)
619 const int index[] = { 0, 1, 2, 3, 4, 5, 6, 9, 10, 11 };
620 // Evaluating the positions will require atoms 1-3, 7-12.
621 ASSERT_NO_THROW_GMX(sc_.parseFromString("whole_res_cog of atomnr 2 7 11"));
622 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
623 ASSERT_NO_THROW_GMX(sc_.compile());
624 topManager_.initFrameIndices(index);
625 EXPECT_THROW_GMX(sc_.evaluate(topManager_.frame(), nullptr), gmx::InconsistentInputError);
628 TEST_F(SelectionCollectionTest, HandlesFramesWithTooSmallAtomSubsets4)
630 ASSERT_NO_THROW_GMX(sc_.parseFromString("mindistance from atomnr 1 to 5 < 2"));
631 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
632 ASSERT_NO_THROW_GMX(sc_.compile());
633 topManager_.frame()->natoms = 10;
634 EXPECT_THROW_GMX(sc_.evaluate(topManager_.frame(), nullptr), gmx::InconsistentInputError);
637 // TODO: Tests for more evaluation errors
639 /********************************************************************
640 * Tests for interactive selection input
643 TEST_F(SelectionCollectionInteractiveTest, HandlesBasicInput)
645 const char* const input[] = { "foo = resname RA", "resname RB", "\"Name\" resname RC" };
646 runTest(-1, true, input);
649 TEST_F(SelectionCollectionInteractiveTest, HandlesContinuation)
651 const char* const input[] = { "resname RB and \\", "resname RC" };
652 runTest(-1, true, input);
655 TEST_F(SelectionCollectionInteractiveTest, HandlesSingleSelectionInput)
657 const char* const input[] = { "foo = resname RA", "resname RA" };
658 runTest(1, true, input);
661 TEST_F(SelectionCollectionInteractiveTest, HandlesTwoSelectionInput)
663 const char* const input[] = { "resname RA", "resname RB" };
664 runTest(2, true, input);
667 TEST_F(SelectionCollectionInteractiveTest, HandlesStatusWithGroups)
669 const char* const input[] = { "resname RA", "" };
670 loadIndexGroups("simple.ndx");
671 runTest(-1, true, input);
674 TEST_F(SelectionCollectionInteractiveTest, HandlesStatusWithExistingSelections)
676 const char* const input[] = { "", "bar = resname RC", "resname RA", "" };
677 ASSERT_NO_THROW_GMX(sc_.parseFromString("foo = resname RA"));
678 ASSERT_NO_THROW_GMX(sc_.parseFromString("resname RB"));
679 runTest(-1, true, input);
682 TEST_F(SelectionCollectionInteractiveTest, HandlesSingleSelectionInputStatus)
684 const char* const input[] = { "foo = resname RA", "", "resname RB" };
685 runTest(1, true, input);
688 TEST_F(SelectionCollectionInteractiveTest, HandlesTwoSelectionInputStatus)
690 const char* const input[] = { "\"Sel\" resname RA", "", "resname RB" };
691 runTest(2, true, input);
694 TEST_F(SelectionCollectionInteractiveTest, HandlesMultiSelectionInputStatus)
696 const char* const input[] = { "\"Sel\" resname RA", "\"Sel2\" resname RB", "" };
697 runTest(-1, true, input);
700 TEST_F(SelectionCollectionInteractiveTest, HandlesNoFinalNewline)
702 // TODO: There is an extra prompt printed after the input is finished; it
703 // would be cleaner not to have it, but it's only a cosmetic issue.
704 const char* const input[] = { "resname RA" };
705 helper_.setLastNewline(false);
706 runTest(-1, true, input);
709 TEST_F(SelectionCollectionInteractiveTest, HandlesEmptySelections)
711 const char* const input[] = { "resname RA;", "; resname RB;;", " ", ";" };
712 runTest(-1, true, input);
715 TEST_F(SelectionCollectionInteractiveTest, HandlesMultipleSelectionsOnLine)
717 const char* const input[] = { "resname RA; resname RB and \\", "resname RC" };
718 runTest(2, true, input);
721 TEST_F(SelectionCollectionInteractiveTest, HandlesNoninteractiveInput)
723 const char* const input[] = { "foo = resname RA", "resname RB", "\"Name\" resname RC" };
724 runTest(-1, false, input);
727 TEST_F(SelectionCollectionInteractiveTest, HandlesSingleSelectionInputNoninteractively)
729 const char* const input[] = { "foo = resname RA", "resname RA" };
730 runTest(1, false, input);
734 /********************************************************************
735 * Tests for selection keywords
738 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
740 static const char* const selections[] = { "all", "none" };
741 runTest(10, selections);
744 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
746 static const char* const selections[] = { "atomnr 1 to 3 6 to 8",
749 runTest(10, selections);
752 TEST_F(SelectionCollectionDataTest, HandlesResnr)
754 static const char* const selections[] = { "resnr 1 2 5", "resid 4 to 3" };
755 runTest("simple.gro", selections);
758 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
760 static const char* const selections[] = { "resindex 1 4", "residue 1 3" };
761 runTest("simple.pdb", selections);
764 TEST_F(SelectionCollectionDataTest, HandlesMolIndex)
766 static const char* const selections[] = { "molindex 1 4", "molecule 2 3 5" };
767 ASSERT_NO_FATAL_FAILURE(runParser(selections));
768 ASSERT_NO_FATAL_FAILURE(topManager_.loadTopology("simple.gro"));
769 topManager_.initUniformMolecules(3);
770 ASSERT_NO_FATAL_FAILURE(setTopology());
771 ASSERT_NO_FATAL_FAILURE(runCompiler());
774 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
776 static const char* const selections[] = { "name CB", "atomname S1 S2" };
777 runTest("simple.gro", selections);
780 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
782 static const char* const selections[] = {
783 "name HG21", "name 1HG2", "pdbname HG21 CB", "pdbatomname 1HG2"
785 runTest("simple.pdb", selections);
789 TEST_F(SelectionCollectionDataTest, HandlesAtomtype)
791 static const char* const selections[] = { "atomtype CA" };
792 ASSERT_NO_FATAL_FAILURE(runParser(selections));
793 ASSERT_NO_FATAL_FAILURE(topManager_.loadTopology("simple.gro"));
794 const char* const types[] = { "CA", "SA", "SB" };
795 topManager_.initAtomTypes(types);
796 ASSERT_NO_FATAL_FAILURE(setTopology());
797 ASSERT_NO_FATAL_FAILURE(runCompiler());
800 TEST_F(SelectionCollectionDataTest, HandlesChain)
802 static const char* const selections[] = { "chain A", "chain B" };
803 runTest("simple.pdb", selections);
806 TEST_F(SelectionCollectionDataTest, HandlesMass)
808 static const char* const selections[] = { "mass > 5" };
809 ASSERT_NO_FATAL_FAILURE(runParser(selections));
810 EXPECT_TRUE(sc_.requiredTopologyProperties().needsMasses);
811 ASSERT_NO_FATAL_FAILURE(topManager_.loadTopology("simple.gro"));
812 t_atoms& atoms = topManager_.atoms();
813 for (int i = 0; i < atoms.nr; ++i)
815 atoms.atom[i].m = 1.0 + i;
817 atoms.haveMass = TRUE;
818 ASSERT_NO_FATAL_FAILURE(setTopology());
819 ASSERT_NO_FATAL_FAILURE(runCompiler());
822 TEST_F(SelectionCollectionDataTest, HandlesCharge)
824 static const char* const selections[] = { "charge < 0.5" };
825 ASSERT_NO_FATAL_FAILURE(runParser(selections));
826 ASSERT_NO_FATAL_FAILURE(topManager_.loadTopology("simple.gro"));
827 t_atoms& atoms = topManager_.atoms();
828 for (int i = 0; i < atoms.nr; ++i)
830 atoms.atom[i].q = i / 10.0;
832 // Ensure exact representation of 0.5 is used, so that the test is
834 atoms.atom[5].q = 0.5;
835 atoms.haveCharge = TRUE;
836 ASSERT_NO_FATAL_FAILURE(setTopology());
837 ASSERT_NO_FATAL_FAILURE(runCompiler());
840 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
842 static const char* const selections[] = { "altloc \" \"", "altloc A" };
843 runTest("simple.pdb", selections);
846 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
848 static const char* const selections[] = { "insertcode \" \"", "insertcode A" };
849 runTest("simple.pdb", selections);
852 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
854 static const char* const selections[] = { "occupancy 1", "occupancy < .5" };
855 runTest("simple.pdb", selections);
858 TEST_F(SelectionCollectionDataTest, HandlesBeta)
860 static const char* const selections[] = { "beta 0", "beta >= 0.3" };
861 runTest("simple.pdb", selections);
864 TEST_F(SelectionCollectionDataTest, HandlesResname)
866 static const char* const selections[] = { "resname RA", "resname RB RC" };
867 runTest("simple.gro", selections);
870 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
872 static const char* const selections[] = { "x < 3", "y >= 3", "x {-1 to 2}" };
873 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
874 runTest("simple.gro", selections);
878 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
880 static const char* const selections[] = { "same residue as atomnr 1 4 12" };
881 runTest("simple.gro", selections);
885 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
887 static const char* const selections[] = { "same resname as atomnr 1 14" };
888 runTest("simple.gro", selections);
892 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
894 static const char* const selections[] = { "cog of resnr 1 3",
895 "res_cog of name CB and resnr 1 3",
896 "whole_res_cog of name CB and resnr 1 3",
897 "part_res_cog of x < 3",
898 "dyn_res_cog of x < 3" };
899 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionAtoms);
900 runTest("simple.gro", selections);
904 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
906 static const char* const selections[] = { "distance from cog of resnr 1 < 2" };
907 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
908 runTest("simple.gro", selections);
912 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
914 static const char* const selections[] = { "mindistance from resnr 1 < 2" };
915 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
916 runTest("simple.gro", selections);
920 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
922 static const char* const selections[] = { "within 1 of resnr 2" };
923 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
924 runTest("simple.gro", selections);
928 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
930 // Both of these should evaluate to empty on a correct implementation.
931 static const char* const selections[] = {
932 "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
933 "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
935 setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
936 runTest("sphere.gro", selections);
940 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
942 static const char* const selections[] = { "all permute 3 1 2",
943 "res_cog of resnr 1 to 4 permute 2 1",
944 "name CB S1 and res_cog x < 3 permute 2 1" };
945 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionAtoms
946 | efTestPositionMapping);
947 runTest("simple.gro", selections);
951 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
953 static const char* const selections[] = {
954 "name S2 plus name S1",
955 "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
956 "name S1 and y < 3 plus res_cog of x < 2.5"
958 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionAtoms
959 | efTestPositionMapping);
960 runTest("simple.gro", selections);
964 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
966 static const char* const selections[] = {
967 "name S2 merge name S1",
968 "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
969 "name S1 and x < 2.5 merge res_cog of x < 2.5"
971 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionAtoms
972 | efTestPositionMapping);
973 runTest("simple.gro", selections);
977 /********************************************************************
978 * Tests for generic selection evaluation
981 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
983 static const char* const selections[] = { "name CB", "y > 2", "res_cog of y > 2" };
984 setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms | efTestPositionMasses
985 | efTestPositionCharges);
986 ASSERT_NO_FATAL_FAILURE(runParser(selections));
987 ASSERT_NO_FATAL_FAILURE(topManager_.loadTopology("simple.gro"));
988 t_atoms& atoms = topManager_.atoms();
989 for (int i = 0; i < atoms.nr; ++i)
991 atoms.atom[i].m = 1.0 + i / 100.0;
992 atoms.atom[i].q = -(1.0 + i / 100.0);
994 atoms.haveMass = TRUE;
995 atoms.haveCharge = TRUE;
996 ASSERT_NO_FATAL_FAILURE(setTopology());
997 ASSERT_NO_FATAL_FAILURE(runCompiler());
998 ASSERT_NO_FATAL_FAILURE(runEvaluate());
999 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
1002 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
1004 static const char* const selections[] = { "atomnr 1 to 3 8 to 9", "y > 2", "cog of (y > 2)" };
1005 setFlags(TestFlags() | efTestPositionAtoms | efTestPositionMasses | efTestPositionCharges);
1006 runTest(10, selections);
1009 TEST_F(SelectionCollectionDataTest, HandlesFramesWithAtomSubsets)
1011 const int index[] = { 0, 1, 2, 3, 4, 5, 9, 10, 11 };
1012 const char* const selections[] = { "resnr 1 4",
1013 "atomnr 1 2 5 11 and y > 2",
1014 "res_cog of atomnr 2 5 11" };
1015 setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms);
1016 ASSERT_NO_FATAL_FAILURE(runParser(selections));
1017 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
1018 ASSERT_NO_FATAL_FAILURE(runCompiler());
1019 topManager_.initFrameIndices(index);
1020 ASSERT_NO_FATAL_FAILURE(runEvaluate());
1021 ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
1025 /********************************************************************
1026 * Tests for selection syntactic constructs
1029 TEST_F(SelectionCollectionDataTest, HandlesSelectionNames)
1031 static const char* const selections[] = { "\"GroupSelection\" group \"GrpA\"",
1032 "\"DynamicSelection\" x < 5",
1034 setFlags(TestFlags() | efTestSelectionNames);
1035 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
1036 runTest(10, selections);
1039 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelections)
1041 static const char* const selections[] = { "group \"GrpA\"",
1044 // These test that the name of the group is not too
1045 // eagerly promoted to the name of the selection.
1046 "group \"GrpB\" and resname RB",
1047 "group \"GrpA\" permute 5 3 2 1 4",
1048 "group \"GrpA\" plus group \"GrpB\"",
1049 "res_cog of group \"GrpA\"" };
1050 setFlags(TestFlags() | efTestSelectionNames);
1051 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
1052 runTest("simple.gro", selections);
1055 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelectionsDelayed)
1057 static const char* const selections[] = {
1058 "group \"GrpA\"", "GrpB", "1", "group \"GrpB\" and resname RB"
1060 setFlags(TestFlags() | efTestSelectionNames);
1061 ASSERT_NO_FATAL_FAILURE(runParser(selections));
1062 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
1063 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
1064 ASSERT_NO_FATAL_FAILURE(runCompiler());
1067 TEST_F(SelectionCollectionDataTest, HandlesUnsortedIndexGroupsInSelections)
1069 static const char* const selections[] = { "foo = group \"GrpUnsorted\"",
1070 "group \"GrpUnsorted\"",
1073 "res_cog of group \"GrpUnsorted\"",
1074 "group \"GrpUnsorted\" permute 2 1",
1076 setFlags(TestFlags() | efTestPositionAtoms | efTestPositionMapping | efTestSelectionNames);
1077 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
1078 runTest("simple.gro", selections);
1081 TEST_F(SelectionCollectionDataTest, HandlesUnsortedIndexGroupsInSelectionsDelayed)
1083 static const char* const selections[] = { "foo = group \"GrpUnsorted\"",
1084 "group \"GrpUnsorted\"",
1087 "res_cog of group \"GrpUnsorted\"",
1088 "group \"GrpUnsorted\" permute 2 1",
1090 ASSERT_NO_FATAL_FAILURE(runParser(selections));
1091 ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
1092 ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
1093 ASSERT_NO_FATAL_FAILURE(runCompiler());
1097 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
1099 static const char* const selections[] = { "[1, -2, 3.5]" };
1100 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionMapping);
1101 runTest("simple.gro", selections);
1105 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionsWithModifiers)
1107 static const char* const selections[] = { "[0, 0, 0] plus [0, 1, 0]" };
1108 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionMapping);
1109 runTest("simple.gro", selections);
1113 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
1115 static const char* const selections[] = { "within 1 of [2, 1, 0]" };
1116 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1117 runTest("simple.gro", selections);
1121 TEST_F(SelectionCollectionDataTest, HandlesOverlappingIntegerRanges)
1123 static const char* const selections[] = { "atomnr 2 to 4 5 to 8", "atomnr 2 to 5 4 to 7" };
1124 ASSERT_NO_FATAL_FAILURE(runTest(10, selections));
1128 TEST_F(SelectionCollectionDataTest, HandlesOverlappingRealRanges)
1130 static const char* const selections[] = { "charge {-0.35 to -0.05 0.25 to 0.75}",
1131 "charge {0.05 to -0.3 -0.05 to 0.55}" };
1132 ASSERT_NO_FATAL_FAILURE(runParser(selections));
1133 ASSERT_NO_FATAL_FAILURE(topManager_.loadTopology("simple.gro"));
1134 t_atoms& atoms = topManager_.atoms();
1135 for (int i = 0; i < atoms.nr; ++i)
1137 atoms.atom[i].q = i / 10.0 - 0.5;
1139 atoms.haveCharge = TRUE;
1140 ASSERT_NO_FATAL_FAILURE(setTopology());
1141 ASSERT_NO_FATAL_FAILURE(runCompiler());
1145 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
1147 static const char* const selections[] = { "name = S1 \"C?\"", "name ? S1 \"C?\"" };
1148 runTest("simple.gro", selections);
1152 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
1154 static const char* const selections[] = { "name \"S?\"", "name ? \"S?\"" };
1155 runTest("simple.gro", selections);
1159 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
1161 static const char* const selections[] = { "resname \"R[BD]\"", "resname ~ \"R[BD]\"" };
1162 runTest("simple.gro", selections);
1166 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
1168 static const char* const selections[] = {
1169 "atomnr 1 to 5 and atomnr 2 to 7",
1170 "atomnr 1 to 5 or not atomnr 3 to 8",
1171 "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
1172 "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
1173 "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
1175 runTest(10, selections);
1179 TEST_F(SelectionCollectionDataTest, HandlesDynamicAtomValuedParameters)
1181 static const char* const selections[] = {
1182 "same residue as (atomnr 3 5 13 or y > 5)",
1183 "(resnr 1 3 5 or x > 10) and same residue as (atomnr 3 5 13 or z > 5)"
1185 setFlags(TestFlags() | efTestEvaluation);
1186 runTest("simple.gro", selections);
1190 TEST_F(SelectionCollectionDataTest, HandlesEmptySelectionWithUnevaluatedExpressions)
1192 static const char* const selections[] = { "none and x > 2", "none and same resname as resnr 2" };
1193 runTest("simple.gro", selections);
1197 TEST_F(SelectionCollectionDataTest, HandlesEmptyReferenceForSame)
1199 static const char* const selections[] = { "same residue as none", "same resname as none" };
1200 runTest("simple.gro", selections);
1204 TEST_F(SelectionCollectionDataTest, HandlesPositionModifiersForKeywords)
1206 static const char* const selections[] = { "res_cog x > 2", "name CB and res_cog y > 2.5" };
1207 setFlags(TestFlags() | efTestEvaluation);
1208 runTest("simple.gro", selections);
1212 TEST_F(SelectionCollectionDataTest, HandlesPositionModifiersForMethods)
1214 static const char* const selections[] = { "res_cog distance from cog of resnr 1 < 2",
1215 "res_cog within 2 of cog of resnr 1" };
1216 setFlags(TestFlags() | efTestEvaluation);
1217 runTest("simple.gro", selections);
1221 TEST_F(SelectionCollectionDataTest, HandlesKeywordOfPositions)
1223 static const char* const selections[] = { "x < y of cog of resnr 2" };
1224 setFlags(TestFlags() | efTestEvaluation);
1225 runTest("simple.gro", selections);
1228 TEST_F(SelectionCollectionDataTest, HandlesKeywordOfPositionsInArithmetic)
1230 static const char* const selections[] = { "x - y of cog of resnr 2 < 0" };
1231 setFlags(TestFlags() | efTestEvaluation);
1232 runTest("simple.gro", selections);
1236 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
1238 static const char* const selections[] = {
1239 "x > 2", "2 < x", "y > resnr", "resnr < 2.5", "2.5 > resnr"
1241 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1242 runTest("simple.gro", selections);
1246 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
1248 static const char* const selections[] = { "x+1 > 3", "(y-1)^2 <= 1", "x+--1 > 3", "-x+-1 < -3" };
1249 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1250 runTest("simple.gro", selections);
1254 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
1256 static const char* const selections[] = {
1257 "value = x + y", "value <= 4", "index = resnr", "index < 3"
1259 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1260 runTest("simple.gro", selections);
1264 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
1266 static const char* const selections[] = {
1267 "value = x + y", "resname RA and value <= 4", "resname RA RB and x < 3 and value <= 4",
1268 "index = atomnr", "resname RA and index < 3", "resname RB and y < 3 and index < 6"
1270 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1271 runTest("simple.gro", selections);
1275 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
1277 static const char* const selections[] = {
1278 "foo = res_cog of resname RA", "foo", "within 1 of foo",
1279 "bar = cog of resname RA", "bar", "within 1 of bar"
1281 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1282 runTest("simple.gro", selections);
1286 TEST_F(SelectionCollectionDataTest, HandlesPositionVariableInModifier)
1288 static const char* const selections[] = { "foo = cog of resnr 1",
1289 "cog of resnr 2 plus foo",
1290 "cog of resnr 3 plus foo" };
1291 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1292 runTest("simple.gro", selections);
1296 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
1298 static const char* const selections[] = { "constpos = [1.0, 2.5, 0.5]",
1300 "within 2 of constpos" };
1301 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates | efTestPositionAtoms);
1302 runTest("simple.gro", selections);
1306 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1308 static const char* const selections[] = { "constint = 4",
1312 "x + constreal1 < constreal2" };
1313 setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1314 runTest("simple.gro", selections);
1318 /********************************************************************
1319 * Tests for complex boolean syntax
1322 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1324 static const char* const selections[] = {
1325 "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1326 "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1327 "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1328 "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1329 "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1331 runTest(10, selections);
1335 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1337 static const char* const selections[] = { "foo = atomnr 4 to 7 or x < 2",
1338 "atomnr 1 to 4 and foo",
1339 "atomnr 2 to 6 and y < 3 and foo",
1340 "atomnr 6 to 10 and not foo" };
1341 runTest(10, selections);
1345 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1347 static const char* const selections[] = { "foo = atomnr 4 to 7",
1348 "bar = foo and x < 2",
1349 "bar2 = foo and y < 2",
1350 "atomnr 1 to 4 and bar",
1351 "atomnr 2 to 6 and y < 3 and bar2",
1352 "atomnr 6 to 10 and not foo" };
1353 runTest(10, selections);
1357 /********************************************************************
1358 * Tests for complex subexpression cases
1360 * These tests use some knowledge of the implementation to trigger different
1361 * paths in the code.
1364 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1366 static const char* const selections[] = { "unused1 = atomnr 1 to 3",
1367 "foo = atomnr 4 to 7",
1368 "atomnr 1 to 6 and foo",
1369 "unused2 = atomnr 3 to 5" };
1370 runTest(10, selections);
1374 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1376 static const char* const selections[] = { "foo = atomnr 4 to 7 and x < 2",
1377 "atomnr 1 to 5 and foo",
1378 "atomnr 3 to 7 and foo" };
1379 runTest(10, selections);
1383 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1385 static const char* const selections[] = {
1386 "foo = atomnr 4 to 7 and x < 2", "atomnr 1 to 6 and foo", "within 1 of foo", "foo"
1388 runTest(10, selections);
1392 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups2)
1394 static const char* const selections[] = { "foo = atomnr 1 to 8 and x < 10",
1395 "atomnr 1 to 5 and y < 10 and foo",
1397 setFlags(TestFlags() | efTestEvaluation);
1398 runTest("simple.gro", selections);