[IIO/test/coverity] Resolve coverity issues
authorParichay Kapoor <pk.kapoor@samsung.com>
Thu, 21 Nov 2019 01:21:26 +0000 (10:21 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 26 Nov 2019 00:41:49 +0000 (09:41 +0900)
Added more checks coherent with in #1859 and #1863 to another location

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
tests/nnstreamer_source/unittest_src_iio.cpp

index e05b09f..6dec98b 100644 (file)
@@ -1189,14 +1189,91 @@ GENERATE_TESTS_TO_VERIFY_DATA_WO_TRIGGER (DATA, 56, 1);
 GENERATE_TESTS_TO_VERIFY_DATA_WO_TRIGGER (DATA, 64, 2);
 
 /**
+ * @brief Run pipeline for a few frames with data verification
+ */
+static gboolean test_tensor_src_iio_data_verify_util (iio_dev_dir_struct *dev0,
+    gint data_value, guint data_bits, gint samp_freq)
+{
+  static const gint MAX_NUM_TRY = 100;
+  gint num_try;
+  gint fd;
+  size_t bytes_to_read, bytes_read;
+  gchar *data_buffer;
+  guint64 expect_val_mask;
+  gfloat expect_val, actual_val;
+  gchar *expect_val_char, *actual_val_char;
+  gint ret;
+  struct stat stat_buf;
+
+  for (int idx = 0; idx < NUM_FRAMES; idx++) {
+    ret = 0;
+    memset (&stat_buf, 0, sizeof (stat_buf));
+    num_try = 0;
+
+    /** wait for filter to process the frame and multifilesink to write it */
+    while ((fd = open (dev0->log_file, O_RDONLY)) < 0) {
+      if (num_try >= MAX_NUM_TRY) {
+        return FALSE;
+      }
+      g_usleep (MAX (10, 1000000 / samp_freq));
+      num_try++;
+    }
+
+    /** wait for filter to process the frame and multifilesink to write it */
+    for (num_try = 0; num_try < MAX_NUM_TRY; ++num_try) {
+      ret = stat (dev0->log_file, &stat_buf);
+      if (ret == 0 && stat_buf.st_size != 0) {
+        break;
+      }
+      g_usleep (MAX (1, 1000000 / samp_freq));
+    }
+
+    /** verify correctness of data */
+    bytes_to_read = sizeof (float) * BUF_LENGTH * dev0->num_scan_elements;
+    data_buffer = (gchar *) malloc (bytes_to_read);
+    if (data_buffer == NULL) {
+      close (fd);
+      return FALSE;
+    }
+    ret = read (fd, data_buffer, bytes_to_read);
+    if (ret < 0) {
+      close (fd);
+      free (data_buffer);
+      return FALSE;
+    }
+    bytes_read = static_cast<size_t>(ret); \
+    EXPECT_EQ (bytes_read, bytes_to_read);
+    expect_val_mask = G_MAXUINT64 >> (64 - data_bits);
+    expect_val = ((data_value & expect_val_mask) + OFFSET) * SCALE;
+    expect_val_char = g_strdup_printf ("%.2f", expect_val);
+    for (size_t idx = 0; idx < bytes_to_read; idx += sizeof (float)) {
+      actual_val = *((gfloat *) data_buffer);
+      actual_val_char = g_strdup_printf ("%.2f", actual_val);
+      EXPECT_STREQ (expect_val_char, actual_val_char);
+      g_free (actual_val_char);
+    }
+    g_free (expect_val_char);
+    close (fd);
+    free (data_buffer);
+    if (safe_remove (dev0->log_file) != 0)
+      return FALSE;
+    /** update data value to check data updates */
+    data_value += 1;
+    ret = build_dev_dir_scan_elements (dev0, data_bits, data_value, data_value);
+    if (ret != 0)
+      return FALSE;
+  }
+
+  return TRUE;
+}
+
+/**
  * @brief tests tensor source IIO data with trigger
  * @note verifies that no frames have been lost as well
  * @note verifies caps for the src pad as well
  */
 TEST (test_tensor_src_iio, data_verify_trigger)
 {
-  static const int MAX_NUM_TRY = 100;
-  int num_try;
   iio_dev_dir_struct *dev0;
   GstElement *src_iio_pipeline;
   GstElement *src_iio;
@@ -1206,13 +1283,6 @@ TEST (test_tensor_src_iio, data_verify_trigger)
   gint samp_freq;
   gint data_value;
   guint data_bits;
-  gint fd, ret;
-  size_t bytes_to_read, bytes_read;
-  gchar *data_buffer;
-  gfloat expect_val, actual_val;
-  guint64 expect_val_mask;
-  gchar *expect_val_char, *actual_val_char;
-  struct stat stat_buf;
   data_value = DATA;
   data_bits = 16;
   GstCaps *caps;
@@ -1267,60 +1337,8 @@ TEST (test_tensor_src_iio, data_verify_trigger)
   gst_object_unref (src_pad);
   gst_caps_unref (caps);
 
-  /** let a few frames transfer */
-  for (int idx = 0; idx < NUM_FRAMES; idx++) {
-    num_try = 0;
-    /** wait for filter to process the frame and multifilesink to write it */
-    while ((fd = open (dev0->log_file, O_RDONLY)) < 0) {
-      if (num_try >= MAX_NUM_TRY) {
-        FAIL() << "Failed to open " << dev0->log_file;
-      }
-      g_usleep (MAX (10, 1000000 / samp_freq));
-      num_try++;
-    }
-
-    /** wait for filter to process the frame and multifilesink to write it */
-    for (num_try = 0; num_try < MAX_NUM_TRY; ++num_try) {
-      ret = stat (dev0->log_file, &stat_buf);
-      if (ret == 0 && stat_buf.st_size != 0) {
-        break;
-      }
-      g_usleep (MAX (1, 1000000 / samp_freq));
-    }
-
-    /** verify correctness of data */
-    bytes_to_read = sizeof (float) * BUF_LENGTH * dev0->num_scan_elements;
-    data_buffer = (gchar *) malloc (bytes_to_read);
-    if (data_buffer == NULL) {
-      close (fd);
-      FAIL() << "Failed to malloc for data_buffer";
-    }
-    ret = read (fd, data_buffer, bytes_to_read);
-    if (ret < 0) {
-      close (fd);
-      free (data_buffer);
-      FAIL() << "Failed to read file descriptor, " << fd;
-    }
-    bytes_read = static_cast<size_t>(ret); \
-    EXPECT_EQ (bytes_read, bytes_to_read);
-    expect_val_mask = G_MAXUINT64 >> (64 - data_bits);
-    expect_val = ((data_value & expect_val_mask) + OFFSET) * SCALE;
-    expect_val_char = g_strdup_printf ("%.2f", expect_val);
-    for (size_t idx = 0; idx < bytes_to_read; idx += sizeof (float)) {
-      actual_val = *((gfloat *) data_buffer);
-      actual_val_char = g_strdup_printf ("%.2f", actual_val);
-      EXPECT_STREQ (expect_val_char, actual_val_char);
-      g_free (actual_val_char);
-    }
-    g_free (expect_val_char);
-    close (fd);
-    free (data_buffer);
-    ASSERT_EQ (safe_remove (dev0->log_file), 0);
-    /** update data value to check data updates */
-    data_value += 1;
-    ASSERT_EQ (build_dev_dir_scan_elements (dev0, data_bits, data_value,
-            data_value), 0);
-  }
+  EXPECT_EQ (test_tensor_src_iio_data_verify_util (dev0, data_value, data_bits,
+        samp_freq), TRUE);
 
   /** verify playing state has been maintained */
   status =
@@ -1452,13 +1470,6 @@ TEST (test_tensor_src_iio, data_verify_freq_generic_type)
   gint samp_freq;
   gint data_value;
   guint data_bits;
-  gint fd, bytes_to_read, bytes_read;
-  gchar *data_buffer;
-  gfloat expect_val, actual_val;
-  guint64 expect_val_mask;
-  gchar *expect_val_char, *actual_val_char;
-  struct stat stat_buf;
-  gint stat_ret;
   GstCaps *caps;
   GstPad *src_pad;
   GstStructure *structure;
@@ -1538,45 +1549,8 @@ TEST (test_tensor_src_iio, data_verify_freq_generic_type)
   gst_object_unref (src_pad);
   gst_caps_unref (caps);
 
-  /** let a few frames transfer */
-  for (int idx = 0; idx < NUM_FRAMES; idx++) {
-    /** wait for filter to process the frame and multifilesink to write it */
-    while (TRUE) {
-      if (g_file_test (dev0->log_file, G_FILE_TEST_IS_REGULAR)) {
-        stat_ret = stat (dev0->log_file, &stat_buf);
-        if (stat_ret == 0 && stat_buf.st_size != 0) {
-          break;
-        }
-      }
-      g_usleep (MAX (1, 1000000 / samp_freq));
-    }
-
-    /** verify correctness of data */
-    fd = open (dev0->log_file, O_RDONLY);
-    ASSERT_GE (fd, 0);
-    bytes_to_read = sizeof (float) * BUF_LENGTH * dev0->num_scan_elements;
-    data_buffer = (gchar *) malloc (bytes_to_read);
-    ASSERT_TRUE (data_buffer != NULL);
-    bytes_read = read (fd, data_buffer, bytes_to_read);
-    EXPECT_EQ (bytes_read, bytes_to_read);
-    expect_val_mask = G_MAXUINT64 >> (64 - data_bits);
-    expect_val = ((data_value & expect_val_mask) + OFFSET) * SCALE;
-    expect_val_char = g_strdup_printf ("%.2f", expect_val);
-    for (int idx = 0; idx < bytes_to_read; idx += sizeof (float)) {
-      actual_val = *((gfloat *) data_buffer);
-      actual_val_char = g_strdup_printf ("%.2f", actual_val);
-      EXPECT_STREQ (expect_val_char, actual_val_char);
-      g_free (actual_val_char);
-    }
-    g_free (expect_val_char);
-    close (fd);
-    free (data_buffer);
-    ASSERT_EQ (safe_remove (dev0->log_file), 0);
-    /** update data value to check data updates */
-    data_value += 1;
-    ASSERT_EQ (build_dev_dir_scan_elements (dev0, data_bits, data_value,
-            data_value), 0);
-  }
+  EXPECT_EQ (test_tensor_src_iio_data_verify_util (dev0, data_value, data_bits,
+        samp_freq), TRUE);
 
   /** verify playing state has been maintained */
   status =