diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-03-05 13:19:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-05 13:19:11 +0100 |
commit | beed3becc8ec1abe88aea8aa88e6e703fb67dc3c (patch) | |
tree | 29697abfe120b451c6da6f8a16736d08bf02cb47 | |
parent | 6823bc45e0a4c9b2a691d54b8abeeb7cd84e0091 (diff) | |
parent | 1734571166515a3454ec8f8025bd38ce4932f117 (diff) |
Merge pull request #21561 from vespa-engine/balder/move-convenience-methods-to-test-only-helpers
As a first step to reduce code complexity around FieldValue move test…
10 files changed, 120 insertions, 126 deletions
diff --git a/document/src/tests/documentselectparsertest.cpp b/document/src/tests/documentselectparsertest.cpp index f40aec41e16..8308c3bc693 100644 --- a/document/src/tests/documentselectparsertest.cpp +++ b/document/src/tests/documentselectparsertest.cpp @@ -1,5 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/document/repo/configbuilder.h> #include <vespa/document/repo/documenttyperepo.h> #include <vespa/document/update/assignvalueupdate.h> @@ -193,17 +194,15 @@ DocumentSelectParserTest::createDocs() amval.put(StringFieldValue("bar"), abval); _doc.back()->setValue("structarrmap", amval); - WeightedSetFieldValue wsval( - _doc.back()->getField("stringweightedset").getDataType()); - wsval.add("foo"); - wsval.add("val1"); - wsval.add("val2"); - wsval.add("val3"); - wsval.add("val4"); + WeightedSetFieldValue wsval(_doc.back()->getField("stringweightedset").getDataType()); + WSetHelper(wsval).add("foo"); + WSetHelper(wsval).add("val1"); + WSetHelper(wsval).add("val2"); + WSetHelper(wsval).add("val3"); + WSetHelper(wsval).add("val4"); _doc.back()->setValue("stringweightedset", wsval); - WeightedSetFieldValue wsbytes( - _doc.back()->getField("byteweightedset").getDataType()); + WeightedSetFieldValue wsbytes(_doc.back()->getField("byteweightedset").getDataType()); wsbytes.add(ByteFieldValue(5)); wsbytes.add(ByteFieldValue(75)); wsbytes.add(ByteFieldValue(static_cast<int8_t>(255))); @@ -211,14 +210,12 @@ DocumentSelectParserTest::createDocs() _doc.back()->setValue("byteweightedset", wsbytes); } - _doc.push_back(createDoc( - "testdoctype1", "id:myspace:testdoctype1:n=1234:footype1", 15, 1.0, "some", "some", 0)); // DOC 2 + _doc.push_back(createDoc("testdoctype1", "id:myspace:testdoctype1:n=1234:footype1", 15, 1.0, "some", "some", 0)); // DOC 2 // Add empty struct and array { StructFieldValue sval(_doc.back()->getField("mystruct").getDataType()); _doc.back()->setValue("mystruct", sval); - ArrayFieldValue aval( - _doc.back()->getField("structarray").getDataType()); + ArrayFieldValue aval(_doc.back()->getField("structarray").getDataType()); _doc.back()->setValue("structarray", aval); } _doc.push_back(createDoc("testdoctype1", "id:myspace:testdoctype1:g=yahoo:bar", 14, 2.4, "Yet", "\xE4\xB8\xBA\xE4\xBB\x80", 0)); // DOC 3 diff --git a/document/src/tests/documenttestcase.cpp b/document/src/tests/documenttestcase.cpp index fde702f2080..bcf0cbad5fe 100644 --- a/document/src/tests/documenttestcase.cpp +++ b/document/src/tests/documenttestcase.cpp @@ -1,5 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/document/base/testdocman.h> #include <vespa/document/datatype/documenttype.h> #include <vespa/document/datatype/mapdatatype.h> @@ -349,11 +350,12 @@ TEST(DocumentTest, testModifyDocument) structmap1.put(StringFieldValue("test"), l2s1); l1s1.setValue(structmapF, structmap1); - WeightedSetFieldValue wset1(wset); + WeightedSetFieldValue wwset1(wset); + WSetHelper wset1(wwset1); wset1.add("foo"); wset1.add("bar"); wset1.add("zoo"); - l1s1.setValue(wsetF, wset1); + l1s1.setValue(wsetF, wwset1); WeightedSetFieldValue wset2(structwset); wset2.add(l2s1); @@ -716,8 +718,8 @@ TEST(DocumentTest,testReadSerializedAllVersions) docInDoc.set("stringindocfield", "Elvis is dead"); doc.setValue("docfield", docInDoc); ArrayFieldValue floatArray(*arrayOfFloatDataType); - floatArray.add(1.0); - floatArray.add(2.0); + CollectionHelper(floatArray).add(1.0); + CollectionHelper(floatArray).add(2.0); doc.setValue("arrayoffloatfield", floatArray); WeightedSetFieldValue weightedSet(*weightedSetDataType); weightedSet.add(StringFieldValue("Weighted 0"), 50); diff --git a/document/src/tests/documentupdatetestcase.cpp b/document/src/tests/documentupdatetestcase.cpp index 8a9aef0bde4..d5905d63455 100644 --- a/document/src/tests/documentupdatetestcase.cpp +++ b/document/src/tests/documentupdatetestcase.cpp @@ -1,5 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/document/base/testdocman.h> #include <vespa/document/base/exceptions.h> #include <vespa/document/datatype/tensor_data_type.h> @@ -1287,9 +1288,9 @@ TEST(DocumentUpdateTest, array_element_update_applies_to_specified_element) ArrayUpdateFixture f; ArrayFieldValue array_value(f.array_field.getDataType()); - array_value.add("foo"); - array_value.add("baz"); - array_value.add("blarg"); + CollectionHelper(array_value).add("foo"); + CollectionHelper(array_value).add("baz"); + CollectionHelper(array_value).add("blarg"); f.doc->setValue(f.array_field, array_value); f.update->applyTo(*f.doc); @@ -1306,7 +1307,7 @@ TEST(DocumentUpdateTest, array_element_update_for_invalid_index_is_ignored) ArrayUpdateFixture f; ArrayFieldValue array_value(f.array_field.getDataType()); - array_value.add("jerry"); + CollectionHelper(array_value).add("jerry"); f.doc->setValue(f.array_field, array_value); f.update->applyTo(*f.doc); // MapValueUpdate for index 1, which does not exist diff --git a/document/src/tests/fieldpathupdatetestcase.cpp b/document/src/tests/fieldpathupdatetestcase.cpp index 3ebf3699763..3d41a5bcacd 100644 --- a/document/src/tests/fieldpathupdatetestcase.cpp +++ b/document/src/tests/fieldpathupdatetestcase.cpp @@ -1,4 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/document/base/testdocman.h> #include <vespa/document/fieldvalue/iteratorhandler.h> #include <vespa/document/fieldvalue/intfieldvalue.h> @@ -128,11 +130,12 @@ createTestDocument(const DocumentTypeRepo &repo) structmap1.put(StringFieldValue("test"), l2s1); l1s1.setValue("structmap", structmap1); - WeightedSetFieldValue wset1(*wset); + WeightedSetFieldValue wwset1(*wset); + WSetHelper wset1(wwset1); wset1.add("foo"); wset1.add("bar"); wset1.add("zoo"); - l1s1.setValue("wset", wset1); + l1s1.setValue("wset", wwset1); WeightedSetFieldValue wset2(*structwset); wset2.add(l2s1); diff --git a/document/src/tests/serialization/vespadocumentserializer_test.cpp b/document/src/tests/serialization/vespadocumentserializer_test.cpp index d85d5240b64..33e8c521e09 100644 --- a/document/src/tests/serialization/vespadocumentserializer_test.cpp +++ b/document/src/tests/serialization/vespadocumentserializer_test.cpp @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. // Unit tests for vespadocumentserializer. +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/document/annotation/annotation.h> #include <vespa/document/annotation/span.h> #include <vespa/document/annotation/spantree.h> @@ -345,7 +346,7 @@ void checkArrayFieldValue(SizeType value_count) { ArrayDataType array_type(*DataType::INT); ArrayFieldValue value(array_type); for (uint32_t i = 0; i < value_count; ++i) { - value.add(static_cast<int32_t>(i)); + CollectionHelper(value).add(static_cast<int32_t>(i)); } nbostream stream; diff --git a/document/src/tests/weightedsetfieldvaluetest.cpp b/document/src/tests/weightedsetfieldvaluetest.cpp index 325267e9da6..61f727120d1 100644 --- a/document/src/tests/weightedsetfieldvaluetest.cpp +++ b/document/src/tests/weightedsetfieldvaluetest.cpp @@ -1,5 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/document/fieldvalue/weightedsetfieldvalue.h> #include <vespa/document/fieldvalue/arrayfieldvalue.h> #include <vespa/document/fieldvalue/longfieldvalue.h> @@ -255,7 +256,8 @@ TEST(WeightedSetFieldValueTest, testWeightedSet) WeightedSetDataType mytype2(*DataType::STRING, true, true); EXPECT_EQ(*DataType::TAG, static_cast<DataType &>(mytype2)); - WeightedSetFieldValue val1(mytype1); + WeightedSetFieldValue wsval1(mytype1); + WSetHelper val1(wsval1); val1.add("foo", 4); try{ val1.increment("bar", 2); @@ -272,25 +274,26 @@ TEST(WeightedSetFieldValueTest, testWeightedSet) val1.decrement("foo", 3); EXPECT_EQ(7, val1.get("foo")); val1.decrement("foo", 7); - EXPECT_TRUE(val1.contains("foo")); + EXPECT_TRUE(CollectionHelper(wsval1).contains("foo")); - WeightedSetFieldValue val2(mytype2); + WeightedSetFieldValue wsval2(mytype2); + WSetHelper val2(wsval2); val2.add("foo", 4); val2.increment("bar", 2); EXPECT_EQ(2, val2.get("bar")); val2.decrement("bar", 4); EXPECT_EQ(-2, val2.get("bar")); val2.increment("bar", 2); - EXPECT_TRUE(!val2.contains("bar")); + EXPECT_TRUE(!CollectionHelper(wsval2).contains("bar")); val2.decrement("foo", 4); - EXPECT_TRUE(!val2.contains("foo")); + EXPECT_TRUE(!CollectionHelper(wsval2).contains("foo")); val2.decrement("foo", 4); EXPECT_EQ(-4, val2.get("foo")); val2.add("foo", 0); - EXPECT_TRUE(!val2.contains("foo")); + EXPECT_TRUE(!CollectionHelper(wsval2).contains("foo")); } } @@ -301,12 +304,12 @@ TEST(WeightedSetFieldValueTest, testAddIgnoreZeroWeight) WeightedSetFieldValue ws(wsetType); ws.addIgnoreZeroWeight(StringFieldValue("yarn"), 0); - EXPECT_TRUE(ws.contains("yarn")); - EXPECT_EQ(0, ws.get("yarn")); + EXPECT_TRUE(CollectionHelper(ws).contains("yarn")); + EXPECT_EQ(0, WSetHelper(ws).get("yarn")); ws.addIgnoreZeroWeight(StringFieldValue("flarn"), 1); - EXPECT_TRUE(ws.contains("flarn")); - EXPECT_EQ(1, ws.get("flarn")); + EXPECT_TRUE(CollectionHelper(ws).contains("flarn")); + EXPECT_EQ(1, WSetHelper(ws).get("flarn")); } } // document diff --git a/document/src/vespa/document/fieldvalue/collectionfieldvalue.h b/document/src/vespa/document/fieldvalue/collectionfieldvalue.h index fcdd7d72ebd..9efd3b91bc6 100644 --- a/document/src/vespa/document/fieldvalue/collectionfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/collectionfieldvalue.h @@ -62,41 +62,6 @@ public: virtual size_t size() const = 0; virtual void clear() = 0; - // Convenience functions for using primitives directly - - bool add(vespalib::stringref val) - { return addValue(*createNested() = val); } - bool add(int32_t val) - { return addValue(*createNested() = val); } - bool add(int64_t val) - { return addValue(*createNested() = val); } - bool add(float val) - { return addValue(*createNested() = val); } - bool add(double val) - { return addValue(*createNested() = val); } - - bool contains(vespalib::stringref val) - { return containsValue(*createNested() = val); } - bool contains(int32_t val) - { return containsValue(*createNested() = val); } - bool contains(int64_t val) - { return containsValue(*createNested() = val); } - bool contains(float val) - { return containsValue(*createNested() = val); } - bool contains(double val) - { return containsValue(*createNested() = val); } - - bool remove(vespalib::stringref val) - { return removeValue(*createNested() = val); } - bool remove(int32_t val) - { return removeValue(*createNested() = val); } - bool remove(int64_t val) - { return removeValue(*createNested() = val); } - bool remove(float val) - { return removeValue(*createNested() = val); } - bool remove(double val) - { return removeValue(*createNested() = val); } - DECLARE_IDENTIFIABLE_ABSTRACT(CollectionFieldValue); }; diff --git a/document/src/vespa/document/fieldvalue/weightedsetfieldvalue.h b/document/src/vespa/document/fieldvalue/weightedsetfieldvalue.h index b88c44085ba..d58819607b4 100644 --- a/document/src/vespa/document/fieldvalue/weightedsetfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/weightedsetfieldvalue.h @@ -64,8 +64,7 @@ public: bool addIgnoreZeroWeight(const FieldValue&, int32_t weight = 1); void push_back(FieldValue::UP, int32_t weight); void increment(const FieldValue& fval, int val = 1); - void decrement(const FieldValue& fval, int val = 1) - { increment(fval, -1*val); } + void decrement(const FieldValue& fval, int val = 1) { increment(fval, -1*val); } int32_t get(const FieldValue&, int32_t defaultValue = 0) const; bool isEmpty() const override { return _map.isEmpty(); } @@ -76,13 +75,12 @@ public: FieldValue& assign(const FieldValue&) override; WeightedSetFieldValue* clone() const override { return new WeightedSetFieldValue(*this); } - virtual int compare(const FieldValue&) const override; - virtual void printXml(XmlOutputStream& out) const override; - virtual void print(std::ostream& out, bool verbose, - const std::string& indent) const override; - virtual bool hasChanged() const override; + int compare(const FieldValue&) const override; + void printXml(XmlOutputStream& out) const override; + void print(std::ostream& out, bool verbose, const std::string& indent) const override; + bool hasChanged() const override; - // Implements iterating through internal content. + // Implements iterating through internal content. typedef WeightedFieldValueMap::const_iterator const_iterator; typedef WeightedFieldValueMap::iterator iterator; @@ -95,54 +93,7 @@ public: const_iterator find(const FieldValue& fv) const; iterator find(const FieldValue& fv); - // Utility functions for easy use of weighted sets of primitives - - bool add(vespalib::stringref val, int32_t weight = 1) - { return add(*createNested() = val, weight); } - bool add(int32_t val, int32_t weight = 1) - { return add(*createNested() = val, weight); } - bool add(int64_t val, int32_t weight = 1) - { return add(*createNested() = val, weight); } - bool add(float val, int32_t weight = 1) - { return add(*createNested() = val, weight); } - bool add(double val, int32_t weight = 1) - { return add(*createNested() = val, weight); } - - int32_t get(vespalib::stringref val) const - { return get(*createNested() = val); } - int32_t get(int32_t val) const - { return get(*createNested() = val); } - int32_t get(int64_t val) const - { return get(*createNested() = val); } - int32_t get(float val) const - { return get(*createNested() = val); } - int32_t get(double val) const - { return get(*createNested() = val); } - - void increment(vespalib::stringref val, int32_t weight = 1) - { increment(*createNested() = val, weight); } - void increment(int32_t val, int32_t weight = 1) - { increment(*createNested() = val, weight); } - void increment(int64_t val, int32_t weight = 1) - { increment(*createNested() = val, weight); } - void increment(float val, int32_t weight = 1) - { increment(*createNested() = val, weight); } - void increment(double val, int32_t weight = 1) - { increment(*createNested() = val, weight); } - - void decrement(vespalib::stringref val, int32_t weight = 1) - { decrement(*createNested() = val, weight); } - void decrement(int32_t val, int32_t weight = 1) - { decrement(*createNested() = val, weight); } - void decrement(int64_t val, int32_t weight = 1) - { decrement(*createNested() = val, weight); } - void decrement(float val, int32_t weight = 1) - { decrement(*createNested() = val, weight); } - void decrement(double val, int32_t weight = 1) - { decrement(*createNested() = val, weight); } - DECLARE_IDENTIFIABLE_ABSTRACT(WeightedSetFieldValue); - }; } // document diff --git a/document/src/vespa/document/test/fieldvalue_helpers.h b/document/src/vespa/document/test/fieldvalue_helpers.h new file mode 100644 index 00000000000..539ee50d2ab --- /dev/null +++ b/document/src/vespa/document/test/fieldvalue_helpers.h @@ -0,0 +1,68 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/document/fieldvalue/collectionfieldvalue.h> +#include <vespa/document/fieldvalue/weightedsetfieldvalue.h> + +namespace document { + +class CollectionHelper { +public: + CollectionHelper(CollectionFieldValue &value) : _cfv(value) {} + + // Convenience functions for using primitives directly + bool add(vespalib::stringref val) { return _cfv.add(*_cfv.createNested() = val); } + bool add(int32_t val) { return _cfv.add(*_cfv.createNested() = val); } + bool add(int64_t val) { return _cfv.add(*_cfv.createNested() = val); } + bool add(float val) { return _cfv.add(*_cfv.createNested() = val); } + bool add(double val) { return _cfv.add(*_cfv.createNested() = val); } + + bool contains(vespalib::stringref val) { return _cfv.contains(*_cfv.createNested() = val); } + bool contains(int32_t val) { return _cfv.contains(*_cfv.createNested() = val); } + bool contains(int64_t val) { return _cfv.contains(*_cfv.createNested() = val); } + bool contains(float val) { return _cfv.contains(*_cfv.createNested() = val); } + bool contains(double val) { return _cfv.contains(*_cfv.createNested() = val); } + + bool remove(vespalib::stringref val) { return _cfv.remove(*_cfv.createNested() = val); } + bool remove(int32_t val) { return _cfv.remove(*_cfv.createNested() = val); } + bool remove(int64_t val) { return _cfv.remove(*_cfv.createNested() = val); } + bool remove(float val) { return _cfv.remove(*_cfv.createNested() = val); } + bool remove(double val) { return _cfv.remove(*_cfv.createNested() = val); } +private: + CollectionFieldValue & _cfv; +}; + +class WSetHelper { +public: + WSetHelper(WeightedSetFieldValue & ws) : _ws(ws) { } + + // Utility functions for easy use of weighted sets of primitives + + bool add(vespalib::stringref val, int32_t weight = 1) { return _ws.add(*_ws.createNested() = val, weight); } + bool add(int32_t val, int32_t weight = 1) { return _ws.add(*_ws.createNested() = val, weight); } + bool add(int64_t val, int32_t weight = 1) { return _ws.add(*_ws.createNested() = val, weight); } + bool add(float val, int32_t weight = 1) { return _ws.add(*_ws.createNested() = val, weight); } + bool add(double val, int32_t weight = 1) { return _ws.add(*_ws.createNested() = val, weight); } + + int32_t get(vespalib::stringref val) const { return _ws.get(*_ws.createNested() = val); } + int32_t get(int32_t val) const { return _ws.get(*_ws.createNested() = val); } + int32_t get(int64_t val) const { return _ws.get(*_ws.createNested() = val); } + int32_t get(float val) const { return _ws.get(*_ws.createNested() = val); } + int32_t get(double val) const { return _ws.get(*_ws.createNested() = val); } + + void increment(vespalib::stringref val, int32_t weight = 1) { _ws.increment(*_ws.createNested() = val, weight); } + void increment(int32_t val, int32_t weight = 1) { _ws.increment(*_ws.createNested() = val, weight); } + void increment(int64_t val, int32_t weight = 1) { _ws.increment(*_ws.createNested() = val, weight); } + void increment(float val, int32_t weight = 1) { _ws.increment(*_ws.createNested() = val, weight); } + void increment(double val, int32_t weight = 1) { _ws.increment(*_ws.createNested() = val, weight); } + + void decrement(vespalib::stringref val, int32_t weight = 1) { _ws.decrement(*_ws.createNested() = val, weight); } + void decrement(int32_t val, int32_t weight = 1) { _ws.decrement(*_ws.createNested() = val, weight); } + void decrement(int64_t val, int32_t weight = 1) { _ws.decrement(*_ws.createNested() = val, weight); } + void decrement(float val, int32_t weight = 1) { _ws.decrement(*_ws.createNested() = val, weight); } + void decrement(double val, int32_t weight = 1) { _ws.decrement(*_ws.createNested() = val, weight); } +private: + WeightedSetFieldValue & _ws; +}; +} diff --git a/searchcore/src/tests/proton/server/documentretriever_test.cpp b/searchcore/src/tests/proton/server/documentretriever_test.cpp index e631388c9b8..bf0d2881675 100644 --- a/searchcore/src/tests/proton/server/documentretriever_test.cpp +++ b/searchcore/src/tests/proton/server/documentretriever_test.cpp @@ -20,6 +20,7 @@ #include <vespa/document/fieldset/fieldsets.h> #include <vespa/document/repo/configbuilder.h> #include <vespa/document/repo/documenttyperepo.h> +#include <vespa/document/test/fieldvalue_helpers.h> #include <vespa/eval/eval/simple_value.h> #include <vespa/eval/eval/tensor_spec.h> #include <vespa/eval/eval/value.h> @@ -66,6 +67,7 @@ using document::StructFieldValue; using document::TensorDataType; using document::TensorFieldValue; using document::WeightedSetFieldValue; +using document::WSetHelper; using search::AttributeFactory; using search::AttributeGuard; using search::AttributeVector; @@ -430,10 +432,11 @@ template <typename T> void checkWset(FieldValue::UP wset, T v) { ASSERT_TRUE(wset); auto *wset_val = dynamic_cast<WeightedSetFieldValue *>(wset.get()); + WSetHelper val(*wset_val); ASSERT_TRUE(wset_val); ASSERT_EQUAL(2u, wset_val->size()); - EXPECT_EQUAL(dyn_weight, wset_val->get(v)); - EXPECT_EQUAL(dyn_weight, wset_val->get(v + 1)); + EXPECT_EQUAL(dyn_weight, val.get(v)); + EXPECT_EQUAL(dyn_weight, val.get(v + 1)); } TEST_F("require that attributes are patched into stored document", Fixture) { |