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