From 914daf8d5e069a59cc9ee41cf9aecdfc925443cd Mon Sep 17 00:00:00 2001 From: Jihoon Kim Date: Mon, 16 May 2022 22:18:39 +0900 Subject: [PATCH] Check the each value after executing CAPI unittests Change-Id: I7e4d69f29c9ef403dfc76b6e9cf93058746198e8 Signed-off-by: Jihoon Kim --- tests/src/nlp_capi_connected_unittests.cpp | 270 +++++++++++++++++++++++++++++ tests/src/nlp_capi_unittests.cpp | 82 --------- 2 files changed, 270 insertions(+), 82 deletions(-) create mode 100644 tests/src/nlp_capi_connected_unittests.cpp diff --git a/tests/src/nlp_capi_connected_unittests.cpp b/tests/src/nlp_capi_connected_unittests.cpp new file mode 100644 index 0000000..1f8a15e --- /dev/null +++ b/tests/src/nlp_capi_connected_unittests.cpp @@ -0,0 +1,270 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include + +#include + +#define WAIT_RESULT_DELAY 10 + +static GMainLoop *loop = NULL; + +static gint service_close_timer = 0; + +using namespace std; + +namespace { + +static void STOP_LOOP(); +static void WAIT_FOR_CALLBACK(); + +typedef struct { + string token; + string tag; +} pos_tag_s; + +gboolean timer_cb(gpointer data) +{ + printf("time runs out\n"); + service_close_timer = 0; + STOP_LOOP(); + + return FALSE; +} + +void stop_timer() +{ + if (service_close_timer > 0) + g_source_remove(service_close_timer); + + service_close_timer = 0; +} + +void language_detect_result_cb(unsigned int request_id, const char *lang, void *user_data) +{ + char **detect_lang = (char **)user_data; + + *detect_lang = strdup(lang); + + printf("[%s] request id: %d. language: %s\n", __func__, request_id, lang); + + STOP_LOOP(); +} + +bool pos_tag_foreach_cb(const char* token, const char *tag, void* user_data) +{ + vector *pos_tag_list = (vector *)user_data; + + pos_tag_s pos_tag; + pos_tag.token = string(token); + pos_tag.tag = string(tag); + pos_tag_list->push_back(pos_tag); + + printf("[%s] token: %s, tag: %s\n", __func__, token, tag); + + return true; +} + +void pos_tag_result_cb(unsigned int request_id, nlp_pos_tag_result_h pos_tag_result_h, void *user_data) +{ + printf("request id: %d\n", request_id); + + nlp_foreach_pos_tag(pos_tag_result_h, pos_tag_foreach_cb, user_data); + + STOP_LOOP(); +} + +bool word_tokenize_foreach_cb(const char* word, void* user_data) +{ + printf("[%s] word(%s)\n", __func__, word); + + vector *word_list = (vector *)user_data; + + word_list->push_back(string(word)); + + return true; +} + +void word_tokenize_result_cb(unsigned int request_id, nlp_word_tokenize_result_h word_tokenize_result_h, void *user_data) +{ + printf("[%s] request id: %d\n", __func__, request_id); + + nlp_foreach_word_tokenize(word_tokenize_result_h, word_tokenize_foreach_cb, user_data); + + STOP_LOOP(); +} + +static void _connection_cb(nlp_h nh, nlp_connection_status_e status, void* user_data) +{ + printf("status : %d\n", status); + + STOP_LOOP(); +} + +static void STOP_LOOP() +{ + printf("quit loop\n"); + + stop_timer(); + + if (loop) + g_main_loop_quit(loop); +} + +static void WAIT_FOR_CALLBACK() +{ + stop_timer(); + + service_close_timer = g_timeout_add(WAIT_RESULT_DELAY*1000, timer_cb, NULL); + if (service_close_timer == 0) + printf("failed to create timer\n"); + + printf("start loop\n"); + + if (loop) { + g_main_loop_quit(loop); + g_main_loop_run(loop); + } + else { + printf("Can't run loop. loop NULL\n"); + } +} + +class NlpCAPIConnectTest : public testing::Test { + public: + nlp_h nh = NULL; + + virtual void SetUp() { + loop = g_main_loop_new(NULL, FALSE); + + int ret = nlp_create(&nh); + EXPECT_EQ(ret, NLP_ERROR_NONE); + EXPECT_NE(nh, nullptr); + + ret = nlp_connect(nh, _connection_cb, NULL); + EXPECT_EQ(ret, NLP_ERROR_NONE); + + WAIT_FOR_CALLBACK(); + } + virtual void TearDown() { + if (nh) + nlp_destroy(nh); + + if (loop) + g_main_loop_unref(loop); + + loop = NULL; + } +}; + +TEST_F(NlpCAPIConnectTest, utc_nlp_request_language_detect_english_p) +{ + /* language detect*/ + unsigned int request_id = 0; + + char *detect_language = nullptr; + const char *str = "Hello World"; + printf("Request language detect. str(%s)\n", str); + nlp_set_language_detect_result_cb(nh, language_detect_result_cb, &detect_language); + + nlp_request_language_detect(nh, str, &request_id); + + WAIT_FOR_CALLBACK(); + + printf("request id : %d, lang : %s\n", request_id, detect_language); + EXPECT_NE(detect_language, nullptr); + EXPECT_STREQ(detect_language, "en"); + + free(detect_language); +} + +TEST_F(NlpCAPIConnectTest, utc_nlp_request_language_detect_korean_p) +{ + /* language detect*/ + unsigned int request_id = 0; + + /* Korean */ + char *detect_language = nullptr; + const char *str = "안녕하세요"; + printf("Request language detect. str(%s)\n", str); + nlp_set_language_detect_result_cb(nh, language_detect_result_cb, &detect_language); + + nlp_request_language_detect(nh, str, &request_id); + + WAIT_FOR_CALLBACK(); + + printf("request id : %d, lang : %s\n", request_id, detect_language); + EXPECT_NE(detect_language, nullptr); + EXPECT_STREQ(detect_language, "ko"); + + free(detect_language); +} + +TEST_F(NlpCAPIConnectTest, utc_nlp_request_pos_tag_english_p) +{ + unsigned int request_id = 0; + + vector pos_tag_list; + + nlp_set_pos_tag_result_cb(nh, pos_tag_result_cb, (void *)&pos_tag_list); + + /* English */ + nlp_request_pos_tag(nh, "Hello World", &request_id); + printf("request id : %d\n", request_id); + + WAIT_FOR_CALLBACK(); + + unsigned int i=0; + for (pos_tag_s pos_tag : pos_tag_list) { + printf("[%s] (%d) token: %s, tag : %s\n", __func__, i++, pos_tag.token.c_str(), pos_tag.tag.c_str()); + } + + EXPECT_EQ(pos_tag_list.size(), 2); + + EXPECT_STREQ(pos_tag_list[0].token.c_str(), "Hello"); + EXPECT_STREQ(pos_tag_list[1].token.c_str(), "World"); + + EXPECT_STREQ(pos_tag_list[0].tag.c_str(), "NNP"); + EXPECT_STREQ(pos_tag_list[1].tag.c_str(), "NNP"); +} + +TEST_F(NlpCAPIConnectTest, utc_nlp_request_word_tokenize_p) +{ + /* Word tokenize */ + unsigned int request_id = 0; + vector word_list; + const char *str = "Good morning"; + + printf("Request word tokenize. str(%s)\n", str); + nlp_set_word_tokenize_result_cb(nh, word_tokenize_result_cb, &word_list); + nlp_request_word_tokenize(nh, str, &request_id); + printf("request id : %d\n", request_id); + + WAIT_FOR_CALLBACK(); + + EXPECT_EQ(word_list.size(), 2); + + EXPECT_STREQ(word_list[0].c_str(), "Good"); + EXPECT_STREQ(word_list[1].c_str(), "morning"); +} + +} // namespace diff --git a/tests/src/nlp_capi_unittests.cpp b/tests/src/nlp_capi_unittests.cpp index bdfbfdf..687d3b3 100644 --- a/tests/src/nlp_capi_unittests.cpp +++ b/tests/src/nlp_capi_unittests.cpp @@ -22,10 +22,8 @@ #include #include -#define GMAINLOOP 1 #define WAIT_RESULT_DELAY 10 -static int g_init = false; static GMainLoop *loop = NULL; static gint service_close_timer = 0; @@ -54,85 +52,9 @@ void stop_timer() service_close_timer = 0; } -bool pos_tag_foreach_cb(const char* token, const char *tag, void* user_data) -{ - printf("token: %s, tag: %s\n", token, tag); - - return true; -} - -void pos_tag_result_cb(unsigned int request_id, nlp_pos_tag_result_h pos_tag_result_h, void *user_data) -{ - printf("[%s] request id: %d\n", __func__, request_id); - - nlp_foreach_pos_tag(pos_tag_result_h, pos_tag_foreach_cb, NULL); - - STOP_LOOP(); -} - -bool word_tokenize_foreach_cb(const char* word, void* user_data) -{ - printf("[%s] word(%s)\n", __func__, word); - - return true; -} - -void word_tokenize_result_cb(unsigned int request_id, nlp_word_tokenize_result_h word_tokenize_result_h, void *user_data) -{ - printf("[%s] request id: %d\n", __func__, request_id); - - nlp_foreach_word_tokenize(word_tokenize_result_h, word_tokenize_foreach_cb, NULL); - - STOP_LOOP(); -} - -void language_detect_result_cb(unsigned int request_id, const char *lang, void *user_data) -{ - printf("[%s] request id: %d. language: %s\n", __func__, request_id, lang); - - STOP_LOOP(); -} - static void _connection_cb(nlp_h nh, nlp_connection_status_e status, void* user_data) { printf("status : %d\n", status); - - unsigned int request_id = 0; - - /* English */ - /* language detect*/ - const char *str = "Hello World"; - printf("Request language detect. str(%s)\n", str); - nlp_set_language_detect_result_cb(nh, language_detect_result_cb, NULL); - nlp_request_language_detect(nh, str, &request_id); - printf("request id : %d\n", request_id); - - WAIT_FOR_CALLBACK(); - - str = "안녕하세요"; - printf("Request language detect. str(%s)\n", str); - nlp_request_language_detect(nh, str, &request_id); - printf("request id : %d\n", request_id); - - WAIT_FOR_CALLBACK(); - - /* POS tag */ - str = "Hello World"; - printf("Request Pos tag. str(%s)\n", str); - nlp_set_pos_tag_result_cb(nh, pos_tag_result_cb, NULL); - - nlp_request_pos_tag(nh, str, &request_id); - printf("request id : %d\n", request_id); - - WAIT_FOR_CALLBACK(); - - /* Word tokenize */ - printf("Request word tokenize. str(%s)\n", str); - nlp_set_word_tokenize_result_cb(nh, word_tokenize_result_cb, NULL); - nlp_request_word_tokenize(nh, str, &request_id); - printf("request id : %d\n", request_id); - - WAIT_FOR_CALLBACK(); } static void STOP_LOOP() @@ -166,10 +88,6 @@ class NlpCAPITest : public testing::Test { nlp_h nh = NULL; virtual void SetUp() { - if (!g_init) { - g_init = true; - } - loop = g_main_loop_new(NULL, FALSE); int ret = nlp_create(&nh); -- 2.7.4