[Refactor/Test] Move iniTest to modelfile
authorJihoon Lee <jhoon.it.lee@samsung.com>
Wed, 21 Oct 2020 02:38:19 +0000 (11:38 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 27 Oct 2020 01:15:30 +0000 (10:15 +0900)
This patch moves a test class and a factory function used in
model file test to `unittest_nntrainer_modelfile` from `test_util`

**Changes proposed in this PR:**
- Move iniTest to `unittest_nntrainer_modelfile`
- Move `mkIniTc`

**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>
test/include/nntrainer_test_util.h
test/nntrainer_test_util.cpp
test/unittest/unittest_nntrainer_modelfile.cpp

index 3b52ba1..770894f 100644 (file)
 
 class IniSection {
 public:
+  IniSection(const std::string &name) : section_name(name) {}
+
   IniSection(const std::string &section_name, const std::string &entry_str) :
-    section_name(section_name) {
+    IniSection(section_name) {
     setEntry(entry_str);
   }
 
@@ -112,34 +114,42 @@ private:
   }
 };
 
-namespace initest {
-typedef enum {
-  LOAD = 1 << 0, /**< should fail at load */
-  INIT = 1 << 1, /**< should fail at init */
-} IniFailAt;
-};
-
-class nntrainerIniTest
-  : public ::testing::TestWithParam<
-      std::tuple<const char *, const std::vector<IniSection>, int>> {
-protected:
-  virtual void SetUp() {
-    name = std::string(std::get<0>(GetParam()));
-    std::cout << "starting test case : " << name << std::endl << std::endl;
-
-    sections = std::get<1>(GetParam());
-    failAt = std::get<2>(GetParam());
-    save_ini();
-  }
-
-  virtual void TearDown() { erase_ini(); }
+/**
+ * @brief IniTestWrapper using IniSection
+ *
+ */
+class IniTestWrapper {
+public:
+  using Sections = std::vector<IniSection>;
 
-  bool failAtLoad() { return failAt & initest::IniFailAt::LOAD; }
+  /**
+   * @brief Construct a new Ini Test Wrapper object
+   *
+   */
+  IniTestWrapper(){};
 
-  bool failAtInit() { return failAt & initest::IniFailAt::INIT; }
+  /**
+   * @brief Construct a new Ini Test Wrapper object
+   *
+   * @param name_ name of the ini without `.ini` extension
+   * @param sections_ sections that should go into ini
+   */
+  IniTestWrapper(const std::string &name_, const Sections &sections_) :
+    name(name_),
+    sections(sections_){};
 
+  /**
+   * @brief Get the Ini Name object
+   *
+   * @return std::string ini name with extension appended
+   */
   std::string getIniName() { return name + ".ini"; }
 
+  /**
+   * @brief Get the Ini object
+   *
+   * @return std::ofstream ini file stream
+   */
   std::ofstream getIni() {
     std::ofstream out(getIniName().c_str());
     if (!out.good()) {
@@ -148,7 +158,11 @@ protected:
     return out;
   }
 
-  virtual void save_ini() {
+  /**
+   * @brief save ini to a file
+   *
+   */
+  void save_ini() {
     std::ofstream out = getIni();
     for (auto &it : sections) {
       it.print(std::cout);
@@ -160,21 +174,17 @@ protected:
     out.close();
   }
 
-  nntrainer::NeuralNetwork NN;
+  /**
+   * @brief erase ini
+   *
+   */
+  void erase_ini() { remove(getIniName().c_str()); }
 
 private:
-  void erase_ini() { name.clear(); }
-  int failAt;
   std::string name;
-  std::vector<IniSection> sections;
+  Sections sections;
 };
 
-/**
- * @brief make ini test case from given parameter
- */
-std::tuple<const char *, const std::vector<IniSection>, int>
-mkIniTc(const char *name, const std::vector<IniSection> vec, int flag);
-
 /// @todo: migrate this to datafile unittest
 const std::string config_str = "[Model]"
                                "\n"
index 38555a4..e85ebd8 100644 (file)
@@ -310,11 +310,3 @@ void IniSection::setEntry(const std::string &entry_str) {
     entry[key] = value;
   }
 }
-
-/**
- * @brief make ini test case from given parameter
- */
-std::tuple<const char *, const std::vector<IniSection>, int>
-mkIniTc(const char *name, const std::vector<IniSection> vec, int flag) {
-  return std::make_tuple(name, vec, flag);
-}
index 0580c24..748090a 100644 (file)
  * @bug No known bugs except for NYI items
  */
 
-#include "nntrainer_test_util.h"
 #include <gtest/gtest.h>
 #include <neuralnet.h>
 
+#include "nntrainer_test_util.h"
+
+namespace initest {
+typedef enum {
+  LOAD = 1 << 0, /**< should fail at load */
+  INIT = 1 << 1, /**< should fail at init */
+} IniFailAt;
+};
+
+class nntrainerIniTest
+  : public ::testing::TestWithParam<
+      std::tuple<const char *, const IniTestWrapper::Sections, int>> {
+
+protected:
+  virtual void SetUp() {
+    name = std::string(std::get<0>(GetParam()));
+    std::cout << "starting test case : " << name << std::endl << std::endl;
+
+    auto sections = std::get<1>(GetParam());
+
+    ini = IniTestWrapper(name, sections);
+
+    failAt = std::get<2>(GetParam());
+    ini.save_ini();
+  }
+
+  virtual void TearDown() { ini.erase_ini(); }
+
+  std::string getIniName() { return ini.getIniName(); }
+
+  bool failAtLoad() { return failAt & initest::IniFailAt::LOAD; }
+
+  bool failAtInit() { return failAt & initest::IniFailAt::INIT; }
+
+  nntrainer::NeuralNetwork NN;
+
+private:
+  int failAt;
+  std::string name;
+  IniTestWrapper ini;
+};
+
 /**
  * @brief check given ini is failing/suceeding at load
  */
@@ -142,6 +183,14 @@ static int ALLFAIL = LOADFAIL | INITFAIL;
 
 using I = IniSection;
 
+/**
+ * @brief make ini test case from given parameter
+ */
+std::tuple<const char *, const IniTestWrapper::Sections, int>
+mkIniTc(const char *name, const IniTestWrapper::Sections vec, int flag) {
+  return std::make_tuple(name, vec, flag);
+}
+
 /// @note each line contains 2 (positive or negative test) + 3 negative test.
 /// if, there are 6 positive tests and 9 negative tests
 /// which sums up to 6 * 2 = 12 positive tests and 9 * 2 + (6 + 9) * 3 = 63