/* Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include // Define a new MatcherBase class with a public 'describe' member function because // Catch::MatcherBase::describe is protected and thus can't be used via a pointer to // Catch::MatcherBase. template class MatcherBase : public Catch::MatcherBase { public: virtual std::string describe() const = 0; virtual ~MatcherBase() = default; }; template class ValidatorBase : public MatcherBase { public: template ValidatorBase(T target, Ts&&... args) : matcher_{std::forward(args)...}, target_{target} {} bool match(const T& val) const override { if (std::isnan(target_)) { return std::isnan(val); } return matcher_.match(val); } std::string describe() const override { if (std::isnan(target_)) { return "is not NaN"; } return matcher_.describe(); } private: Matcher matcher_; T target_; bool nan = false; }; template auto ULPValidatorBuilderFactory(int64_t ulps) { return [=](T target, auto&&...) { return std::make_unique>( target, Catch::WithinULP(target, ulps)); }; }; template auto AbsValidatorBuilderFactory(double margin) { return [=](T target, auto&&...) { return std::make_unique>( target, Catch::WithinAbs(target, margin)); }; } template auto RelValidatorBuilderFactory(T margin) { return [=](T target, auto&&...) { return std::make_unique>( target, Catch::WithinRel(target, margin)); }; } template class EqValidator : public MatcherBase { public: EqValidator(T target) : target_{target} {} bool match(const T& val) const override { if (std::isnan(target_)) { return std::isnan(val); } return target_ == val; } std::string describe() const override { std::stringstream ss; ss << " is not equal to " << target_; return ss.str(); } private: T target_; }; template auto EqValidatorBuilderFactory() { return [](T val, auto&&...) { return std::make_unique>(val); }; } template class PairValidator : public MatcherBase> { public: PairValidator(const std::pair& target, const VBF& vbf, const VBS& vbs) : first_matcher_{vbf(target.first)}, second_matcher_{vbs(target.second)} {} bool match(const std::pair& val) const override { return first_matcher_->match(val.first) && second_matcher_->match(val.second); } std::string describe() const override { return "<" + first_matcher_->describe() + ", " + second_matcher_->describe() + ">"; } private: decltype(std::declval()(std::declval())) first_matcher_; decltype(std::declval()(std::declval())) second_matcher_; }; template auto PairValidatorBuilderFactory(const ValidatorBuilder& vb) { return [=](const std::pair& t, auto&&...) { return std::make_unique>(t, vb, vb); }; } template auto PairValidatorBuilderFactory(const VBF& vbf, const VBS& vbs) { return [=](const std::pair& t, auto&&...) { return std::make_unique>(t, vbf, vbs); }; } template class NopValidator : public MatcherBase { public: bool match(const T&) const override { return true; } std::string describe() const override { return ""; } }; template auto NopValidatorBuilderFactory() { return [](auto&&...) { return std::make_unique>(); }; }