This patch performs dependency refactoring for test utils.
The gtest dependency will be used other tests like plugins as well as
unittests.
Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
-gtest_dep = dependency('gtest', required: false)
-if gtest_dep.found()
+if ne_test_utils_gtest_dep.found()
testenv = environment()
testenv.set('LD_LIBRARY_PATH', ne_libdir)
unittest_ne_core_utils = executable('unittest_ne_core_utils',
['ne_core_utils_test.cc'],
- dependencies: [gtest_dep, ne_core_utils_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_utils_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
)
- test('unittest_ne_utils', unittest_ne_core_utils, env: testenv)
+ test('unittest_ne_core_utils', unittest_ne_core_utils, env: testenv)
unittest_ne_core_npu = executable('unittest_ne_core_npu',
['ne_core_npu_test.cc'],
- dependencies: [gtest_dep, ne_core_npu_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_npu_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_comm = executable('unittest_ne_core_comm',
['ne_core_comm_test.cc'],
include_directories: ne_host_inc,
- dependencies: [gtest_dep, ne_plugin_comm_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_plugin_comm_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_hwmem = executable('unittest_ne_core_hwmem',
['ne_core_hwmem_test.cc'],
- dependencies: [gtest_dep, libdrm_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, libdrm_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_model = executable('unittest_ne_core_model',
['ne_core_model_test.cc'],
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_buffer = executable('unittest_ne_core_buffer',
['ne_core_buffer_test.cc'],
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_segment_table = executable('unittest_ne_core_segment_table',
['ne_core_segment_table_test.cc'],
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_handler = executable('unittest_ne_core_handler',
['ne_core_handler_test.cc'],
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_test_utils_common_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_data = executable('unittest_ne_core_data',
['ne_core_data_test.cc'],
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_mem = executable('unittest_ne_core_mem',
['ne_core_mem_test.cc'],
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_sched = executable('unittest_ne_core_sched',
['ne_core_sched_test.cc'],
include_directories: ne_host_inc,
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_test_utils_common_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_thread_pool = executable('unittest_ne_core_thread_pool',
['ne_core_thread_pool_test.cc'],
include_directories: ne_host_inc,
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_test_utils_common_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_core_inputservice = executable('unittest_ne_core_inputservice',
['ne_core_inputservice_test.cc'],
include_directories: ne_host_inc,
- dependencies: [gtest_dep, ne_core_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_test_utils_common_dep, ne_core_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
unittest_ne_libnpuhost = executable('unittest_ne_libnpuhost',
['ne_libnpuhost_test.cc'],
include_directories: [ne_host_inc, ne_common_inc],
- dependencies: [gtest_dep, ne_host_dep, ne_core_dep, ne_test_utils_dep],
+ dependencies: [ne_test_utils_gtest_dep, ne_host_dep, ne_core_dep, ne_test_utils_dep],
install : true,
install_rpath : ne_libdir,
install_dir : join_paths(ne_bindir, 'unittests')
#include <ne-buffer.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
/**
* @brief fill dummy metadata v1
#include <fstream>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
/**
* @brief Test getCommPlugin()
#include <ne-data.h>
#include <ne-model.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
#include <npubinfmt.h>
#include <cstdlib>
#include <dirent.h>
#include <linux/limits.h>
#include <ne-handler.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
+#include <ne_test_utils_common.h>
/**
* @brief test Device's createInstance()
#include <ne-hwmem.h>
#include <xf86drm.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
+
#if defined(ENABLE_EMUL)
constexpr bool is_enabled_emulation = true;
#else
#include <ne-inputservice.h>
#include <ne-thread-pool.h> /* just for remove() test */
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_common.h>
+#include <ne_test_utils_gtest.h>
/**
* @brief test single instances
#include <ne-mem.h>
#include <ne-conf.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
#define K (1 << 10)
#define M (1 << 20)
#include <ne-model.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
/**
* @brief check set/get primitives of model
#include <mutex>
#include <thread>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
/** 2-TOPS TRIV2 v2.3.2 */
#define TEST_NPU_VERSION (0x02020302)
*/
#include <ne-scheduler.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_common.h>
+#include <ne_test_utils_gtest.h>
/**
* @brief test set/get primitives of request
*/
#include <ne-segment-table.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
/**
* @brief test basic primitives of SegmentTable
#include <gtest/gtest.h>
#include <ne-thread-pool.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_common.h>
+#include <ne_test_utils_gtest.h>
/**
* @brief test getInstance()
#include <unistd.h>
#include <sys/mman.h>
-#include <gtest/gtest.h>
#include <poll.h>
#include <fstream>
#include <ne-utils.h>
#include <ne-conf.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils_gtest.h>
typedef std::vector<std::pair<const char *, char *>> ENV_PAIR;
/**
Logger &logger = Logger::getInstance ();
int ret;
- ret = logger.logwrite (NPU_LOG_ERROR, _N1, "test error\n");
+ ret = logger.logwrite (NPU_LOG_ERROR, _N1,
+ "testing log severity level (error)\n");
EXPECT_GE (ret, 0);
- ret = logger.logwrite (NPU_LOG_WARN, _N1, "test warn\n");
+ ret = logger.logwrite (NPU_LOG_WARN, _N1,
+ "testing log severity level (warn)\n");
EXPECT_GE (ret, 0);
- ret = logger.logwrite (NPU_LOG_INFO, _N1, "test info\n");
+ ret = logger.logwrite (NPU_LOG_INFO, _N1,
+ "testing log severity level (info)\n");
EXPECT_GE (ret, 0);
}
#include <pthread.h>
#include <libnpuhost.h>
-#include <ne_test_utils.h>
-#include "ne_unittest_utils.h"
+#include <ne_test_utils.h>
+#include <ne_test_utils_gtest.h>
/**
* @brief test primitives supported in libnpuhost
+++ /dev/null
-/**
- * Proprietary
- * Copyright (C) 2020 Samsung Electronics
- * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
- */
-/**
- * @file ne_unittest_utils.h
- * @date 09 Apr 2020
- * @brief Utility header for UnitTests
- * @author Dongju Chae <dongju.chae@samsung.com>
- */
-
-#ifndef _NE_UNITTEST_UTILS_H_
-#define _NE_UNITTEST_UTILS_H_
-
-/** DO NOT INCLUDE ANY NPU-ENGINE RELATED HEADERS */
-#include <gtest/gtest.h>
-#include <condition_variable>
-#include <mutex>
-
-#define TEST_SLEEP_MS (100 * 1000) /* 100ms is enough */
-
-/**
- * @brief start gtest with tricky exception handling (due to svace)
- */
-static inline int
-start_gtest (int argc, char **argv) {
- int ret = -1;
- try {
- testing::InitGoogleTest (&argc, argv);
- } catch (...) {
- std::cerr << "catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'"
- << std::endl;
- }
- try {
- ret = RUN_ALL_TESTS ();
- } catch (...) {
- std::cerr << "catch `testing::internal::GoogleTestFailureException`"
- << std::endl;
- }
- return ret;
-}
-
-static inline void
-test_callback (int *num_called, std::mutex *m, std::condition_variable *cv) {
- std::unique_lock<std::mutex> lock (*m);
- (*num_called)++;
- cv->notify_one ();
-}
-
-static inline void
-test_callback_sleep (int *num_called, std::mutex *m,
- std::condition_variable *cv) {
- {
- std::unique_lock<std::mutex> lock (*m);
- (*num_called)++;
- cv->notify_one ();
- }
-
- /** this worker needs to wait a short time for next job */
- usleep (TEST_SLEEP_MS);
-}
-
-static inline void
-wait_callbacks (int &num_called, int max_called, std::mutex &m,
- std::condition_variable &cv) {
- std::unique_lock<std::mutex> lock (m);
- cv.wait (lock, [&]() { return max_called == num_called; });
-}
-
-#endif /* _NE_UNITTEST_UTILS_H_ */
-# Build library
-ne_test_utils_inc = [include_directories('.'), ne_common_inc]
-
-ne_test_utils_common_src = ['ne_test_utils_common.c']
+# Utils for common functions
+ne_test_utils_common_src = ['ne_test_utils_common.cc']
ne_test_utils_common_dep = declare_dependency(
sources : ne_test_utils_common_src,
- include_directories : ne_test_utils_inc
+ include_directories : include_directories('.')
)
+# Utils for libnpuhost
+ne_test_utils_inc = [include_directories('.')]
+ne_test_utils_inc += ne_common_inc
ne_test_utils_inc += ne_host_inc
ne_test_utils_src = ['ne_test_utils.cc']
ne_test_utils_dep = declare_dependency(
sources : ne_test_utils_src,
include_directories : ne_test_utils_inc
)
+
+# Utils for gtest library
+gtest_dep = dependency('gtest', required: false)
+if gtest_dep.found ()
+ ne_test_utils_gtest_src = ['ne_test_utils_gtest.cc']
+ ne_test_utils_gtest_dep = declare_dependency(
+ dependencies: gtest_dep,
+ sources : ne_test_utils_gtest_src,
+ include_directories : include_directories('.')
+ )
+endif
+++ /dev/null
-/**
- * Proprietary
- * Copyright (C) 2021 Samsung Electronics
- * Copyright (C) 2021 Dongju Chae <dongju.chae@samsung.com>
- */
-/**
- * @file ne_test_utils_common.c
- * @date 20 May 2021
- * @brief Common utility functions without any dependency
- * @author Dongju Chae <dongju.chae@samsung.com>
- * @bug No known bugs except for NYI items
- */
-
-#include "ne_test_utils_common.h"
-
-#include <stdio.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <inttypes.h>
-
-/**
- * @brief get the metadata of model file
- * @param[in] model_file the path of model file
- * @param[out] meta the metadata of model
- * @return 0 if no error, otherwise a negative errno
- */
-int
-get_model_meta (const char *model_file, npubin_meta *meta) {
- FILE *fp;
- int ret;
-
- if (!model_file || !meta)
- return -EINVAL;
-
- fp = fopen (model_file, "rb");
- if (!fp)
- return -errno;
-
- ret = fread (meta, 1, NPUBIN_META_SIZE, fp);
- fclose (fp);
-
- if (ret != NPUBIN_META_SIZE)
- return -EINVAL;
-
- return 0;
-}
-
-/**
- * @brief get the size of file
- * @param[in] file path the path of file
- * @return the size of file
- */
-off_t
-get_file_size (const char *file_path) {
- struct stat _stat;
-
- if (stat (file_path, &_stat) != 0) {
- fprintf (stderr, "Fail to find the file %s (errno %d)\n", file_path,
- -errno);
- return -errno;
- }
-
- return _stat.st_size;
-}
-
-/**
- * @brief compare output data with the golden data
- * @param[in] golden_path the filepath of golden data
- * @param[in] output_data the output data which you just got
- * @param[in] output_size the output data size
- * @return 0 if no error, otherwise a negative errno
- */
-int
-compare_data (const char *golden_path, const char *output_data,
- uint64_t output_size) {
- FILE *fp = fopen (golden_path, "r");
- uint64_t idx;
- int c, err = 0;
-
- if (!fp) {
- fprintf (stderr, "Fail to open %s\n", golden_path);
- return -errno;
- }
-
- idx = 0;
- while ((c = fgetc (fp)) != EOF) {
- if (idx >= output_size) {
- fprintf (stderr, "Out-of-range!\n");
- err = -1;
- goto out;
- }
- if ((uint8_t) c != (uint8_t) output_data[idx]) {
- fprintf (stderr, "Hex diff at %" PRIu64 "; %#1x vs. %#1x\n", idx, c,
- output_data[idx]);
- err = -1;
- goto out;
- }
- idx++;
- }
-
-out:
- fclose (fp);
- return err;
-}
--- /dev/null
+/**
+ * Proprietary
+ * Copyright (C) 2021 Samsung Electronics
+ * Copyright (C) 2021 Dongju Chae <dongju.chae@samsung.com>
+ */
+/**
+ * @file ne_test_utils_common.cc
+ * @date 20 May 2021
+ * @brief Common utility functions without any dependency
+ * @author Dongju Chae <dongju.chae@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
+#include <ne_test_utils_common.h>
+
+using namespace std;
+
+#include <iostream>
+#include <fstream>
+#include <iomanip>
+
+#include <errno.h>
+#include <sys/stat.h>
+#include <inttypes.h>
+
+#define HEX(val) hex << setfill ('0') << setw (2) << (0xff & ((uint8_t) val))
+
+/**
+ * @brief get the size of file
+ * @param[in] file path the path of file
+ * @return the size of file if no error, otherwise a negative errno
+ */
+off_t
+get_file_size (const char *file_path) {
+ struct stat _stat;
+
+ if (stat (file_path, &_stat) != 0) {
+ cerr << "Unable to find the file " << file_path << "\n";
+ return -errno;
+ }
+
+ return _stat.st_size;
+}
+
+/**
+ * @brief compare output data with the golden data
+ * @param[in] golden_path the filepath of golden data
+ * @param[in] output_data the output data which you just got
+ * @param[in] output_size the output data size
+ * @return 0 if no error, otherwise a negative errno
+ */
+int
+compare_data (const char *golden_path, const char *output_data,
+ uint64_t output_size) {
+ ifstream ifs (golden_path, ios::binary | ios::ate);
+ if (!ifs.good ()) {
+ cerr << "Unable to find the file " << golden_path << "\n";
+ return -ENOENT;
+ }
+
+ off_t length = ifs.tellg ();
+ if (length != output_size) {
+ cerr << "Output size mismatch detected.. (";
+ cerr << length << " vs. " << output_size << ")\n";
+ return -ERANGE;
+ }
+
+ off_t idx = 0;
+ char byte;
+ int err = 0;
+
+ ios_base::fmtflags flags (cerr.flags ());
+ ifs.seekg (0, ios::beg);
+
+ while (ifs.read ((char *) &byte, 1)) {
+ if (byte != output_data[idx]) {
+ cerr << "Hex diff at " << idx << " (0x" << HEX (byte);
+ cerr << " vs. 0x" << HEX (output_data[idx]) << ")\n";
+ err = -EINVAL;
+ break;
+ }
+ idx++;
+ }
+ cerr.flags (flags);
+
+ return err;
+}
* Copyright (C) 2021 Dongju Chae <dongju.chae@samsung.com>
*/
/**
- * @file ne_test_utils_common.c
+ * @file ne_test_utils_common.h
* @date 20 May 2021
* @brief Common utility functions without any dependency
* @author Dongju Chae <dongju.chae@samsung.com>
#ifndef _NE_TEST_UTILS_COMMON_H_
#define _NE_TEST_UTILS_COMMON_H_
-#include <npubinfmt.h>
-#include <typedef.h>
-
#include <sys/types.h>
#include <stdint.h>
+#include <unistd.h>
#if defined(__cplusplus)
+
+#include <condition_variable>
+#include <mutex>
+
+#define TEST_SLEEP_MS (100 * 1000) /* 100ms is enough */
+
+static inline void
+test_callback (int *num_called, std::mutex *m, std::condition_variable *cv) {
+ std::unique_lock<std::mutex> lock (*m);
+ (*num_called)++;
+ cv->notify_one ();
+}
+
+static inline void
+test_callback_sleep (int *num_called, std::mutex *m,
+ std::condition_variable *cv) {
+ {
+ std::unique_lock<std::mutex> lock (*m);
+ (*num_called)++;
+ cv->notify_one ();
+ }
+
+ /** this worker needs to wait a short time for next job */
+ usleep (TEST_SLEEP_MS);
+}
+
+static inline void
+wait_callbacks (int &num_called, int max_called, std::mutex &m,
+ std::condition_variable &cv) {
+ std::unique_lock<std::mutex> lock (m);
+ cv.wait (lock, [&]() { return max_called == num_called; });
+}
+
extern "C" {
#endif
-/**
- * @brief get the metadata of model file
- * @param[in] model_file the path of model file
- * @param[out] meta the metadata of model
- * @return 0 if no error, otherwise a negative errno
- */
-int get_model_meta (const char *model_file, npubin_meta *meta);
-
/**
* @brief get the size of file
* @param[in] file path the path of file
--- /dev/null
+/**
+ * Proprietary
+ * Copyright (C) 2021 Samsung Electronics
+ * Copyright (C) 2021 Dongju Chae <dongju.chae@samsung.com>
+ */
+/**
+ * @file ne_test_utils_gtest.cc
+ * @date 07 July 2021
+ * @brief C++ test utility using gtest library
+ * @author Dongju Chae <dongju.chae@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
+#include <ne_test_utils_gtest.h>
+#include <iostream>
+
+/**
+ * @brief start gtest with tricky exception handling (due to svace)
+ */
+int
+start_gtest (int argc, char **argv) {
+ int ret = -1;
+ try {
+ testing::InitGoogleTest (&argc, argv);
+ } catch (...) {
+ std::cerr << "catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'"
+ << std::endl;
+ }
+ try {
+ ret = RUN_ALL_TESTS ();
+ } catch (...) {
+ std::cerr << "catch `testing::internal::GoogleTestFailureException`"
+ << std::endl;
+ }
+ return ret;
+}
--- /dev/null
+/**
+ * Proprietary
+ * Copyright (C) 2021 Samsung Electronics
+ * Copyright (C) 2021 Dongju Chae <dongju.chae@samsung.com>
+ */
+/**
+ * @file ne_test_utils_gtest.h
+ * @date 07 July 2021
+ * @brief C++ utility header using gtest library
+ * @author Dongju Chae <dongju.chae@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
+#include <gtest/gtest.h>
+
+/**
+ * @brief start gtest with tricky exception handling (due to svace)
+ */
+int start_gtest (int argc, char **argv);