--- /dev/null
+/*
+ * 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 <gtest/gtest.h>
+
+#include <glib.h>
+#include <string>
+#include <chrono>
+#include <thread>
+#include <list>
+
+#include <nlp.h>
+
+#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_s> *pos_tag_list = (vector<pos_tag_s> *)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<string> *word_list = (vector<string> *)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_s> 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<string> 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
#include <thread>
#include <nlp.h>
-#define GMAINLOOP 1
#define WAIT_RESULT_DELAY 10
-static int g_init = false;
static GMainLoop *loop = NULL;
static gint service_close_timer = 0;
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()
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);