1# Copyright 2006-2020 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 test checks that the if .. else .. end construct works and may
17# contain empty bodies without crashing.
18
19gdb_exit
20gdb_start
21
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.
25
26#    with true condition
27set message "if 1 with empty body"
28gdb_test_multiple "if 1\nend" $message {
29    -re "$gdb_prompt $" {pass $message}
30    eof {
31	fail "$message (crashed)"
32	gdb_exit
33	gdb_start
34    }
35}
36
37#    with false condition
38set message "if 0 with empty body"
39gdb_test_multiple "if 0\nend" $message {
40    -re "$gdb_prompt $" {pass $message}
41    eof {
42	fail "$message (crashed)"
43	gdb_exit
44	gdb_start
45    }
46}
47
48# Second, do the same tests with an empty else body.
49# This fails in GDB <=6.5
50
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).
55
56gdb_test "if 1\necho true\\n\nelse\necho false\\n\nend" "true" \
57	 "if true else false #1"
58
59#    with true condition
60set message "if 1 .. else with empty body"
61gdb_test_multiple "if 1\nelse\nend" $message {
62    -re "$gdb_prompt $" {pass $message}
63    eof {
64	fail "$message (crashed)"
65	gdb_exit
66	gdb_start
67    }
68}
69
70# dirty memory
71gdb_test "if 1\necho true\\n\nelse\necho false\\n\nend" "true" \
72	 "if true else false #2"
73
74#    with false condition
75set message "if 0 .. else with empty body"
76gdb_test_multiple "if 0\nelse\nend" $message {
77    -re "$gdb_prompt $" {pass $message}
78    eof {
79	fail "$message (crashed)"
80	gdb_exit
81	gdb_start
82    }
83}
84
85gdb_test_no_output "set confirm off" ""
86
87# Test that a define with an empty else can be replaced.
88# If there is memory corruption then free will fail.
89# dirty memory
90gdb_test "if 1\necho true\\n\nelse\necho false\\n\nend" "true" \
91	 "if true else false #3"
92# create
93gdb_test "define abc\nif 1\nelse\nend\nend" "" "create define with empty else"
94# call (note that condition is 1 so should pass)
95gdb_test_no_output "abc" "call original define"
96# replace
97set message "replace define with if .. else with empty body"
98gdb_test_multiple "define abc\necho got here\\n\nend" $message {
99    -re "$gdb_prompt $" {pass $message}
100    eof {fail "$message (crashed)"}
101}
102# call
103gdb_test "abc" "got here" "call replacement define"
104