[SimpleShot] Add test util cpp
authorJihoon Lee <jhoon.it.lee@samsung.com>
Fri, 8 Jan 2021 06:11:38 +0000 (15:11 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 2 Mar 2021 01:29:05 +0000 (10:29 +0900)
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 <jhoon.it.lee@samsung.com>
Applications/SimpleShot/meson.build
Applications/SimpleShot/simpleshot_utils.cpp [new file with mode: 0644]
Applications/SimpleShot/simpleshot_utils.h [new file with mode: 0644]
Applications/SimpleShot/test/meson.build
Applications/SimpleShot/test/mock_test.cpp [deleted file]
Applications/SimpleShot/test/simpleshot_utils.cpp [new file with mode: 0644]

index d92bdec..5b16b7d 100644 (file)
@@ -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 (file)
index 0000000..65f38a2
--- /dev/null
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2020 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @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 <jhoon.it.lee@samsung.com>
+ * @bug    No known bugs except for NYI items
+ *
+ */
+#include <iostream>
+#include <regex>
+
+#include <simpleshot_utils.h>
+
+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 (file)
index 0000000..8a26279
--- /dev/null
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2020 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @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 <jhoon.it.lee@samsung.com>
+ * @bug    No known bugs except for NYI items
+ *
+ */
+#include <string>
+
+namespace simpleshot {
+
+namespace util {
+
+struct Entry {
+  std::string key;
+  std::string value;
+};
+
+Entry getKeyValue(const std::string &input);
+
+} // namespace util
+} // namespace simpleshot
index 322f63b..07f408e 100644 (file)
@@ -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 (file)
index 676828c..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-/**
- * Copyright (C) 2020 Jihoon Lee <jhoon.it.lee@samsung.com>
- *
- * @file       mock_test.cpp
- * @date       08 Jan 2021
- * @brief      mock test for simpleshot application
- * @see                https://github.com/nnstreamer/nntrainer
- * @author     Jihoon Lee <jhoon.it.lee@samsung.com>
- * @note this will be deleted or moved as the application is building up
- * @bug                No known bugs except for NYI items
- */
-
-#include <gtest/gtest.h>
-
-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 (file)
index 0000000..8bcf10d
--- /dev/null
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2020 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file       utils_test.cpp
+ * @date       08 Jan 2021
+ * @brief      test for simpleshot utils
+ * @see                https://github.com/nnstreamer/nntrainer
+ * @author     Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug                No known bugs except for NYI items
+ */
+
+#include <gtest/gtest.h>
+#include <simpleshot_utils.h>
+
+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