HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_matcher_function.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4
6#include "matchers/hart_matcher.hpp"
7
8namespace hart
9{
10
11/// @brief Matcher defined by a user-provided function
12/// @details This matcher allows defining custom matching logic using a callable object
13/// (e.g. a lambda, function, or functor), instead of creating a dedicated matcher class.
14/// The function can have one of the following signatures:
15/// @code
16/// bool (const AudioBuffer<SampleType>& output);
17/// bool (const AudioBuffer<SampleType>& input,
18/// const AudioBuffer<SampleType>& output);
19/// @endcode
20/// The matcher operates on the full rendered buffer and does not support
21/// per-block evaluation.
22/// @ingroup Matchers
23template<typename SampleType>
25 public Matcher<SampleType, MatcherFunction<SampleType>>
26{
27public:
28
29 /// @brief Creates a matcher from a function that compares input and output
30 /// @param matcherFunction A callable with signature:
31 /// @code
32 /// bool (const AudioBuffer<SampleType>& input,
33 /// const AudioBuffer<SampleType>& output)
34 /// @endcode
35 /// It should return `true` if the output satisfies the expected condition, `false` otherwise
36 /// @param label Optional human-readable label used in failure reports
37 MatcherFunction (std::function <bool (const AudioBuffer<SampleType>&, const AudioBuffer<SampleType>&)> matcherFunction, const std::string& label = {}) :
38 m_matcherFunctionForInputAndOutput (std::move (matcherFunction)),
39 m_label (label)
40 {
41 }
42
43 /// @brief Constructs a matcher from a function that inspects only the output
44 /// @param matcherFunction A callable with signature:
45 /// @code
46 /// bool (const AudioBuffer<SampleType>& output)
47 /// @endcode
48 /// It should return `true` if the output satisfies the expected condition, `false` otherwise
49 /// @param label Optional human-readable label used in failure reports
50 MatcherFunction (std::function <bool (const AudioBuffer<SampleType>&)> matcherFunction, const std::string& label = {}) :
51 m_matcherFunctionForOutputOnly (std::move (matcherFunction)),
52 m_label (label)
53 {
54 }
55
56 bool match (const AudioBuffer<SampleType>& inputAudio, const AudioBuffer<SampleType>& observedOutputAudio) override
57 {
58 if (m_matcherFunctionForOutputOnly != nullptr)
59 return m_matcherFunctionForOutputOnly (observedOutputAudio);
60
61 if (m_matcherFunctionForInputAndOutput != nullptr)
62 return m_matcherFunctionForInputAndOutput (inputAudio, observedOutputAudio);
63
64 HART_THROW_OR_RETURN (hart::NullPointerError, "Matcher function is a nullptr!", false);
65 }
66
68 {
70 details.frame = 0;
71 details.channel = 0;
72 details.description = "Matcher function (" + (m_label.empty() ? "no label" : m_label) + ") has returned false";
73 return details;
74 }
75
76 void represent (std::ostream& stream) const override
77 {
78 stream << "MatcherFunction (<function>, \"" << m_label << "\")";
79 }
80
81 bool canOperatePerBlock() const override { return false;}
82 void prepare (double /* sampleRateHz */, size_t /* numChannels */, size_t /* maxBlockSizeFrames */) override {}
83
84private:
85 const std::function <bool (const AudioBuffer<SampleType>&, const AudioBuffer<SampleType>&)> m_matcherFunctionForInputAndOutput = nullptr;
86 const std::function <bool (const AudioBuffer<SampleType>&)> m_matcherFunctionForOutputOnly = nullptr;
87 const std::string m_label;
88};
89
91
92} // namespace hart
Container for audio data.
Matcher defined by a user-provided function.
MatcherFunction(std::function< bool(const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Constructs a matcher from a function that inspects only the output.
void represent(std::ostream &stream) const override
Makes a text representation of this Matcher for test failure outputs.
MatcherFunction(std::function< bool(const AudioBuffer< SampleType > &, const AudioBuffer< SampleType > &)> matcherFunction, const std::string &label={})
Creates a matcher from a function that compares input and output.
bool canOperatePerBlock() const override
Tells the host if it can operate on a block-by-block basis.
bool match(const AudioBuffer< SampleType > &inputAudio, const AudioBuffer< SampleType > &observedOutputAudio) override
Tells the host if the piece of audio satisfies Matcher's condition or not.
virtual MatcherFailureDetails getFailureDetails() const override
Returns a description of why the match has failed.
void prepare(double, size_t, size_t) override
Prepare for processing It is guaranteed that all subsequent process() calls will be in line with the ...
Base for audio matchers.
Thrown when a nullptr could be handled gracefully.
#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns a specified ...
#define HART_MATCHER_DECLARE_ALIASES_FOR(ClassName)
size_t channel
Index of channel at which the failure was detected.
std::string description
Readable description of why the match has failed.
size_t frame
Index of frame at which the match has failed.