Merge release-4-6 into master
[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 TEST_F(SelectionCollectionTest, HandlesHelpKeywordInInvalidContext)
459 {
460     EXPECT_THROW_GMX(sc_.parseFromString("resname help"),
461                      gmx::InvalidInputError);
462 }
463
464 // TODO: Tests for more parser errors
465
466 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser1)
467 {
468     ASSERT_NO_THROW_GMX(sc_.setIndexGroups(NULL));
469     EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
470     EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
471 }
472
473 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceParser2)
474 {
475     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
476     EXPECT_THROW_GMX(sc_.parseFromString("group \"foo\""), gmx::InconsistentInputError);
477     EXPECT_THROW_GMX(sc_.parseFromString("4"), gmx::InconsistentInputError);
478 }
479
480 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed1)
481 {
482     ASSERT_NO_THROW_GMX(sc_.parseFromString("group \"foo\""));
483     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
484     EXPECT_THROW_GMX(sc_.setIndexGroups(NULL), gmx::InconsistentInputError);
485     EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
486 }
487
488 TEST_F(SelectionCollectionTest, HandlesUnknownGroupReferenceDelayed2)
489 {
490     ASSERT_NO_THROW_GMX(sc_.parseFromString("group 4; group \"foo\""));
491     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
492     EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
493     EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
494 }
495
496 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReference)
497 {
498     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
499     EXPECT_THROW_GMX(sc_.parseFromString("group \"GrpUnsorted\""),
500                      gmx::InconsistentInputError);
501     EXPECT_THROW_GMX(sc_.parseFromString("2"), gmx::InconsistentInputError);
502 }
503
504 TEST_F(SelectionCollectionTest, HandlesUnsortedGroupReferenceDelayed)
505 {
506     ASSERT_NO_THROW_GMX(sc_.parseFromString("group 2; group \"GrpUnsorted\""));
507     EXPECT_THROW_GMX(loadIndexGroups("simple.ndx"), gmx::InconsistentInputError);
508     EXPECT_THROW_GMX(sc_.compile(), gmx::APIError);
509 }
510
511 TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
512 {
513     ASSERT_NO_THROW_GMX(sc_.parseFromString("molindex 1 to 5"));
514     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
515     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
516 }
517
518 TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
519 {
520     ASSERT_NO_THROW_GMX(sc_.parseFromString("type CA"));
521     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
522     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
523 }
524
525 TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
526 {
527     ASSERT_NO_THROW_GMX(sc_.parseFromString("altloc A"));
528     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
529     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
530 }
531
532 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
533 {
534     ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 1 1"));
535     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
536     EXPECT_THROW_GMX(sc_.compile(), gmx::InvalidInputError);
537 }
538
539 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
540 {
541     ASSERT_NO_THROW_GMX(sc_.parseFromString("all permute 3 2 1"));
542     ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
543     EXPECT_THROW_GMX(sc_.compile(), gmx::InconsistentInputError);
544 }
545
546 TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
547 {
548     ASSERT_NO_THROW_GMX(sc_.parseFromString("x < 1.5 permute 3 2 1"));
549     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
550     ASSERT_NO_THROW_GMX(sc_.compile());
551     EXPECT_THROW_GMX(sc_.evaluate(frame_, NULL), gmx::InconsistentInputError);
552 }
553
554 // TODO: Tests for evaluation errors
555
556
557 /********************************************************************
558  * Tests for selection keywords
559  */
560
561 TEST_F(SelectionCollectionDataTest, HandlesAllNone)
562 {
563     static const char * const selections[] = {
564         "all",
565         "none"
566     };
567     runTest(10, selections);
568 }
569
570 TEST_F(SelectionCollectionDataTest, HandlesAtomnr)
571 {
572     static const char * const selections[] = {
573         "atomnr 1 to 3 6 to 8",
574         "atomnr 4 2 5 to 7",
575         "atomnr <= 5"
576     };
577     runTest(10, selections);
578 }
579
580 TEST_F(SelectionCollectionDataTest, HandlesResnr)
581 {
582     static const char * const selections[] = {
583         "resnr 1 2 5",
584         "resid 4 to 3"
585     };
586     runTest("simple.gro", selections);
587 }
588
589 TEST_F(SelectionCollectionDataTest, HandlesResIndex)
590 {
591     static const char * const selections[] = {
592         "resindex 1 4",
593         "residue 1 3"
594     };
595     runTest("simple.pdb", selections);
596 }
597
598 TEST_F(SelectionCollectionDataTest, HandlesMolIndex)
599 {
600     static const char * const selections[] = {
601         "molindex 1 4",
602         "molecule 2 3 5"
603     };
604     ASSERT_NO_FATAL_FAILURE(runParser(selections));
605     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
606     topManager_.initUniformMolecules(3);
607     ASSERT_NO_FATAL_FAILURE(runCompiler());
608 }
609
610 TEST_F(SelectionCollectionDataTest, HandlesAtomname)
611 {
612     static const char * const selections[] = {
613         "name CB",
614         "atomname S1 S2"
615     };
616     runTest("simple.gro", selections);
617 }
618
619 TEST_F(SelectionCollectionDataTest, HandlesPdbAtomname)
620 {
621     static const char * const selections[] = {
622         "name HG21",
623         "name 1HG2",
624         "pdbname HG21 CB",
625         "pdbatomname 1HG2"
626     };
627     runTest("simple.pdb", selections);
628 }
629
630
631 TEST_F(SelectionCollectionDataTest, HandlesAtomtype)
632 {
633     static const char * const selections[] = {
634         "atomtype CA"
635     };
636     ASSERT_NO_FATAL_FAILURE(runParser(selections));
637     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
638     const char *const types[] = { "CA", "SA", "SB" };
639     topManager_.initAtomTypes(types);
640     ASSERT_NO_FATAL_FAILURE(runCompiler());
641 }
642
643 TEST_F(SelectionCollectionDataTest, HandlesChain)
644 {
645     static const char * const selections[] = {
646         "chain A",
647         "chain B"
648     };
649     runTest("simple.pdb", selections);
650 }
651
652 TEST_F(SelectionCollectionDataTest, HandlesMass)
653 {
654     static const char * const selections[] = {
655         "mass > 5"
656     };
657     ASSERT_NO_FATAL_FAILURE(runParser(selections));
658     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
659     for (int i = 0; i < top_->atoms.nr; ++i)
660     {
661         top_->atoms.atom[i].m = 1.0 + i;
662     }
663     ASSERT_NO_FATAL_FAILURE(runCompiler());
664 }
665
666 TEST_F(SelectionCollectionDataTest, HandlesCharge)
667 {
668     static const char * const selections[] = {
669         "charge < 0.5"
670     };
671     ASSERT_NO_FATAL_FAILURE(runParser(selections));
672     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
673     for (int i = 0; i < top_->atoms.nr; ++i)
674     {
675         top_->atoms.atom[i].q = i / 10.0;
676     }
677     ASSERT_NO_FATAL_FAILURE(runCompiler());
678 }
679
680 TEST_F(SelectionCollectionDataTest, HandlesAltLoc)
681 {
682     static const char * const selections[] = {
683         "altloc \" \"",
684         "altloc A"
685     };
686     runTest("simple.pdb", selections);
687 }
688
689 TEST_F(SelectionCollectionDataTest, HandlesInsertCode)
690 {
691     static const char * const selections[] = {
692         "insertcode \" \"",
693         "insertcode A"
694     };
695     runTest("simple.pdb", selections);
696 }
697
698 TEST_F(SelectionCollectionDataTest, HandlesOccupancy)
699 {
700     static const char * const selections[] = {
701         "occupancy 1",
702         "occupancy < .5"
703     };
704     runTest("simple.pdb", selections);
705 }
706
707 TEST_F(SelectionCollectionDataTest, HandlesBeta)
708 {
709     static const char * const selections[] = {
710         "beta 0",
711         "beta >= 0.3"
712     };
713     runTest("simple.pdb", selections);
714 }
715
716 TEST_F(SelectionCollectionDataTest, HandlesResname)
717 {
718     static const char * const selections[] = {
719         "resname RA",
720         "resname RB RC"
721     };
722     runTest("simple.gro", selections);
723 }
724
725 TEST_F(SelectionCollectionDataTest, HandlesCoordinateKeywords)
726 {
727     static const char * const selections[] = {
728         "x < 3",
729         "y >= 3",
730         "x {-1 to 2}"
731     };
732     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
733     runTest("simple.gro", selections);
734 }
735
736
737 TEST_F(SelectionCollectionDataTest, HandlesSameResidue)
738 {
739     static const char * const selections[] = {
740         "same residue as atomnr 1 4 12"
741     };
742     runTest("simple.gro", selections);
743 }
744
745
746 TEST_F(SelectionCollectionDataTest, HandlesSameResidueName)
747 {
748     static const char * const selections[] = {
749         "same resname as atomnr 1 14"
750     };
751     runTest("simple.gro", selections);
752 }
753
754
755 TEST_F(SelectionCollectionDataTest, HandlesPositionKeywords)
756 {
757     static const char * const selections[] = {
758         "cog of resnr 1 3",
759         "res_cog of name CB and resnr 1 3",
760         "whole_res_cog of name CB and resnr 1 3",
761         "part_res_cog of x < 3",
762         "dyn_res_cog of x < 3"
763     };
764     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
765              | efTestPositionAtoms);
766     runTest("simple.gro", selections);
767 }
768
769
770 TEST_F(SelectionCollectionDataTest, HandlesDistanceKeyword)
771 {
772     static const char * const selections[] = {
773         "distance from cog of resnr 1 < 2"
774     };
775     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
776     runTest("simple.gro", selections);
777 }
778
779
780 TEST_F(SelectionCollectionDataTest, HandlesMinDistanceKeyword)
781 {
782     static const char * const selections[] = {
783         "mindistance from resnr 1 < 2"
784     };
785     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
786     runTest("simple.gro", selections);
787 }
788
789
790 TEST_F(SelectionCollectionDataTest, HandlesWithinKeyword)
791 {
792     static const char * const selections[] = {
793         "within 1 of resnr 2"
794     };
795     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
796     runTest("simple.gro", selections);
797 }
798
799
800 TEST_F(SelectionCollectionDataTest, HandlesInSolidAngleKeyword)
801 {
802     // Both of these should evaluate to empty on a correct implementation.
803     static const char * const selections[] = {
804         "resname TP and not insolidangle center cog of resname C span resname R cutoff 20",
805         "resname TN and insolidangle center cog of resname C span resname R cutoff 20"
806     };
807     setFlags(TestFlags() | efDontTestCompiledAtoms | efTestEvaluation);
808     runTest("sphere.gro", selections);
809 }
810
811
812 TEST_F(SelectionCollectionDataTest, HandlesPermuteModifier)
813 {
814     static const char * const selections[] = {
815         "all permute 3 1 2",
816         "res_cog of resnr 1 to 4 permute 2 1",
817         "name CB S1 and res_cog x < 3 permute 2 1"
818     };
819     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
820              | efTestPositionAtoms | efTestPositionMapping);
821     runTest("simple.gro", selections);
822 }
823
824
825 TEST_F(SelectionCollectionDataTest, HandlesPlusModifier)
826 {
827     static const char * const selections[] = {
828         "name S2 plus name S1",
829         "res_cog of resnr 2 plus res_cog of resnr 1 plus res_cog of resnr 3",
830         "name S1 and y < 3 plus res_cog of x < 2.5"
831     };
832     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
833              | efTestPositionAtoms | efTestPositionMapping);
834     runTest("simple.gro", selections);
835 }
836
837
838 TEST_F(SelectionCollectionDataTest, HandlesMergeModifier)
839 {
840     static const char * const selections[] = {
841         "name S2 merge name S1",
842         "resnr 1 2 and name S2 merge resnr 1 2 and name S1 merge res_cog of resnr 1 2",
843         "name S1 and x < 2.5 merge res_cog of x < 2.5"
844     };
845     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
846              | efTestPositionAtoms | efTestPositionMapping);
847     runTest("simple.gro", selections);
848 }
849
850
851 /********************************************************************
852  * Tests for generic selection evaluation
853  */
854
855 TEST_F(SelectionCollectionDataTest, ComputesMassesAndCharges)
856 {
857     static const char * const selections[] = {
858         "name CB",
859         "y > 2",
860         "res_cog of y > 2"
861     };
862     setFlags(TestFlags() | efTestEvaluation | efTestPositionAtoms
863              | efTestPositionMasses | efTestPositionCharges);
864     ASSERT_NO_FATAL_FAILURE(runParser(selections));
865     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
866     for (int i = 0; i < top_->atoms.nr; ++i)
867     {
868         top_->atoms.atom[i].m =   1.0 + i / 100.0;
869         top_->atoms.atom[i].q = -(1.0 + i / 100.0);
870     }
871     ASSERT_NO_FATAL_FAILURE(runCompiler());
872     ASSERT_NO_FATAL_FAILURE(runEvaluate());
873     ASSERT_NO_FATAL_FAILURE(runEvaluateFinal());
874 }
875
876 TEST_F(SelectionCollectionDataTest, ComputesMassesAndChargesWithoutTopology)
877 {
878     static const char * const selections[] = {
879         "atomnr 1 to 3 8 to 9",
880         "y > 2",
881         "cog of (y > 2)"
882     };
883     setFlags(TestFlags() | efTestPositionAtoms
884              | efTestPositionMasses | efTestPositionCharges);
885     runTest(10, selections);
886 }
887
888
889 /********************************************************************
890  * Tests for selection syntactic constructs
891  */
892
893 TEST_F(SelectionCollectionDataTest, HandlesSelectionNames)
894 {
895     static const char * const selections[] = {
896         "\"GroupSelection\" group \"GrpA\"",
897         "\"DynamicSelection\" x < 5",
898         "y < 3"
899     };
900     setFlags(TestFlags() | efTestSelectionNames);
901     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
902     runTest(10, selections);
903 }
904
905 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelections)
906 {
907     static const char * const selections[] = {
908         "group \"GrpA\"",
909         "GrpB",
910         "1",
911         "group \"GrpB\" and resname RB"
912     };
913     setFlags(TestFlags() | efTestSelectionNames);
914     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
915     runTest("simple.gro", selections);
916 }
917
918 TEST_F(SelectionCollectionDataTest, HandlesIndexGroupsInSelectionsDelayed)
919 {
920     static const char * const selections[] = {
921         "group \"GrpA\"",
922         "GrpB",
923         "1",
924         "group \"GrpB\" and resname RB"
925     };
926     setFlags(TestFlags() | efTestSelectionNames);
927     ASSERT_NO_FATAL_FAILURE(runParser(selections));
928     ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
929     ASSERT_NO_THROW_GMX(loadIndexGroups("simple.ndx"));
930     ASSERT_NO_FATAL_FAILURE(runCompiler());
931 }
932
933 TEST_F(SelectionCollectionDataTest, HandlesConstantPositions)
934 {
935     static const char * const selections[] = {
936         "[1, -2, 3.5]"
937     };
938     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
939     runTest("simple.gro", selections);
940 }
941
942
943 TEST_F(SelectionCollectionDataTest, HandlesWithinConstantPositions)
944 {
945     static const char * const selections[] = {
946         "within 1 of [2, 1, 0]"
947     };
948     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
949     runTest("simple.gro", selections);
950 }
951
952
953 TEST_F(SelectionCollectionDataTest, HandlesForcedStringMatchingMode)
954 {
955     static const char * const selections[] = {
956         "name = S1 \"C?\"",
957         "name ? S1 \"C?\""
958     };
959     runTest("simple.gro", selections);
960 }
961
962
963 TEST_F(SelectionCollectionDataTest, HandlesWildcardMatching)
964 {
965     static const char * const selections[] = {
966         "name \"S?\"",
967         "name ? \"S?\""
968     };
969     runTest("simple.gro", selections);
970 }
971
972
973 TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
974 {
975     static const char * const selections[] = {
976         "resname \"R[BD]\"",
977         "resname ~ \"R[BD]\""
978     };
979     if (gmx::Regex::isSupported())
980     {
981         runTest("simple.gro", selections);
982     }
983 }
984
985
986 TEST_F(SelectionCollectionDataTest, HandlesBasicBoolean)
987 {
988     static const char * const selections[] = {
989         "atomnr 1 to 5 and atomnr 2 to 7",
990         "atomnr 1 to 5 or not atomnr 3 to 8",
991         "not not atomnr 1 to 5 and atomnr 2 to 6 and not not atomnr 3 to 7",
992         "atomnr 1 to 5 and (atomnr 2 to 7 and atomnr 3 to 6)",
993         "x < 5 and atomnr 1 to 5 and y < 3 and atomnr 2 to 4"
994     };
995     runTest(10, selections);
996 }
997
998
999 TEST_F(SelectionCollectionDataTest, HandlesDynamicAtomValuedParameters)
1000 {
1001     static const char * const selections[] = {
1002         "same residue as (atomnr 3 5 13 or y > 5)",
1003         "(resnr 1 3 5 or x > 10) and same residue as (atomnr 3 5 13 or z > 5)"
1004     };
1005     setFlags(TestFlags() | efTestEvaluation);
1006     runTest("simple.gro", selections);
1007 }
1008
1009
1010 TEST_F(SelectionCollectionDataTest, HandlesEmptySelectionWithUnevaluatedExpressions)
1011 {
1012     static const char * const selections[] = {
1013         "none and x > 2",
1014         "none and same resname as resnr 2"
1015     };
1016     runTest("simple.gro", selections);
1017 }
1018
1019
1020 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
1021 {
1022     static const char * const selections[] = {
1023         "x > 2",
1024         "2 < x",
1025         "y > resnr",
1026         "resnr < 2.5",
1027         "2.5 > resnr"
1028     };
1029     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1030     runTest("simple.gro", selections);
1031 }
1032
1033
1034 TEST_F(SelectionCollectionDataTest, HandlesArithmeticExpressions)
1035 {
1036     static const char * const selections[] = {
1037         "x+1 > 3",
1038         "(y-1)^2 <= 1",
1039         "x+--1 > 3",
1040         "-x+-1 < -3"
1041     };
1042     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1043     runTest("simple.gro", selections);
1044 }
1045
1046
1047 TEST_F(SelectionCollectionDataTest, HandlesNumericVariables)
1048 {
1049     static const char * const selections[] = {
1050         "value = x + y",
1051         "value <= 4",
1052         "index = resnr",
1053         "index < 3"
1054     };
1055     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1056     runTest("simple.gro", selections);
1057 }
1058
1059
1060 TEST_F(SelectionCollectionDataTest, HandlesComplexNumericVariables)
1061 {
1062     static const char * const selections[] = {
1063         "value = x + y",
1064         "resname RA and value <= 4",
1065         "resname RA RB and x < 3 and value <= 4",
1066         "index = atomnr",
1067         "resname RA and index < 3",
1068         "resname RB and y < 3 and index < 6"
1069     };
1070     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1071     runTest("simple.gro", selections);
1072 }
1073
1074
1075 TEST_F(SelectionCollectionDataTest, HandlesPositionVariables)
1076 {
1077     static const char * const selections[] = {
1078         "foo = res_cog of resname RA",
1079         "foo",
1080         "within 1 of foo",
1081         "bar = cog of resname RA",
1082         "bar",
1083         "within 1 of bar"
1084     };
1085     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1086     runTest("simple.gro", selections);
1087 }
1088
1089
1090 TEST_F(SelectionCollectionDataTest, HandlesConstantPositionInVariable)
1091 {
1092     static const char * const selections[] = {
1093         "constpos = [1.0, 2.5, 0.5]",
1094         "constpos",
1095         "within 2 of constpos"
1096     };
1097     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates
1098              | efTestPositionAtoms);
1099     runTest("simple.gro", selections);
1100 }
1101
1102
1103 TEST_F(SelectionCollectionDataTest, HandlesNumericConstantsInVariables)
1104 {
1105     static const char * const selections[] = {
1106         "constint = 4",
1107         "constreal1 = 0.5",
1108         "constreal2 = 2.7",
1109         "resnr < constint",
1110         "x + constreal1 < constreal2"
1111     };
1112     setFlags(TestFlags() | efTestEvaluation | efTestPositionCoordinates);
1113     runTest("simple.gro", selections);
1114 }
1115
1116
1117 /********************************************************************
1118  * Tests for complex boolean syntax
1119  */
1120
1121 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysis)
1122 {
1123     static const char * const selections[] = {
1124         "atomnr 1 to 5 and atomnr 2 to 7 and x < 2",
1125         "atomnr 1 to 5 and (atomnr 4 to 7 or x < 2)",
1126         "atomnr 1 to 5 and y < 3 and (atomnr 4 to 7 or x < 2)",
1127         "atomnr 1 to 5 and not (atomnr 4 to 7 or x < 2)",
1128         "atomnr 1 to 5 or (atomnr 4 to 6 and (atomnr 5 to 7 or x < 2))"
1129     };
1130     runTest(10, selections);
1131 }
1132
1133
1134 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithVariables)
1135 {
1136     static const char * const selections[] = {
1137         "foo = atomnr 4 to 7 or x < 2",
1138         "atomnr 1 to 4 and foo",
1139         "atomnr 2 to 6 and y < 3 and foo",
1140         "atomnr 6 to 10 and not foo"
1141     };
1142     runTest(10, selections);
1143 }
1144
1145
1146 TEST_F(SelectionCollectionDataTest, HandlesBooleanStaticAnalysisWithMoreVariables)
1147 {
1148     static const char * const selections[] = {
1149         "foo = atomnr 4 to 7",
1150         "bar = foo and x < 2",
1151         "bar2 = foo and y < 2",
1152         "atomnr 1 to 4 and bar",
1153         "atomnr 2 to 6 and y < 3 and bar2",
1154         "atomnr 6 to 10 and not foo"
1155     };
1156     runTest(10, selections);
1157 }
1158
1159
1160 /********************************************************************
1161  * Tests for complex subexpression cases
1162  *
1163  * These tests use some knowledge of the implementation to trigger different
1164  * paths in the code.
1165  */
1166
1167 TEST_F(SelectionCollectionDataTest, HandlesUnusedVariables)
1168 {
1169     static const char * const selections[] = {
1170         "unused1 = atomnr 1 to 3",
1171         "foo = atomnr 4 to 7",
1172         "atomnr 1 to 6 and foo",
1173         "unused2 = atomnr 3 to 5"
1174     };
1175     runTest(10, selections);
1176 }
1177
1178
1179 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithStaticEvaluationGroups)
1180 {
1181     static const char * const selections[] = {
1182         "foo = atomnr 4 to 7 and x < 2",
1183         "atomnr 1 to 5 and foo",
1184         "atomnr 3 to 7 and foo"
1185     };
1186     runTest(10, selections);
1187 }
1188
1189
1190 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups)
1191 {
1192     static const char * const selections[] = {
1193         "foo = atomnr 4 to 7 and x < 2",
1194         "atomnr 1 to 6 and foo",
1195         "within 1 of foo",
1196         "foo"
1197     };
1198     runTest(10, selections);
1199 }
1200
1201
1202 TEST_F(SelectionCollectionDataTest, HandlesVariablesWithMixedEvaluationGroups2)
1203 {
1204     static const char * const selections[] = {
1205         "foo = atomnr 1 to 8 and x < 10",
1206         "atomnr 1 to 5 and y < 10 and foo",
1207         "foo"
1208     };
1209     setFlags(TestFlags() | efTestEvaluation);
1210     runTest("simple.gro", selections);
1211 }
1212
1213
1214 } // namespace