2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements classes and functions from refdata.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_testutils
45 #include "testutils/refdata.h"
54 #include <gtest/gtest.h>
56 #include "gromacs/math/vectypes.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/ioptionscontainer.h"
59 #include "gromacs/utility/any.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/keyvaluetree.h"
63 #include "gromacs/utility/path.h"
64 #include "gromacs/utility/real.h"
65 #include "gromacs/utility/stringutil.h"
67 #include "testutils/testasserts.h"
68 #include "testutils/testexceptions.h"
69 #include "testutils/testfilemanager.h"
71 #include "refdata_checkers.h"
72 #include "refdata_impl.h"
73 #include "refdata_xml.h"
80 /********************************************************************
81 * TestReferenceData::Impl declaration
88 * Private implementation class for TestReferenceData.
90 * \ingroup module_testutils
92 class TestReferenceDataImpl
95 //! Initializes a checker in the given mode.
96 TestReferenceDataImpl(ReferenceDataMode mode, bool bSelfTestMode);
98 //! Performs final reference data processing when test ends.
99 void onTestEnd(bool testPassed) const;
101 //! Full path of the reference data file.
102 std::string fullFilename_;
104 * Root entry for comparing the reference data.
106 * Null after construction iff in compare mode and reference data was
107 * not loaded successfully.
108 * In all write modes, copies are present for nodes added to
109 * \a outputRootEntry_, and ReferenceDataEntry::correspondingOutputEntry()
110 * points to the copy in the output tree.
112 ReferenceDataEntry::EntryPointer compareRootEntry_;
114 * Root entry for writing new reference data.
116 * Null if only comparing against existing data. Otherwise, starts
118 * When creating new reference data, this is maintained as a copy of
119 * \a compareRootEntry_.
120 * When updating existing data, entries are added either by copying
121 * from \a compareRootEntry_ (if they exist and comparison passes), or
122 * by creating new ones.
124 ReferenceDataEntry::EntryPointer outputRootEntry_;
126 * Whether updating existing reference data.
128 bool updateMismatchingEntries_;
129 //! `true` if self-testing (enables extra failure messages).
132 * Whether any reference checkers have been created for this data.
137 } // namespace internal
139 /********************************************************************
146 //! Convenience typedef for a smart pointer to TestReferenceDataImpl.
147 typedef std::shared_ptr<internal::TestReferenceDataImpl> TestReferenceDataImplPointer;
150 * Global reference data instance.
152 * The object is created when the test creates a TestReferenceData, and the
153 * object is destructed (and other post-processing is done) at the end of each
154 * test by ReferenceDataTestEventListener (which is installed as a Google Test
157 TestReferenceDataImplPointer g_referenceData;
158 //! Global reference data mode set with setReferenceDataMode().
159 ReferenceDataMode g_referenceDataMode = ReferenceDataMode::Compare;
161 //! Returns the global reference data mode.
162 ReferenceDataMode getReferenceDataMode()
164 return g_referenceDataMode;
167 //! Returns a reference to the global reference data object.
168 TestReferenceDataImplPointer initReferenceDataInstance()
170 GMX_RELEASE_ASSERT(!g_referenceData, "Test cannot create multiple TestReferenceData instances");
171 g_referenceData.reset(new internal::TestReferenceDataImpl(getReferenceDataMode(), false));
172 return g_referenceData;
175 //! Handles reference data creation for self-tests.
176 TestReferenceDataImplPointer initReferenceDataInstanceForSelfTest(ReferenceDataMode mode)
180 GMX_RELEASE_ASSERT(g_referenceData.unique(),
181 "Test cannot create multiple TestReferenceData instances");
182 g_referenceData->onTestEnd(true);
183 g_referenceData.reset();
185 g_referenceData.reset(new internal::TestReferenceDataImpl(mode, true));
186 return g_referenceData;
189 class ReferenceDataTestEventListener : public ::testing::EmptyTestEventListener
192 void OnTestEnd(const ::testing::TestInfo& test_info) override
196 GMX_RELEASE_ASSERT(g_referenceData.unique(), "Test leaked TestRefeferenceData objects");
197 g_referenceData->onTestEnd(test_info.result()->Passed());
198 g_referenceData.reset();
202 void OnTestProgramEnd(const ::testing::UnitTest& /*unused*/) override
204 // Could be used e.g. to free internal buffers allocated by an XML parsing library
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)
211 return prefix + "/" + id;
214 //! Formats a path to a reference data entry with a null id.
215 std::string formatSequenceEntryPath(const std::string& prefix, int seqIndex)
217 return formatString("%s/[%d]", prefix.c_str(), seqIndex + 1);
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)
225 if (!root.hasBeenChecked())
227 unusedPaths->push_back(rootPath);
231 for (const auto& child : root.children())
234 if (child->id().empty())
236 path = formatSequenceEntryPath(rootPath, seqIndex);
241 path = formatEntryPath(rootPath, child->id());
243 gatherUnusedEntries(*child, path, unusedPaths);
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)
250 std::vector<std::string> unusedPaths;
251 gatherUnusedEntries(root, rootPath, &unusedPaths);
252 if (!unusedPaths.empty())
255 if (unusedPaths.size() > 5)
257 paths = joinStrings(unusedPaths.begin(), unusedPaths.begin() + 5, "\n ");
258 paths = " " + paths + "\n ...";
262 paths = joinStrings(unusedPaths.begin(), unusedPaths.end(), "\n ");
265 ADD_FAILURE() << "Reference data items not used in test:" << std::endl << paths;
271 void initReferenceData(IOptionsContainer* options)
273 static const gmx::EnumerationArray<ReferenceDataMode, const char*> s_refDataNames = {
274 { "check", "create", "update-changed", "update-all" }
276 options->addOption(EnumOption<ReferenceDataMode>("ref-data")
277 .enumValue(s_refDataNames)
278 .store(&g_referenceDataMode)
279 .description("Operation mode for tests that use reference data"));
280 ::testing::UnitTest::GetInstance()->listeners().Append(new ReferenceDataTestEventListener);
283 /********************************************************************
284 * TestReferenceDataImpl definition
290 TestReferenceDataImpl::TestReferenceDataImpl(ReferenceDataMode mode, bool bSelfTestMode) :
291 updateMismatchingEntries_(false), bSelfTestMode_(bSelfTestMode), bInUse_(false)
293 const std::string dirname = bSelfTestMode ? TestFileManager::getGlobalOutputTempDirectory()
294 : TestFileManager::getInputDataDirectory();
295 const std::string filename = TestFileManager::getTestSpecificFileName(".xml");
296 fullFilename_ = Path::join(dirname, "refdata", filename);
300 case ReferenceDataMode::Compare:
301 if (File::exists(fullFilename_, File::throwOnError))
303 compareRootEntry_ = readReferenceDataFile(fullFilename_);
306 case ReferenceDataMode::CreateMissing:
307 if (File::exists(fullFilename_, File::throwOnError))
309 compareRootEntry_ = readReferenceDataFile(fullFilename_);
313 compareRootEntry_ = ReferenceDataEntry::createRoot();
314 outputRootEntry_ = ReferenceDataEntry::createRoot();
317 case ReferenceDataMode::UpdateChanged:
318 if (File::exists(fullFilename_, File::throwOnError))
320 compareRootEntry_ = readReferenceDataFile(fullFilename_);
324 compareRootEntry_ = ReferenceDataEntry::createRoot();
326 outputRootEntry_ = ReferenceDataEntry::createRoot();
327 updateMismatchingEntries_ = true;
329 case ReferenceDataMode::UpdateAll:
330 compareRootEntry_ = ReferenceDataEntry::createRoot();
331 outputRootEntry_ = ReferenceDataEntry::createRoot();
333 case ReferenceDataMode::Count: GMX_THROW(InternalError("Invalid reference data mode"));
337 void TestReferenceDataImpl::onTestEnd(bool testPassed) const
343 // TODO: Only write the file with update-changed if there were actual changes.
344 if (outputRootEntry_)
348 std::string dirname = Path::getParentPath(fullFilename_);
349 if (!Directory::exists(dirname))
351 if (Directory::create(dirname) != 0)
353 GMX_THROW(TestException("Creation of reference data directory failed: " + dirname));
356 writeReferenceDataFile(fullFilename_, *outputRootEntry_);
359 else if (compareRootEntry_)
361 checkUnusedEntries(*compareRootEntry_, "");
365 } // namespace internal
368 /********************************************************************
369 * TestReferenceChecker::Impl
373 * Private implementation class for TestReferenceChecker.
375 * \ingroup module_testutils
377 class TestReferenceChecker::Impl
380 //! String constant for naming XML elements for boolean values.
381 static const char* const cBooleanNodeName;
382 //! String constant for naming XML elements for string values.
383 static const char* const cStringNodeName;
384 //! String constant for naming XML elements for unsigned char values.
385 static const char* const cUCharNodeName;
386 //! String constant for naming XML elements for integer values.
387 static const char* const cIntegerNodeName;
388 //! String constant for naming XML elements for int32 values.
389 static const char* const cInt32NodeName;
390 //! String constant for naming XML elements for unsigned int32 values.
391 static const char* const cUInt32NodeName;
392 //! String constant for naming XML elements for int32 values.
393 static const char* const cInt64NodeName;
394 //! String constant for naming XML elements for unsigned int64 values.
395 static const char* const cUInt64NodeName;
396 //! String constant for naming XML elements for floating-point values.
397 static const char* const cRealNodeName;
398 //! String constant for naming XML attribute for value identifiers.
399 static const char* const cIdAttrName;
400 //! String constant for naming compounds for vectors.
401 static const char* const cVectorType;
402 //! String constant for naming compounds for key-value tree objects.
403 static const char* const cObjectType;
404 //! String constant for naming compounds for sequences.
405 static const char* const cSequenceType;
406 //! String constant for value identifier for sequence length.
407 static const char* const cSequenceLengthName;
409 //! Creates a checker that does nothing.
410 explicit Impl(bool initialized);
411 //! Creates a checker with a given root entry.
412 Impl(const std::string& path,
413 ReferenceDataEntry* compareRootEntry,
414 ReferenceDataEntry* outputRootEntry,
415 bool updateMismatchingEntries,
417 const FloatingPointTolerance& defaultTolerance);
419 //! Returns the path of this checker with \p id appended.
420 std::string appendPath(const char* id) const;
422 //! Creates an entry with given parameters and fills it with \p checker.
423 static ReferenceDataEntry::EntryPointer createEntry(const char* type,
425 const IReferenceDataEntryChecker& checker)
427 ReferenceDataEntry::EntryPointer entry(new ReferenceDataEntry(type, id));
428 checker.fillEntry(entry.get());
431 //! Checks an entry for correct type and using \p checker.
432 static ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
433 const std::string& fullId,
435 const IReferenceDataEntryChecker& checker)
437 if (entry.type() != type)
439 return ::testing::AssertionFailure() << "Mismatching reference data item type" << std::endl
440 << " In item: " << fullId << std::endl
441 << " Actual: " << type << std::endl
442 << "Reference: " << entry.type();
444 return checker.checkEntry(entry, fullId);
446 //! Finds an entry by id and updates the last found entry pointer.
447 ReferenceDataEntry* findEntry(const char* id);
449 * Finds/creates a reference data entry to match against.
451 * \param[in] type Type of entry to create.
452 * \param[in] id Unique identifier of the entry (can be NULL, in
453 * which case the next entry without an id is matched).
454 * \param[out] checker Checker to use for filling out created entries.
455 * \returns Matching entry, or NULL if no matching entry found
456 * (NULL is never returned in write mode; new entries are created
459 ReferenceDataEntry* findOrCreateEntry(const char* type,
461 const IReferenceDataEntryChecker& checker);
463 * Helper method for checking a reference data value.
465 * \param[in] name Type of entry to find.
466 * \param[in] id Unique identifier of the entry (can be NULL, in
467 * which case the next entry without an id is matched).
468 * \param[in] checker Checker that provides logic specific to the
470 * \returns Whether the reference data matched, including details
471 * of the mismatch if the comparison failed.
472 * \throws TestException if there is a problem parsing the
475 * Performs common tasks in checking a reference value, such as
476 * finding or creating the correct entry.
477 * Caller needs to provide a checker object that provides the string
478 * value for a newly created entry and performs the actual comparison
479 * against a found entry.
481 ::testing::AssertionResult processItem(const char* name,
483 const IReferenceDataEntryChecker& checker);
485 * Whether the checker is initialized.
487 bool initialized() const { return initialized_; }
489 * Whether the checker should ignore all validation calls.
491 * This is used to ignore any calls within compounds for which
492 * reference data could not be found, such that only one error is
493 * issued for the missing compound, instead of every individual value.
495 bool shouldIgnore() const
497 GMX_RELEASE_ASSERT(initialized(), "Accessing uninitialized reference data checker.");
498 return compareRootEntry_ == nullptr;
501 //! Whether initialized with other means than the default constructor.
503 //! Default floating-point comparison tolerance.
504 FloatingPointTolerance defaultTolerance_;
506 * Human-readable path to the root node of this checker.
508 * For the root checker, this will be "/", and for each compound, the
509 * id of the compound is added. Used for reporting comparison
514 * Current entry under which reference data is searched for comparison.
516 * Points to either the TestReferenceDataImpl::compareRootEntry_, or to
517 * a compound entry in the tree rooted at that entry.
519 * Can be NULL, in which case this checker does nothing (doesn't even
520 * report errors, see shouldIgnore()).
522 ReferenceDataEntry* compareRootEntry_;
524 * Current entry under which entries for writing are created.
526 * Points to either the TestReferenceDataImpl::outputRootEntry_, or to
527 * a compound entry in the tree rooted at that entry. NULL if only
528 * comparing, or if shouldIgnore() returns `false`.
530 ReferenceDataEntry* outputRootEntry_;
532 * Iterator to a child of \a compareRootEntry_ that was last found.
534 * If `compareRootEntry_->isValidChild()` returns false, no entry has
536 * After every check, is updated to point to the entry that was used
538 * Subsequent checks start the search for the matching node on this
541 ReferenceDataEntry::ChildIterator lastFoundEntry_;
543 * Whether the reference data is being written (true) or compared
546 bool updateMismatchingEntries_;
547 //! `true` if self-testing (enables extra failure messages).
550 * Current number of unnamed elements in a sequence.
552 * It is the index of the current unnamed element.
557 const char* const TestReferenceChecker::Impl::cBooleanNodeName = "Bool";
558 const char* const TestReferenceChecker::Impl::cStringNodeName = "String";
559 const char* const TestReferenceChecker::Impl::cUCharNodeName = "UChar";
560 const char* const TestReferenceChecker::Impl::cIntegerNodeName = "Int";
561 const char* const TestReferenceChecker::Impl::cInt32NodeName = "Int32";
562 const char* const TestReferenceChecker::Impl::cUInt32NodeName = "UInt32";
563 const char* const TestReferenceChecker::Impl::cInt64NodeName = "Int64";
564 const char* const TestReferenceChecker::Impl::cUInt64NodeName = "UInt64";
565 const char* const TestReferenceChecker::Impl::cRealNodeName = "Real";
566 const char* const TestReferenceChecker::Impl::cIdAttrName = "Name";
567 const char* const TestReferenceChecker::Impl::cVectorType = "Vector";
568 const char* const TestReferenceChecker::Impl::cObjectType = "Object";
569 const char* const TestReferenceChecker::Impl::cSequenceType = "Sequence";
570 const char* const TestReferenceChecker::Impl::cSequenceLengthName = "Length";
573 TestReferenceChecker::Impl::Impl(bool initialized) :
574 initialized_(initialized),
575 defaultTolerance_(defaultRealTolerance()),
576 compareRootEntry_(nullptr),
577 outputRootEntry_(nullptr),
578 updateMismatchingEntries_(false),
579 bSelfTestMode_(false),
585 TestReferenceChecker::Impl::Impl(const std::string& path,
586 ReferenceDataEntry* compareRootEntry,
587 ReferenceDataEntry* outputRootEntry,
588 bool updateMismatchingEntries,
590 const FloatingPointTolerance& defaultTolerance) :
592 defaultTolerance_(defaultTolerance),
594 compareRootEntry_(compareRootEntry),
595 outputRootEntry_(outputRootEntry),
596 lastFoundEntry_(compareRootEntry->children().end()),
597 updateMismatchingEntries_(updateMismatchingEntries),
598 bSelfTestMode_(bSelfTestMode),
604 std::string TestReferenceChecker::Impl::appendPath(const char* id) const
606 return id != nullptr ? formatEntryPath(path_, id) : formatSequenceEntryPath(path_, seqIndex_);
610 ReferenceDataEntry* TestReferenceChecker::Impl::findEntry(const char* id)
612 ReferenceDataEntry::ChildIterator entry = compareRootEntry_->findChild(id, lastFoundEntry_);
613 seqIndex_ = (id == nullptr) ? seqIndex_ + 1 : -1;
614 if (compareRootEntry_->isValidChild(entry))
616 lastFoundEntry_ = entry;
622 ReferenceDataEntry* TestReferenceChecker::Impl::findOrCreateEntry(const char* type,
624 const IReferenceDataEntryChecker& checker)
626 ReferenceDataEntry* entry = findEntry(id);
627 if (entry == nullptr && outputRootEntry_ != nullptr)
629 lastFoundEntry_ = compareRootEntry_->addChild(createEntry(type, id, checker));
630 entry = lastFoundEntry_->get();
635 ::testing::AssertionResult TestReferenceChecker::Impl::processItem(const char* type,
637 const IReferenceDataEntryChecker& checker)
641 return ::testing::AssertionSuccess();
643 std::string fullId = appendPath(id);
644 ReferenceDataEntry* entry = findOrCreateEntry(type, id, checker);
645 if (entry == nullptr)
647 return ::testing::AssertionFailure() << "Reference data item " << fullId << " not found";
650 ::testing::AssertionResult result(checkEntry(*entry, fullId, type, checker));
651 if (outputRootEntry_ != nullptr && entry->correspondingOutputEntry() == nullptr)
653 if (!updateMismatchingEntries_ || result)
655 outputRootEntry_->addChild(entry->cloneToOutputEntry());
659 ReferenceDataEntry::EntryPointer outputEntry(createEntry(type, id, checker));
660 entry->setCorrespondingOutputEntry(outputEntry.get());
661 outputRootEntry_->addChild(move(outputEntry));
662 return ::testing::AssertionSuccess();
665 if (bSelfTestMode_ && !result)
667 ReferenceDataEntry expected(type, id);
668 checker.fillEntry(&expected);
670 << "String value: '" << expected.value() << "'" << std::endl
671 << " Ref. string: '" << entry->value() << "'";
677 /********************************************************************
681 TestReferenceData::TestReferenceData() : impl_(initReferenceDataInstance()) {}
684 TestReferenceData::TestReferenceData(ReferenceDataMode mode) :
685 impl_(initReferenceDataInstanceForSelfTest(mode))
690 TestReferenceData::~TestReferenceData() {}
693 TestReferenceChecker TestReferenceData::rootChecker()
695 if (!impl_->bInUse_ && !impl_->compareRootEntry_)
697 ADD_FAILURE() << "Reference data file not found: " << impl_->fullFilename_;
699 impl_->bInUse_ = true;
700 if (!impl_->compareRootEntry_)
702 return TestReferenceChecker(new TestReferenceChecker::Impl(true));
704 impl_->compareRootEntry_->setChecked();
705 return TestReferenceChecker(new TestReferenceChecker::Impl("",
706 impl_->compareRootEntry_.get(),
707 impl_->outputRootEntry_.get(),
708 impl_->updateMismatchingEntries_,
709 impl_->bSelfTestMode_,
710 defaultRealTolerance()));
714 /********************************************************************
715 * TestReferenceChecker
718 TestReferenceChecker::TestReferenceChecker() : impl_(new Impl(false)) {}
720 TestReferenceChecker::TestReferenceChecker(Impl* impl) : impl_(impl) {}
722 TestReferenceChecker::TestReferenceChecker(const TestReferenceChecker& other) :
723 impl_(new Impl(*other.impl_))
727 TestReferenceChecker::TestReferenceChecker(TestReferenceChecker&& other) noexcept :
728 impl_(std::move(other.impl_))
732 TestReferenceChecker& TestReferenceChecker::operator=(TestReferenceChecker&& other) noexcept
734 impl_ = std::move(other.impl_);
738 TestReferenceChecker::~TestReferenceChecker() {}
740 bool TestReferenceChecker::isValid() const
742 return impl_->initialized();
746 void TestReferenceChecker::setDefaultTolerance(const FloatingPointTolerance& tolerance)
748 impl_->defaultTolerance_ = tolerance;
752 void TestReferenceChecker::checkUnusedEntries()
754 if (impl_->compareRootEntry_)
756 gmx::test::checkUnusedEntries(*impl_->compareRootEntry_, impl_->path_);
757 // Mark them checked so that they are reported only once.
758 impl_->compareRootEntry_->setCheckedIncludingChildren();
762 void TestReferenceChecker::disableUnusedEntriesCheck()
764 if (impl_->compareRootEntry_)
766 impl_->compareRootEntry_->setCheckedIncludingChildren();
771 bool TestReferenceChecker::checkPresent(bool bPresent, const char* id)
773 if (impl_->shouldIgnore() || impl_->outputRootEntry_ != nullptr)
777 ReferenceDataEntry::ChildIterator entry =
778 impl_->compareRootEntry_->findChild(id, impl_->lastFoundEntry_);
779 const bool bFound = impl_->compareRootEntry_->isValidChild(entry);
780 if (bFound != bPresent)
782 ADD_FAILURE() << "Mismatch while checking reference data item '" << impl_->appendPath(id) << "'\n"
783 << "Expected: " << (bPresent ? "it is present.\n" : "it is absent.\n")
784 << " Actual: " << (bFound ? "it is present." : "it is absent.");
786 if (bFound && bPresent)
788 impl_->lastFoundEntry_ = entry;
795 TestReferenceChecker TestReferenceChecker::checkCompound(const char* type, const char* id)
797 if (impl_->shouldIgnore())
799 return TestReferenceChecker(new Impl(true));
801 std::string fullId = impl_->appendPath(id);
803 ReferenceDataEntry* entry = impl_->findOrCreateEntry(type, id, checker);
804 if (entry == nullptr)
806 ADD_FAILURE() << "Reference data item " << fullId << " not found";
807 return TestReferenceChecker(new Impl(true));
810 if (impl_->updateMismatchingEntries_)
812 entry->makeCompound(type);
816 ::testing::AssertionResult result(impl_->checkEntry(*entry, fullId, type, checker));
817 EXPECT_PLAIN(result);
820 return TestReferenceChecker(new Impl(true));
823 if (impl_->outputRootEntry_ != nullptr && entry->correspondingOutputEntry() == nullptr)
825 impl_->outputRootEntry_->addChild(entry->cloneToOutputEntry());
827 return TestReferenceChecker(new Impl(fullId,
829 entry->correspondingOutputEntry(),
830 impl_->updateMismatchingEntries_,
831 impl_->bSelfTestMode_,
832 impl_->defaultTolerance_));
835 TestReferenceChecker TestReferenceChecker::checkCompound(const char* type, const std::string& id)
837 return checkCompound(type, id.c_str());
840 /*! \brief Throw a TestException if the caller tries to write particular refdata that can't work.
842 * If the string to write is non-empty and has only whitespace,
843 * TinyXML2 can't read it correctly, so throw an exception for this
844 * case, so that we can't accidentally use it and run into mysterious
847 * \todo Eliminate this limitation of TinyXML2. See
848 * e.g. https://github.com/leethomason/tinyxml2/issues/432
850 static void throwIfNonEmptyAndOnlyWhitespace(const std::string& s, const char* id)
852 if (!s.empty() && std::all_of(s.cbegin(), s.cend(), [](const char& c) { return std::isspace(c); }))
854 std::string message("String '" + s + "' with ");
855 message += (id != nullptr) ? "null " : "";
857 message += (id != nullptr) ? "" : id;
859 " cannot be handled. We must refuse to write a refdata String"
860 "field for a non-empty string that contains only whitespace, "
861 "because it will not be read correctly by TinyXML2.";
862 GMX_THROW(TestException(message));
866 void TestReferenceChecker::checkBoolean(bool value, const char* id)
868 EXPECT_PLAIN(impl_->processItem(
869 Impl::cBooleanNodeName, id, ExactStringChecker(value ? "true" : "false")));
873 void TestReferenceChecker::checkString(const char* value, const char* id)
875 throwIfNonEmptyAndOnlyWhitespace(value, id);
876 EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id, ExactStringChecker(value)));
880 void TestReferenceChecker::checkString(const std::string& value, const char* id)
882 throwIfNonEmptyAndOnlyWhitespace(value, id);
883 EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id, ExactStringChecker(value)));
887 void TestReferenceChecker::checkTextBlock(const std::string& value, const char* id)
889 EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id, ExactStringBlockChecker(value)));
893 void TestReferenceChecker::checkUChar(unsigned char value, const char* id)
895 EXPECT_PLAIN(impl_->processItem(
896 Impl::cUCharNodeName, id, ExactStringChecker(formatString("%d", value))));
899 void TestReferenceChecker::checkInteger(int value, const char* id)
901 EXPECT_PLAIN(impl_->processItem(
902 Impl::cIntegerNodeName, id, ExactStringChecker(formatString("%d", value))));
905 void TestReferenceChecker::checkInt32(int32_t value, const char* id)
907 EXPECT_PLAIN(impl_->processItem(
908 Impl::cInt32NodeName, id, ExactStringChecker(formatString("%" PRId32, value))));
911 void TestReferenceChecker::checkUInt32(uint32_t value, const char* id)
913 EXPECT_PLAIN(impl_->processItem(
914 Impl::cUInt32NodeName, id, ExactStringChecker(formatString("%" PRIu32, value))));
917 void TestReferenceChecker::checkInt64(int64_t value, const char* id)
919 EXPECT_PLAIN(impl_->processItem(
920 Impl::cInt64NodeName, id, ExactStringChecker(formatString("%" PRId64, value))));
923 void TestReferenceChecker::checkUInt64(uint64_t value, const char* id)
925 EXPECT_PLAIN(impl_->processItem(
926 Impl::cUInt64NodeName, id, ExactStringChecker(formatString("%" PRIu64, value))));
929 void TestReferenceChecker::checkDouble(double value, const char* id)
931 FloatingPointChecker<double> checker(value, impl_->defaultTolerance_);
932 EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, checker));
936 void TestReferenceChecker::checkFloat(float value, const char* id)
938 FloatingPointChecker<float> checker(value, impl_->defaultTolerance_);
939 EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, checker));
943 void TestReferenceChecker::checkReal(float value, const char* id)
945 checkFloat(value, id);
949 void TestReferenceChecker::checkReal(double value, const char* id)
951 checkDouble(value, id);
955 void TestReferenceChecker::checkRealFromString(const std::string& value, const char* id)
957 FloatingPointFromStringChecker<real> checker(value, impl_->defaultTolerance_);
958 EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, checker));
962 void TestReferenceChecker::checkVector(const BasicVector<int>& value, const char* id)
964 TestReferenceChecker compound(checkCompound(Impl::cVectorType, id));
965 compound.checkInteger(value[0], "X");
966 compound.checkInteger(value[1], "Y");
967 compound.checkInteger(value[2], "Z");
971 void TestReferenceChecker::checkVector(const BasicVector<float>& value, const char* id)
973 TestReferenceChecker compound(checkCompound(Impl::cVectorType, id));
974 compound.checkReal(value[0], "X");
975 compound.checkReal(value[1], "Y");
976 compound.checkReal(value[2], "Z");
980 void TestReferenceChecker::checkVector(const BasicVector<double>& value, const char* id)
982 TestReferenceChecker compound(checkCompound(Impl::cVectorType, id));
983 compound.checkReal(value[0], "X");
984 compound.checkReal(value[1], "Y");
985 compound.checkReal(value[2], "Z");
989 void TestReferenceChecker::checkVector(const int value[3], const char* id)
991 checkVector(BasicVector<int>(value), id);
995 void TestReferenceChecker::checkVector(const float value[3], const char* id)
997 checkVector(BasicVector<float>(value), id);
1001 void TestReferenceChecker::checkVector(const double value[3], const char* id)
1003 checkVector(BasicVector<double>(value), id);
1007 void TestReferenceChecker::checkAny(const Any& any, const char* id)
1009 if (any.isType<bool>())
1011 checkBoolean(any.cast<bool>(), id);
1013 else if (any.isType<int>())
1015 checkInteger(any.cast<int>(), id);
1017 else if (any.isType<int32_t>())
1019 checkInt32(any.cast<int32_t>(), id);
1021 else if (any.isType<uint32_t>())
1023 checkInt32(any.cast<uint32_t>(), id);
1025 else if (any.isType<int64_t>())
1027 checkInt64(any.cast<int64_t>(), id);
1029 else if (any.isType<uint64_t>())
1031 checkInt64(any.cast<uint64_t>(), id);
1033 else if (any.isType<float>())
1035 checkFloat(any.cast<float>(), id);
1037 else if (any.isType<double>())
1039 checkDouble(any.cast<double>(), id);
1041 else if (any.isType<std::string>())
1043 checkString(any.cast<std::string>(), id);
1047 GMX_THROW(TestException("Unsupported any type"));
1052 void TestReferenceChecker::checkKeyValueTreeObject(const KeyValueTreeObject& tree, const char* id)
1054 TestReferenceChecker compound(checkCompound(Impl::cObjectType, id));
1055 for (const auto& prop : tree.properties())
1057 compound.checkKeyValueTreeValue(prop.value(), prop.key().c_str());
1059 compound.checkUnusedEntries();
1063 void TestReferenceChecker::checkKeyValueTreeValue(const KeyValueTreeValue& value, const char* id)
1065 if (value.isObject())
1067 checkKeyValueTreeObject(value.asObject(), id);
1069 else if (value.isArray())
1071 const auto& values = value.asArray().values();
1072 checkSequence(values.begin(), values.end(), id);
1076 checkAny(value.asAny(), id);
1081 TestReferenceChecker TestReferenceChecker::checkSequenceCompound(const char* id, size_t length)
1083 TestReferenceChecker compound(checkCompound(Impl::cSequenceType, id));
1084 compound.checkInteger(static_cast<int>(length), Impl::cSequenceLengthName);
1089 unsigned char TestReferenceChecker::readUChar(const char* id)
1091 if (impl_->shouldIgnore())
1093 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1096 EXPECT_PLAIN(impl_->processItem(Impl::cUCharNodeName, id, ValueExtractor<int>(&value)));
1101 int TestReferenceChecker::readInteger(const char* id)
1103 if (impl_->shouldIgnore())
1105 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1108 EXPECT_PLAIN(impl_->processItem(Impl::cIntegerNodeName, id, ValueExtractor<int>(&value)));
1113 int32_t TestReferenceChecker::readInt32(const char* id)
1115 if (impl_->shouldIgnore())
1117 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1120 EXPECT_PLAIN(impl_->processItem(Impl::cInt32NodeName, id, ValueExtractor<int32_t>(&value)));
1125 int64_t TestReferenceChecker::readInt64(const char* id)
1127 if (impl_->shouldIgnore())
1129 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1132 EXPECT_PLAIN(impl_->processItem(Impl::cInt64NodeName, id, ValueExtractor<int64_t>(&value)));
1137 float TestReferenceChecker::readFloat(const char* id)
1139 if (impl_->shouldIgnore())
1141 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1144 EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, ValueExtractor<float>(&value)));
1149 double TestReferenceChecker::readDouble(const char* id)
1151 if (impl_->shouldIgnore())
1153 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1156 EXPECT_PLAIN(impl_->processItem(Impl::cRealNodeName, id, ValueExtractor<double>(&value)));
1161 std::string TestReferenceChecker::readString(const char* id)
1163 if (impl_->shouldIgnore())
1165 GMX_THROW(TestException("Trying to read from non-existent reference data value"));
1168 EXPECT_PLAIN(impl_->processItem(Impl::cStringNodeName, id, ValueExtractor<std::string>(&value)));