xref: /openbmc/linux/tools/perf/tests/util.c (revision a3be6776)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "tests.h"
3 #include "util/debug.h"
4 
5 #include <linux/compiler.h>
6 #include <stdlib.h>
7 #include <string2.h>
8 
9 static int test_strreplace(char needle, const char *haystack,
10 			   const char *replace, const char *expected)
11 {
12 	char *new = strreplace_chars(needle, haystack, replace);
13 	int ret = strcmp(new, expected);
14 
15 	free(new);
16 	return ret == 0;
17 }
18 
19 static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
20 {
21 	TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", ""));
22 	TEST_ASSERT_VAL("no match", test_strreplace('5', "123", "4", "123"));
23 	TEST_ASSERT_VAL("replace 1", test_strreplace('3', "123", "4", "124"));
24 	TEST_ASSERT_VAL("replace 2", test_strreplace('a', "abcabc", "ef", "efbcefbc"));
25 	TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong",
26 							"longlongbclonglongbc"));
27 
28 	return 0;
29 }
30 
31 DEFINE_SUITE("util", util);
32