aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2023-12-15 11:40:51 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2023-12-18 11:42:48 +0000
commit64a0c8e97e097ea6efb8e828cd67025d3da83a92 (patch)
treea0864db6e6d29eeebf498a75f9d1e58dcbc13d38 /vespalib
parent3129fe54642666b554bb21c6126984fe2ff6fced (diff)
dump blueprint cost
and ignore it when comparing structures
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/CMakeLists.txt1
-rw-r--r--vespalib/src/tests/slime/are_equal/CMakeLists.txt8
-rw-r--r--vespalib/src/tests/slime/are_equal/slime_are_equal_test.cpp173
-rw-r--r--vespalib/src/vespa/vespalib/data/slime/inspector.cpp104
-rw-r--r--vespalib/src/vespa/vespalib/data/slime/inspector.h11
5 files changed, 261 insertions, 36 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt
index 83711ab8200..f1ea5761f98 100644
--- a/vespalib/CMakeLists.txt
+++ b/vespalib/CMakeLists.txt
@@ -162,6 +162,7 @@ vespa_define_module(
src/tests/simple_thread_bundle
src/tests/singleexecutor
src/tests/slime
+ src/tests/slime/are_equal
src/tests/slime/external_data_value
src/tests/slime/summary-feature-benchmark
src/tests/small_vector
diff --git a/vespalib/src/tests/slime/are_equal/CMakeLists.txt b/vespalib/src/tests/slime/are_equal/CMakeLists.txt
new file mode 100644
index 00000000000..0fa6e99900f
--- /dev/null
+++ b/vespalib/src/tests/slime/are_equal/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespalib_slime_are_equal_test_app TEST
+ SOURCES
+ slime_are_equal_test.cpp
+ DEPENDS
+ vespalib
+)
+vespa_add_test(NAME vespalib_slime_are_equal_test_app COMMAND vespalib_slime_are_equal_test_app)
diff --git a/vespalib/src/tests/slime/are_equal/slime_are_equal_test.cpp b/vespalib/src/tests/slime/are_equal/slime_are_equal_test.cpp
new file mode 100644
index 00000000000..4751d51958a
--- /dev/null
+++ b/vespalib/src/tests/slime/are_equal/slime_are_equal_test.cpp
@@ -0,0 +1,173 @@
+// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/testkit/test_kit.h>
+#include <vespa/vespalib/data/slime/slime.h>
+#include <vespa/vespalib/data/slime/json_format.h>
+#include <vespa/vespalib/data/simple_buffer.h>
+#include <vespa/vespalib/util/stringfmt.h>
+#include <vespa/vespalib/util/overload.h>
+#include <functional>
+#include <variant>
+
+using namespace vespalib::slime::convenience;
+using vespalib::make_string_short::fmt;
+using vespalib::slime::NIX;
+
+using Path = std::vector<std::variant<size_t,vespalib::stringref>>;
+using Hook = std::function<bool(const Path &, const Inspector &, const Inspector &)>;
+using vespalib::slime::Inspector;
+
+Slime parse(const std::string &json) {
+ Slime slime;
+ ASSERT_TRUE(vespalib::slime::JsonFormat::decode(json, slime));
+ return slime;
+}
+
+const Inspector &full_obj() {
+ static vespalib::string str =
+ "{"
+ " a: 'foo',"
+ " b: 'bar',"
+ " c: 'baz',"
+ " d: [1,2,3,4,5]"
+ "}";
+ static Slime slime = parse(str);
+ return slime.get();
+}
+
+const Inspector &subset_obj() {
+ static vespalib::string str =
+ "{"
+ " a: 'foo',"
+ " c: 'baz',"
+ " d: [1,2,3]"
+ "}";
+ static Slime slime = parse(str);
+ return slime.get();
+}
+
+const Inspector &wildcard_obj() {
+ static vespalib::string str =
+ "{"
+ " a: 'foo',"
+ " b: null,"
+ " c: null,"
+ " d: [null,2,3,null]"
+ "}";
+ static Slime slime = parse(str);
+ return slime.get();
+}
+
+Slime add_data_and_nix(Slime slime) {
+ Cursor &root = slime.get();
+ char space1[3] = { 1, 2, 3 };
+ char space2[3] = { 2, 4, 6 };
+ root["ref"].addData(Memory(space1, 3));
+ root["ref"].addNix();
+ root["same"].addData(Memory(space1, 3));
+ root["same"].addNix();
+ root["err1"].addData(Memory(space2, 3));
+ // err1: invalid nix vs valid nix
+ return slime;
+}
+
+const Inspector &leaf_cmp_obj() {
+ static vespalib::string str =
+ "{"
+ " ref: [ true, 7, 2.0, 'foo'],"
+ "same: [ true, 7, 2.0, 'foo'],"
+ "err1: [false, 5, 2.5, 'bar'],"
+ "err2: [ 1, 7.0, 2, 3, '0x010203', 'null']"
+ "}";
+ static Slime slime = add_data_and_nix(parse(str));
+ return slime.get();
+}
+
+vespalib::string path_to_str(const Path &path) {
+ size_t cnt = 0;
+ vespalib::string str("[");
+ for (const auto &item: path) {
+ if (cnt++ > 0) {
+ str.append(",");
+ }
+ std::visit(vespalib::overload{
+ [&str](size_t value)noexcept{ str.append(fmt("%zu", value)); },
+ [&str](vespalib::stringref value)noexcept{ str.append(value); }}, item);
+ }
+ str.append("]");
+ return str;
+}
+
+vespalib::string to_str(const Inspector &value) {
+ if (!value.valid()) {
+ return "<missing>";
+ }
+ vespalib::SimpleBuffer buf;
+ vespalib::slime::JsonFormat::encode(value, buf, true);
+ return buf.get().make_string();
+}
+
+Hook dump_mismatches(Hook hook) {
+ return [hook](const Path &path, const Inspector &a, const Inspector &b)
+ {
+ bool result = hook(path, a, b);
+ fprintf(stderr, "mismatch at %s: %s vs %s (%s)\n", path_to_str(path).c_str(),
+ to_str(a).c_str(), to_str(b).c_str(), result ? "allowed" : "FAIL");
+ return result;
+ };
+}
+
+void verify(const Inspector &a, const Inspector &b, Hook c, bool expect) {
+ fprintf(stderr, "---> cmp\n");
+ Hook my_hook = dump_mismatches(c);
+ bool result = vespalib::slime::are_equal(a, b, my_hook);
+ fprintf(stderr, "<--- cmp\n");
+ EXPECT_EQUAL(result, expect);
+}
+
+TEST("strict compare (used by == operator)") {
+ auto allow_nothing = [](const auto &, const auto &, const auto &)noexcept{ return false; };
+ TEST_DO(verify( full_obj(), full_obj(), allow_nothing, true));
+ TEST_DO(verify( full_obj(), subset_obj(), allow_nothing, false));
+ TEST_DO(verify( subset_obj(), full_obj(), allow_nothing, false));
+ TEST_DO(verify( full_obj(), wildcard_obj(), allow_nothing, false));
+ TEST_DO(verify(wildcard_obj(), full_obj(), allow_nothing, false));
+}
+
+TEST("subset compare") {
+ auto allow_subset_ab = [](const auto &, const auto &a, const auto &)noexcept{ return !a.valid(); };
+ auto allow_subset_ba = [](const auto &, const auto &, const auto &b)noexcept{ return !b.valid(); };
+ TEST_DO(verify( subset_obj(), full_obj(), allow_subset_ab, true));
+ TEST_DO(verify( full_obj(), subset_obj(), allow_subset_ab, false));
+ TEST_DO(verify( full_obj(), subset_obj(), allow_subset_ba, true));
+ TEST_DO(verify( subset_obj(), full_obj(), allow_subset_ba, false));
+}
+
+TEST("wildcard compare") {
+ auto allow_wildcard_a = [](const auto &, const auto &a, const auto &)noexcept
+ { return a.valid() && a.type().getId() == NIX::ID; };
+ auto allow_wildcard_b = [](const auto &, const auto &, const auto &b)noexcept
+ { return b.valid() && b.type().getId() == NIX::ID; };
+ TEST_DO(verify(wildcard_obj(), full_obj(), allow_wildcard_a, false));
+ TEST_DO(verify(wildcard_obj(), subset_obj(), allow_wildcard_a, true));
+ TEST_DO(verify( subset_obj(), wildcard_obj(), allow_wildcard_a, false));
+ TEST_DO(verify( full_obj(), wildcard_obj(), allow_wildcard_b, false));
+ TEST_DO(verify( subset_obj(), wildcard_obj(), allow_wildcard_b, true));
+ TEST_DO(verify(wildcard_obj(), subset_obj(), allow_wildcard_b, false));
+}
+
+TEST("leaf nodes") {
+ auto allow_nothing = [](const auto &, const auto &, const auto &)noexcept{ return false; };
+ const Inspector &root = leaf_cmp_obj();
+ EXPECT_EQUAL(root["ref"].entries(), 6u);
+ EXPECT_EQUAL(root["same"].entries(), 6u);
+ EXPECT_EQUAL(root["err1"].entries(), 5u); // invalid nix at end
+ EXPECT_EQUAL(root["err2"].entries(), 6u);
+ for (size_t i = 0; i < 6; ++i) {
+ TEST_DO(verify(root["ref"][i], root["same"][i], allow_nothing, true));
+ TEST_DO(verify(root["ref"][i], root["err1"][i], allow_nothing, false));
+ TEST_DO(verify(root["ref"][i], root["err2"][i], allow_nothing, false));
+ }
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/data/slime/inspector.cpp b/vespalib/src/vespa/vespalib/data/slime/inspector.cpp
index bd3a1079682..742c5ab0623 100644
--- a/vespalib/src/vespa/vespalib/data/slime/inspector.cpp
+++ b/vespalib/src/vespa/vespalib/data/slime/inspector.cpp
@@ -7,39 +7,58 @@
namespace vespalib::slime {
-class Equal {
-public:
- Equal(const Inspector & rhs) : _rhs(rhs), _equal(true) { }
- bool isEqual() const { return _equal; }
-protected:
- const Inspector & _rhs;
- bool _equal;
+using Path = std::vector<std::variant<size_t,vespalib::stringref>>;
+using Hook = std::function<bool(const Path &, const Inspector &, const Inspector &)>;
+
+namespace {
+
+struct EqualState {
+ Path path;
+ Hook hook;
+ bool failed;
+ explicit EqualState(Hook hook_in)
+ : path(), hook(std::move(hook_in)), failed(false) {}
+ void mismatch(const Inspector &a, const Inspector &b) {
+ if (!failed && !hook(path, a, b)) {
+ failed = true;
+ }
+ }
+ void check_equal(const Inspector &a, const Inspector &b);
};
-
-class EqualObject : public ObjectTraverser, public Equal {
-public:
- EqualObject(const Inspector & rhs) : Equal(rhs) { }
-private:
- void field(const Memory &symbol, const Inspector &inspector) override {
- if ( _equal ) {
- _equal = (inspector == _rhs[symbol]);
+
+struct EqualObject : ObjectTraverser {
+ EqualState &state;
+ const Inspector &rhs;
+ EqualObject(EqualState &state_in, const Inspector &rhs_in) noexcept
+ : state(state_in), rhs(rhs_in) {}
+ void field(const Memory &symbol, const Inspector &inspector) final {
+ if (!state.failed) {
+ state.path.emplace_back(symbol.make_stringref());
+ state.check_equal(inspector, rhs[symbol]);
+ state.path.pop_back();
}
}
};
-class EqualArray : public ArrayTraverser, public Equal {
-public:
- EqualArray(const Inspector & rhs) : Equal(rhs) { }
-private:
- void entry(size_t idx, const Inspector &inspector) override {
- if ( _equal ) {
- _equal = (inspector == _rhs[idx]);
+struct MissingFields : ObjectTraverser {
+ EqualState &state;
+ const Inspector &lhs;
+ MissingFields(EqualState &state_in, const Inspector &lhs_in) noexcept
+ : state(state_in), lhs(lhs_in) {}
+ void field(const Memory &symbol, const Inspector &inspector) final {
+ if (!state.failed) {
+ const Inspector &field = lhs[symbol];
+ if (!field.valid()) {
+ state.path.emplace_back(symbol.make_stringref());
+ state.mismatch(field, inspector);
+ state.path.pop_back();
+ }
}
}
};
-bool operator == (const Inspector & a, const Inspector & b)
-{
+void
+EqualState::check_equal(const Inspector &a, const Inspector &b) {
bool equal(a.type().getId() == b.type().getId());
if (equal) {
switch (a.type().getId()) {
@@ -63,28 +82,45 @@ bool operator == (const Inspector & a, const Inspector & b)
break;
case ARRAY::ID:
{
- EqualArray traverser(b);
- a.traverse(traverser);
- equal = traverser.isEqual() && (a.entries() == b.entries());
+ size_t cnt = std::max(a.entries(), b.entries());
+ for (size_t i = 0; !failed && i < cnt; ++i) {
+ path.emplace_back(i);
+ check_equal(a[i], b[i]);
+ path.pop_back();
+ }
}
break;
case OBJECT::ID:
{
- EqualObject traverser(b);
- a.traverse(traverser);
- equal = traverser.isEqual() && (a.fields() == b.fields());
+ EqualObject cmp(*this, b);
+ MissingFields missing(*this, a);
+ a.traverse(cmp);
+ b.traverse(missing);
}
break;
default:
- assert(false);
+ abort();
break;
}
}
- return equal;
+ if (!equal) {
+ mismatch(a, b);
+ }
+}
+
+} // <unnamed>
+
+bool are_equal(const Inspector &a, const Inspector &b, Hook allow_mismatch) {
+ EqualState state(std::move(allow_mismatch));
+ state.check_equal(a, b);
+ return !state.failed;
+}
+
+bool operator==(const Inspector &a, const Inspector &b) {
+ return are_equal(a, b, [](const Path &, const Inspector &, const Inspector &)noexcept{ return false; });
}
-std::ostream & operator << (std::ostream & os, const Inspector & inspector)
-{
+std::ostream &operator<<(std::ostream &os, const Inspector &inspector) {
os << inspector.toString();
return os;
}
diff --git a/vespalib/src/vespa/vespalib/data/slime/inspector.h b/vespalib/src/vespa/vespalib/data/slime/inspector.h
index 11380405c4c..f035e179d95 100644
--- a/vespalib/src/vespa/vespalib/data/slime/inspector.h
+++ b/vespalib/src/vespa/vespalib/data/slime/inspector.h
@@ -5,6 +5,8 @@
#include "type.h"
#include "symbol.h"
#include <vespa/vespalib/data/memory.h>
+#include <functional>
+#include <variant>
namespace vespalib::slime {
@@ -38,8 +40,13 @@ struct Inspector {
virtual ~Inspector() {}
};
-bool operator == (const Inspector & a, const Inspector & b);
-std::ostream & operator << (std::ostream & os, const Inspector & inspector);
+bool operator==(const Inspector &a, const Inspector &b);
+std::ostream &operator<<(std::ostream &os, const Inspector &inspector);
+// check if two inspectors are equal
+// has support for allowing specific mismatches
+bool are_equal(const Inspector &a, const Inspector &b,
+ std::function<bool(const std::vector<std::variant<size_t,vespalib::stringref>> &path,
+ const Inspector &sub_a, const Inspector &sub_b)> allow_mismatch);
} // namespace vespalib::slime