run copyright.sh for 2011.
[external/binutils.git] / gdb / testsuite / gdb.python / py-function.exp
1 # Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the GDB testsuite.  It tests the mechanism
17 # exposing convenience functions to Python.
18
19 if $tracelevel then {
20     strace $tracelevel
21 }
22
23 # Usage: gdb_py_test_multiple NAME INPUT RESULT {INPUT RESULT}...
24 # Run a test named NAME, consisting of multiple lines of input.
25 # After each input line INPUT, search for result line RESULT.
26 # Succeed if all results are seen; fail otherwise.
27 proc gdb_py_test_multiple {name args} {
28     global gdb_prompt
29     foreach {input result} $args {
30         if {[gdb_test_multiple $input "$name - $input" {
31             -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
32                 pass "$name - $input"
33             }
34         }]} {
35             return 1
36         }
37     }
38     return 0
39 }
40
41 # Start with a fresh gdb.
42
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46
47 # Skip all tests if Python scripting is not enabled.
48 if { [skip_python_tests] } { continue }
49
50 gdb_py_test_multiple "input convenience function" \
51   "python" "" \
52   "class test_func (gdb.Function):" "" \
53   "  def __init__ (self):" "" \
54   "    super (test_func, self).__init__ (\"test_func\")" "" \
55   "  def invoke (self, arg):" "" \
56   "    return \"test_func output, arg = %s\" % arg.string ()" "" \
57   "test_func ()" "" \
58   "end" ""
59
60 gdb_test "print \$test_func (\"ugh\")" "= \"test_func output, arg = ugh\"" "call function"
61
62 # Test returning a gdb.Value from the function. This segfaulted GDB at one point.
63
64 gdb_py_test_multiple "input value-returning convenience function" \
65   "python" "" \
66   "class Double (gdb.Function):" "" \
67   "  def __init__ (self):" "" \
68   "    super (Double, self).__init__ (\"double\")" "" \
69   "  def invoke (self, n):" "" \
70   "    return n*2" "" \
71   "Double ()" "" \
72   "end" ""
73
74 gdb_test "print \$double (1)" "= 2" "call value-returning function"
75
76 gdb_py_test_multiple "input int-returning function" \
77   "python" "" \
78   "class Yes(gdb.Function):" "" \
79   "    def __init__(self):" "" \
80   "        gdb.Function.__init__(self, 'yes')" "" \
81   "    def invoke(self):" "" \
82   "        return 1" "" \
83   "Yes ()" "" \
84   "end" ""
85
86 gdb_test "print \$yes() && \$yes()" " = 1" "call yes with &&"
87 gdb_test "print \$yes() || \$yes()" " = 1" "call yes with ||"