Fix regression of r23.
[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 DEFINE_bool(demangle_filter, false, "Run demangle_unittest in filter mode");
45
46 using namespace std;
47 using namespace GOOGLE_NAMESPACE;
48
49 // A wrapper function for Demangle() to make the unit test simple.
50 static const char *DemangleIt(const char * const mangled) {
51   static char demangled[4096];
52   if (Demangle(mangled, demangled, sizeof(demangled))) {
53     return demangled;
54   } else {
55     return mangled;
56   }
57 }
58
59 // Test corner cases of bounary conditions.
60 TEST(Demangle, CornerCases) {
61   char tmp[10];
62   EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
63   // sizeof("foobar()") == 9
64   EXPECT_STREQ("foobar()", tmp);
65   EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
66   EXPECT_STREQ("foobar()", tmp);
67   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8));  // Not enough.
68   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
69   EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
70   EXPECT_FALSE(Demangle("_Z6foobarv", NULL, 0));  // Should not cause SEGV.
71 }
72
73 TEST(Demangle, FromFile) {
74   string test_file = FLAGS_test_srcdir + "/src/demangle_unittest.txt";
75   ifstream f(test_file.c_str());  // The file should exist.
76   EXPECT_FALSE(f.fail());
77
78   string line;
79   while (getline(f, line)) {
80     // Lines start with '#' are considered as comments.
81     if (line.empty() || line[0] == '#') {
82       continue;
83     }
84     // Each line should contain a mangled name and a demangled name
85     // separated by '\t'.  Example: "_Z3foo\tfoo"
86     string::size_type tab_pos = line.find('\t');
87     EXPECT_NE(string::npos, tab_pos);
88     string mangled = line.substr(0, tab_pos);
89     string demangled = line.substr(tab_pos + 1);
90     EXPECT_EQ(demangled, DemangleIt(mangled.c_str()));
91   }
92 }
93
94 int main(int argc, char **argv) {
95 #ifdef HAVE_LIB_GFLAGS
96   ParseCommandLineFlags(&argc, &argv, true);
97 #endif
98   InitGoogleTest(&argc, argv);
99
100   FLAGS_logtostderr = true;
101   InitGoogleLogging(argv[0]);
102   if (FLAGS_demangle_filter) {
103     // Read from cin and write to cout.
104     string line;
105     while (getline(cin, line, '\n')) {
106       cout << DemangleIt(line.c_str()) << endl;
107     }
108     return 0;
109   } else if (argc > 1) {
110     cout << DemangleIt(argv[1]) << endl;
111     return 0;
112   } else {
113     return RUN_ALL_TESTS();
114   }
115 }