[CodeQuality] release memory when test is failed
authorHyoung Joo Ahn <hello.ahn@samsung.com>
Tue, 30 Mar 2021 08:25:35 +0000 (17:25 +0900)
committerSangjung Woo <again4you@gmail.com>
Tue, 30 Mar 2021 09:48:15 +0000 (18:48 +0900)
when a test case is finalized with ASSERT, the allocated memory should be freed properly.

Signed-off-by: Hyoung Joo Ahn <hello.ahn@samsung.com>
tests/nnstreamer_plugins/unittest_plugins.cc
tests/nnstreamer_rate/unittest_rate.cc

index e19b342..cbc53fc 100644 (file)
@@ -3483,9 +3483,12 @@ TEST (testTensorTransform, orcPerformance)
   gint64 start_ts, stop_ts, diff_loop, diff_orc;
   uint8_t *data_u8 = (uint8_t *)g_malloc0 (sizeof (uint8_t) * array_size);
   float *data_float = (float *)g_malloc0 (sizeof (float) * array_size);
+  gboolean ret = true;
 
-  ASSERT_TRUE (data_u8 != NULL);
-  ASSERT_TRUE (data_float != NULL);
+  ret = data_u8 != NULL;
+  if (!ret) goto error;
+  ret = data_float != NULL;
+  if (!ret) goto error;
 
   /* orc add u8 */
   start_ts = g_get_real_time ();
@@ -3650,8 +3653,11 @@ TEST (testTensorTransform, orcPerformance)
   diff_loop = stop_ts - start_ts;
   _print_log ("combined loop: %" G_GINT64_FORMAT, diff_loop);
 
+error:
   g_free (data_u8);
   g_free (data_float);
+
+  ASSERT_TRUE (ret);
 }
 #endif /* HAVE_ORC */
 
index afa1494..7ea4e38 100644 (file)
@@ -390,17 +390,20 @@ TEST (nnstreamerRate, passthrough)
 TEST (nnstreamerRate, noThrottling)
 {
   TestOption option;
-  GstElement *rate;
+  GstElement *rate = NULL;
   guint64 in, out, dup, drop;
+  gboolean ret = true;
 
   _set_default_option (option);
   option.mode = TENSOR_RATE_MODE_NO_THROTTLE;
   option.target_framerate = g_strdup ("15/1");
 
-  ASSERT_TRUE (_setup_pipeline (option));
+  ret = _setup_pipeline (option);
+  if (!ret) goto error;
 
   rate = gst_bin_get_by_name (GST_BIN (test_data.pipeline), "rate");
-  ASSERT_TRUE (rate != NULL);
+  ret = rate != NULL;
+  if (!ret) goto error;
 
   EXPECT_EQ (setPipelineStateSync (test_data.pipeline, GST_STATE_PLAYING,
         UNITTEST_STATECHANGE_TIMEOUT), 0);
@@ -425,10 +428,15 @@ TEST (nnstreamerRate, noThrottling)
   EXPECT_EQ (setPipelineStateSync (test_data.pipeline, GST_STATE_NULL,
         UNITTEST_STATECHANGE_TIMEOUT), 0);
 
+error:
   g_free (option.target_framerate);
 
-  gst_object_unref (rate);
-  gst_object_unref (test_data.pipeline);
+  if (rate)
+    gst_object_unref (rate);
+  if (test_data.pipeline)
+    gst_object_unref (test_data.pipeline);
+
+  ASSERT_TRUE (ret);
 }
 
 /**
@@ -437,8 +445,9 @@ TEST (nnstreamerRate, noThrottling)
 TEST (nnstreamerRate, throttling)
 {
   TestOption option;
-  GstElement *rate;
+  GstElement *rate = NULL;
   guint64 in, out, dup, drop;
+  gboolean ret = true;
 
   const gchar *root_path = g_getenv ("NNSTREAMER_SOURCE_ROOT_PATH");
   if (root_path == NULL)
@@ -455,10 +464,12 @@ TEST (nnstreamerRate, throttling)
   option.mode = TENSOR_RATE_MODE_THROTTLE;
   option.target_framerate = g_strdup ("15/1");
 
-  ASSERT_TRUE (_setup_pipeline (option));
+  ret = _setup_pipeline (option);
+  if (!ret) goto error;
 
   rate = gst_bin_get_by_name (GST_BIN (test_data.pipeline), "rate");
-  ASSERT_TRUE (rate != NULL);
+  ret = rate != NULL;
+  if (!ret) goto error;
 
   EXPECT_EQ (setPipelineStateSync (test_data.pipeline, GST_STATE_PLAYING,
         UNITTEST_STATECHANGE_TIMEOUT), 0);
@@ -480,12 +491,17 @@ TEST (nnstreamerRate, throttling)
   EXPECT_EQ (setPipelineStateSync (test_data.pipeline, GST_STATE_NULL,
         UNITTEST_STATECHANGE_TIMEOUT), 0);
 
+error:
   g_free (option.target_framerate);
   g_free (option.model_file);
   g_free (option.framework);
 
-  gst_object_unref (rate);
-  gst_object_unref (test_data.pipeline);
+  if (rate)
+    gst_object_unref (rate);
+  if (test_data.pipeline)
+    gst_object_unref (test_data.pipeline);
+
+  ASSERT_TRUE (ret);
 }