[Utils] Increase test coverage in ne-conf/utils.c
authorDongju Chae <dongju.chae@samsung.com>
Fri, 9 Aug 2019 07:15:55 +0000 (16:15 +0900)
committer임근식/On-Device Lab(SR)/Principal Engineer/삼성전자 <geunsik.lim@samsung.com>
Mon, 12 Aug 2019 06:25:35 +0000 (15:25 +0900)
This commit adds test cases to increase test coverage for ne-conf/utils.c

- ne-conf.c: 51.6% --> 94%
- ne-utils.c: 85.9% --> 92.3%

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
meson_options.txt
src/core/ne-conf.c
src/test/unittests/ne_core_utils_test.cpp

index 9b721b8..5ada8ec 100644 (file)
@@ -2,4 +2,4 @@ option('comm_opt', type : 'string', value : 'ip')
 option('resv_mem_size', type : 'string', value : '512M')
 option('working_dir', type : 'string', value : '/tmp/')
 option('log_dir', type : 'string', value : '/tmp')
-option('enable_test_mode', type : 'boolean', value : false)
+option('enable_test_mode', type : 'boolean', value : true)
index a2cd097..2ccbfa3 100644 (file)
@@ -84,7 +84,8 @@ load_conf_each (const char *resv_mem_size, const char *working_dir,
     c->log_dir[strlen(log_dir)] = '\x00';
   }
 
-  c->test_mode = test_mode;
+  if (test_mode != -1)
+    c->test_mode = test_mode;
 }
 
 /**
@@ -97,7 +98,7 @@ static void load_conf_ini(dictionary *ini, conf_t *c)
   const char *resv_mem_size = iniparser_getstring(ini, "main:resv_mem_size", NULL);
   const char *working_dir = iniparser_getstring(ini, "main:working_dir", NULL);
   const char *log_dir = iniparser_getstring(ini, "main:log_dir", NULL);
-  int test_mode = iniparser_getboolean(ini, "main:test_mode", DEFAULT_TEST_MODE);
+  int test_mode = iniparser_getboolean(ini, "main:test_mode", -1);
 
   load_conf_each (resv_mem_size, working_dir, log_dir, test_mode, c);
 }
@@ -112,13 +113,14 @@ static void load_conf_envvar(conf_t *c)
   const char *working_dir = getenv (ENV_WORKING_DIR);
   const char *log_dir = getenv (ENV_LOG_DIR);
   const char *test_mode_str = getenv (ENV_TEST_MODE);
-  int test_mode = DEFAULT_TEST_MODE;
+  int test_mode = -1;
 
   /* the below is iniparser's getboolean condition */
-  if (test_mode_str && (test_mode_str[0] == '1'
-      || test_mode_str[0] == 't' || test_mode_str[0] == 'T'
-      || test_mode_str[0] == 'y' || test_mode_str[0] == 'Y'))
-    test_mode = 1;
+  if (test_mode_str) {
+    test_mode = (test_mode_str[0] == '1'
+        || test_mode_str[0] == 't' || test_mode_str[0] == 'T'
+        || test_mode_str[0] == 'y' || test_mode_str[0] == 'Y');
+  }
 
   load_conf_each (resv_mem_size, working_dir, log_dir, test_mode, c);
 }
index b06e191..6e325af 100644 (file)
@@ -19,6 +19,7 @@
 extern "C"
 {
   #include <ne-utils.h>
+  #include <ne-conf.h>
 }
 
 /**
@@ -118,6 +119,43 @@ TEST (ne_core_utils_test, hash_table)
   }
   ASSERT_EQ (i, num - 1);
   ASSERT_EQ (test_ht.num, 0);
+  ASSERT_EQ (hash_table_destroy (&test_ht), 0);
+}
+
+/**
+ * @brief test functionality of load_conf (.ini and env)
+ */
+TEST (ne_core_utils_test, load_conf)
+{
+  FILE *fp = fopen ("dummy.ini", "w");
+  const char *ini_str = "[main]\n"
+                        "resv_mem_size=128k\n"
+                        "working_dir=/tmp/test_working\n"
+                        "log_dir=/tmp/test_log\n"
+                        "test_mode=n\n";
+
+  /** from .ini file */
+  ASSERT_NE (fp, nullptr);
+  ASSERT_EQ (fwrite (ini_str, strlen(ini_str), 1, fp), 1);
+  ASSERT_EQ (fclose (fp), 0);
+
+  ASSERT_EQ (load_conf ("dummy.ini"), 0);
+  ASSERT_EQ (conf->reserved_mem_size, (uint64_t) 128 * 1024);
+  ASSERT_STREQ (conf->working_dir, "/tmp/test_working");
+  ASSERT_STREQ (conf->log_dir, "/tmp/test_log");
+  ASSERT_EQ (conf->test_mode, 0);
+
+  /** from env */
+  ASSERT_EQ (setenv("NE_RESV_MEM_SIZE", "512G", 1), 0);
+  ASSERT_EQ (setenv("NE_WORKING_DIR", "/tmp/", 1), 0);
+  ASSERT_EQ (setenv("NE_LOG_DIR", "/tmp/", 1), 0);
+  ASSERT_EQ (setenv("NE_TEST_MODE", "YES", 1), 0);
+
+  ASSERT_EQ (load_conf (NULL), 0);
+  ASSERT_EQ (conf->reserved_mem_size, (uint64_t) 512 * 1024 * 1024 * 1024);
+  ASSERT_STREQ (conf->working_dir, "/tmp/");
+  ASSERT_STREQ (conf->log_dir, "/tmp/");
+  ASSERT_EQ (conf->test_mode, 1);
 }
 
 /**