summaryrefslogtreecommitdiffstats
path: root/eval
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-10-01 06:08:12 +0000
committerArne Juul <arnej@verizonmedia.com>2020-10-01 06:08:12 +0000
commita23cb1bb18e4130652bcf28d088e644b88d69ce3 (patch)
tree6de3add43c4c3f6c0f998758f048ff5c2dfde472 /eval
parent7bfba51845a84f8ac876780182026bcc05a39159 (diff)
also extend test with bad input data
Diffstat (limited to 'eval')
-rw-r--r--eval/src/tests/eval/value_codec/value_codec_test.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/eval/src/tests/eval/value_codec/value_codec_test.cpp b/eval/src/tests/eval/value_codec/value_codec_test.cpp
index b742a802382..9c87a3384c4 100644
--- a/eval/src/tests/eval/value_codec/value_codec_test.cpp
+++ b/eval/src/tests/eval/value_codec/value_codec_test.cpp
@@ -7,6 +7,7 @@
#include <vespa/vespalib/data/memory.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/objects/nbostream.h>
+#include <vespa/vespalib/util/exceptions.h>
using namespace vespalib;
using namespace vespalib::eval;
@@ -281,4 +282,114 @@ TEST(ValueCodecTest, mixed_tensors_can_be_encoded_and_decoded) {
//-----------------------------------------------------------------------------
+struct BadSparseTensorExample : TensorExample {
+ TensorSpec make_spec(bool use_float) const override {
+ return TensorSpec(make_type_spec(use_float, "(x{},y{})"))
+ .add({{"x","a"},{"y","a"}}, 1)
+ .add({{"x","b"},{"y","a"}}, 3);
+ }
+ std::unique_ptr<Value> make_tensor(bool use_float) const override {
+ return value_from_spec(make_spec(use_float), factory);
+ }
+ template <typename T>
+ void encode_inner(nbostream &dst) const {
+ dst.putInt1_4Bytes(2);
+ dst.writeSmallString("x");
+ dst.writeSmallString("y");
+ dst.putInt1_4Bytes(12345678);
+ dst.writeSmallString("a");
+ dst.writeSmallString("a");
+ dst << (T) 1;
+ dst.writeSmallString("b");
+ dst.writeSmallString("a");
+ dst << (T) 3;
+ }
+ void encode_default(nbostream &dst) const override {
+ dst.putInt1_4Bytes(1);
+ encode_inner<double>(dst);
+ }
+ void encode_with_double(nbostream &dst) const override {
+ dst.putInt1_4Bytes(5);
+ dst.putInt1_4Bytes(0);
+ encode_inner<double>(dst);
+ }
+ void encode_with_float(nbostream &dst) const override {
+ dst.putInt1_4Bytes(5);
+ dst.putInt1_4Bytes(1);
+ encode_inner<float>(dst);
+ }
+};
+
+TEST(ValueCodecTest, bad_sparse_tensors_are_caught) {
+ BadSparseTensorExample bad;
+ nbostream data_default;
+ nbostream data_double;
+ nbostream data_float;
+ bad.encode_default(data_default);
+ bad.encode_with_double(data_double);
+ bad.encode_with_float(data_float);
+ EXPECT_EXCEPTION(decode_value(data_default, factory), vespalib::IllegalStateException,
+ "serialized input claims 12345678 blocks of size 1*8, but only");
+ EXPECT_EXCEPTION(decode_value(data_double, factory), vespalib::IllegalStateException,
+ "serialized input claims 12345678 blocks of size 1*8, but only");
+ EXPECT_EXCEPTION(decode_value(data_float, factory), vespalib::IllegalStateException,
+ "serialized input claims 12345678 blocks of size 1*4, but only");
+}
+
+//-----------------------------------------------------------------------------
+
+struct BadDenseTensorExample : TensorExample {
+ TensorSpec make_spec(bool use_float) const override {
+ return TensorSpec(make_type_spec(use_float, "(x[3],y[2])"))
+ .add({{"x",0},{"y",0}}, 1)
+ .add({{"x",2},{"y",1}}, 6);
+ }
+ std::unique_ptr<Value> make_tensor(bool use_float) const override {
+ return value_from_spec(make_spec(use_float), factory);
+ }
+ template <typename T>
+ void encode_inner(nbostream &dst) const {
+ dst.putInt1_4Bytes(2);
+ dst.writeSmallString("x");
+ dst.putInt1_4Bytes(300);
+ dst.writeSmallString("y");
+ dst.putInt1_4Bytes(200);
+ dst << (T) 1;
+ dst << (T) 6;
+ }
+ void encode_default(nbostream &dst) const override {
+ dst.putInt1_4Bytes(2);
+ encode_inner<double>(dst);
+ }
+ void encode_with_double(nbostream &dst) const override {
+ dst.putInt1_4Bytes(6);
+ dst.putInt1_4Bytes(0);
+ encode_inner<double>(dst);
+ }
+ void encode_with_float(nbostream &dst) const override {
+ dst.putInt1_4Bytes(6);
+ dst.putInt1_4Bytes(1);
+ encode_inner<float>(dst);
+ }
+};
+
+TEST(ValueCodecTest, bad_dense_tensors_are_caught) {
+ BadDenseTensorExample bad;
+ nbostream data_default;
+ nbostream data_double;
+ nbostream data_float;
+ bad.encode_default(data_default);
+ bad.encode_with_double(data_double);
+ bad.encode_with_float(data_float);
+ EXPECT_EXCEPTION(decode_value(data_default, factory), vespalib::IllegalStateException,
+ "serialized input claims 1 blocks of size 60000*8, but only");
+ EXPECT_EXCEPTION(decode_value(data_double, factory), vespalib::IllegalStateException,
+ "serialized input claims 1 blocks of size 60000*8, but only");
+ EXPECT_EXCEPTION(decode_value(data_float, factory), vespalib::IllegalStateException,
+ "serialized input claims 1 blocks of size 60000*4, but only");
+}
+
+//-----------------------------------------------------------------------------
+
+
GTEST_MAIN_RUN_ALL_TESTS()