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/misc | |
parent | 3ad689f0803519e343c36d5700646e86059df961 (diff) | |
download | nnfw-tizen_7.0_hotfix.tar.gz nnfw-tizen_7.0_hotfix.tar.bz2 nnfw-tizen_7.0_hotfix.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/misc')
-rw-r--r-- | runtime/libs/misc/CMakeLists.txt | 19 | ||||
-rw-r--r-- | runtime/libs/misc/include/misc/EnvConfigSource.h | 41 | ||||
-rw-r--r-- | runtime/libs/misc/include/misc/GeneralConfigSource.h | 44 | ||||
-rw-r--r-- | runtime/libs/misc/include/misc/IConfigSource.h | 46 | ||||
-rw-r--r-- | runtime/libs/misc/include/misc/string_helpers.h | 2 | ||||
-rw-r--r-- | runtime/libs/misc/src/EnvConfigSource.cpp | 40 | ||||
-rw-r--r-- | runtime/libs/misc/src/GeneralConfigSource.cpp | 40 | ||||
-rw-r--r-- | runtime/libs/misc/src/string_helpers.test.cpp | 81 | ||||
-rw-r--r-- | runtime/libs/misc/src/tensor/IndexEnumerator.test.cpp | 59 | ||||
-rw-r--r-- | runtime/libs/misc/src/tensor/IndexIterator.test.cpp (renamed from runtime/libs/misc/examples/tensor_index_iterator.cpp) | 37 |
10 files changed, 379 insertions, 30 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(); } |