From 6e95c62167c56cd95f5c77195061c06045a4dc89 Mon Sep 17 00:00:00 2001 From: Dongju Chae Date: Mon, 24 May 2021 20:03:31 +0900 Subject: [PATCH] [Tests/Utils] Separate common functions from test utils This patch separates common functions from test utils. It will be useful for some tests without npu-engine dependencies. Signed-off-by: Dongju Chae --- tests/utils/meson.build | 14 +++-- tests/utils/ne_test_utils.cc | 80 +--------------------------- tests/utils/ne_test_utils.h | 27 +--------- tests/utils/ne_test_utils_common.c | 104 +++++++++++++++++++++++++++++++++++++ tests/utils/ne_test_utils_common.h | 56 ++++++++++++++++++++ 5 files changed, 174 insertions(+), 107 deletions(-) create mode 100644 tests/utils/ne_test_utils_common.c create mode 100644 tests/utils/ne_test_utils_common.h diff --git a/tests/utils/meson.build b/tests/utils/meson.build index 9d8d582..3c38353 100644 --- a/tests/utils/meson.build +++ b/tests/utils/meson.build @@ -1,8 +1,16 @@ # Build library -ne_test_utils_inc = include_directories('.') -ne_test_utils_src = ['ne_test_utils.cc'] +ne_test_utils_inc = [include_directories('.'), ne_common_inc] + +ne_test_utils_common_src = ['ne_test_utils_common.c'] +ne_test_utils_common_dep = declare_dependency( + sources : ne_test_utils_common_src, + include_directories : ne_test_utils_inc +) +ne_test_utils_inc += ne_host_inc +ne_test_utils_src = ['ne_test_utils.cc'] ne_test_utils_dep = declare_dependency( + dependencies: ne_test_utils_common_dep, sources : ne_test_utils_src, - include_directories : ne_test_utils_inc, + include_directories : ne_test_utils_inc ) diff --git a/tests/utils/ne_test_utils.cc b/tests/utils/ne_test_utils.cc index 2f7d966..515c346 100644 --- a/tests/utils/ne_test_utils.cc +++ b/tests/utils/ne_test_utils.cc @@ -28,6 +28,7 @@ #include #include +#include "ne_test_utils_common.h" #include "ne_test_utils.h" #define ALIGN_SIZE 4096 @@ -116,33 +117,6 @@ destroy_model (npudev_h dev, generic_buffer *model) { } /** - * @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 entry function to run apptest * @param[in] dev npu device * @param[in] argv arguments from main function @@ -198,24 +172,6 @@ run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func) { return -total_fails; } -/** - * @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 check memory leak by calling memory status API */ int check_memory_leak (npudev_h dev) { @@ -245,40 +201,6 @@ recheck: return 0; } -/** @brief compare output tensor with the golden data */ -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; -} - std::mutex UtilTrinity::m_; std::condition_variable UtilTrinity::cv_; uint32_t UtilTrinity::success_ = 0; diff --git a/tests/utils/ne_test_utils.h b/tests/utils/ne_test_utils.h index 5a0345e..55b165e 100644 --- a/tests/utils/ne_test_utils.h +++ b/tests/utils/ne_test_utils.h @@ -16,6 +16,8 @@ #ifndef _NE_TEST_UTILS_H_ #define _NE_TEST_UTILS_H_ +#include "ne_test_utils_common.h" + #include #include #include @@ -227,37 +229,12 @@ generic_buffer *make_model (npudev_h dev, npubin_meta *meta, void destroy_model (npudev_h dev, generic_buffer *model); /** - * @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 - * @return the size of file - */ -off_t get_file_size (const char *file_path); - -/** * @brief check memory leak by calling memory status API * @param[in] dev npu device handle * @return 0 if no error, otherwise a negative errno */ int check_memory_leak (npudev_h dev); -/** - * @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); - #if defined(__cplusplus) } #endif diff --git a/tests/utils/ne_test_utils_common.c b/tests/utils/ne_test_utils_common.c new file mode 100644 index 0000000..658de76 --- /dev/null +++ b/tests/utils/ne_test_utils_common.c @@ -0,0 +1,104 @@ +/** + * Proprietary + * Copyright (C) 2021 Samsung Electronics + * Copyright (C) 2021 Dongju Chae + */ +/** + * @file ne_test_utils_common.c + * @date 20 May 2021 + * @brief Common utility functions without any dependency + * @author Dongju Chae + * @bug No known bugs except for NYI items + */ + +#include "ne_test_utils_common.h" + +#include +#include +#include +#include + +/** + * @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; +} diff --git a/tests/utils/ne_test_utils_common.h b/tests/utils/ne_test_utils_common.h new file mode 100644 index 0000000..1e4095d --- /dev/null +++ b/tests/utils/ne_test_utils_common.h @@ -0,0 +1,56 @@ +/** + * Proprietary + * Copyright (C) 2021 Samsung Electronics + * Copyright (C) 2021 Dongju Chae + */ +/** + * @file ne_test_utils_common.c + * @date 20 May 2021 + * @brief Common utility functions without any dependency + * @author Dongju Chae + * @bug No known bugs except for NYI items + */ + +#ifndef _NE_TEST_UTILS_COMMON_H_ +#define _NE_TEST_UTILS_COMMON_H_ + +#include +#include + +#include +#include + +#if defined(__cplusplus) +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 + * @return the size of file + */ +off_t get_file_size (const char *file_path); + +/** + * @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); + +#if defined(__cplusplus) +} +#endif + +#endif -- 2.7.4