Go to the documentation of this file.
33 #include <google/protobuf/util/field_comparator.h>
38 #include <google/protobuf/descriptor.h>
39 #include <google/protobuf/message.h>
40 #include <google/protobuf/util/message_differencer.h>
41 #include <google/protobuf/stubs/map_util.h>
42 #include <google/protobuf/stubs/mathutil.h>
52 : float_comparison_(EXACT),
53 treat_nan_as_equal_(
false),
54 has_default_tolerance_(
false) {}
65 switch (
field->cpp_type()) {
66 #define COMPARE_FIELD(METHOD) \
67 if (field->is_repeated()) { \
68 return ResultFromBoolean(Compare##METHOD( \
69 *field, reflection_1->GetRepeated##METHOD(message_1, field, index_1), \
70 reflection_2->GetRepeated##METHOD(message_2, field, index_2))); \
72 return ResultFromBoolean( \
73 Compare##METHOD(*field, reflection_1->Get##METHOD(message_1, field), \
74 reflection_2->Get##METHOD(message_2, field))); \
76 break; // Make sure no fall-through is introduced.
91 if (
field->is_repeated()) {
99 message_1,
field, index_1, &scratch1),
101 message_2,
field, index_2, &scratch2)));
125 <<
" of CppType = " <<
field->cpp_type();
133 return differencer->
Compare(message1, message2,
148 <<
"Field has to be float or double type. Field name is: "
149 <<
field->full_name();
154 double value_1,
double value_2) {
165 float value_1,
float value_2) {
169 template <
typename T>
171 T value_1,
T value_2) {
172 if (value_1 == value_2) {
190 if (tolerance == NULL) {
197 value_1, value_2,
static_cast<T>(tolerance->
fraction),
198 static_cast<T>(tolerance->
margin));
204 bool boolean_result)
const {
ComparisonResult SimpleCompare(const Message &message_1, const Message &message_2, const FieldDescriptor *field, int index_1, int index_2, const util::FieldContext *field_context)
const std::string & GetStringReference(const Message &message, const FieldDescriptor *field, std::string *scratch) const
bool CompareDouble(const FieldDescriptor &field, double value_1, double value_2)
TypeWithSize< 4 >::Int Int32
void SetFractionAndMargin(const FieldDescriptor *field, double fraction, double margin)
static bool WithinFractionOrMargin(const T x, const T y, const T fraction, const T margin)
virtual ~FieldComparator()
const std::string & GetRepeatedStringReference(const Message &message, const FieldDescriptor *field, int index, std::string *scratch) const
const Reflection * GetReflection() const
TypeWithSize< 8 >::UInt UInt64
bool CompareString(const FieldDescriptor &, const std::string &value_1, const std::string &value_2)
FloatComparison float_comparison_
TypeWithSize< 4 >::UInt UInt32
bool CompareFloat(const FieldDescriptor &field, float value_1, float value_2)
#define T(upbtypeconst, upbtype, ctype, default_value)
bool CompareEnum(const FieldDescriptor &field, const EnumValueDescriptor *value_1, const EnumValueDescriptor *value_2)
Tolerance default_tolerance_
bool Compare(const Message &message1, const Message &message2)
FloatingPoint< float > Float
std::vector< MessageDifferencer::SpecificField > * parent_fields() const
bool has_default_tolerance_
void SetDefaultFractionAndMargin(double fraction, double margin)
const FieldDescriptor * field
bool CompareDoubleOrFloat(const FieldDescriptor &field, T value_1, T value_2)
static bool AlmostEquals(T a, T b)
~SimpleFieldComparator() override
FloatingPoint< double > Double
ToleranceMap map_tolerance_
#define GOOGLE_CHECK(EXPRESSION)
TypeWithSize< 8 >::Int Int64
bool CompareWithDifferencer(MessageDifferencer *differencer, const Message &message1, const Message &message2, const util::FieldContext *field_context)
#define GOOGLE_LOG(LEVEL)
ComparisonResult ResultFromBoolean(bool boolean_result) const
const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
#define COMPARE_FIELD(METHOD)
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22