1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: Satoru Takabayashi
32 // Unit tests for functions in demangle.c.
37 #include "glog/logging.h"
39 #include "googletest.h"
42 DEFINE_bool(demangle_filter, false, "Run demangle_unittest in filter mode");
45 using namespace GOOGLE_NAMESPACE;
47 // A wrapper function for Demangle() to make the unit test simple.
48 static const char *DemangleIt(const char * const mangled) {
49 static char demangled[4096];
50 if (Demangle(mangled, demangled, sizeof(demangled))) {
57 // Test corner cases of bounary conditions.
58 TEST(Demangle, CornerCases) {
60 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
61 // sizeof("foobar()") == 9
62 EXPECT_STREQ("foobar()", tmp);
63 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
64 EXPECT_STREQ("foobar()", tmp);
65 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8)); // Not enough.
66 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
67 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
68 EXPECT_FALSE(Demangle("_Z6foobarv", NULL, 0)); // Should not cause SEGV.
71 TEST(Demangle, FromFile) {
72 string test_file = FLAGS_test_srcdir + "/src/demangle_unittest.txt";
73 ifstream f(test_file.c_str()); // The file should exist.
74 EXPECT_FALSE(f.fail());
77 while (getline(f, line)) {
78 // Lines start with '#' are considered as comments.
79 if (line.empty() || line[0] == '#') {
82 // Each line should contain a mangled name and a demangled name
83 // separated by '\t'. Example: "_Z3foo\tfoo"
84 string::size_type tab_pos = line.find('\t');
85 EXPECT_NE(string::npos, tab_pos);
86 string mangled = line.substr(0, tab_pos);
87 string demangled = line.substr(tab_pos + 1);
88 EXPECT_EQ(demangled, DemangleIt(mangled.c_str()));
92 int main(int argc, char **argv) {
93 #ifdef HAVE_LIB_GFLAGS
94 ParseCommandLineFlags(&argc, &argv, true);
96 InitGoogleTest(&argc, argv);
98 FLAGS_logtostderr = true;
99 InitGoogleLogging(argv[0]);
100 if (FLAGS_demangle_filter) {
101 // Read from cin and write to cout.
103 while (getline(cin, line, '\n')) {
104 cout << DemangleIt(line.c_str()) << endl;
107 } else if (argc > 1) {
108 cout << DemangleIt(argv[1]) << endl;
111 return RUN_ALL_TESTS();