Merge branch 'release-4-6' into release-5-0
[alexxy/gromacs.git] / src / gromacs / selection / tests / selectioncollection.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests selection parsing and compilation.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include <gtest/gtest.h>
43
44 #include "gromacs/options/basicoptions.h"
45 #include "gromacs/options/options.h"
46 #include "gromacs/selection/indexutil.h"
47 #include "gromacs/selection/selectioncollection.h"
48 #include "gromacs/selection/selection.h"
49 #include "gromacs/utility/arrayref.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/flags.h"
52 #include "gromacs/utility/gmxregex.h"
53 #include "gromacs/utility/stringutil.h"
54
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testfilemanager.h"
58 #include "testutils/testoptions.h"
59
60 #include "toputils.h"
61
62 namespace
63 {
64
65 /********************************************************************
66  * Test fixture for selection testing
67  */
68
69 class SelectionCollectionTest : public ::testing::Test
70 {
71     public:
72         static int               s_debugLevel;
73
74         SelectionCollectionTest();
75         ~SelectionCollectionTest();
76
77         void setAtomCount(int natoms)
78         {
79             ASSERT_NO_THROW_GMX(sc_.setTopology(NULL, natoms));
80         }
81         void loadTopology(const char *filename);
82         void setTopology();
83         void loadIndexGroups(const char *filename);
84
85         gmx::test::TopologyManager  topManager_;
86         gmx::SelectionCollection    sc_;
87         gmx::SelectionList          sel_;
88         t_topology                 *top_;
89         t_trxframe                 *frame_;
90         gmx_ana_indexgrps_t        *grps_;
91 };
92
93 int SelectionCollectionTest::s_debugLevel = 0;
94
95 // \cond/\endcond do not seem to work here with Doxygen 1.8.5 parser.
96 #ifndef DOXYGEN
97 GMX_TEST_OPTIONS(SelectionCollectionTestOptions, options)
98 {
99     options->addOption(gmx::IntegerOption("seldebug")
100                            .store(&SelectionCollectionTest::s_debugLevel)
101                            .description("Set selection debug level"));
102 }
103 #endif
104
105 SelectionCollectionTest::SelectionCollectionTest()
106     : top_(NULL), frame_(NULL), grps_(NULL)
107 {
108     topManager_.requestFrame();
109     sc_.setDebugLevel(s_debugLevel);
110     sc_.setReferencePosType("atom");
111     sc_.setOutputPosType("atom");
112 }
113
114 SelectionCollectionTest::~SelectionCollectionTest()
115 {
116     if (grps_ != NULL)
117     {
118         gmx_ana_indexgrps_free(grps_);
119     }
120 }
121
122 void
123 SelectionCollectionTest::loadTopology(const char *filename)
124 {
125     topManager_.loadTopology(filename);
126     setTopology();
127 }
128
129 void
130 SelectionCollectionTest::setTopology()
131 {
132     top_   = topManager_.topology();
133     frame_ = topManager_.frame();
134
135     ASSERT_NO_THROW_GMX(sc_.setTopology(top_, -1));
136 }
137
138 void
139 SelectionCollectionTest::loadIndexGroups(const char *filename)
140 {
141     GMX_RELEASE_ASSERT(grps_ == NULL,
142                        "External groups can only be loaded once");
143     std::string fullpath =
144         gmx::test::TestFileManager::getInputFilePath(filename);
145     gmx_ana_indexgrps_init(&grps_, NULL, fullpath.c_str());
146     sc_.setIndexGroups(grps_);
147 }
148
149
150 /********************************************************************
151  * Test fixture for selection testing with reference data
152  */
153
154 class SelectionCollectionDataTest : public SelectionCollectionTest
155 {
156     public:
157         enum TestFlag
158         {
159             efTestEvaluation            = 1<<0,
160             efTestPositionAtoms         = 1<<1,
161             efTestPositionCoordinates   = 1<<2,
162             efTestPositionMapping       = 1<<3,
163             efTestPositionMasses        = 1<<4,
164             efTestPositionCharges       = 1<<5,
165             efTestSelectionNames        = 1<<6,
166             efDontTestCompiledAtoms     = 1<<8
167         };
168         typedef gmx::FlagsTemplate<TestFlag> TestFlags;
169
170         SelectionCollectionDataTest()
171             : checker_(data_.rootChecker()), count_(0), framenr_(0)
172         {
173         }
174
175         void setFlags(TestFlags flags) { flags_ = flags; }
176
177         void runParser(const gmx::ConstArrayRef<const char *> &selections);
178         void runCompiler();
179         void runEvaluate();
180         void runEvaluateFinal();
181
182         void runTest(int                                     natoms,
183                      const gmx::ConstArrayRef<const char *> &selections);
184         void runTest(const char                             *filename,
185                      const gmx::ConstArrayRef<const char *> &selections);
186
187     private:
188         static void checkSelection(gmx::test::TestReferenceChecker *checker,
189                                    const gmx::Selection &sel, TestFlags flags);
190
191         void checkCompiled();
192
193         gmx::test::TestReferenceData    data_;
194         gmx::test::TestReferenceChecker checker_;
195         size_t                          count_;
196         int                             framenr_;
197         TestFlags                       flags_;
198 };
199
200
201 void
202 SelectionCollectionDataTest::checkSelection(
203         gmx::test::TestReferenceChecker *checker,
204         const gmx::Selection &sel, TestFlags flags)
205 {
206     using gmx::test::TestReferenceChecker;
207
208     {
209         gmx::ConstArrayRef<int> atoms = sel.atomIndices();
210         checker->checkSequence(atoms.begin(), atoms.end(), "Atoms");
211     }
212     if (flags.test(efTestPositionAtoms)
213         || flags.test(efTestPositionCoordinates)
214         || flags.test(efTestPositionMapping)
215         || flags.test(efTestPositionMasses)
216         || flags.test(efTestPositionCharges))
217     {
218         TestReferenceChecker compound(
219                 checker->checkSequenceCompound("Positions", sel.posCount()));
220         for (int i = 0; i < sel.posCount(); ++i)
221         {
222             TestReferenceChecker          poscompound(compound.checkCompound("Position", NULL));
223             const gmx::SelectionPosition &p = sel.position(i);
224             if (flags.test(efTestPositionAtoms))
225             {
226                 gmx::ConstArrayRef<int> atoms = p.atomIndices();
227                 poscompound.checkSequence(atoms.begin(), atoms.end(), "Atoms");
228             }
229             if (flags.test(efTestPositionCoordinates))
230             {
231                 poscompound.checkVector(p.x(), "Coordinates");
232             }
233             if (flags.test(efTestPositionMapping))
234             {
235                 poscompound.checkInteger(p.refId(), "RefId");
236                 poscompound.checkInteger(p.mappedId(), "MappedId");
237             }
238             if (flags.test(efTestPositionMasses))
239             {
240                 poscompound.checkReal(p.mass(), "Mass");
241             }
242             if (flags.test(efTestPositionCharges))
243             {
244                 poscompound.checkReal(p.charge(), "Charge");
245             }
246         }
247     }
248 }
249
250
251 void
252 SelectionCollectionDataTest::runParser(
253         const gmx::ConstArrayRef<const char *> &selections)
254 {
255     using gmx::test::TestReferenceChecker;
256
257     TestReferenceChecker compound(checker_.checkCompound("ParsedSelections", "Parsed"));
258     size_t               varcount = 0;
259     count_ = 0;
260     for (size_t i = 0; i < selections.size(); ++i)
261     {
262         SCOPED_TRACE(std::string("Parsing selection \"")
263                      + selections[i] + "\"");
264         gmx::SelectionList result;
265         ASSERT_NO_THROW_GMX(result = sc_.parseFromString(selections[i]));
266         sel_.insert(sel_.end(), result.begin(), result.end());
267         if (sel_.size() == count_)
268         {
269             std::string          id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
270             TestReferenceChecker varcompound(
271                     compound.checkCompound("ParsedVariable", id.c_str()));
272             varcompound.checkString(selections[i], "Input");
273             ++varcount;
274         }
275         else
276         {
277             std::string          id = gmx::formatString("Selection%d", static_cast<int>(count_ + 1));
278             TestReferenceChecker selcompound(
279                     compound.checkCompound("ParsedSelection", id.c_str()));
280             selcompound.checkString(selections[i], "Input");
281             if (flags_.test(efTestSelectionNames))
282             {
283                 selcompound.checkString(sel_[count_].name(), "Name");
284             }
285             selcompound.checkString(sel_[count_].selectionText(), "Text");
286             selcompound.checkBoolean(sel_[count_].isDynamic(), "Dynamic");
287             ++count_;
288         }
289     }
290 }
291
292
293 void
294 SelectionCollectionDataTest::runCompiler()
295 {
296     ASSERT_NO_THROW_GMX(sc_.compile());
297     ASSERT_EQ(count_, sel_.size());
298     checkCompiled();
299 }
300
301
302 void
303 SelectionCollectionDataTest::checkCompiled()
304 {
305     using gmx::test::TestReferenceChecker;
306     const TestFlags      mask = ~TestFlags(efTestPositionCoordinates);
307
308     TestReferenceChecker compound(checker_.checkCompound("CompiledSelections", "Compiled"));
309     for (size_t i = 0; i < count_; ++i)
310     {
311         SCOPED_TRACE(std::string("Checking selection \"") +
312                      sel_[i].selectionText() + "\"");
313         std::string          id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
314         TestReferenceChecker selcompound(
315                 compound.checkCompound("Selection", id.c_str()));
316         if (flags_.test(efTestSelectionNames))
317         {
318             selcompound.checkString(sel_[i].name(), "Name");
319         }
320         if (!flags_.test(efDontTestCompiledAtoms))
321         {
322             checkSelection(&selcompound, sel_[i], flags_ & mask);
323         }
324     }
325 }
326
327
328 void
329 SelectionCollectionDataTest::runEvaluate()
330 {
331     using gmx::test::TestReferenceChecker;
332
333     ++framenr_;
334     ASSERT_NO_THROW_GMX(sc_.evaluate(frame_, NULL));
335     std::string          frame = gmx::formatString("Frame%d", framenr_);
336     TestReferenceChecker compound(
337             checker_.checkCompound("EvaluatedSelections", frame.c_str()));
338     for (size_t i = 0; i < count_; ++i)
339     {
340         SCOPED_TRACE(std::string("Checking selection \"") +
341                      sel_[i].selectionText() + "\"");
342         std::string          id = gmx::formatString("Selection%d", static_cast<int>(i + 1));
343         TestReferenceChecker selcompound(
344                 compound.checkCompound("Selection", id.c_str()));
345         checkSelection(&selcompound, sel_[i], flags_);
346     }
347 }
348
349
350 void
351 SelectionCollectionDataTest::runEvaluateFinal()
352 {
353     ASSERT_NO_THROW_GMX(sc_.evaluateFinal(framenr_));
354     checkCompiled();
355 }
356
357
358 void
359 SelectionCollectionDataTest::runTest(
360         int natoms, const gmx::ConstArrayRef<const char *> &selections)
361 {
362     ASSERT_NO_FATAL_FAILURE(runParser(selections));
363     ASSERT_NO_FATAL_FAILURE(setAtomCount(natoms));
364     ASSERT_NO_FATAL_FAILURE(runCompiler());
365 }
366
367
368 void
369 SelectionCollectionDataTest::runTest(
370         const char *filename, const gmx::ConstArrayRef<const char *> &selections)
371 {
372     ASSERT_NO_FATAL_FAILURE(runParser(selections));
373     ASSERT_NO_FATAL_FAILURE(loadTopology(filename));
374     ASSERT_NO_FATAL_FAILURE(runCompiler());
375     if (flags_.test(efTestEvaluation))
376     {
377         ASSERT_NO_FATAL_FAILURE(runEvaluate());
378         ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
379     }
380 }
381
382
383 /********************************************************************
384  * Tests for SelectionCollection functionality without reference data
385  */
386
387 TEST_F(SelectionCollectionTest, HandlesNoSelections)
388 {
389     EXPECT_FALSE(sc_.requiresTopology());
390     EXPECT_NO_THROW_GMX(sc_.compile());
391 }
392
393 TEST_F(SelectionCollectionTest, HandlesVelocityAndForceRequests)
394 {
395     ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromString("atomnr 1 to 10; none"));
396     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
397     ASSERT_EQ(2U, sel_.size());
398     ASSERT_NO_THROW_GMX(sel_[0].setEvaluateVelocities(true));
399     ASSERT_NO_THROW_GMX(sel_[1].setEvaluateVelocities(true));
400     ASSERT_NO_THROW_GMX(sel_[0].setEvaluateForces(true));
401     ASSERT_NO_THROW_GMX(sel_[1].setEvaluateForces(true));
402     ASSERT_NO_THROW_GMX(sc_.compile());
403     EXPECT_TRUE(sel_[0].hasVelocities());
404     EXPECT_TRUE(sel_[1].hasVelocities());
405     EXPECT_TRUE(sel_[0].hasForces());
406     EXPECT_TRUE(sel_[1].hasForces());
407 }
408
409 TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
410 {
411     ASSERT_NO_THROW_GMX(sel_ = sc_.parseFromFile(
412                                     gmx::test::TestFileManager::getInputFilePath("selfile.dat")));
413     // These should match the contents of selfile.dat
414     ASSERT_EQ(2U, sel_.size());
415     EXPECT_STREQ("resname RA RB", sel_[0].selectionText());
416     EXPECT_STREQ("resname RB RC", sel_[1].selectionText());
417 }
418
419 TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
420 {
421     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
422     EXPECT_THROW_GMX({
423                          sc_.parseFromString("resname ~ \"R[A\"");
424                          sc_.compile();
425                      }, gmx::InvalidInputError);
426 }
427
428 TEST_F(SelectionCollectionTest, HandlesUnsupportedRegularExpressions)
429 {
430     if (!gmx::Regex::isSupported())
431     {
432         ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
433         EXPECT_THROW_GMX({
434                              sc_.parseFromString("resname \"R[AD]\"");
435                              sc_.compile();
436                          }, gmx::InvalidInputError);
437     }
438 }
439
440 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
441 {
442     EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"),
443                      gmx::InvalidInputError);
444 }
445
446 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
447 {
448     EXPECT_THROW_GMX(sc_.parseFromString("within 1 of"),
449                      gmx::InvalidInputError);
450 }
451
452 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
453 {
454     EXPECT_THROW_GMX(sc_.parseFromString("within of atomnr 1"),
455                      gmx::InvalidInputError);
456 }
457
458 // TODO: Tests for more parser errors
459
460 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser1)
461 {
462     ASSERT_NO_THROW_GMX(sc_.setIndexGroups(NULL));
463     EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
464     EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
465 }
466
467 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser2)
468 {
469     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
470     EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
471     EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
472 }
473
474 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed1)
475 {
476     ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
477     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
478     EXPECT_THROW_GMX(sc_.setIndexGroups(NULL), gmx::InconsistentInputError);
479     EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
480 }
481
482 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed2)
483 {
484     ASSERT_NO_THROW_GMX(sc_.parseFromString("group 4; group \"foo\""));
485     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
486     EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
487     EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
488 }
489
490 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReference)
491 {
492     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
493     EXPECT_THROW_GMX(sc_.parseFromString("group \"GrpUnsorted\""),
494                      gmx::InconsistentInputError);
495     EXPECT_THROW_GMX(sc_.parseFromString("2"), gmx::InconsistentInputError);
496 }
497
498 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReferenceDelayed)
499 {
500     ASSERT_NO_THROW_GMX(sc_.parseFromString("group 2; group \"GrpUnsorted\""));
501     EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
502     EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
503 }
504
505 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
506 {
507     ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
508     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
509     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
510 }
511
512 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
513 {
514     ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
515     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
516     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
517 }
518
519 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
520 {
521     ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
522     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
523     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
524 }
525
526 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
527 {
528     ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
529     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
530     EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
531 }
532
533 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
534 {
535     ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
536     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
537     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
538 }
539
540 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
541 {
542     ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
543     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
544     ASSERT_NO_THROW_GMX(sc_.compile());
545     EXPECT_THROW_GMX(sc_.evaluate(frame_, NULL), gmx::InconsistentInputError);
546 }
547
548 // TODO: Tests for evaluation errors
549
550
551 /********************************************************************
552  * Tests for selection keywords
553  */
554
555 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
556 {
557     static const char * const selections[] = {
558         "all",
559         "none"
560     };
561     runTest(10, selections);
562 }
563
564 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
565 {
566     static const char * const selections[] = {
567         "atomnr 1 to 3 6 to 8",
568         "atomnr 4 2 5 to 7",
569         "atomnr <= 5"
570     };
571     runTest(10, selections);
572 }
573
574 TEST_F(SelectionCollectionDataTest, HandlesResnr)
575 {
576     static const char * const selections[] = {
577         "resnr 1 2 5",
578         "resid 4 to 3"
579     };
580     runTest("simple.gro", selections);
581 }
582
583 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
584 {
585     static const char * const selections[] = {
586         "resindex 1 4",
587         "residue 1 3"
588     };
589     runTest("simple.pdb", selections);
590 }
591
592 TEST_F(SelectionCollectionDataTest, HandlesMolIndex)
593 {
594     static const char * const selections[] = {
595         "molindex 1 4",
596         "molecule 2 3 5"
597     };
598     ASSERT_NO_FATAL_FAILURE(runParser(selections));
599     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
600     topManager_.initUniformMolecules(3);
601     ASSERT_NO_FATAL_FAILURE(runCompiler());
602 }
603
604 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
605 {
606     static const char * const selections[] = {
607         "name CB",
608         "atomname S1 S2"
609     };
610     runTest("simple.gro", selections);
611 }
612
613 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
614 {
615     static const char * const selections[] = {
616         "name HG21",
617         "name 1HG2",
618         "pdbname HG21 CB",
619         "pdbatomname 1HG2"
620     };
621     runTest("simple.pdb", selections);
622 }
623
624
625 TEST_F(SelectionCollectionDataTest, HandlesAtomtype)
626 {
627     static const char * const selections[] = {
628         "atomtype CA"
629     };
630     ASSERT_NO_FATAL_FAILURE(runParser(selections));
631     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
632     const char *const types[] = { "CA", "SA", "SB" };
633     topManager_.initAtomTypes(types);
634     ASSERT_NO_FATAL_FAILURE(runCompiler());
635 }
636
637 TEST_F(SelectionCollectionDataTest, HandlesChain)
638 {
639     static const char * const selections[] = {
640         "chain A",
641         "chain B"
642     };
643     runTest("simple.pdb", selections);
644 }
645
646 TEST_F(SelectionCollectionDataTest, HandlesMass)
647 {
648     static const char * const selections[] = {
649         "mass > 5"
650     };
651     ASSERT_NO_FATAL_FAILURE(runParser(selections));
652     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
653     for (int i = 0; i < top_->atoms.nr; ++i)
654     {
655         top_->atoms.atom[i].m = 1.0 + i;
656     }
657     ASSERT_NO_FATAL_FAILURE(runCompiler());
658 }
659
660 TEST_F(SelectionCollectionDataTest, HandlesCharge)
661 {
662     static const char * const selections[] = {
663         "charge < 0.5"
664     };
665     ASSERT_NO_FATAL_FAILURE(runParser(selections));
666     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
667     for (int i = 0; i < top_->atoms.nr; ++i)
668     {
669         top_->atoms.atom[i].q = i / 10.0;
670     }
671     ASSERT_NO_FATAL_FAILURE(runCompiler());
672 }
673
674 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
675 {
676     static const char * const selections[] = {
677         "altloc \" \"",
678         "altloc A"
679     };
680     runTest("simple.pdb", selections);
681 }
682
683 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
684 {
685     static const char * const selections[] = {
686         "insertcode \" \"",
687         "insertcode A"
688     };
689     runTest("simple.pdb", selections);
690 }
691
692 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
693 {
694     static const char * const selections[] = {
695         "occupancy 1",
696         "occupancy < .5"
697     };
698     runTest("simple.pdb", selections);
699 }
700
701 TEST_F(SelectionCollectionDataTest, HandlesBeta)
702 {
703     static const char * const selections[] = {
704         "beta 0",
705         "beta >= 0.3"
706     };
707     runTest("simple.pdb", selections);
708 }
709
710 TEST_F(SelectionCollectionDataTest, HandlesResname)
711 {
712     static const char * const selections[] = {
713         "resname RA",
714         "resname RB RC"
715     };
716     runTest("simple.gro", selections);
717 }
718
719 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
720 {
721     static const char * const selections[] = {
722         "x < 3",
723         "y >= 3",
724         "x {-1 to 2}"
725     };
726     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
727     runTest("simple.gro", selections);
728 }
729
730
731 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
732 {
733     static const char * const selections[] = {
734         "same residue as atomnr 1 4 12"
735     };
736     runTest("simple.gro", selections);
737 }
738
739
740 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
741 {
742     static const char * const selections[] = {
743         "same resname as atomnr 1 14"
744     };
745     runTest("simple.gro", selections);
746 }
747
748
749 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
750 {
751     static const char * const selections[] = {
752         "cog of resnr 1 3",
753         "res_cog of name CB and resnr 1 3",
754         "whole_res_cog of name CB and resnr 1 3",
755         "part_res_cog of x < 3",
756         "dyn_res_cog of x < 3"
757     };
758     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
759              | efTestPositionAtoms);
760     runTest("simple.gro", selections);
761 }
762
763
764 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
765 {
766     static const char * const selections[] = {
767         "distance from cog of resnr 1 < 2"
768     };
769     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
770     runTest("simple.gro", selections);
771 }
772
773
774 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
775 {
776     static const char * const selections[] = {
777         "mindistance from resnr 1 < 2"
778     };
779     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
780     runTest("simple.gro", selections);
781 }
782
783
784 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
785 {
786     static const char * const selections[] = {
787         "within 1 of resnr 2"
788     };
789     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
790     runTest("simple.gro", selections);
791 }
792
793
794 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
795 {
796     // Both of these should evaluate to empty on a correct implementation.
797     static const char * const selections[] = {
798         "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
799         "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
800     };
801     setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
802     runTest("sphere.gro", selections);
803 }
804
805
806 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
807 {
808     static const char * const selections[] = {
809         "all permute 3 1 2",
810         "res_cog of resnr 1 to 4 permute 2 1",
811         "name CB S1 and res_cog x < 3 permute 2 1"
812     };
813     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
814              | efTestPositionAtoms | efTestPositionMapping);
815     runTest("simple.gro", selections);
816 }
817
818
819 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
820 {
821     static const char * const selections[] = {
822         "name S2 plus name S1",
823         "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
824         "name S1 and y < 3 plus res_cog of x < 2.5"
825     };
826     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
827              | efTestPositionAtoms | efTestPositionMapping);
828     runTest("simple.gro", selections);
829 }
830
831
832 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
833 {
834     static const char * const selections[] = {
835         "name S2 merge name S1",
836         "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
837         "name S1 and x < 2.5 merge res_cog of x < 2.5"
838     };
839     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
840              | efTestPositionAtoms | efTestPositionMapping);
841     runTest("simple.gro", selections);
842 }
843
844
845 /********************************************************************
846  * Tests for generic selection evaluation
847  */
848
849 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
850 {
851     static const char * const selections[] = {
852         "name CB",
853         "y > 2",
854         "res_cog of y > 2"
855     };
856     setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms
857              | efTestPositionMasses | efTestPositionCharges);
858     ASSERT_NO_FATAL_FAILURE(runParser(selections));
859     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
860     for (int i = 0; i < top_->atoms.nr; ++i)
861     {
862         top_->atoms.atom[i].m =   1.0 + i / 100.0;
863         top_->atoms.atom[i].q = -(1.0 + i / 100.0);
864     }
865     ASSERT_NO_FATAL_FAILURE(runCompiler());
866     ASSERT_NO_FATAL_FAILURE(runEvaluate());
867     ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
868 }
869
870 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
871 {
872     static const char * const selections[] = {
873         "atomnr 1 to 3 8 to 9",
874         "y > 2",
875         "cog of (y > 2)"
876     };
877     setFlags(TestFlags() | efTestPositionAtoms
878              | efTestPositionMasses | efTestPositionCharges);
879     runTest(10, selections);
880 }
881
882
883 /********************************************************************
884  * Tests for selection syntactic constructs
885  */
886
887 TEST_F(SelectionCollectionDataTest, HandlesSelectionNames)
888 {
889     static const char * const selections[] = {
890         "\"GroupSelection\" group \"GrpA\"",
891         "\"DynamicSelection\" x < 5",
892         "y < 3"
893     };
894     setFlags(TestFlags() | efTestSelectionNames);
895     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
896     runTest(10, selections);
897 }
898
899 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelections)
900 {
901     static const char * const selections[] = {
902         "group \"GrpA\"",
903         "GrpB",
904         "1",
905         "group \"GrpB\" and resname RB"
906     };
907     setFlags(TestFlags() | efTestSelectionNames);
908     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
909     runTest("simple.gro", selections);
910 }
911
912 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelectionsDelayed)
913 {
914     static const char * const selections[] = {
915         "group \"GrpA\"",
916         "GrpB",
917         "1",
918         "group \"GrpB\" and resname RB"
919     };
920     setFlags(TestFlags() | efTestSelectionNames);
921     ASSERT_NO_FATAL_FAILURE(runParser(selections));
922     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
923     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
924     ASSERT_NO_FATAL_FAILURE(runCompiler());
925 }
926
927 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
928 {
929     static const char * const selections[] = {
930         "[1, -2, 3.5]"
931     };
932     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
933     runTest("simple.gro", selections);
934 }
935
936
937 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
938 {
939     static const char * const selections[] = {
940         "within 1 of [2, 1, 0]"
941     };
942     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
943     runTest("simple.gro", selections);
944 }
945
946
947 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
948 {
949     static const char * const selections[] = {
950         "name = S1 \"C?\"",
951         "name ? S1 \"C?\""
952     };
953     runTest("simple.gro", selections);
954 }
955
956
957 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
958 {
959     static const char * const selections[] = {
960         "name \"S?\"",
961         "name ? \"S?\""
962     };
963     runTest("simple.gro", selections);
964 }
965
966
967 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
968 {
969     static const char * const selections[] = {
970         "resname \"R[BD]\"",
971         "resname ~ \"R[BD]\""
972     };
973     if (gmx::Regex::isSupported())
974     {
975         runTest("simple.gro", selections);
976     }
977 }
978
979
980 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
981 {
982     static const char * const selections[] = {
983         "atomnr 1 to 5 and atomnr 2 to 7",
984         "atomnr 1 to 5 or not atomnr 3 to 8",
985         "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
986         "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
987         "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
988     };
989     runTest(10, selections);
990 }
991
992
993 TEST_F(SelectionCollectionDataTest, HandlesDynamicAtomValuedParameters)
994 {
995     static const char * const selections[] = {
996         "same residue as (atomnr 3 5 13 or y > 5)",
997         "(resnr 1 3 5 or x > 10) and same residue as (atomnr 3 5 13 or z > 5)"
998     };
999     setFlags(TestFlags() | efTestEvaluation);
1000     runTest("simple.gro", selections);
1001 }
1002
1003
1004 TEST_F(SelectionCollectionDataTest, HandlesEmptySelectionWithUnevaluatedExpressions)
1005 {
1006     static const char * const selections[] = {
1007         "none and x > 2",
1008         "none and same resname as resnr 2"
1009     };
1010     runTest("simple.gro", selections);
1011 }
1012
1013
1014 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
1015 {
1016     static const char * const selections[] = {
1017         "x > 2",
1018         "2 < x",
1019         "y > resnr",
1020         "resnr < 2.5",
1021         "2.5 > resnr"
1022     };
1023     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1024     runTest("simple.gro", selections);
1025 }
1026
1027
1028 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
1029 {
1030     static const char * const selections[] = {
1031         "x+1 > 3",
1032         "(y-1)^2 <= 1",
1033         "x+--1 > 3",
1034         "-x+-1 < -3"
1035     };
1036     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1037     runTest("simple.gro", selections);
1038 }
1039
1040
1041 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
1042 {
1043     static const char * const selections[] = {
1044         "value = x + y",
1045         "value <= 4",
1046         "index = resnr",
1047         "index < 3"
1048     };
1049     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1050     runTest("simple.gro", selections);
1051 }
1052
1053
1054 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
1055 {
1056     static const char * const selections[] = {
1057         "value = x + y",
1058         "resname RA and value <= 4",
1059         "resname RA RB and x < 3 and value <= 4",
1060         "index = atomnr",
1061         "resname RA and index < 3",
1062         "resname RB and y < 3 and index < 6"
1063     };
1064     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1065     runTest("simple.gro", selections);
1066 }
1067
1068
1069 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
1070 {
1071     static const char * const selections[] = {
1072         "foo = res_cog of resname RA",
1073         "foo",
1074         "within 1 of foo",
1075         "bar = cog of resname RA",
1076         "bar",
1077         "within 1 of bar"
1078     };
1079     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1080     runTest("simple.gro", selections);
1081 }
1082
1083
1084 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
1085 {
1086     static const char * const selections[] = {
1087         "constpos = [1.0, 2.5, 0.5]",
1088         "constpos",
1089         "within 2 of constpos"
1090     };
1091     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
1092              | efTestPositionAtoms);
1093     runTest("simple.gro", selections);
1094 }
1095
1096
1097 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1098 {
1099     static const char * const selections[] = {
1100         "constint = 4",
1101         "constreal1 = 0.5",
1102         "constreal2 = 2.7",
1103         "resnr < constint",
1104         "x + constreal1 < constreal2"
1105     };
1106     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1107     runTest("simple.gro", selections);
1108 }
1109
1110
1111 /********************************************************************
1112  * Tests for complex boolean syntax
1113  */
1114
1115 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1116 {
1117     static const char * const selections[] = {
1118         "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1119         "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1120         "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1121         "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1122         "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1123     };
1124     runTest(10, selections);
1125 }
1126
1127
1128 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1129 {
1130     static const char * const selections[] = {
1131         "foo = atomnr 4 to 7 or x < 2",
1132         "atomnr 1 to 4 and foo",
1133         "atomnr 2 to 6 and y < 3 and foo",
1134         "atomnr 6 to 10 and not foo"
1135     };
1136     runTest(10, selections);
1137 }
1138
1139
1140 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1141 {
1142     static const char * const selections[] = {
1143         "foo = atomnr 4 to 7",
1144         "bar = foo and x < 2",
1145         "bar2 = foo and y < 2",
1146         "atomnr 1 to 4 and bar",
1147         "atomnr 2 to 6 and y < 3 and bar2",
1148         "atomnr 6 to 10 and not foo"
1149     };
1150     runTest(10, selections);
1151 }
1152
1153
1154 /********************************************************************
1155  * Tests for complex subexpression cases
1156  *
1157  * These tests use some knowledge of the implementation to trigger different
1158  * paths in the code.
1159  */
1160
1161 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1162 {
1163     static const char * const selections[] = {
1164         "unused1 = atomnr 1 to 3",
1165         "foo = atomnr 4 to 7",
1166         "atomnr 1 to 6 and foo",
1167         "unused2 = atomnr 3 to 5"
1168     };
1169     runTest(10, selections);
1170 }
1171
1172
1173 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1174 {
1175     static const char * const selections[] = {
1176         "foo = atomnr 4 to 7 and x < 2",
1177         "atomnr 1 to 5 and foo",
1178         "atomnr 3 to 7 and foo"
1179     };
1180     runTest(10, selections);
1181 }
1182
1183
1184 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1185 {
1186     static const char * const selections[] = {
1187         "foo = atomnr 4 to 7 and x < 2",
1188         "atomnr 1 to 6 and foo",
1189         "within 1 of foo",
1190         "foo"
1191     };
1192     runTest(10, selections);
1193 }
1194
1195
1196 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups2)
1197 {
1198     static const char * const selections[] = {
1199         "foo = atomnr 1 to 8 and x < 10",
1200         "atomnr 1 to 5 and y < 10 and foo",
1201         "foo"
1202     };
1203     setFlags(TestFlags() | efTestEvaluation);
1204     runTest("simple.gro", selections);
1205 }
1206
1207
1208 } // namespace