From 7d7fb11c5f687a50d050433e66834166822a0ee8 Mon Sep 17 00:00:00 2001 From: River Li Date: Mon, 3 Oct 2022 12:09:37 +0800 Subject: [PATCH] [CAPI] add more test cases for element type (#13265) * Avoid duplicated code Change-Id: I86fe59bab13010aedc62a2c92102b00e060ceeec * Add more test cased for element type Change-Id: I1f02254cee715d53ede0422c14a4732267d3db27 * Apply INSTANTIATE_TEST_SUITE_P for tensor element type test Change-Id: I8bbeeb00ae872b7b286b5a776a5c2681e6b5ee94 --- src/bindings/c/src/ov_core.cpp | 10 +- src/bindings/c/tests/ov_core_test.cpp | 8 ++ src/bindings/c/tests/ov_tensor_test.cpp | 170 +++++++++++------------- src/bindings/c/tests/ov_test.cpp | 11 -- src/bindings/c/tests/ov_test.hpp | 2 - 5 files changed, 88 insertions(+), 113 deletions(-) diff --git a/src/bindings/c/src/ov_core.cpp b/src/bindings/c/src/ov_core.cpp index 69640127538..2eea2518c79 100644 --- a/src/bindings/c/src/ov_core.cpp +++ b/src/bindings/c/src/ov_core.cpp @@ -324,18 +324,16 @@ void ov_core_versions_free(ov_core_version_list_t* versions) { #ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT ov_status_e ov_core_create_with_config_unicode(const wchar_t* xml_config_file_ws, ov_core_t** core) { - if (!core || !xml_config_file_ws) { + if (!xml_config_file_ws) { return ov_status_e::INVALID_C_PARAM; } + std::string xml_config_file; try { - std::string xml_config_file = ov::util::wstring_to_string(std::wstring(xml_config_file_ws)); - std::unique_ptr _core(new ov_core_t); - _core->object = std::make_shared(xml_config_file); - *core = _core.release(); + xml_config_file = ov::util::wstring_to_string(std::wstring(xml_config_file_ws)); } CATCH_OV_EXCEPTIONS - return ov_status_e::OK; + return ov_core_create_with_config(xml_config_file.c_str(), core); } ov_status_e ov_core_read_model_unicode(const ov_core_t* core, diff --git a/src/bindings/c/tests/ov_core_test.cpp b/src/bindings/c/tests/ov_core_test.cpp index 285f862da83..588c80706b5 100644 --- a/src/bindings/c/tests/ov_core_test.cpp +++ b/src/bindings/c/tests/ov_core_test.cpp @@ -472,6 +472,14 @@ TEST_P(ov_core, ov_core_get_versions_by_device_name) { } #ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT +const std::vector test_unicode_postfix_vector = {L"unicode_Яㅎあ", + L"ひらがな日本語", + L"大家有天分", + L"עפצקרשתםןףץ", + L"ث خ ذ ض ظ غ", + L"그것이정당하다", + L"АБВГДЕЁЖЗИЙ", + L"СТУФХЦЧШЩЬЮЯ"}; TEST(ov_core, ov_core_create_with_config_unicode) { ov_core_t* core = nullptr; diff --git a/src/bindings/c/tests/ov_tensor_test.cpp b/src/bindings/c/tests/ov_tensor_test.cpp index 411025b7506..e36ff51ed0b 100644 --- a/src/bindings/c/tests/ov_tensor_test.cpp +++ b/src/bindings/c/tests/ov_tensor_test.cpp @@ -31,68 +31,6 @@ TEST(ov_tensor, ov_tensor_create_from_host_ptr) { ov_shape_free(&shape); } -TEST(ov_tensor, ov_tensor_get_shape) { - ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape; - setup_4d_shape(&shape, 10, 20, 30, 40); - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); - EXPECT_NE(nullptr, tensor); - - ov_shape_t shape_res; - OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res)); - EXPECT_EQ(shape.rank, shape_res.rank); - EXPECT_EQ(shape.dims[0], shape_res.dims[0]); - EXPECT_EQ(shape.dims[1], shape_res.dims[1]); - EXPECT_EQ(shape.dims[2], shape_res.dims[2]); - EXPECT_EQ(shape.dims[3], shape_res.dims[3]); - - ov_shape_free(&shape); - ov_shape_free(&shape_res); - ov_tensor_free(tensor); -} - -TEST(ov_tensor, ov_tensor_set_shape) { - ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape; - setup_4d_shape(&shape, 1, 1, 1, 1); - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); - EXPECT_NE(nullptr, tensor); - - ov_shape_t shape_update; - setup_4d_shape(&shape_update, 10, 20, 30, 40); - OV_EXPECT_OK(ov_tensor_set_shape(tensor, shape_update)); - ov_shape_t shape_res; - OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res)); - EXPECT_EQ(shape_update.rank, shape_res.rank); - EXPECT_EQ(shape_update.dims[0], shape_res.dims[0]); - EXPECT_EQ(shape_update.dims[1], shape_res.dims[1]); - EXPECT_EQ(shape_update.dims[2], shape_res.dims[2]); - EXPECT_EQ(shape_update.dims[3], shape_res.dims[3]); - - ov_shape_free(&shape_update); - ov_shape_free(&shape_res); - ov_shape_free(&shape); - ov_tensor_free(tensor); -} - -TEST(ov_tensor, ov_tensor_get_element_type) { - ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape; - setup_4d_shape(&shape, 10, 20, 30, 40); - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); - EXPECT_NE(nullptr, tensor); - - ov_element_type_e type_res; - OV_EXPECT_OK(ov_tensor_get_element_type(tensor, &type_res)); - EXPECT_EQ(type, type_res); - - ov_shape_free(&shape); - ov_tensor_free(tensor); -} - static size_t product(const std::vector& dims) { if (dims.empty()) return 0; @@ -109,52 +47,96 @@ inline size_t calculate_byteSize(ov_shape_t shape, ov_element_type_e type) { return (calculate_size(shape) * GET_ELEMENT_TYPE_SIZE(type) + 7) >> 3; } -TEST(ov_tensor, ov_tensor_get_size) { - ov_element_type_e type = ov_element_type_e::I16; - ov_shape_t shape; - setup_4d_shape(&shape, 1, 3, 4, 4); - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); - EXPECT_NE(nullptr, tensor); +class ov_tensor_create_test : public ::testing::TestWithParam { +protected: + void SetUp() override { + ov_element_type_e type = GetParam(); + setup_4d_shape(&shape, 10, 20, 30, 40); + tensor = nullptr; + OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); + EXPECT_NE(nullptr, tensor); + } + void TearDown() override { + ov_shape_free(&shape); + ov_tensor_free(tensor); + } + +public: + ov_shape_t shape; + ov_tensor_t* tensor; +}; + +INSTANTIATE_TEST_SUITE_P(ov_tensor, + ov_tensor_create_test, + ::testing::Values(ov_element_type_e::BOOLEAN, + ov_element_type_e::BF16, + ov_element_type_e::F16, + ov_element_type_e::F32, + ov_element_type_e::F64, + ov_element_type_e::I4, + ov_element_type_e::I8, + ov_element_type_e::I16, + ov_element_type_e::I32, + ov_element_type_e::I64, + ov_element_type_e::U1, + ov_element_type_e::U4, + ov_element_type_e::U8, + ov_element_type_e::U16, + ov_element_type_e::U32, + ov_element_type_e::U64)); + +TEST_P(ov_tensor_create_test, get_tensor_element_type) { + ov_element_type_e type = GetParam(); + ov_element_type_e type_res; + OV_EXPECT_OK(ov_tensor_get_element_type(tensor, &type_res)); + EXPECT_EQ(type, type_res); +} + +TEST_P(ov_tensor_create_test, get_tensor_size) { size_t size = calculate_size(shape); size_t size_res; OV_EXPECT_OK(ov_tensor_get_size(tensor, &size_res)); EXPECT_EQ(size_res, size); - - ov_shape_free(&shape); - ov_tensor_free(tensor); } -TEST(ov_tensor, ov_tensor_get_byte_size) { - ov_element_type_e type = ov_element_type_e::I16; - ov_shape_t shape; - setup_4d_shape(&shape, 1, 3, 4, 4); - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); - EXPECT_NE(nullptr, tensor); +TEST_P(ov_tensor_create_test, get_tensor_byte_size) { + void* data = nullptr; + OV_EXPECT_OK(ov_tensor_data(tensor, &data)); + EXPECT_NE(nullptr, data); +} +TEST_P(ov_tensor_create_test, get_tensor_data) { + ov_element_type_e type = GetParam(); size_t size = calculate_byteSize(shape, type); size_t size_res; OV_EXPECT_OK(ov_tensor_get_byte_size(tensor, &size_res)); EXPECT_EQ(size_res, size); - - ov_shape_free(&shape); - ov_tensor_free(tensor); } -TEST(ov_tensor, ov_tensor_data) { - ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape; - setup_4d_shape(&shape, 10, 20, 30, 40); - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); - EXPECT_NE(nullptr, tensor); +TEST_P(ov_tensor_create_test, get_tensor_shape) { + ov_shape_t shape_res; + OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res)); + EXPECT_EQ(shape.rank, shape_res.rank); + EXPECT_EQ(shape.dims[0], shape_res.dims[0]); + EXPECT_EQ(shape.dims[1], shape_res.dims[1]); + EXPECT_EQ(shape.dims[2], shape_res.dims[2]); + EXPECT_EQ(shape.dims[3], shape_res.dims[3]); + ov_shape_free(&shape_res); +} - void* data = nullptr; - OV_EXPECT_OK(ov_tensor_data(tensor, &data)); - EXPECT_NE(nullptr, data); +TEST_P(ov_tensor_create_test, set_tensor_shape) { + ov_shape_t shape_update; + setup_4d_shape(&shape_update, 16, 16, 16, 16); + OV_EXPECT_OK(ov_tensor_set_shape(tensor, shape_update)); + ov_shape_t shape_res; + OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res)); + EXPECT_EQ(shape_update.rank, shape_res.rank); + EXPECT_EQ(shape_update.dims[0], shape_res.dims[0]); + EXPECT_EQ(shape_update.dims[1], shape_res.dims[1]); + EXPECT_EQ(shape_update.dims[2], shape_res.dims[2]); + EXPECT_EQ(shape_update.dims[3], shape_res.dims[3]); - ov_shape_free(&shape); - ov_tensor_free(tensor); + ov_shape_free(&shape_update); + ov_shape_free(&shape_res); } \ No newline at end of file diff --git a/src/bindings/c/tests/ov_test.cpp b/src/bindings/c/tests/ov_test.cpp index 970d06e6d6c..a09be5d1262 100644 --- a/src/bindings/c/tests/ov_test.cpp +++ b/src/bindings/c/tests/ov_test.cpp @@ -40,14 +40,3 @@ std::map element_type_size_map = {{ov_element_type_e: {ov_element_type_e::U16, 16}, {ov_element_type_e::U32, 32}, {ov_element_type_e::U64, 64}}; - -#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT -const std::vector test_unicode_postfix_vector = {L"unicode_Яㅎあ", - L"ひらがな日本語", - L"大家有天分", - L"עפצקרשתםןףץ", - L"ث خ ذ ض ظ غ", - L"그것이정당하다", - L"АБВГДЕЁЖЗИЙ", - L"СТУФХЦЧШЩЬЮЯ"}; -#endif diff --git a/src/bindings/c/tests/ov_test.hpp b/src/bindings/c/tests/ov_test.hpp index 072033a9863..8c2d6358559 100644 --- a/src/bindings/c/tests/ov_test.hpp +++ b/src/bindings/c/tests/ov_test.hpp @@ -69,8 +69,6 @@ inline static std::vector content_from_file(const char* filename, bool } #ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT -extern const std::vector test_unicode_postfix_vector; - inline void fix_slashes(std::string& str) { std::replace(str.begin(), str.end(), '/', '\\'); }