From: Jihoon Lee Date: Fri, 8 Jan 2021 06:11:38 +0000 (+0900) Subject: [SimpleShot] Add test util cpp X-Git-Tag: accepted/tizen/unified/20210305.034114~28 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7f814bbc8af391b8293a501cd1d6be92f97450e3;p=platform%2Fcore%2Fml%2Fnntrainer.git [SimpleShot] Add test util cpp Add a simple utility to the simpleshot app **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- diff --git a/Applications/SimpleShot/meson.build b/Applications/SimpleShot/meson.build index d92bdec..5b16b7d 100644 --- a/Applications/SimpleShot/meson.build +++ b/Applications/SimpleShot/meson.build @@ -1,16 +1,21 @@ simpleshot_sources = [ - 'task_runner.cpp' + 'simpleshot_utils.cpp' ] simpleshot_inc = include_directories('.') + e = executable('simpleshot_runner', - simpleshot_sources, + ['task_runner.cpp'] + simpleshot_sources, dependencies: [app_utils_dep, nntrainer_dep, tflite_dep], install: get_option('install-app'), install_dir: application_install_dir ) if get_option('enable-test') + simpleshot_test_dep = declare_dependency( + sources: simpleshot_sources, + include_directories: simpleshot_inc, + ) subdir('test') endif diff --git a/Applications/SimpleShot/simpleshot_utils.cpp b/Applications/SimpleShot/simpleshot_utils.cpp new file mode 100644 index 0000000..65f38a2 --- /dev/null +++ b/Applications/SimpleShot/simpleshot_utils.cpp @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 +/** + * Copyright (C) 2020 Jihoon Lee + * + * @file utils.cpp + * @date 08 Jan 2021 + * @brief This file contains simple utilities used across the application + * @see https://github.com/nnstreamer/nntrainer + * @author Jihoon Lee + * @bug No known bugs except for NYI items + * + */ +#include +#include + +#include + +namespace simpleshot { + +namespace util { + +Entry getKeyValue(const std::string &input) { + Entry entry; + static const std::regex words_regex("[^\\s=]+"); + + std::string input_str(input); + input_str.erase(std::remove(input_str.begin(), input_str.end(), ' '), + input_str.end()); + auto words_begin = + std::sregex_iterator(input_str.begin(), input_str.end(), words_regex); + auto words_end = std::sregex_iterator(); + + int nwords = std::distance(words_begin, words_end); + if (nwords != 2) { + throw std::invalid_argument("key, value is not found"); + } + + entry.key = words_begin->str(); + entry.value = (++words_begin)->str(); + return entry; +} + +} // namespace util +} // namespace simpleshot diff --git a/Applications/SimpleShot/simpleshot_utils.h b/Applications/SimpleShot/simpleshot_utils.h new file mode 100644 index 0000000..8a26279 --- /dev/null +++ b/Applications/SimpleShot/simpleshot_utils.h @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0 +/** + * Copyright (C) 2020 Jihoon Lee + * + * @file utils.h + * @date 08 Jan 2021 + * @brief This file contains simple utilities used across the application + * @see https://github.com/nnstreamer/nntrainer + * @author Jihoon Lee + * @bug No known bugs except for NYI items + * + */ +#include + +namespace simpleshot { + +namespace util { + +struct Entry { + std::string key; + std::string value; +}; + +Entry getKeyValue(const std::string &input); + +} // namespace util +} // namespace simpleshot diff --git a/Applications/SimpleShot/test/meson.build b/Applications/SimpleShot/test/meson.build index 322f63b..07f408e 100644 --- a/Applications/SimpleShot/test/meson.build +++ b/Applications/SimpleShot/test/meson.build @@ -2,14 +2,14 @@ gtest_dep_with_main = dependency('gtest', main : true, required : false) test_target = [ - 'mock_test', + 'simpleshot_utils' ] foreach target: test_target exe = executable( target, target + '.cpp', - dependencies: gtest_dep_with_main, + dependencies: [gtest_dep_with_main, simpleshot_test_dep], install: get_option('enable-test'), install_dir: application_install_dir ) diff --git a/Applications/SimpleShot/test/mock_test.cpp b/Applications/SimpleShot/test/mock_test.cpp deleted file mode 100644 index 676828c..0000000 --- a/Applications/SimpleShot/test/mock_test.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/** - * Copyright (C) 2020 Jihoon Lee - * - * @file mock_test.cpp - * @date 08 Jan 2021 - * @brief mock test for simpleshot application - * @see https://github.com/nnstreamer/nntrainer - * @author Jihoon Lee - * @note this will be deleted or moved as the application is building up - * @bug No known bugs except for NYI items - */ - -#include - -TEST(sample_test, test_01_p) { EXPECT_TRUE(true); } diff --git a/Applications/SimpleShot/test/simpleshot_utils.cpp b/Applications/SimpleShot/test/simpleshot_utils.cpp new file mode 100644 index 0000000..8bcf10d --- /dev/null +++ b/Applications/SimpleShot/test/simpleshot_utils.cpp @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: Apache-2.0 +/** + * Copyright (C) 2020 Jihoon Lee + * + * @file utils_test.cpp + * @date 08 Jan 2021 + * @brief test for simpleshot utils + * @see https://github.com/nnstreamer/nntrainer + * @author Jihoon Lee + * @bug No known bugs except for NYI items + */ + +#include +#include + +namespace simpleshot { +TEST(getKeyValue, parse_okay_p) { + { + util::Entry e = util::getKeyValue("abc=123"); + EXPECT_EQ(e.key, "abc"); + EXPECT_EQ(e.value, "123"); + } + { + util::Entry e = util::getKeyValue("abc = 123"); + EXPECT_EQ(e.key, "abc"); + EXPECT_EQ(e.value, "123"); + } + { + util::Entry e = util::getKeyValue("abc = 123"); + EXPECT_EQ(e.key, "abc"); + EXPECT_EQ(e.value, "123"); + } + { + util::Entry e = util::getKeyValue("abc = 123"); + EXPECT_EQ(e.key, "abc"); + EXPECT_EQ(e.value, "123"); + } +} + +TEST(getKeyValue, invalid_format_01_n) { + EXPECT_THROW(util::getKeyValue("abc"), std::invalid_argument); +} + +TEST(getKeyValue, invalid_format_02_n) { + EXPECT_THROW(util::getKeyValue("abc="), std::invalid_argument); +} + +TEST(getKeyValue, invalid_format_03_n) { + EXPECT_THROW(util::getKeyValue("abc=1=2"), std::invalid_argument); +} + +TEST(getKeyValue, invalid_format_04_n) { + EXPECT_THROW(util::getKeyValue("=12"), std::invalid_argument); +} +} // namespace simpleshot