Merge pull request #278 from shinh/contrib
[platform/upstream/glog.git] / src / demangle_unittest.sh
1 #! /bin/sh
2 #
3 # Copyright (c) 2006, Google Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 #     * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
15 # distribution.
16 #     * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32 # Author: Satoru Takabayashi
33 #
34 # Unit tests for demangle.c with a real binary.
35
36 set -e
37
38 die () {
39     echo $1
40     exit 1
41 }
42
43 BINDIR=".libs"
44 LIBGLOG="$BINDIR/libglog.so"
45
46 DEMANGLER="$BINDIR/demangle_unittest"
47
48 if test -e "$DEMANGLER"; then
49   # We need shared object.
50   export LD_LIBRARY_PATH=$BINDIR
51   export DYLD_LIBRARY_PATH=$BINDIR
52 else
53   # For windows
54   DEMANGLER="./demangle_unittest.exe"
55   if ! test -e "$DEMANGLER"; then
56     echo "We coundn't find demangle_unittest binary."
57     exit 1
58   fi
59 fi
60
61 # Extract C++ mangled symbols from libbase.so.
62 NM_OUTPUT="demangle.nm"
63 nm "$LIBGLOG" | perl -nle 'print $1 if /\s(_Z\S+$)/' > "$NM_OUTPUT"
64
65 # Check if mangled symbols exist. If there are none, we quit.
66 # The binary is more likely compiled with GCC 2.95 or something old.
67 if ! grep --quiet '^_Z' "$NM_OUTPUT"; then
68     echo "PASS"
69     exit 0
70 fi
71
72 # Demangle the symbols using our demangler.
73 DM_OUTPUT="demangle.dm"
74 GLOG_demangle_filter=1 "$DEMANGLER" --demangle_filter < "$NM_OUTPUT" > "$DM_OUTPUT"
75
76 # Calculate the numbers of lines.
77 NM_LINES=`wc -l "$NM_OUTPUT" | awk '{ print $1 }'`
78 DM_LINES=`wc -l "$DM_OUTPUT" | awk '{ print $1 }'`
79
80 # Compare the numbers of lines.  They must be the same.
81 if test "$NM_LINES" != "$DM_LINES"; then
82     die "$NM_OUTPUT and $DM_OUTPUT don't have the same numbers of lines"
83 fi
84
85 # Check if mangled symbols exist.  They must not exist.
86 if grep --quiet '^_Z' "$DM_OUTPUT"; then
87     MANGLED=`grep '^_Z' "$DM_OUTPUT" | wc -l | awk '{ print \$1 }'`
88     echo "Mangled symbols ($MANGLED out of $NM_LINES) found in $DM_OUTPUT:"
89     grep '^_Z' "$DM_OUTPUT"
90     die "Mangled symbols ($MANGLED out of $NM_LINES) found in $DM_OUTPUT"
91 fi
92
93 # All C++ symbols are demangled successfully.
94 echo "PASS"
95 exit 0