Reduce dynamic allocation from 3 to 1 per log message
[platform/upstream/glog.git] / src / demangle_unittest.cc
1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Satoru Takabayashi
31 //
32 // Unit tests for functions in demangle.c.
33
34 #include "utilities.h"
35
36 #include <iostream>
37 #include <fstream>
38 #include <string>
39 #include "glog/logging.h"
40 #include "demangle.h"
41 #include "googletest.h"
42 #include "config.h"
43
44 GLOG_DEFINE_bool(demangle_filter, false,
45                  "Run demangle_unittest in filter mode");
46
47 using namespace std;
48 using namespace GOOGLE_NAMESPACE;
49
50 // A wrapper function for Demangle() to make the unit test simple.
51 static const char *DemangleIt(const char * const mangled) {
52   static char demangled[4096];
53   if (Demangle(mangled, demangled, sizeof(demangled))) {
54     return demangled;
55   } else {
56     return mangled;
57   }
58 }
59
60 // Test corner cases of bounary conditions.
61 TEST(Demangle, CornerCases) {
62   char tmp[10];
63   EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
64   // sizeof("foobar()") == 9
65   EXPECT_STREQ("foobar()", tmp);
66   EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
67   EXPECT_STREQ("foobar()", tmp);
68   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8));  // Not enough.
69   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
70   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
71   EXPECT_FALSE(Demangle("_Z6foobarv", NULL, 0));  // Should not cause SEGV.
72 }
73
74 // Test handling of functions suffixed with .clone.N, which is used by GCC
75 // 4.5.x, and .constprop.N and .isra.N, which are used by GCC 4.6.x.  These
76 // suffixes are used to indicate functions which have been cloned during
77 // optimization.  We ignore these suffixes.
78 TEST(Demangle, Clones) {
79   char tmp[20];
80   EXPECT_TRUE(Demangle("_ZL3Foov", tmp, sizeof(tmp)));
81   EXPECT_STREQ("Foo()", tmp);
82   EXPECT_TRUE(Demangle("_ZL3Foov.clone.3", tmp, sizeof(tmp)));
83   EXPECT_STREQ("Foo()", tmp);
84   EXPECT_TRUE(Demangle("_ZL3Foov.constprop.80", tmp, sizeof(tmp)));
85   EXPECT_STREQ("Foo()", tmp);
86   EXPECT_TRUE(Demangle("_ZL3Foov.isra.18", tmp, sizeof(tmp)));
87   EXPECT_STREQ("Foo()", tmp);
88   EXPECT_TRUE(Demangle("_ZL3Foov.isra.2.constprop.18", tmp, sizeof(tmp)));
89   EXPECT_STREQ("Foo()", tmp);
90   // Invalid (truncated), should not demangle.
91   EXPECT_FALSE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
92   // Invalid (.clone. not followed by number), should not demangle.
93   EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
94   // Invalid (.clone. followed by non-number), should not demangle.
95   EXPECT_FALSE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
96   // Invalid (.constprop. not followed by number), should not demangle.
97   EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
98 }
99
100 TEST(Demangle, FromFile) {
101   string test_file = FLAGS_test_srcdir + "/src/demangle_unittest.txt";
102   ifstream f(test_file.c_str());  // The file should exist.
103   EXPECT_FALSE(f.fail());
104
105   string line;
106   while (getline(f, line)) {
107     // Lines start with '#' are considered as comments.
108     if (line.empty() || line[0] == '#') {
109       continue;
110     }
111     // Each line should contain a mangled name and a demangled name
112     // separated by '\t'.  Example: "_Z3foo\tfoo"
113     string::size_type tab_pos = line.find('\t');
114     EXPECT_NE(string::npos, tab_pos);
115     string mangled = line.substr(0, tab_pos);
116     string demangled = line.substr(tab_pos + 1);
117     EXPECT_EQ(demangled, DemangleIt(mangled.c_str()));
118   }
119 }
120
121 int main(int argc, char **argv) {
122 #ifdef HAVE_LIB_GFLAGS
123   ParseCommandLineFlags(&argc, &argv, true);
124 #endif
125   InitGoogleTest(&argc, argv);
126
127   FLAGS_logtostderr = true;
128   InitGoogleLogging(argv[0]);
129   if (FLAGS_demangle_filter) {
130     // Read from cin and write to cout.
131     string line;
132     while (getline(cin, line, '\n')) {
133       cout << DemangleIt(line.c_str()) << endl;
134     }
135     return 0;
136   } else if (argc > 1) {
137     cout << DemangleIt(argv[1]) << endl;
138     return 0;
139   } else {
140     return RUN_ALL_TESTS();
141   }
142 }