Add safeguard to prevent loadFromConf twice
authorJihoon Lee <jhoon.it.lee@samsung.com>
Thu, 6 Aug 2020 07:53:05 +0000 (16:53 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 11 Aug 2020 02:33:46 +0000 (11:33 +0900)
**Changes proposed in this PR:**
- This patch disables loadFromConfig after loading

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

index bcbca60608c07a5bdbe02520eabab7946fd35588..6617f53a9728f8b0e64d6f78cbbef6e658a78e20 100644 (file)
@@ -431,6 +431,8 @@ private:
    * @param[in] ini will be casted to iniparser::dictionary *
    */
   int loadNetworkConfig(void *ini);
+
+  bool loadedFromConfig; /**< Check if config is loaded to prevent load twice */
 };
 
 } /* namespace nntrainer */
index 986a4ab038cdcb14b38d3ee74aea4dba24c6b24c..fdd8c5b947e65a98d40219a3ff705c9191644c13 100644 (file)
@@ -66,7 +66,8 @@ NeuralNetwork::NeuralNetwork() :
   continue_train(false),
   iter(0),
   initialized(false),
-  def_name_count(0) {}
+  def_name_count(0),
+  loadedFromConfig(false) {}
 
 NeuralNetwork::NeuralNetwork(std::string config) : NeuralNetwork() {
   this->setConfig(config);
@@ -164,6 +165,11 @@ int NeuralNetwork::loadDatasetConfig(void *_ini) {
 }
 
 int NeuralNetwork::loadFromConfig() {
+  if (loadedFromConfig == true) {
+    ml_loge("cannnot do loadFromConfig twice");
+    return ML_ERROR_INVALID_PARAMETER;
+  }
+
   int status = ML_ERROR_NONE;
   std::string ini_file = config;
   int num_ini_sec = 0;
@@ -199,6 +205,9 @@ int NeuralNetwork::loadFromConfig() {
     return ML_ERROR_INVALID_PARAMETER;
   }
 
+  /// @fixme: move this to end of function after resolving #382
+  loadedFromConfig = true;
+
   ml_logd("parsing ini started");
   /** Get all the section names */
   ml_logi("==========================parsing ini...");
index a773f0805d056bd6f954f6b509c8d6e51434f336..32d89712b01de912910172a73d6eeb4ca4c3b1b0 100644 (file)
@@ -28,6 +28,16 @@ TEST_P(nntrainerIniTest, loadConfig) {
   }
 }
 
+/**
+ * @brief Negative test given ini is failing at loadingTwice
+ */
+TEST_P(nntrainerIniTest, loadConfigTwice) {
+  std::cout << std::get<0>(GetParam()) << std::endl;
+  NN.loadFromConfig();
+  int status = NN.loadFromConfig();
+  EXPECT_EQ(status, ML_ERROR_INVALID_PARAMETER);
+}
+
 /**
  * @brief check given ini is failing/succeeding at init
  */
@@ -57,6 +67,20 @@ TEST_P(nntrainerIniTest, initTwice_n) {
   EXPECT_NE(status, ML_ERROR_NONE);
 }
 
+/**
+ * @brief check given ini is failing/succeeding when init happens three times.
+ * this should fail at all time.
+ */
+TEST_P(nntrainerIniTest, initThreetime_n) {
+  std::cout << std::get<0>(GetParam()) << std::endl;
+  int status = NN.loadFromConfig();
+  status = NN.init();
+  status = NN.init();
+  status = NN.init();
+
+  EXPECT_NE(status, ML_ERROR_NONE);
+}
+
 /// @todo add run test could be added with iniTest flag to control skip
 
 static IniSection nw_base("network", "Type = NeuralNetwork | "