diff options
author | Haavard <havardpe@yahoo-inc.com> | 2017-01-23 12:14:40 +0000 |
---|---|---|
committer | Haavard <havardpe@yahoo-inc.com> | 2017-01-23 12:14:40 +0000 |
commit | 145659f1d677face587b710726285df872a319c0 (patch) | |
tree | 074eafbf9d3b9ee030ff2ec584667b0386f37618 /eval/src/tests/tensor/tensor_slime_serialization | |
parent | 31690a1baa64d046d7ba25510b4570aa20792134 (diff) |
move code
Diffstat (limited to 'eval/src/tests/tensor/tensor_slime_serialization')
4 files changed, 196 insertions, 0 deletions
diff --git a/eval/src/tests/tensor/tensor_slime_serialization/.gitignore b/eval/src/tests/tensor/tensor_slime_serialization/.gitignore new file mode 100644 index 00000000000..9cb3b664d58 --- /dev/null +++ b/eval/src/tests/tensor/tensor_slime_serialization/.gitignore @@ -0,0 +1 @@ +vespalib_tensor_slime_serialization_test_app diff --git a/eval/src/tests/tensor/tensor_slime_serialization/CMakeLists.txt b/eval/src/tests/tensor/tensor_slime_serialization/CMakeLists.txt new file mode 100644 index 00000000000..a0323928fd3 --- /dev/null +++ b/eval/src/tests/tensor/tensor_slime_serialization/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_tensor_slime_serialization_test_app TEST + SOURCES + tensor_slime_serialization_test.cpp + DEPENDS + vespalib + vespalib_vespalib_tensor +) +vespa_add_test(NAME vespalib_tensor_slime_serialization_test_app COMMAND vespalib_tensor_slime_serialization_test_app) diff --git a/eval/src/tests/tensor/tensor_slime_serialization/FILES b/eval/src/tests/tensor/tensor_slime_serialization/FILES new file mode 100644 index 00000000000..874f951beb5 --- /dev/null +++ b/eval/src/tests/tensor/tensor_slime_serialization/FILES @@ -0,0 +1 @@ +tensor_slime_serialization_test.cpp diff --git a/eval/src/tests/tensor/tensor_slime_serialization/tensor_slime_serialization_test.cpp b/eval/src/tests/tensor/tensor_slime_serialization/tensor_slime_serialization_test.cpp new file mode 100644 index 00000000000..f3005a21730 --- /dev/null +++ b/eval/src/tests/tensor/tensor_slime_serialization/tensor_slime_serialization_test.cpp @@ -0,0 +1,185 @@ +// Copyright 2016 Yahoo Inc. 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/tensor/sparse/sparse_tensor.h> +#include <vespa/vespalib/tensor/sparse/sparse_tensor_builder.h> +#include <vespa/vespalib/tensor/types.h> +#include <vespa/vespalib/tensor/default_tensor.h> +#include <vespa/vespalib/tensor/tensor_factory.h> +#include <vespa/vespalib/tensor/serialization/typed_binary_format.h> +#include <vespa/vespalib/tensor/serialization/slime_binary_format.h> +#include <vespa/vespalib/data/slime/slime.h> +#include <iostream> + +using namespace vespalib::tensor; + +template <typename BuilderType> +struct Fixture +{ + BuilderType _builder; + Fixture() : _builder() {} + + Tensor::UP createTensor(const TensorCells &cells) { + return vespalib::tensor::TensorFactory::create(cells, _builder); + } + Tensor::UP createTensor(const TensorCells &cells, const TensorDimensions &dimensions) { + return TensorFactory::create(cells, dimensions, _builder); + } + + static inline uint32_t getTensorTypeId(); + + void assertSerialized(const vespalib::string &exp, const TensorCells &rhs, + const TensorDimensions &rhsDimensions) { + Tensor::UP rhsTensor(createTensor(rhs, rhsDimensions)); + auto slime = SlimeBinaryFormat::serialize(*rhsTensor); + vespalib::slime::Memory memory_exp(exp); + vespalib::Slime expSlime; + size_t used = vespalib::slime::JsonFormat::decode(memory_exp, expSlime); + EXPECT_EQUAL(used, memory_exp.size); + EXPECT_EQUAL(expSlime, *slime); + } +}; + +template <> +uint32_t +Fixture<SparseTensorBuilder>::getTensorTypeId() { return 2u; } + + +using SparseFixture = Fixture<SparseTensorBuilder>; + + +namespace { +vespalib::string twoCellsJson[3] = +{ + "{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { y:'3'}, value: 4.0 }," + "{ address: { x:'1'}, value: 3.0 }" + "] }", + "{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x:'1'}, value: 3.0 }," + "{ address: { y:'3'}, value: 4.0 }" + "] }", + "{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x:'1'}, value: 3.0 }," + "{ address: { y:'3'}, value: 4.0 }" + "] }", +}; +} + + +template <typename FixtureType> +void +testTensorSlimeSerialization(FixtureType &f) +{ + TEST_DO(f.assertSerialized("{ dimensions: [], cells: [] }", {}, {})); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x' ], cells: [] }", + {}, { "x" })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ], cells: [] }", + {}, { "x", "y" })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x' ]," + "cells: [" + "{ address: { x: '1' }, value: 3.0 }" + "] }", + { {{{"x","1"}}, 3} }, { "x" })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { }, value: 3.0 }" + "] }", + { {{}, 3} }, { "x", "y"})); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x: '1' }, value: 3.0 }" + "] }", + { {{{"x","1"}}, 3} }, { "x", "y" })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { y: '3' }, value: 3.0 }" + "] }", + { {{{"y","3"}}, 3} }, { "x", "y" })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x:'2', y:'4'}, value: 3.0 }" + "] }", + { {{{"x","2"}, {"y", "4"}}, 3} }, { "x", "y" })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x:'1'}, value: 3.0 }" + "] }", + { {{{"x","1"}}, 3} }, {"x", "y"})); + TEST_DO(f.assertSerialized(twoCellsJson[FixtureType::getTensorTypeId()], + { {{{"x","1"}}, 3}, {{{"y","3"}}, 4} }, + {"x", "y"})); +} + +TEST_F("test tensor slime serialization for SparseTensor", SparseFixture) +{ + testTensorSlimeSerialization(f); +} + + +struct DenseFixture +{ + DenseFixture() {} + + Tensor::UP createTensor(const DenseTensorCells &cells) { + return vespalib::tensor::TensorFactory::createDense(cells); + } + + void assertSerialized(const vespalib::string &exp, + const DenseTensorCells &rhs) { + Tensor::UP rhsTensor(createTensor(rhs)); + auto slime = SlimeBinaryFormat::serialize(*rhsTensor); + vespalib::slime::Memory memory_exp(exp); + vespalib::Slime expSlime; + size_t used = vespalib::slime::JsonFormat::decode(memory_exp, expSlime); + EXPECT_EQUAL(used, memory_exp.size); + EXPECT_EQUAL(expSlime, *slime); + } +}; + + +TEST_F("test tensor slime serialization for DenseTensor", DenseFixture) +{ + TEST_DO(f.assertSerialized("{ dimensions: [], cells: [" + "{ address: { }, value: 0.0 }" + "] }", {})); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x' ], cells: [" + "{ address: { x: '0' }, value: 0.0 }" + "] }", + { {{{"x",0}}, 0} })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ], cells: [" + "{ address: { x: '0', y: '0' }, value: 0.0 }" + "] }", + { {{{"x",0},{"y",0}}, 0} })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x' ]," + "cells: [" + "{ address: { x: '0' }, value: 0.0 }," + "{ address: { x: '1' }, value: 3.0 }" + "] }", + { {{{"x",1}}, 3} })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x: '0', y: '0' }, value: 3.0 }" + "] }", + { {{{"x",0},{"y",0}}, 3} })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x: '0', y: '0' }, value: 0.0 }," + "{ address: { x: '1', y: '0' }, value: 3.0 }" + "] }", + { {{{"x",1},{"y", 0}}, 3} })); + TEST_DO(f.assertSerialized("{ dimensions: [ 'x', 'y' ]," + " cells: [" + "{ address: { x: '0', y: '0' }, value: 0.0 }," + "{ address: { x: '0', y: '1' }, value: 0.0 }," + "{ address: { x: '0', y: '2' }, value: 0.0 }," + "{ address: { x: '0', y: '3' }, value: 3.0 }" + "] }", + { {{{"x",0},{"y",3}}, 3} })); +} + + +TEST_MAIN() { TEST_RUN_ALL(); } |