1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
4 // Unit tests for functions in demangle.c.
9 #include "glog/logging.h"
11 #include "googletest.h"
14 DEFINE_bool(demangle_filter, false, "Run demangle_unittest in filter mode");
17 using namespace GOOGLE_NAMESPACE;
19 // A wrapper function for Demangle() to make the unit test simple.
20 static const char *DemangleIt(const char * const mangled) {
21 static char demangled[4096];
22 if (Demangle(mangled, demangled, sizeof(demangled))) {
29 // Test corner cases of bounary conditions.
30 TEST(Demangle, CornerCases) {
32 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
33 // sizeof("foobar()") == 9
34 EXPECT_STREQ("foobar()", tmp);
35 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
36 EXPECT_STREQ("foobar()", tmp);
37 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8)); // Not enough.
38 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
39 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
40 EXPECT_FALSE(Demangle("_Z6foobarv", NULL, 0)); // Should not cause SEGV.
43 TEST(Demangle, FromFile) {
44 string test_file = FLAGS_test_srcdir + "/src/demangle_unittest.txt";
45 ifstream f(test_file.c_str()); // The file should exist.
46 EXPECT_FALSE(f.fail());
49 while (getline(f, line)) {
50 // Lines start with '#' are considered as comments.
51 if (line.empty() || line[0] == '#') {
54 // Each line should contain a mangled name and a demangled name
55 // separated by '\t'. Example: "_Z3foo\tfoo"
56 string::size_type tab_pos = line.find('\t');
57 EXPECT_NE(string::npos, tab_pos);
58 string mangled = line.substr(0, tab_pos);
59 string demangled = line.substr(tab_pos + 1);
60 EXPECT_EQ(demangled, DemangleIt(mangled.c_str()));
64 int main(int argc, char **argv) {
65 #ifdef HAVE_LIB_GFLAGS
66 ParseCommandLineFlags(&argc, &argv, true);
68 FLAGS_logtostderr = true;
69 InitGoogleLogging(argv[0]);
70 if (FLAGS_demangle_filter) {
71 // Read from cin and write to cout.
73 while (getline(cin, line, '\n')) {
74 cout << DemangleIt(line.c_str()) << endl;
77 } else if (argc > 1) {
78 cout << DemangleIt(argv[1]) << endl;
81 return RUN_ALL_TESTS();