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