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