xref: /linux/tools/perf/tests/demangle-ocaml-test.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "debug.h"
6 #include "symbol.h"
7 #include "tests.h"
8 
test__demangle_ocaml(struct test_suite * test __maybe_unused,int subtest __maybe_unused)9 static int test__demangle_ocaml(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
10 {
11 	int ret = TEST_OK;
12 	char *buf = NULL;
13 	size_t i;
14 
15 	struct {
16 		const char *mangled, *demangled;
17 	} test_cases[] = {
18 		{ "main",
19 		  NULL },
20 		{ "camlStdlib__array__map_154",
21 		  "Stdlib.array.map_154" },
22 		{ "camlStdlib__anon_fn$5bstdlib$2eml$3a334$2c0$2d$2d54$5d_1453",
23 		  "Stdlib.anon_fn[stdlib.ml:334,0--54]_1453" },
24 		{ "camlStdlib__bytes__$2b$2b_2205",
25 		  "Stdlib.bytes.++_2205" },
26 	};
27 
28 	for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
29 		buf = dso__demangle_sym(/*dso=*/NULL, /*kmodule=*/0, test_cases[i].mangled);
30 		if ((buf == NULL && test_cases[i].demangled != NULL)
31 				|| (buf != NULL && test_cases[i].demangled == NULL)
32 				|| (buf != NULL && strcmp(buf, test_cases[i].demangled))) {
33 			pr_debug("FAILED: %s: %s != %s\n", test_cases[i].mangled,
34 				 buf == NULL ? "(null)" : buf,
35 				 test_cases[i].demangled == NULL ? "(null)" : test_cases[i].demangled);
36 			ret = TEST_FAIL;
37 		}
38 		free(buf);
39 	}
40 
41 	return ret;
42 }
43 
44 DEFINE_SUITE("Demangle OCaml", demangle_ocaml);
45