diff options
author | Arne H Juul <arnej27959@users.noreply.github.com> | 2018-02-27 17:49:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-27 17:49:15 +0100 |
commit | 1fdcffcb00ab6cd3b0895dce379d9d214fca4745 (patch) | |
tree | 8d3ce9c902f79a4b5e74fb54bdd2f76acc7a3a90 | |
parent | d338264315807294252cfa5f41b52d5fc99e78fc (diff) | |
parent | c80659827c1059ef08c22b670e2d6bbb36e604ac (diff) |
Merge pull request #5163 from vespa-engine/havardpe/unconstify
added unconstify for ConstArrayRef -> ArrayRef conversion
-rw-r--r-- | vespalib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/tests/arrayref/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/arrayref/arrayref_test.cpp | 56 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/arrayref.h | 6 |
4 files changed, 71 insertions, 0 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt index 9e11562c0b0..9ef6daea91c 100644 --- a/vespalib/CMakeLists.txt +++ b/vespalib/CMakeLists.txt @@ -17,6 +17,7 @@ vespa_define_module( src/tests/approx src/tests/array src/tests/arrayqueue + src/tests/arrayref src/tests/barrier src/tests/benchmark_timer src/tests/box diff --git a/vespalib/src/tests/arrayref/CMakeLists.txt b/vespalib/src/tests/arrayref/CMakeLists.txt new file mode 100644 index 00000000000..c851ff3e277 --- /dev/null +++ b/vespalib/src/tests/arrayref/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_arrayref_test_app TEST + SOURCES + arrayref_test.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_arrayref_test_app COMMAND vespalib_arrayref_test_app) diff --git a/vespalib/src/tests/arrayref/arrayref_test.cpp b/vespalib/src/tests/arrayref/arrayref_test.cpp new file mode 100644 index 00000000000..ad11d9b6ba4 --- /dev/null +++ b/vespalib/src/tests/arrayref/arrayref_test.cpp @@ -0,0 +1,56 @@ +// Copyright 2018 Yahoo Holdings. 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/util/arrayref.h> + +using namespace vespalib; + +TEST("require that default constructors create references to empty arrays") { + ArrayRef<int> array_ref; + ConstArrayRef<int> const_ref; + EXPECT_EQUAL(array_ref.size(), 0u); + EXPECT_EQUAL(array_ref.begin(), array_ref.end()); + EXPECT_EQUAL(const_ref.size(), 0u); + EXPECT_EQUAL(const_ref.begin(), const_ref.end()); +} + +TEST("require that data can be referenced") { + std::vector<int> data({1,2,3}); + ArrayRef<int> array_ref(data); + ConstArrayRef<int> const_ref(data); + EXPECT_EQUAL(array_ref.size(), 3u); + EXPECT_EQUAL(array_ref.end() - array_ref.begin(), 3); + EXPECT_EQUAL(array_ref[1], 2); + EXPECT_EQUAL(const_ref.size(), 3u); + EXPECT_EQUAL(const_ref.end() - const_ref.begin(), 3); + EXPECT_EQUAL(const_ref[2], 3); +} + +TEST("require that non-const array ref can be written to") { + std::vector<int> data({1,2,3}); + ArrayRef<int> array_ref(data); + array_ref[1] = 5; + EXPECT_EQUAL(data[1], 5); +} + +TEST("require that references can be constified") { + std::vector<int> data({1,2,3}); + const ArrayRef<int> array_ref(data); + ConstArrayRef<int> const_ref(array_ref); + EXPECT_EQUAL(const_ref.size(), 3u); + EXPECT_EQUAL(const_ref.end() - const_ref.begin(), 3); + EXPECT_EQUAL(const_ref[2], 3); +} + +TEST("require that references can be unconstified") { + std::vector<int> data({1,2,3}); + const ConstArrayRef<int> const_ref(data); + ArrayRef<int> array_ref = unconstify(const_ref); + EXPECT_EQUAL(array_ref.size(), 3u); + EXPECT_EQUAL(array_ref.end() - array_ref.begin(), 3); + EXPECT_EQUAL(array_ref[1], 2); + array_ref[1] = 5; + EXPECT_EQUAL(data[1], 5); +} + +TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/util/arrayref.h b/vespalib/src/vespa/vespalib/util/arrayref.h index edda3d7b0c6..186316c10d3 100644 --- a/vespalib/src/vespa/vespalib/util/arrayref.h +++ b/vespalib/src/vespa/vespalib/util/arrayref.h @@ -46,4 +46,10 @@ private: size_t _sz; }; +// const-cast for array references; use with care +template <typename T> +ArrayRef<T> unconstify(const ConstArrayRef<T> &ref) { + return ArrayRef<T>(const_cast<T*>(&ref[0]), ref.size()); +} + } |