Rename gmx Variant to Any
[alexxy/gromacs.git] / src / testutils / refdata.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013,2014,2015,2016,2017,2018,2019, 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  * Implements classes and functions from refdata.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_testutils
41  */
42 #include "gmxpre.h"
43
44 #include "refdata.h"
45
46 #include <cctype>
47 #include <cstdlib>
48
49 #include <algorithm>
50 #include <limits>
51 #include <string>
52
53 #include <gtest/gtest.h>
54
55 #include "gromacs/options/basicoptions.h"
56 #include "gromacs/options/ioptionscontainer.h"
57 #include "gromacs/utility/any.h"
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/keyvaluetree.h"
61 #include "gromacs/utility/path.h"
62 #include "gromacs/utility/real.h"
63 #include "gromacs/utility/stringutil.h"
64
65 #include "testutils/refdata-checkers.h"
66 #include "testutils/refdata-impl.h"
67 #include "testutils/refdata-xml.h"
68 #include "testutils/testasserts.h"
69 #include "testutils/testexceptions.h"
70 #include "testutils/testfilemanager.h"
71
72 namespace gmx
73 {
74 namespace test
75 {
76
77 /********************************************************************
78  * TestReferenceData::Impl declaration
79  */
80
81 namespace internal
82 {
83
84 /*! \internal \brief
85  * Private implementation class for TestReferenceData.
86  *
87  * \ingroup module_testutils
88  */
89 class TestReferenceDataImpl
90 {
91     public:
92         //! Initializes a checker in the given mode.
93         TestReferenceDataImpl(ReferenceDataMode mode, bool bSelfTestMode);
94
95         //! Performs final reference data processing when test ends.
96         void onTestEnd(bool testPassed);
97
98         //! Full path of the reference data file.
99         std::string             fullFilename_;
100         /*! \brief
101          * Root entry for comparing the reference data.
102          *
103          * Null after construction iff in compare mode and reference data was
104          * not loaded successfully.
105          * In all write modes, copies are present for nodes added to
106          * \a outputRootEntry_, and ReferenceDataEntry::correspondingOutputEntry()
107          * points to the copy in the output tree.
108          */
109         ReferenceDataEntry::EntryPointer  compareRootEntry_;
110         /*! \brief
111          * Root entry for writing new reference data.
112          *
113          * Null if only comparing against existing data.  Otherwise, starts
114          * always as empty.
115          * When creating new reference data, this is maintained as a copy of
116          * \a compareRootEntry_.
117          * When updating existing data, entries are added either by copying
118          * from \a compareRootEntry_ (if they exist and comparison passes), or
119          * by creating new ones.
120          */
121         ReferenceDataEntry::EntryPointer  outputRootEntry_;
122         /*! \brief
123          * Whether updating existing reference data.
124          */
125         bool                    updateMismatchingEntries_;
126         //! `true` if self-testing (enables extra failure messages).
127         bool                    bSelfTestMode_;
128         /*! \brief
129          * Whether any reference checkers have been created for this data.
130          */
131         bool                    bInUse_;
132 };
133
134 }       // namespace internal
135
136 /********************************************************************
137  * Internal helpers
138  */
139
140 namespace
141 {
142
143 //! Convenience typedef for a smart pointer to TestReferenceDataImpl.
144 typedef std::shared_ptr<internal::TestReferenceDataImpl>
145     TestReferenceDataImplPointer;
146
147 /*! \brief
148  * Global reference data instance.
149  *
150  * The object is created when the test creates a TestReferenceData, and the
151  * object is destructed (and other post-processing is done) at the end of each
152  * test by ReferenceDataTestEventListener (which is installed as a Google Test
153  * test listener).
154  */
155 TestReferenceDataImplPointer g_referenceData;
156 //! Global reference data mode set with setReferenceDataMode().
157 ReferenceDataMode            g_referenceDataMode = erefdataCompare;
158
159 //! Returns the global reference data mode.
160 ReferenceDataMode getReferenceDataMode()
161 {
162     return g_referenceDataMode;
163 }
164
165 //! Returns a reference to the global reference data object.
166 TestReferenceDataImplPointer initReferenceDataInstance()
167 {
168     GMX_RELEASE_ASSERT(!g_referenceData,
169                        "Test cannot create multiple TestReferenceData instances");
170     g_referenceData.reset(new internal::TestReferenceDataImpl(getReferenceDataMode(), false));
171     return g_referenceData;
172 }
173
174 //! Handles reference data creation for self-tests.
175 TestReferenceDataImplPointer initReferenceDataInstanceForSelfTest(ReferenceDataMode mode)
176 {
177     if (g_referenceData)
178     {
179         GMX_RELEASE_ASSERT(g_referenceData.unique(),
180                            "Test cannot create multiple TestReferenceData instances");
181         g_referenceData->onTestEnd(true);
182         g_referenceData.reset();
183     }
184     g_referenceData.reset(new internal::TestReferenceDataImpl(mode, true));
185     return g_referenceData;
186 }
187
188 class ReferenceDataTestEventListener : public ::testing::EmptyTestEventListener
189 {
190     public:
191         void OnTestEnd(const ::testing::TestInfo &test_info) override
192         {
193             if (g_referenceData)
194             {
195                 GMX_RELEASE_ASSERT(g_referenceData.unique(),
196                                    "Test leaked TestRefeferenceData objects");
197                 g_referenceData->onTestEnd(test_info.result()->Passed());
198                 g_referenceData.reset();
199             }
200         }
201
202         void OnTestProgramEnd(const ::testing::UnitTest & /*unused*/) override
203         {
204             // Could be used e.g. to free internal buffers allocated by an XML parsing library
205         }
206 };
207
208 //! Formats a path to a reference data entry with a non-null id.
209 std::string formatEntryPath(const std::string &prefix, const std::string &id)
210 {
211     return prefix + "/" + id;
212 }
213
214 //! Formats a path to a reference data entry with a null id.
215 std::string formatSequenceEntryPath(const std::string &prefix, int seqIndex)
216 {
217     return formatString("%s/[%d]", prefix.c_str(), seqIndex+1);
218 }
219
220 //! Finds all entries that have not been checked under a given root.
221 void gatherUnusedEntries(const ReferenceDataEntry &root,
222                          const std::string        &rootPath,
223                          std::vector<std::string> *unusedPaths)
224 {
225     if (!root.hasBeenChecked())
226     {
227         unusedPaths->push_back(rootPath);
228         return;
229     }
230     int seqIndex = 0;
231     for (const auto &child : root.children())
232     {
233         std::string path;
234         if (child->id().empty())
235         {
236             path = formatSequenceEntryPath(rootPath, seqIndex);
237             ++seqIndex;
238         }
239         else
240         {
241             path = formatEntryPath(rootPath, child->id());
242         }
243         gatherUnusedEntries(*child, path, unusedPaths);
244     }
245 }
246
247 //! Produces a GTest assertion of any entries under given root have not been checked.
248 void checkUnusedEntries(const ReferenceDataEntry &root, const std::string &rootPath)
249 {
250     std::vector<std::string> unusedPaths;
251     gatherUnusedEntries(root, rootPath, &unusedPaths);
252     if (!unusedPaths.empty())
253     {
254         std::string paths;
255         if (unusedPaths.size() > 5)
256         {
257             paths = joinStrings(unusedPaths.begin(), unusedPaths.begin() + 5, "\n  ");
258             paths = "  " + paths + "\n  ...";
259         }
260         else
261         {
262             paths = joinStrings(unusedPaths.begin(), unusedPaths.end(), "\n  ");
263             paths = "  " + paths;
264         }
265         ADD_FAILURE() << "Reference data items not used in test:" << std::endl << paths;
266     }
267 }
268
269 }       // namespace
270
271 void initReferenceData(IOptionsContainer *options)
272 {
273     // Needs to correspond to the enum order in refdata.h.
274     const char *const refDataEnum[] =
275     { "check", "create", "update-changed", "update-all" };
276     options->addOption(
277             EnumOption<ReferenceDataMode>("ref-data")
278                 .enumValue(refDataEnum).store(&g_referenceDataMode)
279                 .description("Operation mode for tests that use reference data"));
280     ::testing::UnitTest::GetInstance()->listeners().Append(
281             new ReferenceDataTestEventListener);
282 }
283
284 /********************************************************************
285  * TestReferenceDataImpl definition
286  */
287
288 namespace internal
289 {
290
291 TestReferenceDataImpl::TestReferenceDataImpl(
292         ReferenceDataMode mode, bool bSelfTestMode)
293     : updateMismatchingEntries_(false), bSelfTestMode_(bSelfTestMode), bInUse_(false)
294 {
295     const std::string dirname =
296         bSelfTestMode
297         ? TestFileManager::getGlobalOutputTempDirectory()
298         : TestFileManager::getInputDataDirectory();
299     const std::string filename = TestFileManager::getTestSpecificFileName(".xml");
300     fullFilename_ = Path::join(dirname, "refdata", filename);
301
302     switch (mode)
303     {
304         case erefdataCompare:
305             if (File::exists(fullFilename_, File::throwOnError))
306             {
307                 compareRootEntry_ = readReferenceDataFile(fullFilename_);
308             }
309             break;
310         case erefdataCreateMissing:
311             if (File::exists(fullFilename_, File::throwOnError))
312             {
313                 compareRootEntry_ = readReferenceDataFile(fullFilename_);
314             }
315             else
316             {
317                 compareRootEntry_ = ReferenceDataEntry::createRoot();
318                 outputRootEntry_  = ReferenceDataEntry::createRoot();
319             }
320             break;
321         case erefdataUpdateChanged:
322             if (File::exists(fullFilename_, File::throwOnError))
323             {
324                 compareRootEntry_ = readReferenceDataFile(fullFilename_);
325             }
326             else
327             {
328                 compareRootEntry_ = ReferenceDataEntry::createRoot();
329             }
330             outputRootEntry_          = ReferenceDataEntry::createRoot();
331             updateMismatchingEntries_ = true;
332             break;
333         case erefdataUpdateAll:
334             compareRootEntry_ = ReferenceDataEntry::createRoot();
335             outputRootEntry_  = ReferenceDataEntry::createRoot();
336             break;
337     }
338 }
339
340 void TestReferenceDataImpl::onTestEnd(bool testPassed)
341 {
342     if (!bInUse_)
343     {
344         return;
345     }
346     // TODO: Only write the file with update-changed if there were actual changes.
347     if (outputRootEntry_)
348     {
349         if (testPassed)
350         {
351             std::string dirname = Path::getParentPath(fullFilename_);
352             if (!Directory::exists(dirname))
353             {
354                 if (Directory::create(dirname) != 0)
355                 {
356                     GMX_THROW(TestException("Creation of reference data directory failed: " + dirname));
357                 }
358             }
359             writeReferenceDataFile(fullFilename_, *outputRootEntry_);
360         }
361     }
362     else if (compareRootEntry_)
363     {
364         checkUnusedEntries(*compareRootEntry_, "");
365     }
366 }
367
368 }       // namespace internal
369
370
371 /********************************************************************
372  * TestReferenceChecker::Impl
373  */
374
375 /*! \internal \brief
376  * Private implementation class for TestReferenceChecker.
377  *
378  * \ingroup module_testutils
379  */
380 class TestReferenceChecker::Impl
381 {
382     public:
383         //! String constant for naming XML elements for boolean values.
384         static const char * const    cBooleanNodeName;
385         //! String constant for naming XML elements for string values.
386         static const char * const    cStringNodeName;
387         //! String constant for naming XML elements for unsigned char values.
388         static const char * const    cUCharNodeName;
389         //! String constant for naming XML elements for integer values.
390         static const char * const    cIntegerNodeName;
391         //! String constant for naming XML elements for int64 values.
392         static const char * const    cInt64NodeName;
393         //! String constant for naming XML elements for unsigned int64 values.
394         static const char * const    cUInt64NodeName;
395         //! String constant for naming XML elements for floating-point values.
396         static const char * const    cRealNodeName;
397         //! String constant for naming XML attribute for value identifiers.
398         static const char * const    cIdAttrName;
399         //! String constant for naming compounds for vectors.
400         static const char * const    cVectorType;
401         //! String constant for naming compounds for key-value tree objects.
402         static const char * const    cObjectType;
403         //! String constant for naming compounds for sequences.
404         static const char * const    cSequenceType;
405         //! String constant for value identifier for sequence length.
406         static const char * const    cSequenceLengthName;
407
408         //! Creates a checker that does nothing.
409         explicit Impl(bool initialized);
410         //! Creates a checker with a given root entry.
411         Impl(const std::string &path, ReferenceDataEntry *compareRootEntry,
412              ReferenceDataEntry *outputRootEntry, bool updateMismatchingEntries,
413              bool bSelfTestMode, const FloatingPointTolerance &defaultTolerance);
414
415         //! Returns the path of this checker with \p id appended.
416         std::string appendPath(const char *id) const;
417
418         //! Creates an entry with given parameters and fills it with \p checker.
419         ReferenceDataEntry::EntryPointer
420         createEntry(const char *type, const char *id,
421                     const IReferenceDataEntryChecker &checker) const
422         {
423             ReferenceDataEntry::EntryPointer entry(new ReferenceDataEntry(type, id));
424             checker.fillEntry(entry.get());
425             return entry;
426         }
427         //! Checks an entry for correct type and using \p checker.
428         ::testing::AssertionResult
429         checkEntry(const ReferenceDataEntry &entry, const std::string &fullId,
430                    const char *type, const IReferenceDataEntryChecker &checker) const
431         {
432             if (entry.type() != type)
433             {
434                 return ::testing::AssertionFailure()
435                        << "Mismatching reference data item type" << std::endl
436                        << "  In item: " << fullId << std::endl
437                        << "   Actual: " << type << std::endl
438                        << "Reference: " << entry.type();
439             }
440             return checker.checkEntry(entry, fullId);
441         }
442         //! Finds an entry by id and updates the last found entry pointer.
443         ReferenceDataEntry *findEntry(const char *id);
444         /*! \brief
445          * Finds/creates a reference data entry to match against.
446          *
447          * \param[in]  type   Type of entry to create.
448          * \param[in]  id     Unique identifier of the entry (can be NULL, in
449          *      which case the next entry without an id is matched).
450          * \param[out] checker  Checker to use for filling out created entries.
451          * \returns    Matching entry, or NULL if no matching entry found
452          *      (NULL is never returned in write mode; new entries are created
453          *      instead).
454          */
455         ReferenceDataEntry *
456         findOrCreateEntry(const char *type, const char *id,
457                           const IReferenceDataEntryChecker &checker);
458         /*! \brief
459          * Helper method for checking a reference data value.
460          *
461          * \param[in]  name   Type of entry to find.
462          * \param[in]  id     Unique identifier of the entry (can be NULL, in
463          *     which case the next entry without an id is matched).
464          * \param[in]  checker  Checker that provides logic specific to the
465          *     type of the entry.
466          * \returns    Whether the reference data matched, including details
467          *     of the mismatch if the comparison failed.
468          * \throws     TestException if there is a problem parsing the
469          *     reference data.
470          *
471          * Performs common tasks in checking a reference value, such as
472          * finding or creating the correct entry.
473          * Caller needs to provide a checker object that provides the string
474          * value for a newly created entry and performs the actual comparison
475          * against a found entry.
476          */
477         ::testing::AssertionResult
478         processItem(const char *name, const char *id,
479                     const IReferenceDataEntryChecker &checker);
480         /*! \brief
481          * Whether the checker is initialized.
482          */
483         bool initialized() const { return initialized_; }
484         /*! \brief
485          * Whether the checker should ignore all validation calls.
486          *
487          * This is used to ignore any calls within compounds for which
488          * reference data could not be found, such that only one error is
489          * issued for the missing compound, instead of every individual value.
490          */
491         bool shouldIgnore() const
492         {
493             GMX_RELEASE_ASSERT(initialized(),
494                                "Accessing uninitialized reference data checker.");
495             return compareRootEntry_ == nullptr;
496         }
497
498         //! Whether initialized with other means than the default constructor.
499         bool                    initialized_;
500         //! Default floating-point comparison tolerance.
501         FloatingPointTolerance  defaultTolerance_;
502         /*! \brief
503          * Human-readable path to the root node of this checker.
504          *
505          * For the root checker, this will be "/", and for each compound, the
506          * id of the compound is added.  Used for reporting comparison
507          * mismatches.
508          */
509         std::string             path_;
510         /*! \brief
511          * Current entry under which reference data is searched for comparison.
512          *
513          * Points to either the TestReferenceDataImpl::compareRootEntry_, or to
514          * a compound entry in the tree rooted at that entry.
515          *
516          * Can be NULL, in which case this checker does nothing (doesn't even
517          * report errors, see shouldIgnore()).
518          */
519         ReferenceDataEntry     *compareRootEntry_;
520         /*! \brief
521          * Current entry under which entries for writing are created.
522          *
523          * Points to either the TestReferenceDataImpl::outputRootEntry_, or to
524          * a compound entry in the tree rooted at that entry.  NULL if only
525          * comparing, or if shouldIgnore() returns `false`.
526          */
527         ReferenceDataEntry     *outputRootEntry_;
528         /*! \brief
529          * Iterator to a child of \a compareRootEntry_ that was last found.
530          *
531          * If `compareRootEntry_->isValidChild()` returns false, no entry has
532          * been found yet.
533          * After every check, is updated to point to the entry that was used
534          * for the check.
535          * Subsequent checks start the search for the matching node on this
536          * node.
537          */
538         ReferenceDataEntry::ChildIterator lastFoundEntry_;
539         /*! \brief
540          * Whether the reference data is being written (true) or compared
541          * (false).
542          */
543         bool                    updateMismatchingEntries_;
544         //! `true` if self-testing (enables extra failure messages).
545         bool                    bSelfTestMode_;
546         /*! \brief
547          * Current number of unnamed elements in a sequence.
548          *
549          * It is the index of the current unnamed element.
550          */
551         int                     seqIndex_;
552 };
553
554 const char *const TestReferenceChecker::Impl::cBooleanNodeName    = "Bool";
555 const char *const TestReferenceChecker::Impl::cStringNodeName     = "String";
556 const char *const TestReferenceChecker::Impl::cUCharNodeName      = "UChar";
557 const char *const TestReferenceChecker::Impl::cIntegerNodeName    = "Int";
558 const char *const TestReferenceChecker::Impl::cInt64NodeName      = "Int64";
559 const char *const TestReferenceChecker::Impl::cUInt64NodeName     = "UInt64";
560 const char *const TestReferenceChecker::Impl::cRealNodeName       = "Real";
561 const char *const TestReferenceChecker::Impl::cIdAttrName         = "Name";
562 const char *const TestReferenceChecker::Impl::cVectorType         = "Vector";
563 const char *const TestReferenceChecker::Impl::cObjectType         = "Object";
564 const char *const TestReferenceChecker::Impl::cSequenceType       = "Sequence";
565 const char *const TestReferenceChecker::Impl::cSequenceLengthName = "Length";
566
567
568 TestReferenceChecker::Impl::Impl(bool initialized)
569     : initialized_(initialized), defaultTolerance_(defaultRealTolerance()),
570       compareRootEntry_(nullptr), outputRootEntry_(nullptr),
571       updateMismatchingEntries_(false), bSelfTestMode_(false), seqIndex_(-1)
572 {
573 }
574
575
576 TestReferenceChecker::Impl::Impl(const std::string &path,
577                                  ReferenceDataEntry *compareRootEntry,
578                                  ReferenceDataEntry *outputRootEntry,
579                                  bool updateMismatchingEntries, bool bSelfTestMode,
580                                  const FloatingPointTolerance &defaultTolerance)
581     : initialized_(true), defaultTolerance_(defaultTolerance), path_(path),
582       compareRootEntry_(compareRootEntry), outputRootEntry_(outputRootEntry),
583       lastFoundEntry_(compareRootEntry->children().end()),
584       updateMismatchingEntries_(updateMismatchingEntries),
585       bSelfTestMode_(bSelfTestMode), seqIndex_(-1)
586 {
587 }
588
589
590 std::string
591 TestReferenceChecker::Impl::appendPath(const char *id) const
592 {
593     return id != nullptr
594            ? formatEntryPath(path_, id)
595            : formatSequenceEntryPath(path_, seqIndex_);
596 }
597
598
599 ReferenceDataEntry *TestReferenceChecker::Impl::findEntry(const char *id)
600 {
601     ReferenceDataEntry::ChildIterator entry = compareRootEntry_->findChild(id, lastFoundEntry_);
602     seqIndex_ = (id == nullptr) ? seqIndex_+1 : -1;
603     if (compareRootEntry_->isValidChild(entry))
604     {
605         lastFoundEntry_ = entry;
606         return entry->get();
607     }
608     return nullptr;
609 }
610
611 ReferenceDataEntry *
612 TestReferenceChecker::Impl::findOrCreateEntry(
613         const char *type, const char *id,
614         const IReferenceDataEntryChecker &checker)
615 {
616     ReferenceDataEntry *entry = findEntry(id);
617     if (entry == nullptr && outputRootEntry_ != nullptr)
618     {
619         lastFoundEntry_ = compareRootEntry_->addChild(createEntry(type, id, checker));
620         entry           = lastFoundEntry_->get();
621     }
622     return entry;
623 }
624
625 ::testing::AssertionResult
626 TestReferenceChecker::Impl::processItem(const char *type, const char *id,
627                                         const IReferenceDataEntryChecker &checker)
628 {
629     if (shouldIgnore())
630     {
631         return ::testing::AssertionSuccess();
632     }
633     std::string         fullId = appendPath(id);
634     ReferenceDataEntry *entry  = findOrCreateEntry(type, id, checker);
635     if (entry == nullptr)
636     {
637         return ::testing::AssertionFailure()
638                << "Reference data item " << fullId << " not found";
639     }
640     entry->setChecked();
641     ::testing::AssertionResult result(checkEntry(*entry, fullId, type, checker));
642     if (outputRootEntry_ != nullptr && entry->correspondingOutputEntry() == nullptr)
643     {
644         if (!updateMismatchingEntries_ || result)
645         {
646             outputRootEntry_->addChild(entry->cloneToOutputEntry());
647         }
648         else
649         {
650             ReferenceDataEntry::EntryPointer outputEntry(createEntry(type, id, checker));
651             entry->setCorrespondingOutputEntry(outputEntry.get());
652             outputRootEntry_->addChild(move(outputEntry));
653             return ::testing::AssertionSuccess();
654         }
655     }
656     if (bSelfTestMode_ && !result)
657     {
658         ReferenceDataEntry expected(type, id);
659         checker.fillEntry(&expected);
660         result << std::endl
661         << "String value: '" << expected.value() << "'" << std::endl
662         << " Ref. string: '" << entry->value() << "'";
663     }
664     return result;
665 }
666
667
668 /********************************************************************
669  * TestReferenceData
670  */
671
672 TestReferenceData::TestReferenceData()
673     : impl_(initReferenceDataInstance())
674 {
675 }
676
677
678 TestReferenceData::TestReferenceData(ReferenceDataMode mode)
679     : impl_(initReferenceDataInstanceForSelfTest(mode))
680 {
681 }
682
683
684 TestReferenceData::~TestReferenceData()
685 {
686 }
687
688
689 TestReferenceChecker TestReferenceData::rootChecker()
690 {
691     if (!impl_->bInUse_ && !impl_->compareRootEntry_)
692     {
693         ADD_FAILURE() << "Reference data file not found: "
694         << impl_->fullFilename_;
695     }
696     impl_->bInUse_ = true;
697     if (!impl_->compareRootEntry_)
698     {
699         return TestReferenceChecker(new TestReferenceChecker::Impl(true));
700     }
701     impl_->compareRootEntry_->setChecked();
702     return TestReferenceChecker(
703             new TestReferenceChecker::Impl("", impl_->compareRootEntry_.get(),
704                                            impl_->outputRootEntry_.get(),
705                                            impl_->updateMismatchingEntries_, impl_->bSelfTestMode_,
706                                            defaultRealTolerance()));
707 }
708
709
710 /********************************************************************
711  * TestReferenceChecker
712  */
713
714 TestReferenceChecker::TestReferenceChecker()
715     : impl_(new Impl(false))
716 {
717 }
718
719 TestReferenceChecker::TestReferenceChecker(Impl *impl)
720     : impl_(impl)
721 {
722 }
723
724 TestReferenceChecker::TestReferenceChecker(const TestReferenceChecker &other)
725     : impl_(new Impl(*other.impl_))
726 {
727 }
728
729 TestReferenceChecker::TestReferenceChecker(TestReferenceChecker &&other) noexcept
730     : impl_(std::move(other.impl_))
731 {
732 }
733
734 TestReferenceChecker &
735 TestReferenceChecker::operator=(TestReferenceChecker &&other) noexcept
736 {
737     impl_ = std::move(other.impl_);
738     return *this;
739 }
740
741 TestReferenceChecker::~TestReferenceChecker()
742 {
743 }
744
745 bool TestReferenceChecker::isValid() const
746 {
747     return impl_->initialized();
748 }
749
750
751 void TestReferenceChecker::setDefaultTolerance(
752         const FloatingPointTolerance &tolerance)
753 {
754     impl_->defaultTolerance_ = tolerance;
755 }
756
757
758 void TestReferenceChecker::checkUnusedEntries()
759 {
760     if (impl_->compareRootEntry_)
761     {
762         gmx::test::checkUnusedEntries(*impl_->compareRootEntry_, impl_->path_);
763         // Mark them checked so that they are reported only once.
764         impl_->compareRootEntry_->setCheckedIncludingChildren();
765     }
766 }
767
768
769 bool TestReferenceChecker::checkPresent(bool bPresent, const char *id)
770 {
771     if (impl_->shouldIgnore() || impl_->outputRootEntry_ != nullptr)
772     {
773         return bPresent;
774     }
775     ReferenceDataEntry::ChildIterator  entry
776         = impl_->compareRootEntry_->findChild(id, impl_->lastFoundEntry_);
777     const bool                         bFound
778         = impl_->compareRootEntry_->isValidChild(entry);
779     if (bFound != bPresent)
780     {
781         ADD_FAILURE() << "Mismatch while checking reference data item '"
782         << impl_->appendPath(id) << "'\n"
783         << "Expected: " << (bPresent ? "it is present.\n" : "it is absent.\n")
784         << "  Actual: " << (bFound ? "it is present." : "it is absent.");
785     }
786     if (bFound && bPresent)
787     {
788         impl_->lastFoundEntry_ = entry;
789         return true;
790     }
791     return false;
792 }
793
794
795 TestReferenceChecker TestReferenceChecker::checkCompound(const char *type, const char *id)
796 {
797     if (impl_->shouldIgnore())
798     {
799         return TestReferenceChecker(new Impl(true));
800     }
801     std::string         fullId = impl_->appendPath(id);
802     NullChecker         checker;
803     ReferenceDataEntry *entry  = impl_->findOrCreateEntry(type, id, checker);
804     if (entry == nullptr)
805     {
806         ADD_FAILURE() << "Reference data item " << fullId << " not found";
807         return TestReferenceChecker(new Impl(true));
808     }
809     entry->setChecked();
810     if (impl_->updateMismatchingEntries_)
811     {
812         entry->makeCompound(type);
813     }
814     else
815     {
816         ::testing::AssertionResult result(impl_->checkEntry(*entry, fullId, type, checker));
817         EXPECT_PLAIN(result);
818         if (!result)
819         {
820             return TestReferenceChecker(new Impl(true));
821         }
822     }
823     if (impl_->outputRootEntry_ != nullptr && entry->correspondingOutputEntry() == nullptr)
824     {
825         impl_->outputRootEntry_->addChild(entry->cloneToOutputEntry());
826     }
827     return TestReferenceChecker(
828             new Impl(fullId, entry, entry->correspondingOutputEntry(),
829                      impl_->updateMismatchingEntries_, impl_->bSelfTestMode_,
830                      impl_->defaultTolerance_));
831 }
832
833 TestReferenceChecker TestReferenceChecker::checkCompound(const char *type, const std::string &id)
834 {
835     return checkCompound(type, id.c_str());
836 }
837
838 /*! \brief Throw a TestException if the caller tries to write particular refdata that can't work.
839  *
840  * If the string to write is non-empty and has only whitespace,
841  * TinyXML2 can't read it correctly, so throw an exception for this
842  * case, so that we can't accidentally use it and run into mysterious
843  * problems.
844  *
845  * \todo Eliminate this limitation of TinyXML2. See
846  * e.g. https://github.com/leethomason/tinyxml2/issues/432
847  */
848 static void
849 throwIfNonEmptyAndOnlyWhitespace(const std::string &s, const char *id)
850 {
851     if (!s.empty() && std::all_of(s.cbegin(), s.cend(), [](const char &c){ return std::isspace(c); }))
852     {
853         std::string message("String '" + s + "' with ");
854         message += (id != nullptr) ? "null " : "";
855         message += "ID ";
856         message += (id != nullptr) ? "" : id;
857         message += " cannot be handled. We must refuse to write a refdata String"
858             "field for a non-empty string that contains only whitespace, "
859             "because it will not be read correctly by TinyXML2.";
860         GMX_THROW(TestException(message));
861     }
862 }
863
864 void TestReferenceChecker::checkBoolean(bool value, const char *id)
865 {
866     EXPECT_PLAIN(impl_->processItem(Impl::cBooleanNodeName, id,
867                                     ExactStringChecker(value ? "true" : "false")));
868 }
869
870
871 void TestReferenceChecker::checkString(const char *value, const char *id)
872 {
873     throwIfNonEmptyAndOnlyWhitespace(value, id);
874     EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id,
875                                     ExactStringChecker(value)));
876 }
877
878
879 void TestReferenceChecker::checkString(const std::string &value, const char *id)
880 {
881     throwIfNonEmptyAndOnlyWhitespace(value, id);
882     EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id,
883                                     ExactStringChecker(value)));
884 }
885
886
887 void TestReferenceChecker::checkTextBlock(const std::string &value,
888                                           const char        *id)
889 {
890     EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id,
891                                     ExactStringBlockChecker(value)));
892 }
893
894
895 void TestReferenceChecker::checkUChar(unsigned char value, const char *id)
896 {
897     EXPECT_PLAIN(impl_->processItem(Impl::cUCharNodeName, id,
898                                     ExactStringChecker(formatString("%d", value))));
899 }
900
901 void TestReferenceChecker::checkInteger(int value, const char *id)
902 {
903     EXPECT_PLAIN(impl_->processItem(Impl::cIntegerNodeName, id,
904                                     ExactStringChecker(formatString("%d", value))));
905 }
906
907 void TestReferenceChecker::checkInt64(int64_t value, const char *id)
908 {
909     EXPECT_PLAIN(impl_->processItem(Impl::cInt64NodeName, id,
910                                     ExactStringChecker(formatString("%" PRId64, value))));
911 }
912
913 void TestReferenceChecker::checkUInt64(uint64_t value, const char *id)
914 {
915     EXPECT_PLAIN(impl_->processItem(Impl::cUInt64NodeName, id,
916                                     ExactStringChecker(formatString("%" PRIu64, value))));
917 }
918
919 void TestReferenceChecker::checkDouble(double value, const char *id)
920 {
921     FloatingPointChecker<double> checker(value, impl_->defaultTolerance_);
922     EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, checker));
923 }
924
925
926 void TestReferenceChecker::checkFloat(float value, const char *id)
927 {
928     FloatingPointChecker<float> checker(value, impl_->defaultTolerance_);
929     EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, checker));
930 }
931
932
933 void TestReferenceChecker::checkReal(float value, const char *id)
934 {
935     checkFloat(value, id);
936 }
937
938
939 void TestReferenceChecker::checkReal(double value, const char *id)
940 {
941     checkDouble(value, id);
942 }
943
944
945 void TestReferenceChecker::checkRealFromString(const std::string &value, const char *id)
946 {
947     FloatingPointFromStringChecker<real> checker(value, impl_->defaultTolerance_);
948     EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, checker));
949 }
950
951
952 void TestReferenceChecker::checkVector(const int value[3], const char *id)
953 {
954     TestReferenceChecker compound(checkCompound(Impl::cVectorType, id));
955     compound.checkInteger(value[0], "X");
956     compound.checkInteger(value[1], "Y");
957     compound.checkInteger(value[2], "Z");
958 }
959
960
961 void TestReferenceChecker::checkVector(const float value[3], const char *id)
962 {
963     TestReferenceChecker compound(checkCompound(Impl::cVectorType, id));
964     compound.checkReal(value[0], "X");
965     compound.checkReal(value[1], "Y");
966     compound.checkReal(value[2], "Z");
967 }
968
969
970 void TestReferenceChecker::checkVector(const double value[3], const char *id)
971 {
972     TestReferenceChecker compound(checkCompound(Impl::cVectorType, id));
973     compound.checkReal(value[0], "X");
974     compound.checkReal(value[1], "Y");
975     compound.checkReal(value[2], "Z");
976 }
977
978
979 void TestReferenceChecker::checkAny(const Any &any, const char *id)
980 {
981     if (any.isType<bool>())
982     {
983         checkBoolean(any.cast<bool>(), id);
984     }
985     else if (any.isType<int>())
986     {
987         checkInteger(any.cast<int>(), id);
988     }
989     else if (any.isType<int64_t>())
990     {
991         checkInt64(any.cast<int64_t>(), id);
992     }
993     else if (any.isType<float>())
994     {
995         checkFloat(any.cast<float>(), id);
996     }
997     else if (any.isType<double>())
998     {
999         checkDouble(any.cast<double>(), id);
1000     }
1001     else if (any.isType<std::string>())
1002     {
1003         checkString(any.cast<std::string>(), id);
1004     }
1005     else
1006     {
1007         GMX_THROW(TestException("Unsupported any type"));
1008     }
1009 }
1010
1011
1012 void TestReferenceChecker::checkKeyValueTreeObject(const KeyValueTreeObject &tree, const char *id)
1013 {
1014     TestReferenceChecker compound(checkCompound(Impl::cObjectType, id));
1015     for (const auto &prop : tree.properties())
1016     {
1017         compound.checkKeyValueTreeValue(prop.value(), prop.key().c_str());
1018     }
1019     compound.checkUnusedEntries();
1020 }
1021
1022
1023 void TestReferenceChecker::checkKeyValueTreeValue(const KeyValueTreeValue &value, const char *id)
1024 {
1025     if (value.isObject())
1026     {
1027         checkKeyValueTreeObject(value.asObject(), id);
1028     }
1029     else if (value.isArray())
1030     {
1031         const auto &values = value.asArray().values();
1032         checkSequence(values.begin(), values.end(), id);
1033     }
1034     else
1035     {
1036         checkAny(value.asAny(), id);
1037     }
1038 }
1039
1040
1041 TestReferenceChecker
1042 TestReferenceChecker::checkSequenceCompound(const char *id, size_t length)
1043 {
1044     TestReferenceChecker compound(checkCompound(Impl::cSequenceType, id));
1045     compound.checkInteger(static_cast<int>(length), Impl::cSequenceLengthName);
1046     return compound;
1047 }
1048
1049
1050 unsigned char TestReferenceChecker::readUChar(const char *id)
1051 {
1052     if (impl_->shouldIgnore())
1053     {
1054         GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1055     }
1056     int value = 0;
1057     EXPECT_PLAIN(impl_->processItem(Impl::cUCharNodeName, id,
1058                                     ValueExtractor<int>(&value)));
1059     return value;
1060 }
1061
1062
1063 int TestReferenceChecker::readInteger(const char *id)
1064 {
1065     if (impl_->shouldIgnore())
1066     {
1067         GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1068     }
1069     int value = 0;
1070     EXPECT_PLAIN(impl_->processItem(Impl::cIntegerNodeName, id,
1071                                     ValueExtractor<int>(&value)));
1072     return value;
1073 }
1074
1075
1076 int64_t TestReferenceChecker::readInt64(const char *id)
1077 {
1078     if (impl_->shouldIgnore())
1079     {
1080         GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1081     }
1082     int64_t value = 0;
1083     EXPECT_PLAIN(impl_->processItem(Impl::cInt64NodeName, id,
1084                                     ValueExtractor<int64_t>(&value)));
1085     return value;
1086 }
1087
1088
1089 float TestReferenceChecker::readFloat(const char *id)
1090 {
1091     if (impl_->shouldIgnore())
1092     {
1093         GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1094     }
1095     float value = 0;
1096     EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id,
1097                                     ValueExtractor<float>(&value)));
1098     return value;
1099 }
1100
1101
1102 double TestReferenceChecker::readDouble(const char *id)
1103 {
1104     if (impl_->shouldIgnore())
1105     {
1106         GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1107     }
1108     double value = 0;
1109     EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id,
1110                                     ValueExtractor<double>(&value)));
1111     return value;
1112 }
1113
1114
1115 std::string TestReferenceChecker::readString(const char *id)
1116 {
1117     if (impl_->shouldIgnore())
1118     {
1119         GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1120     }
1121     std::string value;
1122     EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id,
1123                                     ValueExtractor<std::string>(&value)));
1124     return value;
1125 }
1126
1127 } // namespace test
1128 } // namespace gmx