1/*
2 * Copyright 2018, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "DemangleTest.h"
8
9#include <cppunit/TestCaller.h>
10#include <cppunit/TestSuite.h>
11
12#include "Demangler.h"
13#include "demangle.h"
14
15
16DemangleTest::DemangleTest()
17{
18}
19
20
21DemangleTest::~DemangleTest()
22{
23}
24
25
26#define TEST(expect, input) \
27	NextSubTest(); \
28	CPPUNIT_ASSERT_EQUAL(BString(expect), Demangler::Demangle(input))
29void
30DemangleTest::RunGCC2Tests()
31{
32	// Long and complex things
33	TEST("BPrivate::IconCache::SyncDraw(BPrivate::Model*, BView*, BPoint, BPrivate::IconDrawMode, icon_size, void*, void*)",
34		"SyncDraw__Q28BPrivate9IconCachePQ28BPrivate5ModelP5BViewG6BPointQ28BPrivate12IconDrawMode9icon_sizePFP5BViewG6BPointP7BBitmapPv_vPv");
35	TEST("BPrivate::BContainerWindow::UpdateMenu(BMenu*, BPrivate::BContainerWindow::UpdateMenuContext)",
36		"UpdateMenu__Q28BPrivate16BContainerWindowP5BMenuQ38BPrivate16BContainerWindow17UpdateMenuContext");
37	TEST("icu_57::BreakIterator::registerInstance(icu_57::BreakIterator*, icu_57::Locale&, UBreakIteratorType, UErrorCode&)",
38		"registerInstance__Q26icu_5713BreakIteratorPQ26icu_5713BreakIteratorRCQ26icu_576Locale18UBreakIteratorTypeR10UErrorCode");
39
40	// Previously caused crashes
41	TEST("_GLOBAL_::SetTo()", "SetTo__Q282_GLOBAL_");
42}
43
44
45void
46DemangleTest::RunGCC3PTests()
47{
48	// Long and complex things
49	TEST("BPrivate::IconCache::SyncDraw(BPrivate::Model*, BView*, BPoint, BPrivate::IconDrawMode, icon_size, void (*)(BView*, BPoint, BBitmap*, void*), void*)",
50		"_ZN8BPrivate9IconCache8SyncDrawEPNS_5ModelEP5BView6BPointNS_12IconDrawModeE9icon_sizePFvS4_S5_P7BBitmapPvESA_");
51	TEST("BPrivate::BContainerWindow::UpdateMenu(BMenu*, BPrivate::BContainerWindow::UpdateMenuContext)",
52		"_ZN8BPrivate16BContainerWindow10UpdateMenuEP5BMenuNS0_17UpdateMenuContextE");
53	TEST("icu_57::BreakIterator::registerInstance(icu_57::BreakIterator*, icu_57::Locale const&, UBreakIteratorType, UErrorCode&)",
54		"_ZN6icu_5713BreakIterator16registerInstanceEPS0_RKNS_6LocaleE18UBreakIteratorTypeR10UErrorCode");
55	TEST("void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) [clone .isra.25]",
56		"_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag.isra.25");
57	TEST("foo(int) [clone .part.1.123456] [clone .constprop.777.54321]", "_Z3fooi.part.1.123456.constprop.777.54321");
58
59	// Names independent of the full symbol
60	char buffer[1024];
61	NextSubTest();
62	demangle_symbol_gcc3("_Z3fooi.part.1.123456.constprop.777.1", buffer, sizeof(buffer), NULL);
63	CPPUNIT_ASSERT_EQUAL(BString("foo[clone .part.1.123456] [clone .constprop.777.1] "), buffer);
64}
65#undef TEST
66
67
68/* static */ void
69DemangleTest::AddTests(BTestSuite& parent)
70{
71	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DemangleTest");
72
73	suite.addTest(new CppUnit::TestCaller<DemangleTest>(
74		"DemangleTest::RunGCC2Tests", &DemangleTest::RunGCC2Tests));
75	suite.addTest(new CppUnit::TestCaller<DemangleTest>(
76		"DemangleTest::RunGCC3+Tests", &DemangleTest::RunGCC3PTests));
77
78	parent.addTest("DemangleTest", &suite);
79}
80