diff options
author | Chunseok Lee <chunseok.lee@samsung.com> | 2022-09-07 19:04:21 +0900 |
---|---|---|
committer | Chunseok Lee <chunseok.lee@samsung.com> | 2022-09-07 19:04:21 +0900 |
commit | c690d52bdd137ed6a17353aa7af35e8141ece77b (patch) | |
tree | dbb7dd99133132dfbffcb8c9e9af4f1ffc2f4808 /runtime/libs | |
parent | 3ad689f0803519e343c36d5700646e86059df961 (diff) | |
download | nnfw-c690d52bdd137ed6a17353aa7af35e8141ece77b.tar.gz nnfw-c690d52bdd137ed6a17353aa7af35e8141ece77b.tar.bz2 nnfw-c690d52bdd137ed6a17353aa7af35e8141ece77b.zip |
Imported Upstream version 1.21.0upstream/1.21.0tizen_7.0_m2_releaseaccepted/tizen/unified/20220912.170817accepted/tizen/unified/20220912.164738accepted/tizen/7.0/unified/hotfix/20221116.105341accepted/tizen/7.0/unified/20221110.060236tizen_7.0_hotfixtizen_7.0accepted/tizen_7.0_unified_hotfixaccepted/tizen_7.0_unified
Diffstat (limited to 'runtime/libs')
17 files changed, 1041 insertions, 260 deletions
diff --git a/runtime/libs/misc/CMakeLists.txt b/runtime/libs/misc/CMakeLists.txt index 557d403ec..69d6a9208 100644 --- a/runtime/libs/misc/CMakeLists.txt +++ b/runtime/libs/misc/CMakeLists.txt @@ -1,11 +1,22 @@ # Library `nnfw_lib_misc` -file(GLOB_RECURSE NNFW_UTILITY_SRCS "src/*.cpp") +file(GLOB_RECURSE SOURCES "src/*.cpp") +file(GLOB_RECURSE TESTS "src/*.test.cpp") +list(REMOVE_ITEM SOURCES ${TESTS}) -add_library(nnfw_lib_misc STATIC ${NNFW_UTILITY_SRCS}) +add_library(nnfw_lib_misc STATIC ${SOURCES}) target_include_directories(nnfw_lib_misc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) set_target_properties(nnfw_lib_misc PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(nnfw_lib_misc PRIVATE nnfw_common) target_link_libraries(nnfw_lib_misc PRIVATE nnfw_coverage) -add_executable(nnfw_tensor_index_iterator "examples/tensor_index_iterator.cpp") -target_link_libraries(nnfw_tensor_index_iterator nnfw_lib_misc) +if(NOT ENABLE_TEST) + return() +endif(NOT ENABLE_TEST) + +add_executable(nnfw_lib_misc_test ${TESTS}) +target_link_libraries(nnfw_lib_misc_test PRIVATE nnfw_lib_misc) +target_link_libraries(nnfw_lib_misc_test PRIVATE nnfw_coverage) +target_link_libraries(nnfw_lib_misc_test PUBLIC gtest gtest_main ${LIB_PTHREAD}) + +add_test(nnfw_lib_misc_test nnfw_lib_misc_test) +install(TARGETS nnfw_lib_misc_test DESTINATION unittest_standalone) diff --git a/runtime/libs/misc/include/misc/EnvConfigSource.h b/runtime/libs/misc/include/misc/EnvConfigSource.h new file mode 100644 index 000000000..63c8ae9c0 --- /dev/null +++ b/runtime/libs/misc/include/misc/EnvConfigSource.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NNFW_MISC_ENV_CONFIG_SOURCE_H__ +#define __NNFW_MISC_ENV_CONFIG_SOURCE_H__ + +#include "GeneralConfigSource.h" + +#include <unordered_map> + +namespace nnfw +{ +namespace misc +{ + +class EnvConfigSource final : public GeneralConfigSource +{ +public: + std::string get(const std::string &key) const override; + +private: + std::unordered_map<std::string, std::string> _default_attributes; +}; + +} // namespace misc +} // namespace nnfw + +#endif // __NNFW_MISC_ENV_CONFIG_SOURCE_H__ diff --git a/runtime/libs/misc/include/misc/GeneralConfigSource.h b/runtime/libs/misc/include/misc/GeneralConfigSource.h new file mode 100644 index 000000000..a3de66e81 --- /dev/null +++ b/runtime/libs/misc/include/misc/GeneralConfigSource.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NNFW_MISC_GENERAL_CONFIG_SOURCE_H__ +#define __NNFW_MISC_GENERAL_CONFIG_SOURCE_H__ + +#include "IConfigSource.h" + +#include <unordered_map> + +namespace nnfw +{ +namespace misc +{ + +class GeneralConfigSource : public IConfigSource +{ +public: + GeneralConfigSource() = default; + + std::string get(const std::string &key) const override; + void set(const std::string &key, const std::string &val); + +private: + std::unordered_map<std::string, std::string> _map; +}; + +} // namespace misc +} // namespace nnfw + +#endif // __NNFW_MISC_GENERAL_CONFIG_SOURCE_H__ diff --git a/runtime/libs/misc/include/misc/IConfigSource.h b/runtime/libs/misc/include/misc/IConfigSource.h new file mode 100644 index 000000000..fe2c48ecf --- /dev/null +++ b/runtime/libs/misc/include/misc/IConfigSource.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NNFW_MISC_I_CONFIG_SOURCE_H__ +#define __NNFW_MISC_I_CONFIG_SOURCE_H__ + +#include <string> + +namespace nnfw +{ +namespace misc +{ + +struct IConfigSource +{ + /** + * @brief Destroy the IConfigSource object + */ + virtual ~IConfigSource() = default; + + /** + * @brief get the value for the matching key + * + * @param key string key to search + * @return string value associated with the key + */ + virtual std::string get(const std::string &key) const = 0; +}; + +} // namespace misc +} // namespace nnfw + +#endif // __NNFW_MISC_I_CONFIG_SOURCE_H__ diff --git a/runtime/libs/misc/include/misc/string_helpers.h b/runtime/libs/misc/include/misc/string_helpers.h index 46fecca71..c9d72034f 100644 --- a/runtime/libs/misc/include/misc/string_helpers.h +++ b/runtime/libs/misc/include/misc/string_helpers.h @@ -50,7 +50,7 @@ inline std::vector<std::string> split(const std::string &s, char delim) std::vector<std::string> elems; while (std::getline(ss, item, delim)) { - elems.push_back(std::move(item)); + elems.push_back(item); } return elems; } diff --git a/runtime/libs/misc/src/EnvConfigSource.cpp b/runtime/libs/misc/src/EnvConfigSource.cpp new file mode 100644 index 000000000..3abc9d196 --- /dev/null +++ b/runtime/libs/misc/src/EnvConfigSource.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "misc/EnvConfigSource.h" + +#include <cstdlib> + +namespace nnfw +{ +namespace misc +{ + +std::string EnvConfigSource::get(const std::string &key) const +{ + const char *value = std::getenv(key.c_str()); + if (value != nullptr) + { + return value; + } + else + { + return GeneralConfigSource::get(key); + } +} + +} // namespace misc +} // namespace nnfw diff --git a/runtime/libs/misc/src/GeneralConfigSource.cpp b/runtime/libs/misc/src/GeneralConfigSource.cpp new file mode 100644 index 000000000..298c1663e --- /dev/null +++ b/runtime/libs/misc/src/GeneralConfigSource.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "misc/GeneralConfigSource.h" + +namespace nnfw +{ +namespace misc +{ + +std::string GeneralConfigSource::get(const std::string &key) const +{ + auto itr = _map.find(key); + if (itr == _map.end()) + { + return ""; + } + else + { + return itr->second; + } +} + +void GeneralConfigSource::set(const std::string &key, const std::string &val) { _map[key] = val; } + +} // namespace misc +} // namespace nnfw diff --git a/runtime/libs/misc/src/string_helpers.test.cpp b/runtime/libs/misc/src/string_helpers.test.cpp new file mode 100644 index 000000000..1111425d0 --- /dev/null +++ b/runtime/libs/misc/src/string_helpers.test.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "misc/string_helpers.h" + +#include <gtest/gtest.h> + +TEST(StringHelpersTest, split) +{ + const std::string example = "abc;def;ghi"; + + auto str_vector = nnfw::misc::split(example, ';'); + + ASSERT_EQ(str_vector.size(), 3); + EXPECT_STREQ(str_vector[0].c_str(), "abc"); + EXPECT_STREQ(str_vector[1].c_str(), "def"); + EXPECT_STREQ(str_vector[2].c_str(), "ghi"); +} + +TEST(StringHelpersTest, neg_split_empty) +{ + const std::string example = ""; + + auto str_vector = nnfw::misc::split(example, ';'); + + ASSERT_EQ(str_vector.size(), 0); +} + +TEST(StringHelpersTest, neg_nonsplit) +{ + const std::string example = "abc;def;ghi"; + + auto str_vector = nnfw::misc::split(example, ':'); + + ASSERT_EQ(str_vector.size(), 1); + EXPECT_STREQ(str_vector[0].c_str(), example.c_str()); +} + +TEST(StringHelpersTest, append) +{ + auto append_str = nnfw::misc::str("abc", "-", 1); + + EXPECT_STREQ(append_str.c_str(), "abc-1"); +} + +TEST(StringHelpersTest, neg_append_nullstr) +{ + const char *null_str = nullptr; + auto append_str = nnfw::misc::str(null_str, null_str); + + ASSERT_EQ(append_str.size(), 0); +} + +TEST(StringHelpersTest, join) +{ + const std::vector<std::string> example = {"abc", "def", "ghi"}; + + auto join_str = nnfw::misc::join(example.begin(), example.end(), ";"); + EXPECT_STREQ(join_str.c_str(), "abc;def;ghi"); +} + +TEST(StringHelpersTest, neg_join_empty) +{ + const std::vector<std::string> example = {}; + + auto join_str = nnfw::misc::join(example.begin(), example.end(), ";"); + ASSERT_EQ(join_str.size(), 0); +} diff --git a/runtime/libs/misc/src/tensor/IndexEnumerator.test.cpp b/runtime/libs/misc/src/tensor/IndexEnumerator.test.cpp new file mode 100644 index 000000000..4cff6067f --- /dev/null +++ b/runtime/libs/misc/src/tensor/IndexEnumerator.test.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "misc/tensor/IndexEnumerator.h" + +#include <vector> +#include <algorithm> + +#include <gtest/gtest.h> + +using nnfw::misc::tensor::Shape; +using nnfw::misc::tensor::Index; +using nnfw::misc::tensor::IndexEnumerator; + +TEST(MiscIndexEnumeratorTest, iterate_full_range) +{ + const uint32_t H = 3; + const uint32_t W = 4; + + const Shape shape{H, W}; + + std::vector<uint32_t> count; + + count.resize(H * W, 0); + + for (IndexEnumerator e{shape}; e.valid(); e.advance()) + { + const auto &ind = e.curr(); + + ASSERT_EQ(2, ind.rank()); + count.at(ind.at(0) * W + ind.at(1)) += 1; + } + + ASSERT_TRUE(std::all_of(count.begin(), count.end(), [](uint32_t n) { return n == 1; })); +} + +TEST(MiscIndexEnumeratorTest, neg_zero_rank_shape) +{ + // Test abnormal case of empty shape + // It is expected not to throw any exception, do nothing + const Shape shape{}; + IndexEnumerator e{shape}; + ASSERT_NO_THROW(e.valid()); + ASSERT_NO_THROW(e.advance()); + SUCCEED(); +} diff --git a/runtime/libs/misc/examples/tensor_index_iterator.cpp b/runtime/libs/misc/src/tensor/IndexIterator.test.cpp index 590b433df..875786bdd 100644 --- a/runtime/libs/misc/examples/tensor_index_iterator.cpp +++ b/runtime/libs/misc/src/tensor/IndexIterator.test.cpp @@ -16,24 +16,21 @@ #include "misc/tensor/IndexIterator.h" -#include <array> +#include <gtest/gtest.h> -#include <iostream> #include <algorithm> +#include <array> -#include <cassert> +using namespace nnfw::misc::tensor; -void test_iterate(void) +TEST(MiscIndexIteratorTest, iterate) { - const nnfw::misc::tensor::Shape shape{3, 4, 7}; + const Shape shape{3, 4, 7}; std::array<int, 3 * 4 * 7> array; array.fill(0); - using nnfw::misc::tensor::Index; - using nnfw::misc::tensor::iterate; - iterate(shape) << [&](const Index &index) { assert(index.rank() == shape.rank()); @@ -50,25 +47,15 @@ void test_iterate(void) array[offset] += 1; }; - assert(std::all_of(array.begin(), array.end(), [](int num) { return num == 1; })); + ASSERT_TRUE(std::all_of(array.begin(), array.end(), [](int num) { return num == 1; })); } -int main(int argc, char **argv) +TEST(MiscIndexIteratorTest, neg_zero_rank_shape) { - test_iterate(); - - nnfw::misc::tensor::Shape shape{3, 4, 3, 4}; - - std::cout << "Iterate over tensor{3, 4, 3, 4}" << std::endl; - - nnfw::misc::tensor::iterate(shape) << [](const nnfw::misc::tensor::Index &index) { - std::cout << "rank: " << index.rank() << std::endl; - - for (uint32_t d = 0; d < index.rank(); ++d) - { - std::cout << " offset(" << d << ") = " << index.at(d) << std::endl; - } - }; + // Test abnormal case of empty shape + // It is expected not to throw any exception, do nothing + const Shape shape{}; - return 0; + ASSERT_NO_THROW(iterate(shape) << ([](const Index &index) {})); + SUCCEED(); } diff --git a/runtime/libs/ndarray/CMakeLists.txt b/runtime/libs/ndarray/CMakeLists.txt index f88f13186..cf8c5208a 100644 --- a/runtime/libs/ndarray/CMakeLists.txt +++ b/runtime/libs/ndarray/CMakeLists.txt @@ -3,8 +3,6 @@ add_library(ndarray STATIC src/Array.cpp src/ContiguousSpan.cpp) set_target_properties(ndarray PROPERTIES POSITION_INDEPENDENT_CODE ON) target_include_directories(ndarray PUBLIC include) -#can't make this private because of c++ templates -target_include_directories(ndarray PUBLIC src) option(NDARRAY_INLINE_TEMPLATES "Set to ON to disable extern declarations for common types") @@ -19,5 +17,12 @@ if(NOT ENABLE_TEST) return() endif(NOT ENABLE_TEST) -add_subdirectory(test) +add_executable(ndarray_test src/Array.test.cpp src/ContiguousSpan.test.cpp) +target_link_libraries(ndarray_test PRIVATE ndarray) +target_link_libraries(ndarray_test PRIVATE nnfw_coverage) +target_link_libraries(ndarray_test PUBLIC gtest gtest_main ${LIB_PTHREAD}) + +add_test(ndarray_test ndarray_test) +install(TARGETS ndarray_test DESTINATION unittest_standalone) + add_subdirectory(example) diff --git a/runtime/libs/ndarray/include/ndarray/Array.h b/runtime/libs/ndarray/include/ndarray/Array.h index 09e791763..568fe1c77 100644 --- a/runtime/libs/ndarray/include/ndarray/Array.h +++ b/runtime/libs/ndarray/include/ndarray/Array.h @@ -22,37 +22,21 @@ #include "ContiguousSpan.h" #include "Shape.h" -#if __cplusplus < 201402L -#include "detail/cxx14.h" //integer_sequence and make_index_dequence definitions -#else -#include <utility> -#endif - #include <algorithm> -#include <cassert> -#include <type_traits> #include <array> -#include <tuple> +#include <cassert> #include <cstddef> +#include <tuple> +#include <type_traits> +#include <utility> namespace ndarray { -// there is no index_sequence before c++14 -#if __cplusplus < 201402L - -template <size_t... Nums> using index_sequence = cxx14::index_sequence<Nums...>; - -template <size_t Num> using make_index_sequence = cxx14::make_index_sequence<Num>; - -#else - template <size_t... Nums> using index_sequence = std::index_sequence<Nums...>; template <size_t _Num> using make_index_sequence = std::make_index_sequence<_Num>; -#endif //__cplusplus < 201402L - struct Strides { explicit Strides(Shape s) : _strides{} { fillStrides(s); } diff --git a/runtime/libs/ndarray/src/Array.test.cpp b/runtime/libs/ndarray/src/Array.test.cpp new file mode 100644 index 000000000..15e67600d --- /dev/null +++ b/runtime/libs/ndarray/src/Array.test.cpp @@ -0,0 +1,452 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ndarray/Array.h" + +#include <gtest/gtest.h> + +using namespace ndarray; + +TEST(NDArrayArrayTests, basic_data_test) +{ + float raw_data[] = {1, 2, 3, 4}; + int32_t raw_data_int[] = {1, 2, 3, 4}; + uint32_t raw_data_uint[] = {1, 2, 3, 4}; + int8_t raw_data_int8[] = {1, 2, 3, 4}; + + Array<float> data22{raw_data, {2, 2}}; + Array<int32_t> data22_int{raw_data_int, {2, 2}}; + Array<uint32_t> data22_uint{raw_data_uint, {2, 2}}; + Array<int8_t> data22_int8{raw_data_int8, {2, 2}}; + + ASSERT_FLOAT_EQ(data22.at(0, 0), 1); + ASSERT_FLOAT_EQ(data22.at(0, 1), 2); + ASSERT_FLOAT_EQ(data22.at(1, 0), 3); + ASSERT_FLOAT_EQ(data22.at(1, 1), 4); + ASSERT_EQ(data22.shape().rank(), 2); + ASSERT_EQ(data22.shape().dim(0), 2); + ASSERT_EQ(data22.shape().dim(1), 2); + + Array<float> data14{raw_data, {1, 4}}; + ASSERT_FLOAT_EQ(data14.at(0, 0), 1); + ASSERT_FLOAT_EQ(data14.at(0, 1), 2); + ASSERT_FLOAT_EQ(data14.at(0, 2), 3); + ASSERT_FLOAT_EQ(data14.at(0, 3), 4); + ASSERT_EQ(data14.shape().rank(), 2); + ASSERT_EQ(data14.shape().dim(0), 1); + ASSERT_EQ(data14.shape().dim(1), 4); + + // <float, false> + { + ContiguousSpan<float> cs = data22.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_FLOAT_EQ(cs.at(3), 4); + + ContiguousSpan<float> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_FLOAT_EQ(cs2.at(3), 4); + + float sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<float> array_data{1, 2, 3, 4}; + auto cs3 = std::make_unique<ContiguousSpan<float>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_FLOAT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_FLOAT_EQ(cs4->at(3), 4); + } + + // <float, true> + { + ContiguousSpan<float, true> cs = data22.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_FLOAT_EQ(cs.at(3), 4); + + ContiguousSpan<float, true> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_FLOAT_EQ(cs2.at(3), 4); + + float sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_FLOAT_EQ(sum, 10); + + std::vector<float> array_data{1, 2, 3, 4}; + auto cs3 = std::make_unique<ContiguousSpan<float, true>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_FLOAT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_FLOAT_EQ(cs4->at(3), 4); + } + + // <int32_t, false> + { + ContiguousSpan<int32_t> cs = data22_int.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_EQ(cs.at(3), 4); + + ContiguousSpan<int32_t> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_EQ(cs2.at(3), 4); + + int32_t sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<int32_t> array_data{1, 2, 3, 4}; + auto cs3 = std::make_unique<ContiguousSpan<int32_t>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_EQ(cs4->at(3), 4); + } + + // <int32_t, true> + { + ContiguousSpan<int32_t, true> cs = data22_int.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_EQ(cs.at(3), 4); + + ContiguousSpan<int32_t, true> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_EQ(cs2.at(3), 4); + + int32_t sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<int32_t> array_data{1, 2, 3, 4}; + auto cs3 = + std::make_unique<ContiguousSpan<int32_t, true>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_EQ(cs4->at(3), 4); + } + + // <uint32_t, false> + { + ContiguousSpan<uint32_t> cs = data22_uint.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_EQ(cs.at(3), 4); + + ContiguousSpan<uint32_t> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_EQ(cs2.at(3), 4); + + uint32_t sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<uint32_t> array_data{1, 2, 3, 4}; + auto cs3 = std::make_unique<ContiguousSpan<uint32_t>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + } + + // <uint32_t, true> + { + ContiguousSpan<uint32_t, true> cs = data22_uint.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_EQ(cs.at(3), 4); + + ContiguousSpan<uint32_t, true> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_EQ(cs2.at(3), 4); + + uint32_t sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<uint32_t> array_data{1, 2, 3, 4}; + auto cs3 = + std::make_unique<ContiguousSpan<uint32_t, true>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_EQ(cs4->at(3), 4); + } + + // <int8_t, false> + { + ContiguousSpan<int8_t> cs = data22_int8.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_FLOAT_EQ(cs.at(3), 4); + + ContiguousSpan<int8_t> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_FLOAT_EQ(cs2.at(3), 4); + + int8_t sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<int8_t> array_data{1, 2, 3, 4}; + auto cs3 = std::make_unique<ContiguousSpan<int8_t>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_EQ(cs4->at(3), 4); + + auto cs5 = ContiguousSpan<int8_t>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs5.size(), 4); + ASSERT_EQ(cs5.at(3), 4); + } + + // <int8_t, true> + { + ContiguousSpan<int8_t, true> cs = data22_int8.flat(); + ASSERT_EQ(cs.size(), 4); + ASSERT_FLOAT_EQ(cs.at(3), 4); + + ContiguousSpan<int8_t, true> cs2 = std::move(cs); + ASSERT_EQ(cs2.size(), 4); + ASSERT_FLOAT_EQ(cs2.at(3), 4); + + int8_t sum = 0; + for (auto it = cs2.begin(); it < cs2.end(); it++) + { + sum += *it; + } + ASSERT_EQ(sum, 10); + + std::vector<int8_t> array_data{1, 2, 3, 4}; + auto cs3 = std::make_unique<ContiguousSpan<int8_t, true>>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs3->size(), 4); + ASSERT_EQ(cs3->at(3), 4); + + auto cs4 = std::move(cs3); + ASSERT_EQ(cs3, nullptr); + ASSERT_EQ(cs4->size(), 4); + ASSERT_EQ(cs4->at(3), 4); + + auto cs5 = ContiguousSpan<int8_t, true>(array_data.begin(), array_data.end()); + ASSERT_EQ(cs5.size(), 4); + ASSERT_EQ(cs5.at(3), 4); + } + + Array<float> lv = std::move(data14); + ASSERT_FLOAT_EQ(lv.at(0, 0), 1); + ASSERT_FLOAT_EQ(lv.at(0, 1), 2); + ASSERT_FLOAT_EQ(lv.at(0, 2), 3); + ASSERT_FLOAT_EQ(lv.at(0, 3), 4); +} + +TEST(NDArrayArrayTests, slice_write_test) +{ + // float + { + float raw_data[4] = {0}; + + Array<float> data22{raw_data, {2, 2}}; + + data22.slice(1) = {1, 2}; + + ASSERT_FLOAT_EQ(data22.at(0, 0), 0); + ASSERT_FLOAT_EQ(data22.at(0, 1), 0); + ASSERT_FLOAT_EQ(data22.at(1, 0), 1); + ASSERT_FLOAT_EQ(data22.at(1, 1), 2); + } + + // int32_t + { + int32_t raw_data[4] = {0}; + Array<int32_t> data22{raw_data, {2, 2}}; + + data22.slice(1) = {1, 2}; + + ASSERT_EQ(data22.at(0, 0), 0); + ASSERT_EQ(data22.at(0, 1), 0); + ASSERT_EQ(data22.at(1, 0), 1); + ASSERT_EQ(data22.at(1, 1), 2); + } + + // uint32_t + { + uint32_t raw_data[4] = {0}; + Array<uint32_t> data22{raw_data, {2, 2}}; + + data22.slice(1) = {1, 2}; + + ASSERT_EQ(data22.at(0, 0), 0); + ASSERT_EQ(data22.at(0, 1), 0); + ASSERT_EQ(data22.at(1, 0), 1); + ASSERT_EQ(data22.at(1, 1), 2); + } + + // int8_t + { + int8_t raw_data[4] = {0}; + Array<int8_t> data22{raw_data, {2, 2}}; + + data22.slice(1) = {1, 2}; + + ASSERT_EQ(data22.at(0, 0), 0); + ASSERT_EQ(data22.at(0, 1), 0); + ASSERT_EQ(data22.at(1, 0), 1); + ASSERT_EQ(data22.at(1, 1), 2); + } +} + +TEST(NDArrayArrayTests, slice_read_test) +{ + // float + { + float raw_data[4] = {1, 2, 3, 4}; + + Array<float> data22{raw_data, {2, 2}}; + + auto slice = data22.slice(1); + + ASSERT_FLOAT_EQ(slice[0], 3); + ASSERT_FLOAT_EQ(slice[1], 4); + } + + // int32_t + { + int32_t raw_data[4] = {1, 2, 3, 4}; + + Array<int32_t> data22{raw_data, {2, 2}}; + + auto slice = data22.slice(1); + + ASSERT_EQ(slice[0], 3); + ASSERT_EQ(slice[1], 4); + } + + // uint32_t + { + uint32_t raw_data[4] = {1, 2, 3, 4}; + + Array<uint32_t> data22{raw_data, {2, 2}}; + + auto slice = data22.slice(1); + + ASSERT_EQ(slice[0], 3); + ASSERT_EQ(slice[1], 4); + } + + // int8_t + { + int8_t raw_data[4] = {1, 2, 3, 4}; + + Array<int8_t> data22{raw_data, {2, 2}}; + + auto slice = data22.slice(1); + + ASSERT_EQ(slice[0], 3); + ASSERT_EQ(slice[1], 4); + } +} + +TEST(NDArrayArrayTests, multidim_test) +{ + // float + { + float raw_data[5] = {0, 1, 2, 3, 4}; + + Array<float> data22{raw_data, {1, 1, 1, 1, 5}}; + + ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 0), 0); + ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 1), 1); + ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 2), 2); + ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 3), 3); + ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 4), 4); + } + + // int32_t + { + int32_t raw_data[5] = {0, 1, 2, 3, 4}; + + Array<int32_t> data22{raw_data, {1, 1, 1, 1, 5}}; + + ASSERT_EQ(data22.at(0, 0, 0, 0, 0), 0); + ASSERT_EQ(data22.at(0, 0, 0, 0, 1), 1); + ASSERT_EQ(data22.at(0, 0, 0, 0, 2), 2); + ASSERT_EQ(data22.at(0, 0, 0, 0, 3), 3); + ASSERT_EQ(data22.at(0, 0, 0, 0, 4), 4); + } + + // uint32_t + { + uint32_t raw_data[5] = {0, 1, 2, 3, 4}; + + Array<uint32_t> data22{raw_data, {1, 1, 1, 1, 5}}; + + ASSERT_EQ(data22.at(0, 0, 0, 0, 0), 0); + ASSERT_EQ(data22.at(0, 0, 0, 0, 1), 1); + ASSERT_EQ(data22.at(0, 0, 0, 0, 2), 2); + ASSERT_EQ(data22.at(0, 0, 0, 0, 3), 3); + ASSERT_EQ(data22.at(0, 0, 0, 0, 4), 4); + } + + // int8_t + { + int8_t raw_data[5] = {0, 1, 2, 3, 4}; + + Array<int8_t> data22{raw_data, {1, 1, 1, 1, 5}}; + + ASSERT_EQ(data22.at(0, 0, 0, 0, 0), 0); + ASSERT_EQ(data22.at(0, 0, 0, 0, 1), 1); + ASSERT_EQ(data22.at(0, 0, 0, 0, 2), 2); + ASSERT_EQ(data22.at(0, 0, 0, 0, 3), 3); + ASSERT_EQ(data22.at(0, 0, 0, 0, 4), 4); + } +} diff --git a/runtime/libs/ndarray/src/ContiguousSpan.test.cpp b/runtime/libs/ndarray/src/ContiguousSpan.test.cpp new file mode 100644 index 000000000..dd1108697 --- /dev/null +++ b/runtime/libs/ndarray/src/ContiguousSpan.test.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ndarray/ContiguousSpan.h" + +#include <gtest/gtest.h> + +using namespace ndarray; + +TEST(NDArrayContiguousSpanTests, slice_assign_test) +{ + // float + { + std::vector<float> v1{1, 2, 3, 4, 5}; + std::vector<float> v2(5); + + ContiguousSpan<float> span1(v1.begin(), v1.end()); + ContiguousSpan<float> span2(v2.begin(), v2.end()); + + span2.assign(span1); + + ASSERT_EQ(v1, v2); + ASSERT_EQ(span1.size(), 5); + ASSERT_EQ(span2.size(), 5); + + ASSERT_EQ(span2.at(2), 3); + ASSERT_EQ(span2.at(4), 5); + + ASSERT_EQ(*(span1.data() + 2), *(span1.data() + 2)); + + ContiguousSpan<float> span3(span2.offset(1)); + ASSERT_EQ(span3.size(), 4); + ASSERT_EQ(span3.at(0), 2); + ASSERT_EQ(span3.at(1), 3); + ASSERT_EQ(span3[2], 4); + ASSERT_EQ(span3[3], 5); + + // const + ContiguousSpan<float, true> span4(v1.begin(), v1.end()); + ASSERT_EQ(span4.size(), 5); + ASSERT_EQ(span4.at(0), 1); + ASSERT_EQ(span4.at(1), 2); + ASSERT_EQ(span4.at(2), 3); + ASSERT_EQ(span4[3], 4); + ASSERT_EQ(span4[4], 5); + + ContiguousSpan<float, true> span5(span4.offset(1)); + ASSERT_EQ(span5.size(), 4); + ASSERT_EQ(span5.at(0), 2); + ASSERT_EQ(span5.at(1), 3); + ASSERT_EQ(span5[2], 4); + ASSERT_EQ(span5[3], 5); + + ASSERT_EQ(*(span5.data() + 2), *(span5.data() + 2)); + } + + // int32_t + { + std::vector<int32_t> v1{1, 2, 3, 4, 5}; + std::vector<int32_t> v2(5); + + ContiguousSpan<int32_t> span1(v1.begin(), v1.end()); + ContiguousSpan<int32_t> span2(v2.begin(), v2.end()); + + span2.assign(span1); + + ASSERT_EQ(v1, v2); + ASSERT_EQ(span1.size(), 5); + ASSERT_EQ(span2.size(), 5); + + ASSERT_EQ(span2.at(2), 3); + ASSERT_EQ(span2.at(4), 5); + + ASSERT_EQ(*(span1.data() + 2), *(span1.data() + 2)); + + ContiguousSpan<int32_t> span3(span2.offset(1)); + ASSERT_EQ(span3.size(), 4); + ASSERT_EQ(span3.at(0), 2); + ASSERT_EQ(span3.at(1), 3); + ASSERT_EQ(span3[2], 4); + ASSERT_EQ(span3[3], 5); + + // const + ContiguousSpan<int32_t, true> span4(v1.begin(), v1.end()); + ASSERT_EQ(span4.size(), 5); + ASSERT_EQ(span4.at(0), 1); + ASSERT_EQ(span4.at(1), 2); + ASSERT_EQ(span4.at(2), 3); + ASSERT_EQ(span4[3], 4); + ASSERT_EQ(span4[4], 5); + + ContiguousSpan<int32_t, true> span5(span4.offset(1)); + ASSERT_EQ(span5.size(), 4); + ASSERT_EQ(span5.at(0), 2); + ASSERT_EQ(span5.at(1), 3); + ASSERT_EQ(span5[2], 4); + ASSERT_EQ(span5[3], 5); + } + + // uint32_t + { + std::vector<uint32_t> v1{1, 2, 3, 4, 5}; + std::vector<uint32_t> v2(5); + + ContiguousSpan<uint32_t> span1(v1.begin(), v1.end()); + ContiguousSpan<uint32_t> span2(v2.begin(), v2.end()); + + span2.assign(span1); + + ASSERT_EQ(v1, v2); + ASSERT_EQ(span1.size(), 5); + ASSERT_EQ(span2.size(), 5); + + ASSERT_EQ(span2.at(2), 3); + ASSERT_EQ(span2.at(4), 5); + + ASSERT_EQ(*(span1.data() + 2), *(span1.data() + 2)); + + ContiguousSpan<uint32_t> span3(span2.offset(1)); + ASSERT_EQ(span3.size(), 4); + ASSERT_EQ(span3.at(0), 2); + ASSERT_EQ(span3.at(1), 3); + ASSERT_EQ(span3[2], 4); + ASSERT_EQ(span3[3], 5); + + // const + ContiguousSpan<uint32_t, true> span4(v1.begin(), v1.end()); + ASSERT_EQ(span4.size(), 5); + ASSERT_EQ(span4.at(0), 1); + ASSERT_EQ(span4.at(1), 2); + ASSERT_EQ(span4.at(2), 3); + ASSERT_EQ(span4[3], 4); + ASSERT_EQ(span4[4], 5); + + ContiguousSpan<uint32_t, true> span5(span4.offset(1)); + ASSERT_EQ(span5.size(), 4); + ASSERT_EQ(span5.at(0), 2); + ASSERT_EQ(span5.at(1), 3); + ASSERT_EQ(span5[2], 4); + ASSERT_EQ(span5[3], 5); + } + + // int8_t + { + std::vector<int8_t> v1{1, 2, 3, 4, 5}; + std::vector<int8_t> v2(5); + + ContiguousSpan<int8_t> span1(v1.begin(), v1.end()); + ContiguousSpan<int8_t> span2(v2.begin(), v2.end()); + + span2.assign(span1); + + ASSERT_EQ(v1, v2); + ASSERT_EQ(span1.size(), 5); + ASSERT_EQ(span2.size(), 5); + + ASSERT_EQ(span2.at(2), 3); + ASSERT_EQ(span2.at(4), 5); + + ASSERT_EQ(*(span1.data() + 2), *(span1.data() + 2)); + + ContiguousSpan<int8_t> span3(span2.offset(1)); + ASSERT_EQ(span3.size(), 4); + ASSERT_EQ(span3.at(0), 2); + ASSERT_EQ(span3.at(1), 3); + ASSERT_EQ(span3[2], 4); + ASSERT_EQ(span3[3], 5); + + // const + ContiguousSpan<int8_t, true> span4(v1.begin(), v1.end()); + ASSERT_EQ(span4.size(), 5); + ASSERT_EQ(span4.at(0), 1); + ASSERT_EQ(span4.at(1), 2); + ASSERT_EQ(span4.at(2), 3); + ASSERT_EQ(span4[3], 4); + ASSERT_EQ(span4[4], 5); + + ContiguousSpan<int8_t, true> span5(span4.offset(1)); + ASSERT_EQ(span5.size(), 4); + ASSERT_EQ(span5.at(0), 2); + ASSERT_EQ(span5.at(1), 3); + ASSERT_EQ(span5[2], 4); + ASSERT_EQ(span5[3], 5); + } +} diff --git a/runtime/libs/ndarray/src/detail/cxx14.h b/runtime/libs/ndarray/src/detail/cxx14.h deleted file mode 100644 index 8b78fb985..000000000 --- a/runtime/libs/ndarray/src/detail/cxx14.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _NDARRAY_CXX14_H_ -#define _NDARRAY_CXX14_H_ - -namespace ndarray -{ - -namespace cxx14 -{ - -template <size_t... Nums> struct index_sequence -{ - using value_type = size_t; - - static constexpr std::size_t size() noexcept { return sizeof...(Nums); } -}; - -namespace detail -{ - -template <size_t v, typename Seq> struct _append; - -template <size_t v, size_t... Nums> struct _append<v, index_sequence<Nums...>> -{ - using result = index_sequence<Nums..., v>; -}; - -template <size_t Len> struct make_index_sequence -{ - using result = - typename detail::_append<Len - 1, typename make_index_sequence<Len - 1>::result>::result; -}; - -template <> struct make_index_sequence<1> -{ - using result = index_sequence<0>; -}; - -template <> struct make_index_sequence<0> -{ - using result = index_sequence<>; -}; - -} // namespace detail - -template <size_t Num> using make_index_sequence = typename detail::make_index_sequence<Num>::result; - -} // namespace cxx14 - -} // namespace ndarray - -#endif //_NDARRAY_CXX14_H_ diff --git a/runtime/libs/ndarray/test/CMakeLists.txt b/runtime/libs/ndarray/test/CMakeLists.txt deleted file mode 100644 index be1ed6510..000000000 --- a/runtime/libs/ndarray/test/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -if(NOT TARGET ndarray) - return() -endif() - -add_executable(ndarray_test ndarray_test.cpp) - -target_link_libraries(ndarray_test PRIVATE ndarray) - -nnfw_find_package(GTest) -if(NOT GTest_FOUND) - message(STATUS "GTest not avaialble. Skipping NDArray test build") - return() -endif(NOT GTest_FOUND) - -target_link_libraries(ndarray_test PUBLIC gtest gtest_main ${LIB_PTHREAD}) - -add_test(ndarray_test ndarray_test) -install(TARGETS ndarray_test DESTINATION unittest_standalone) diff --git a/runtime/libs/ndarray/test/ndarray_test.cpp b/runtime/libs/ndarray/test/ndarray_test.cpp deleted file mode 100644 index 4b5ad5765..000000000 --- a/runtime/libs/ndarray/test/ndarray_test.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "gtest/gtest.h" - -#include "ndarray/Array.h" - -using namespace ndarray; - -TEST(NDArray_tests, basic_data_test) -{ - - float raw_data[] = {1, 2, 3, 4}; - - Array<float> data22{raw_data, {2, 2}}; - - ASSERT_FLOAT_EQ(data22.at(0, 0), 1); - ASSERT_FLOAT_EQ(data22.at(0, 1), 2); - ASSERT_FLOAT_EQ(data22.at(1, 0), 3); - ASSERT_FLOAT_EQ(data22.at(1, 1), 4); - ASSERT_EQ(data22.shape().rank(), 2); - ASSERT_EQ(data22.shape().dim(0), 2); - ASSERT_EQ(data22.shape().dim(1), 2); - - Array<float> data14{raw_data, {1, 4}}; - ASSERT_FLOAT_EQ(data14.at(0, 0), 1); - ASSERT_FLOAT_EQ(data14.at(0, 1), 2); - ASSERT_FLOAT_EQ(data14.at(0, 2), 3); - ASSERT_FLOAT_EQ(data14.at(0, 3), 4); - ASSERT_EQ(data14.shape().rank(), 2); - ASSERT_EQ(data14.shape().dim(0), 1); - ASSERT_EQ(data14.shape().dim(1), 4); - - ContiguousSpan<float> cs = data22.flat(); - ASSERT_EQ(cs.size(), 4); - ASSERT_FLOAT_EQ(cs.at(3), 4); - - Array<float> lv = std::move(data14); - ASSERT_FLOAT_EQ(lv.at(0, 0), 1); - ASSERT_FLOAT_EQ(lv.at(0, 1), 2); - ASSERT_FLOAT_EQ(lv.at(0, 2), 3); - ASSERT_FLOAT_EQ(lv.at(0, 3), 4); -} - -TEST(NDArray_tests, slice_write_test) -{ - float raw_data[4] = {0}; - - Array<float> data22{raw_data, {2, 2}}; - - data22.slice(1) = {1, 2}; - - ASSERT_FLOAT_EQ(data22.at(0, 0), 0); - ASSERT_FLOAT_EQ(data22.at(0, 1), 0); - ASSERT_FLOAT_EQ(data22.at(1, 0), 1); - ASSERT_FLOAT_EQ(data22.at(1, 1), 2); -} - -TEST(NDArray_tests, slice_read_test) -{ - float raw_data[4] = {1, 2, 3, 4}; - - Array<float> data22{raw_data, {2, 2}}; - - auto slice = data22.slice(1); - - ASSERT_FLOAT_EQ(slice[0], 3); - ASSERT_FLOAT_EQ(slice[1], 4); -} - -TEST(NDArray_tests, multidim_test) -{ - float raw_data[5] = {0, 1, 2, 3, 4}; - - Array<float> data22{raw_data, {1, 1, 1, 1, 5}}; - - ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 0), 0); - ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 1), 1); - ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 2), 2); - ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 3), 3); - ASSERT_FLOAT_EQ(data22.at(0, 0, 0, 0, 4), 4); -} - -TEST(NDArray_tests, slice_assign_test) -{ - std::vector<float> v1{1, 2, 3, 4, 5}; - std::vector<float> v2(5); - - ContiguousSpan<float> span1(v1.begin(), v1.end()); - ContiguousSpan<float> span2(v2.begin(), v2.end()); - - span2.assign(span1); - - ASSERT_EQ(v1, v2); - ASSERT_EQ(span1.size(), 5); - ASSERT_EQ(span2.size(), 5); - - ASSERT_EQ(span2.at(2), 3); - ASSERT_EQ(span2.at(4), 5); - - ASSERT_EQ(*(span1.data() + 2), *(span1.data() + 2)); - - ContiguousSpan<float> span3(span2.offset(1)); - ASSERT_EQ(span3.size(), 4); - ASSERT_EQ(span3.at(0), 2); - ASSERT_EQ(span3.at(1), 3); - ASSERT_EQ(span3.at(2), 4); - ASSERT_EQ(span3.at(3), 5); -} |