From 45b1f109b6c69209bd98f104b2495f83853f370f Mon Sep 17 00:00:00 2001 From: gichan Date: Wed, 5 Jan 2022 12:27:59 +0900 Subject: [PATCH] [TEST] Move replace_string to unittest_util. Since `repace_string` is copied to nnstreamer/api and `replace_string` is only used for unit test, moves it to unit test util. And the test related to `replace_string` is moved to nnstreamer/api. Test to replace string to check unintended change. Signed-off-by: gichan --- gst/nnstreamer/include/nnstreamer_plugin_api.h | 13 ----- gst/nnstreamer/tensor_common.c | 79 -------------------------- tests/common/unittest_common.cc | 58 ------------------- tests/unittest_util.c | 79 ++++++++++++++++++++++++++ tests/unittest_util.h | 13 +++++ 5 files changed, 92 insertions(+), 150 deletions(-) diff --git a/gst/nnstreamer/include/nnstreamer_plugin_api.h b/gst/nnstreamer/include/nnstreamer_plugin_api.h index d07b08b..4469cbb 100644 --- a/gst/nnstreamer/include/nnstreamer_plugin_api.h +++ b/gst/nnstreamer/include/nnstreamer_plugin_api.h @@ -403,19 +403,6 @@ extern gint find_key_strv (const gchar ** strv, const gchar * key); /** - * @brief Replaces string. - * This function deallocates the input source string. - * @param[in] source The input string. This will be freed when returning the replaced string. - * @param[in] what The string to search for. - * @param[in] to The string to be replaced. - * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string. - * @param[out] count The count of replaced. Set NULL if it is unnecessary. - * @return Newly allocated string. The returned string should be freed with g_free(). - */ -extern gchar * -replace_string (gchar * source, const gchar * what, const gchar * to, const gchar * delimiters, guint * count); - -/** * @brief Initialize the tensor meta info structure. * @param[in,out] meta tensor meta structure to be initialized */ diff --git a/gst/nnstreamer/tensor_common.c b/gst/nnstreamer/tensor_common.c index fee9930..dd783e2 100644 --- a/gst/nnstreamer/tensor_common.c +++ b/gst/nnstreamer/tensor_common.c @@ -1852,85 +1852,6 @@ find_key_strv (const gchar ** strv, const gchar * key) } /** - * @brief Replaces string. - * This function deallocates the input source string. - * @param[in] source The input string. This will be freed when returning the replaced string. - * @param[in] what The string to search for. - * @param[in] to The string to be replaced. - * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string. - * @param[out] count The count of replaced. Set NULL if it is unnecessary. - * @return Newly allocated string. The returned string should be freed with g_free(). - */ -gchar * -replace_string (gchar * source, const gchar * what, const gchar * to, - const gchar * delimiters, guint * count) -{ - GString *builder; - gchar *start, *pos, *result; - guint changed = 0; - gsize len; - - g_return_val_if_fail (source, NULL); - g_return_val_if_fail (what && to, source); - - len = strlen (what); - start = source; - - builder = g_string_new (NULL); - while ((pos = g_strstr_len (start, -1, what)) != NULL) { - gboolean skip = FALSE; - - if (delimiters) { - const gchar *s; - gchar *prev, *next; - gboolean prev_split, next_split; - - prev = next = NULL; - prev_split = next_split = FALSE; - - if (pos != source) - prev = pos - 1; - if (*(pos + len) != '\0') - next = pos + len; - - for (s = delimiters; *s != '\0'; ++s) { - if (!prev || *s == *prev) - prev_split = TRUE; - if (!next || *s == *next) - next_split = TRUE; - if (prev_split && next_split) - break; - } - - if (!prev_split || !next_split) - skip = TRUE; - } - - builder = g_string_append_len (builder, start, pos - start); - - /* replace string if found */ - if (skip) - builder = g_string_append_len (builder, pos, len); - else - builder = g_string_append (builder, to); - - start = pos + len; - if (!skip) - changed++; - } - - /* append remains */ - builder = g_string_append (builder, start); - result = g_string_free (builder, FALSE); - - if (count) - *count = changed; - - g_free (source); - return result; -} - -/** * @brief Get the version of NNStreamer (string). * @return Newly allocated string. The returned string should be freed with g_free(). */ diff --git a/tests/common/unittest_common.cc b/tests/common/unittest_common.cc index 91e8128..0b40b82 100644 --- a/tests/common/unittest_common.cc +++ b/tests/common/unittest_common.cc @@ -1450,64 +1450,6 @@ TEST (commonMetaInfo, convertMetaInvalidParam03_n) } /** - * @brief Test to replace string. - */ -TEST (commonStringUtil, replaceStr01) -{ - gchar *result; - guint changed; - - result = g_strdup ("sourceelement ! parser ! converter ! format ! converter ! format ! converter ! sink"); - - result = replace_string (result, "sourceelement", "src", NULL, &changed); - EXPECT_EQ (changed, 1U); - EXPECT_STREQ (result, "src ! parser ! converter ! format ! converter ! format ! converter ! sink"); - - result = replace_string (result, "format", "fmt", NULL, &changed); - EXPECT_EQ (changed, 2U); - EXPECT_STREQ (result, "src ! parser ! converter ! fmt ! converter ! fmt ! converter ! sink"); - - result = replace_string (result, "converter", "conv", NULL, &changed); - EXPECT_EQ (changed, 3U); - EXPECT_STREQ (result, "src ! parser ! conv ! fmt ! conv ! fmt ! conv ! sink"); - - result = replace_string (result, "invalidname", "invalid", NULL, &changed); - EXPECT_EQ (changed, 0U); - EXPECT_STREQ (result, "src ! parser ! conv ! fmt ! conv ! fmt ! conv ! sink"); - - g_free (result); -} - -/** - * @brief Test to replace string. - */ -TEST (commonStringUtil, replaceStr02) -{ - gchar *result; - guint changed; - - result = g_strdup ("source! parser ! sources ! mysource ! source ! format !source! conv source"); - - result = replace_string (result, "source", "src", " !", &changed); - EXPECT_EQ (changed, 4U); - EXPECT_STREQ (result, "src! parser ! sources ! mysource ! src ! format !src! conv src"); - - result = replace_string (result, "src", "mysource", "! ", &changed); - EXPECT_EQ (changed, 4U); - EXPECT_STREQ (result, "mysource! parser ! sources ! mysource ! mysource ! format !mysource! conv mysource"); - - result = replace_string (result, "source", "src", NULL, &changed); - EXPECT_EQ (changed, 6U); - EXPECT_STREQ (result, "mysrc! parser ! srcs ! mysrc ! mysrc ! format !mysrc! conv mysrc"); - - result = replace_string (result, "mysrc", "src", ";", &changed); - EXPECT_EQ (changed, 0U); - EXPECT_STREQ (result, "mysrc! parser ! srcs ! mysrc ! mysrc ! format !mysrc! conv mysrc"); - - g_free (result); -} - -/** * @brief Test for aggregation utils (clear data). */ TEST (commonAggregationUtil, clearData) diff --git a/tests/unittest_util.c b/tests/unittest_util.c index 75fe326..eec8573 100644 --- a/tests/unittest_util.c +++ b/tests/unittest_util.c @@ -91,6 +91,85 @@ wait_pipeline_process_buffers (const guint * data_received, return TRUE; } +/** + * @brief Replaces string. + * This function deallocates the input source string. + * @param[in] source The input string. This will be freed when returning the replaced string. + * @param[in] what The string to search for. + * @param[in] to The string to be replaced. + * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string. + * @param[out] count The count of replaced. Set NULL if it is unnecessary. + * @return Newly allocated string. The returned string should be freed with g_free(). + */ +gchar * +replace_string (gchar * source, const gchar * what, const gchar * to, + const gchar * delimiters, guint * count) +{ + GString *builder; + gchar *start, *pos, *result; + guint changed = 0; + gsize len; + + g_return_val_if_fail (source, NULL); + g_return_val_if_fail (what && to, source); + + len = strlen (what); + start = source; + + builder = g_string_new (NULL); + while ((pos = g_strstr_len (start, -1, what)) != NULL) { + gboolean skip = FALSE; + + if (delimiters) { + const gchar *s; + gchar *prev, *next; + gboolean prev_split, next_split; + + prev = next = NULL; + prev_split = next_split = FALSE; + + if (pos != source) + prev = pos - 1; + if (*(pos + len) != '\0') + next = pos + len; + + for (s = delimiters; *s != '\0'; ++s) { + if (!prev || *s == *prev) + prev_split = TRUE; + if (!next || *s == *next) + next_split = TRUE; + if (prev_split && next_split) + break; + } + + if (!prev_split || !next_split) + skip = TRUE; + } + + builder = g_string_append_len (builder, start, pos - start); + + /* replace string if found */ + if (skip) + builder = g_string_append_len (builder, pos, len); + else + builder = g_string_append (builder, to); + + start = pos + len; + if (!skip) + changed++; + } + + /* append remains */ + builder = g_string_append (builder, start); + result = g_string_free (builder, FALSE); + + if (count) + *count = changed; + + g_free (source); + return result; +} + #ifdef FAKEDLOG /** * @brief Hijack dlog Tizen infra for unit testing to force printing out. diff --git a/tests/unittest_util.h b/tests/unittest_util.h index 542e68d..9aa78f3 100644 --- a/tests/unittest_util.h +++ b/tests/unittest_util.h @@ -48,6 +48,19 @@ extern gchar * getTempFilename (void); extern gboolean wait_pipeline_process_buffers (const guint * data_received, guint expected_num_buffers, guint timeout_ms); /** + * @brief Replaces string. + * This function deallocates the input source string. + * @param[in] source The input string. This will be freed when returning the replaced string. + * @param[in] what The string to search for. + * @param[in] to The string to be replaced. + * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string. + * @param[out] count The count of replaced. Set NULL if it is unnecessary. + * @return Newly allocated string. The returned string should be freed with g_free(). + */ +extern gchar * +replace_string (gchar * source, const gchar * what, const gchar * to, const gchar * delimiters, guint * count); + +/** * @brief Wait until the pipeline saving the file * @return TRUE on success, FALSE when a time-out occurs */ -- 2.7.4