From 00b20f25d47d126f28cc4e87f8cc9e4e63564863 Mon Sep 17 00:00:00 2001 From: Gichan Jang Date: Wed, 25 Aug 2021 18:29:50 +0900 Subject: [PATCH] [TEST] Add tensor query tests Add tensor query tests. line coverage increased from 6.6% to 77.6%. Signed-off-by: gichan Signed-off-by: Gichan Jang --- tests/meson.build | 10 + tests/nnstreamer_query/runTest.sh | 52 ++++++ tests/nnstreamer_query/unittest_query.cc | 311 +++++++++++++++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 tests/nnstreamer_query/runTest.sh create mode 100644 tests/nnstreamer_query/unittest_query.cc diff --git a/tests/meson.build b/tests/meson.build index cf5cbfc..967dbcd 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -132,6 +132,16 @@ if gtest_dep.found() test('unittest_join', unittest_join, env: testenv) + # Run unittest_query + unittest_query = executable('unittest_query', + join_paths('nnstreamer_query', 'unittest_query.cc'), + dependencies: [nnstreamer_unittest_deps, unittest_util_dep], + install: get_option('install-test'), + install_dir: unittest_install_dir + ) + + test('unittest_query', unittest_query, env: testenv) + # Run unittest_mqtt if mqtt_support_is_available unittest_mqtt_w_helper = executable('unittest_mqtt_w_helper', diff --git a/tests/nnstreamer_query/runTest.sh b/tests/nnstreamer_query/runTest.sh new file mode 100644 index 0000000..5f47971 --- /dev/null +++ b/tests/nnstreamer_query/runTest.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: LGPL-2.1-only +# +# @file runTest.sh +# @author Gichan Jang +# @date Aug 25 2021 +# @brief SSAT Test Cases for tensor query +# +if [[ "$SSATAPILOADED" != "1" ]]; then + SILENT=0 + INDEPENDENT=1 + search="ssat-api.sh" + source $search + printf "${Blue}Independent Mode${NC} +" +fi + +# This is compatible with SSAT (https://github.com/myungjoo/SSAT) +testInit $1 + +PATH_TO_PLUGIN="../../build" +PERFORMANCE=0 +TIMEOUT_SEC=5 + +# Run tensor query server as echo server with default adress option. +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} tensor_query_serversrc num-buffers=3 ! other/tensors,num_tensors=1,dimensions=3:300:300:1,types=uint8 ! tensor_query_serversink" 1-1 0 0 $PERFORMANCE $TIMEOUT_SEC & +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} videotestsrc num-buffers=3 ! videoconvert ! videoscale ! video/x-raw,width=300,height=300,format=RGB ! tensor_converter ! tee name = t t. ! queue ! multifilesink location= raw1_%1d.log t. ! queue ! tensor_query_client ! multifilesink location=result1_%1d.log" 1-2 0 0 $PERFORMANCE +callCompareTest raw1_0.log result1_0.log 1-3 "Compare 1-3" 1 0 +callCompareTest raw1_1.log result1_1.log 1-4 "Compare 1-4" 1 0 +callCompareTest raw1_2.log result1_2.log 1-5 "Compare 1-5" 1 0 + +# Run tensor query server as echo server with given address option. (multi clients) +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} tensor_query_serversrc host=127.0.0.1 port=5001 num-buffers=6 ! other/tensors,num_tensors=1,dimensions=3:300:300:1,types=uint8 ! tensor_query_serversink host=127.0.0.1 port=5002" 2-1 0 0 $PERFORMANCE $TIMEOUT_SEC & +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} videotestsrc num-buffers=3 ! videoconvert ! videoscale ! video/x-raw,width=300,height=300,format=RGB ! tensor_converter ! tee name = t t. ! queue ! multifilesink location= raw2_%1d.log t. ! queue ! tensor_query_client src-host=127.0.0.1 src-port=5001 sink-host=127.0.0.1 sink-port=5002 ! multifilesink location=result2_%1d.log" 2-2 0 0 $PERFORMANCE & +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} videotestsrc num-buffers=3 ! videoconvert ! videoscale ! video/x-raw,width=300,height=300,format=RGB ! tensor_converter ! tee name = t t. ! queue ! multifilesink location= raw2_2_%1d.log t. ! queue ! tensor_query_client src-host=127.0.0.1 src-port=5001 sink-host=127.0.0.1 sink-port=5002 ! multifilesink location=result2_2_%1d.log" 2-3 0 0 $PERFORMANCE +callCompareTest raw2_0.log result2_0.log 2-4 "Compare 2-4" 1 0 +callCompareTest raw2_1.log result2_1.log 2-5 "Compare 2-5" 1 0 +callCompareTest raw2_2.log result2_2.log 2-6 "Compare 2-6" 1 0 +callCompareTest raw2_2_0.log result2_2_0.log 2-7 "Compare 2-7" 1 0 +callCompareTest raw2_2_1.log result2_2_1.log 2-8 "Compare 2-8" 1 0 +callCompareTest raw2_2_2.log result2_2_2.log 2-9 "Compare 2-9" 1 0 + +# Test flexible tensors +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} tensor_query_serversrc num-buffers=3 ! other/tensors,format=flexible ! tensor_query_serversink" 3-1 0 0 $PERFORMANCE $TIMEOUT_SEC & +gstTest "--gst-plugin-path=${PATH_TO_PLUGIN} videotestsrc num-buffers=3 ! videoconvert ! videoscale ! video/x-raw,width=300,height=300,format=RGB ! tensor_converter ! other/tensors,format=flexible ! tee name = t t. ! queue ! multifilesink location= raw3_%1d.log t. ! queue ! tensor_query_client ! multifilesink location=result3_%1d.log" 3-2 0 0 $PERFORMANCE +callCompareTest raw3_0.log result3_0.log 3-3 "Compare 3-3" 1 0 +callCompareTest raw3_1.log result3_1.log 3-4 "Compare 3-4" 1 0 +callCompareTest raw3_2.log result3_2.log 3-5 "Compare 3-5" 1 0 + +rm *.log + +report diff --git a/tests/nnstreamer_query/unittest_query.cc b/tests/nnstreamer_query/unittest_query.cc new file mode 100644 index 0000000..6972200 --- /dev/null +++ b/tests/nnstreamer_query/unittest_query.cc @@ -0,0 +1,311 @@ +/** + * @file unittest_query.cc + * @date 27 Aug 2021 + * @brief Unit test for tensor_query + * @see https://github.com/nnstreamer/nnstreamer + * @author Gichan Jang + * @bug No known bugs + */ + +#include +#include +#include +#include +#include +#include +#include "../gst/nnstreamer/tensor_query/tensor_query_common.h" + +/** + * @brief Test for tensor_query_server get and set properties + */ +TEST (tensorQuery, serverProperties0) +{ + gchar *pipeline; + GstElement *gstpipe; + GstElement *srv_handle; + gint int_val; + guint uint_val; + gchar *str_val; + + /* Create a nnstreamer pipeline */ + pipeline = g_strdup_printf ( + "tensor_query_serversrc host=127.0.0.1 name=serversrc ! " + "other/tensors,num_tensors=1,dimensions=3:300:300:1,types=uint8 ! " + "tensor_query_serversink host=127.0.0.1 name=serversink"); + gstpipe = gst_parse_launch (pipeline, NULL); + EXPECT_NE (pipeline, nullptr); + + /* Get properties of query server source */ + srv_handle = gst_bin_get_by_name (GST_BIN (gstpipe), "serversrc"); + EXPECT_NE (srv_handle, nullptr); + + g_object_get (srv_handle, "host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.1", str_val); + g_free (str_val); + + g_object_get (srv_handle, "port", &uint_val, NULL); + EXPECT_EQ (3001U, uint_val); + + g_object_get (srv_handle, "protocol", &int_val, NULL); + EXPECT_EQ (0, int_val); + + g_object_get (srv_handle, "timeout", &uint_val, NULL); + EXPECT_EQ (10U, uint_val); + + /* Set properties of query server source */ + g_object_set (srv_handle, "host", "127.0.0.2", NULL); + g_object_get (srv_handle, "host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.2", str_val); + g_free (str_val); + + g_object_set (srv_handle, "port", 5001U, NULL); + g_object_get (srv_handle, "port", &uint_val, NULL); + EXPECT_EQ (5001U, uint_val); + + g_object_set (srv_handle, "protocol", 1, NULL); + g_object_get (srv_handle, "protocol", &int_val, NULL); + EXPECT_EQ (1, int_val); + + + g_object_set (srv_handle, "timeout", 20U, NULL); + g_object_get (srv_handle, "timeout", &uint_val, NULL); + EXPECT_EQ (20U, uint_val); + + gst_object_unref (srv_handle); + + /* Get properties of query server sink */ + srv_handle = gst_bin_get_by_name (GST_BIN (gstpipe), "serversink"); + EXPECT_NE (srv_handle, nullptr); + g_object_get (srv_handle, "host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.1", str_val); + g_free (str_val); + + g_object_get (srv_handle, "port", &uint_val, NULL); + EXPECT_EQ (3000U, uint_val); + + g_object_get (srv_handle, "protocol", &int_val, NULL); + EXPECT_EQ (0, int_val); + + g_object_get (srv_handle, "timeout", &uint_val, NULL); + EXPECT_EQ (10U, uint_val); + + /* Set properties of query server sink */ + g_object_set (srv_handle, "host", "127.0.0.2", NULL); + g_object_get (srv_handle, "host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.2", str_val); + g_free (str_val); + + g_object_set (srv_handle, "port", 5000U, NULL); + g_object_get (srv_handle, "port", &uint_val, NULL); + EXPECT_EQ (5000U, uint_val); + + g_object_set (srv_handle, "protocol", 1, NULL); + g_object_get (srv_handle, "protocol", &int_val, NULL); + EXPECT_EQ (1, int_val); + + + g_object_set (srv_handle, "timeout", 20U, NULL); + g_object_get (srv_handle, "timeout", &uint_val, NULL); + EXPECT_EQ (20U, uint_val); + + gst_object_unref (srv_handle); + gst_object_unref (gstpipe); + g_free (pipeline); +} + +/** + * @brief Test for tensor_query_server with same port. + */ +TEST (tensorQuery, serverProperties1_n) +{ + gchar *pipeline; + GstElement *gstpipe; + + /* Create a nnstreamer pipeline */ + pipeline = g_strdup_printf ( + "tensor_query_serversrc name=serversrc port=3000 ! " + "other/tensors,num_tensors=1,dimensions=3:300:300:1,types=uint8 ! " + "tensor_query_serversink port=3000 sync=false async=false"); + gstpipe = gst_parse_launch (pipeline, NULL); + EXPECT_NE (pipeline, nullptr); + + EXPECT_NE (setPipelineStateSync (gstpipe, GST_STATE_PLAYING, UNITTEST_STATECHANGE_TIMEOUT), 0); + + gst_object_unref (gstpipe); + g_free (pipeline); +} + +/** + * @brief Test for tensor_query_server with invalid host name. + */ +TEST (tensorQuery, serverProperties2_n) +{ + gchar *pipeline; + GstElement *gstpipe; + + /* Create a nnstreamer pipeline */ + pipeline = g_strdup_printf ( + "tensor_query_serversrc name=serversrc host=f.a.i.l ! " + "other/tensors,num_tensors=1,dimensions=3:300:300:1,types=uint8 ! " + "tensor_query_serversink sync=false async=false"); + gstpipe = gst_parse_launch (pipeline, NULL); + EXPECT_NE (pipeline, nullptr); + + EXPECT_NE (setPipelineStateSync (gstpipe, GST_STATE_PLAYING, UNITTEST_STATECHANGE_TIMEOUT), 0); + + gst_object_unref (gstpipe); + g_free (pipeline); +} + +/** + * @brief Test for tensor_query_client get and set properties + */ +TEST (tensorQuery, clientProperties0) +{ + gchar *pipeline; + GstElement *gstpipe; + GstElement *client_handle; + guint uint_val; + gchar *str_val; + gboolean bool_val; + + /* Create a query client pipeline */ + pipeline = g_strdup_printf ( + "videotestsrc ! videoconvert ! videoscale ! video/x-raw,width=300,height=300,format=RGB !" + "tensor_converter ! tensor_query_client name=client protocol=tcp src-host=127.0.0.1 sink-host=127.0.0.1 ! tensor_sink"); + gstpipe = gst_parse_launch (pipeline, NULL); + EXPECT_NE (gstpipe, nullptr); + + /* Get properties of query client */ + client_handle = gst_bin_get_by_name (GST_BIN (gstpipe), "client"); + EXPECT_NE (client_handle, nullptr); + + g_object_get (client_handle, "src-host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.1", str_val); + g_free (str_val); + + g_object_get (client_handle, "src-port", &uint_val, NULL); + EXPECT_EQ (3001U, uint_val); + + g_object_get (client_handle, "sink-host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.1", str_val); + g_free (str_val); + + g_object_get (client_handle, "sink-port", &uint_val, NULL); + EXPECT_EQ (3000U, uint_val); + + g_object_get (client_handle, "protocol", &str_val, NULL); + EXPECT_STREQ ("tcp", str_val); + g_free (str_val); + + g_object_get (client_handle, "silent", &bool_val, NULL); + EXPECT_EQ (TRUE, bool_val); + + /* Set properties of query client */ + g_object_set (client_handle, "src-host", "127.0.0.2", NULL); + g_object_get (client_handle, "src-host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.2", str_val); + g_free (str_val); + + g_object_set (client_handle, "src-port", 5001U, NULL); + g_object_get (client_handle, "src-port", &uint_val, NULL); + EXPECT_EQ (5001U, uint_val); + + g_object_set (client_handle, "sink-host", "127.0.0.2", NULL); + g_object_get (client_handle, "sink-host", &str_val, NULL); + EXPECT_STREQ ("127.0.0.2", str_val); + g_free (str_val); + + g_object_set (client_handle, "sink-port", 5001U, NULL); + g_object_get (client_handle, "sink-port", &uint_val, NULL); + EXPECT_EQ (5001U, uint_val); + + g_object_set (client_handle, "silent", FALSE, NULL); + g_object_get (client_handle, "silent", &bool_val, NULL); + EXPECT_EQ (FALSE, bool_val); + + gst_object_unref (client_handle); + gst_object_unref (gstpipe); + g_free (pipeline); +} + +/** + * @brief Run tensor query client without server + */ +TEST (tensorQuery, clientAlone_n) +{ + gchar *pipeline; + GstElement *gstpipe; + + /* Create a query client pipeline */ + pipeline = g_strdup_printf ( + "videotestsrc ! videoconvert ! videoscale ! video/x-raw,width=300,height=300,format=RGB !" + "tensor_converter ! tensor_query_client ! tensor_sink"); + gstpipe = gst_parse_launch (pipeline, NULL); + EXPECT_NE (gstpipe, nullptr); + + EXPECT_NE (setPipelineStateSync (gstpipe, GST_STATE_PLAYING, UNITTEST_STATECHANGE_TIMEOUT), 0); + + gst_object_unref (gstpipe); + g_free (pipeline); +} + +/** + * @brief Test for nnstreamer_query_server_init + */ +TEST (tensorQueryCommon, serverInit0) +{ + query_server_handle server_data = NULL; + server_data = nnstreamer_query_server_data_new (); + EXPECT_NE ((void *) NULL, server_data); + + EXPECT_EQ (0, nnstreamer_query_server_init (server_data, _TENSOR_QUERY_PROTOCOL_TCP, "localhost", 3001)); + nnstreamer_query_server_data_free (server_data); +} + +/** + * @brief Test for nnstreamer_query_server_init with invalid parameter. + */ +TEST (tensorQueryCommon, serverInit1_n) +{ + EXPECT_NE (0, nnstreamer_query_server_init (NULL, _TENSOR_QUERY_PROTOCOL_TCP, "localhost", 3001)); +} + +/** + * @brief Test for nnstreamer_query_server_init with invalid parameter. + */ +TEST (tensorQueryCommon, serverInit2_n) +{ + query_server_handle server_data = NULL; + server_data = nnstreamer_query_server_data_new (); + EXPECT_NE ((void *) NULL, server_data); + + EXPECT_NE (0, nnstreamer_query_server_init (server_data, _TENSOR_QUERY_PROTOCOL_END, "localhost", 3001)); + + nnstreamer_query_server_data_free (server_data); +} + +/** + * @brief Main GTest + */ +int +main (int argc, char **argv) +{ + int result = -1; + + try { + testing::InitGoogleTest (&argc, argv); + } catch (...) { + g_warning ("catch 'testing::internal::::ClassUniqueToAlwaysTrue'"); + } + + gst_init (&argc, &argv); + + try { + result = RUN_ALL_TESTS (); + } catch (...) { + g_warning ("catch `testing::internal::GoogleTestFailureException`"); + } + + return result; +} -- 2.7.4