2 * SPDX-License-Identifier: LGPL-2.1-only
4 * @file unittest_util.c
6 * @brief Unit test utility.
7 * @see https://github.com/nnstreamer/nnstreamer
8 * @author MyungJoo Ham <myungjoo.ham@samsung.com>
12 #include <glib/gstdio.h>
14 #include "unittest_util.h"
17 * @ref https://github.com/nnstreamer/nnstreamer/commit/7f38acb78c26f0f144b6d6fe7fb887b7431d395b
19 #if !defined(ESTRPIPE)
20 #define ESTRPIPE EPIPE
21 #endif /* !defined(ESTRPIPE) */
24 * @brief Set pipeline state, wait until it's done.
25 * @return 0 success, -ESTRPIPE if failed, -ETIME if timeout happens.
28 setPipelineStateSync (GstElement * pipeline, GstState state,
31 GstState cur_state = GST_STATE_VOID_PENDING;
32 GstStateChangeReturn ret;
34 ret = gst_element_set_state (pipeline, state);
36 if (ret == GST_STATE_CHANGE_FAILURE)
40 ret = gst_element_get_state (pipeline, &cur_state, NULL, 10 * GST_MSECOND);
41 if (ret == GST_STATE_CHANGE_FAILURE)
43 if (cur_state == state)
46 } while ((timeout_ms / 20) > counter++);
51 * @brief Get temp file name.
52 * @return file name (should finalize it with g_remove() and g_free() after use)
55 getTempFilename (void)
61 if ((tmp_dir = g_get_tmp_dir ()) == NULL) {
62 _print_log ("failed to get tmp dir");
66 tmp_fn = g_build_filename (tmp_dir, "nnstreamer_unittest_temp_XXXXXX", NULL);
67 fd = g_mkstemp (tmp_fn);
70 _print_log ("failed to create temp file %s", tmp_fn);
81 * @brief Remove temp file and release file name.
83 void removeTempFile (char **file_name)
86 if (g_remove (*file_name) != 0) {
87 _print_log ("failed to remove temp file %s", *file_name);
95 * @brief Wait until the pipeline processing the buffers
96 * @return TRUE on success, FALSE when a time-out occurs
99 wait_pipeline_process_buffers (const guint * data_received,
100 guint expected_num_buffers, guint timeout_ms)
103 guint tick = TEST_DEFAULT_SLEEP_TIME / 1000U;
105 /* Waiting for expected buffers to arrive */
106 while (*data_received < expected_num_buffers) {
107 g_usleep (TEST_DEFAULT_SLEEP_TIME);
109 if (timer > timeout_ms)
116 * @brief Replaces string.
117 * This function deallocates the input source string.
118 * @param[in] source The input string. This will be freed when returning the replaced string.
119 * @param[in] what The string to search for.
120 * @param[in] to The string to be replaced.
121 * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string.
122 * @param[out] count The count of replaced. Set NULL if it is unnecessary.
123 * @return Newly allocated string. The returned string should be freed with g_free().
126 replace_string (gchar * source, const gchar * what, const gchar * to,
127 const gchar * delimiters, guint * count)
130 gchar *start, *pos, *result;
134 g_return_val_if_fail (source, NULL);
135 g_return_val_if_fail (what && to, source);
140 builder = g_string_new (NULL);
141 while ((pos = g_strstr_len (start, -1, what)) != NULL) {
142 gboolean skip = FALSE;
147 gboolean prev_split, next_split;
150 prev_split = next_split = FALSE;
154 if (*(pos + len) != '\0')
157 for (s = delimiters; *s != '\0'; ++s) {
158 if (!prev || *s == *prev)
160 if (!next || *s == *next)
162 if (prev_split && next_split)
166 if (!prev_split || !next_split)
170 builder = g_string_append_len (builder, start, pos - start);
172 /* replace string if found */
174 builder = g_string_append_len (builder, pos, len);
176 builder = g_string_append (builder, to);
184 builder = g_string_append (builder, start);
185 result = g_string_free (builder, FALSE);
195 * @brief Get available port number.
198 get_available_port (void)
200 struct sockaddr_in sin;
203 socklen_t len = sizeof (struct sockaddr);
205 sin.sin_family = AF_INET;
206 sin.sin_addr.s_addr = INADDR_ANY;
207 sin.sin_port = htons(0);
209 sock = socket (AF_INET, SOCK_STREAM, 0);
213 if (bind (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr)) == 0) {
214 if (getsockname (sock, (struct sockaddr *) &sin, &len) == 0) {
215 port = ntohs (sin.sin_port);
225 * @brief Hijack dlog Tizen infra for unit testing to force printing out.
226 * @bug The original dlog_print returns the number of bytes printed.
230 dlog_print (log_priority prio, const char *tag, const char *fmt, ...)
233 GLogLevelFlags level;
236 level = G_LOG_LEVEL_ERROR;
239 level = G_LOG_LEVEL_CRITICAL;
242 level = G_LOG_LEVEL_WARNING;
245 level = G_LOG_LEVEL_INFO;
248 level = G_LOG_LEVEL_DEBUG;
251 level = G_LOG_LEVEL_DEBUG;
253 va_start (arg_ptr, fmt);
254 g_logv (tag, level, fmt, arg_ptr);