1 # Copyright 2006-2019 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This test checks that the if .. else .. end construct works and may
17 # contain empty bodies without crashing.
22 # First test that the if command works with an empty body
23 # Test with different conditions because the body is ignored
24 # if it is not executed.
27 set message "if 1 with empty body"
28 gdb_test_multiple "if 1\nend" $message {
29 -re "$gdb_prompt $" {pass $message}
31 fail "$message (crashed)"
37 # with false condition
38 set message "if 0 with empty body"
39 gdb_test_multiple "if 0\nend" $message {
40 -re "$gdb_prompt $" {pass $message}
42 fail "$message (crashed)"
48 # Second, do the same tests with an empty else body.
49 # This fails in GDB <=6.5
51 # Unfortunately it was an uninitialised memory problem so
52 # sometimes it just works. Precede it with an if else end with
53 # bodies and hopefully the memory with be dirty and the problem
54 # will show itself (this works at time of writing).
56 gdb_test "if 1\necho true\\n\nelse\necho false\\n\nend" "true" \
57 "if true else false #1"
60 set message "if 1 .. else with empty body"
61 gdb_test_multiple "if 1\nelse\nend" $message {
62 -re "$gdb_prompt $" {pass $message}
64 fail "$message (crashed)"
71 gdb_test "if 1\necho true\\n\nelse\necho false\\n\nend" "true" \
72 "if true else false #2"
74 # with false condition
75 set message "if 0 .. else with empty body"
76 gdb_test_multiple "if 0\nelse\nend" $message {
77 -re "$gdb_prompt $" {pass $message}
79 fail "$message (crashed)"
85 gdb_test_no_output "set confirm off" ""
87 # Test that a define with an empty else can be replaced.
88 # If there is memory corruption then free will fail.
90 gdb_test "if 1\necho true\\n\nelse\necho false\\n\nend" "true" \
91 "if true else false #3"
93 gdb_test "define abc\nif 1\nelse\nend\nend" "" "create define with empty else"
94 # call (note that condition is 1 so should pass)
95 gdb_test_no_output "abc" "call original define"
97 set message "replace define with if .. else with empty body"
98 gdb_test_multiple "define abc\necho got here\\n\nend" $message {
99 -re "$gdb_prompt $" {pass $message}
100 eof {fail "$message (crashed)"}
103 gdb_test "abc" "got here" "call replacement define"