1# Tests for anonymous union support.
2# Copyright 1998, 1999, 2003, 2004 Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18# Written by Satish Pai <pai@apollo.hp.com> 1997-08-19
19
20# This file is part of the gdb testsuite
21
22if $tracelevel then {
23        strace $tracelevel
24        }
25
26#
27# test running programs
28#
29
30set prms_id 0
31set bug_id 0
32
33if { [skip_cplus_tests] } { continue }
34
35set testfile "anon-union"
36set srcfile ${testfile}.cc
37set binfile ${objdir}/${subdir}/${testfile}
38
39if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
40     perror "Testcase compile failed" 0
41     continue
42}
43
44# Start with a fresh gdb
45gdb_exit
46gdb_start
47gdb_reinitialize_dir $srcdir/$subdir
48gdb_load ${binfile}
49
50
51if ![runto_main] then {
52    perror "couldn't run to breakpoint"
53    continue
54}
55
56send_gdb "set width 0\n"
57gdb_expect -re "$gdb_prompt $"
58
59#send_gdb "ptype foo\n"
60#gdb_expect {
61#   -re "\r\n$gdb_prompt $" {
62#      pass "ptype foo"
63#    }
64#    -re ".*$gdb_prompt $" { fail "ptype foo" }
65#    timeout { fail "(timeout) ptype foo" }
66#}
67
68#send_gdb "ptype bar\n"
69#gdb_expect {
70#   -re "\r\n$gdb_prompt $" {
71#      pass "ptype foo"
72#    }
73#    -re ".*$gdb_prompt $" { fail "ptype foo" }
74#    timeout { fail "(timeout) ptype foo" }
75#}
76
77# NOTE: Add -- ptype foo.x, etc. when things are working
78
79#Initialize foo
80send_gdb "next\n"
81gdb_expect {
82   -re "40\[ \t\]*foo.paper = 33;\r\n$gdb_prompt $" {
83      pass "next 1"
84    }
85    -re ".*$gdb_prompt $" { fail "next 1" }
86    timeout { fail "(timeout) next 1" }
87}
88
89# Print out the big anon union.
90send_gdb "print foo\n"
91gdb_expect {
92   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 0, x = \{rock = 0, rock2 = 0\}, \{qux = 0, mux = 0\}, boulder = 0\}, \{paper = 0, cloth = 0\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
93      pass "print foo 1"
94    }
95    -re ".*$gdb_prompt $" { fail "print foo 1" }
96    timeout { fail "(timeout) print foo 1" }
97}
98
99# Step over assignment to member
100
101send_gdb "next\n"
102gdb_expect {
103   -re "41\[ \t\]*foo.pebble = 44;\r\n$gdb_prompt $" {
104      pass "next 1"
105    }
106    -re ".*$gdb_prompt $" { fail "next 1" }
107    timeout { fail "(timeout) next 1" }
108}
109
110# Now print out anon union again
111send_gdb "print foo\n"
112gdb_expect {
113   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 0, x = \{rock = 0, rock2 = 0\}, \{qux = 0, mux = 0\}, boulder = 0\}, \{paper = 33, cloth = 33\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
114      pass "print foo 2"
115    }
116    -re ".*$gdb_prompt $" { fail "print foo 2" }
117    timeout { fail "(timeout) print foo 2" }
118}
119
120# Modify the member just set
121send_gdb "set var foo.cloth = 35\n"
122gdb_expect {
123   -re "\r\n$gdb_prompt $" {
124      pass "set var foo.cloth"
125    }
126    timeout { fail "(timeout) set var foo.cloth" }
127}
128
129# Now print out anon union again to see if the right member was set
130send_gdb "print foo\n"
131gdb_expect {
132   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 0, x = \{rock = 0, rock2 = 0\}, \{qux = 0, mux = 0\}, boulder = 0\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
133      pass "print foo 3"
134    }
135    -re ".*$gdb_prompt $" { fail "print foo 3" }
136    timeout { fail "(timeout) print foo 3" }
137}
138
139
140# Step over next assignment to member
141
142send_gdb "next\n"
143gdb_expect {
144   -re "42\[ \t\]*foo.mux = 55;\r\n$gdb_prompt $" {
145      pass "next 2"
146    }
147    -re ".*$gdb_prompt $" { fail "next 2" }
148    timeout { fail "(timeout) next 2" }
149}
150
151# Now print out anon union again
152send_gdb "print foo\n"
153gdb_expect {
154   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 44, x = \{rock = 44, rock2 = 0\}, \{qux = 44, mux = 44\}, boulder = 44\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
155      pass "print foo 4"
156    }
157    -re ".*$gdb_prompt $" { fail "print foo 4" }
158    timeout { fail "(timeout) print foo 4" }
159}
160
161# Modify the member just set
162send_gdb "set var foo.pebble = 45\n"
163gdb_expect {
164   -re "\r\n$gdb_prompt $" {
165      pass "set var foo.pebble"
166    }
167    timeout { fail "(timeout) set var foo.pebble" }
168}
169
170# Now print out anon union again to see if the right member was set
171send_gdb "print foo\n"
172gdb_expect {
173   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 45, x = \{rock = 45, rock2 = 0\}, \{qux = 45, mux = 45\}, boulder = 45\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
174      pass "print foo 5"
175    }
176    -re ".*$gdb_prompt $" { fail "print foo 5" }
177    timeout { fail "(timeout) print foo 5" }
178}
179
180# Modify another member at a different level
181send_gdb "set var foo.qux = 46\n"
182gdb_expect {
183   -re "\r\n$gdb_prompt $" {
184      pass "set var foo.qux"
185    }
186    timeout { fail "(timeout) set var foo.qux" }
187}
188
189# Now print out anon union again to see if the right member was set
190send_gdb "print foo\n"
191gdb_expect {
192   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 46, x = \{rock = 46, rock2 = 0\}, \{qux = 46, mux = 46\}, boulder = 46\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
193      pass "print foo 6"
194    }
195    -re ".*$gdb_prompt $" { fail "print foo 6" }
196    timeout { fail "(timeout) print foo 6" }
197}
198
199# Modify the member at another level, but not the first one in the union
200send_gdb "set var foo.mux = 47\n"
201gdb_expect {
202   -re "\r\n$gdb_prompt $" {
203      pass "set var foo.mux"
204    }
205    timeout { fail "(timeout) set var foo.mux" }
206}
207
208# Now print out anon union again to see if things worked
209send_gdb "print foo\n"
210gdb_expect {
211   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 47, x = \{rock = 47, rock2 = 0\}, \{qux = 47, mux = 47\}, boulder = 47\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
212      pass "print foo 7"
213    }
214    -re ".*$gdb_prompt $" { fail "print foo 7" }
215    timeout { fail "(timeout) print foo 7" }
216}
217
218# Modify a member of a struct in an anon union
219send_gdb "set var foo.x.rock = 48\n"
220gdb_expect {
221   -re "\r\n$gdb_prompt $" {
222      pass "set var foo.x.rock"
223    }
224    timeout { fail "(timeout) set var foo.x.rock" }
225}
226
227# Now print out anon union again to see if things worked
228send_gdb "print foo\n"
229gdb_expect {
230   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 48, x = \{rock = 48, rock2 = 0\}, \{qux = 48, mux = 48\}, boulder = 48\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
231      pass "print foo 8"
232    }
233    -re ".*$gdb_prompt $" { fail "print foo 8" }
234    timeout { fail "(timeout) print foo 8" }
235}
236
237# Modify a member of a struct in an anon union, but something
238# that doesn't alias to some other union member
239send_gdb "set var foo.x.rock2 = 49\n"
240gdb_expect {
241   -re "\r\n$gdb_prompt $" {
242      pass "set var foo.x.rock2"
243    }
244    timeout { fail "(timeout) set var foo.x.rock2" }
245}
246
247# Now print out anon union again to see if things worked
248send_gdb "print foo\n"
249gdb_expect {
250   -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 48, x = \{rock = 48, rock2 = 49\}, \{qux = 48, mux = 48\}, boulder = 48\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
251      pass "print foo 9"
252    }
253    -re ".*$gdb_prompt $" { fail "print foo 9" }
254    timeout { fail "(timeout) print foo 9" }
255}
256
257
258# Step over next four assignments
259send_gdb "next 4\n"
260gdb_expect {
261   -re "53\[ \t\]*w = 45;\r\n$gdb_prompt $" {
262      pass "next 3"
263    }
264    -re ".*$gdb_prompt $" { fail "next 3" }
265    timeout { fail "(timeout) next 3" }
266}
267
268# Tests for anon unions that are not members of a class or struct
269
270send_gdb "print w\n"
271gdb_expect {
272   -re "\\$\[0-9\]* = 0\r\n$gdb_prompt $" {
273      pass "print w 1"
274    }
275    -re ".*$gdb_prompt $" { fail "print w 1" }
276    timeout { fail "(timeout) print w 1" }
277}
278
279send_gdb "print z\n"
280gdb_expect {
281   -re "\\$\[0-9\]* = 0\r\n$gdb_prompt $" {
282      pass "print z 1"
283    }
284    -re ".*$gdb_prompt $" { fail "print z 1" }
285    timeout { fail "(timeout) print z 1" }
286}
287
288# Step over next assignment to w
289send_gdb "next\n"
290gdb_expect {
291   -re "55\[ \t\]*\}\r\n$gdb_prompt $" {
292      pass "next 4"
293    }
294    -re ".*$gdb_prompt $" { fail "next 4" }
295    timeout { fail "(timeout) next 4" }
296}
297
298# See if the change in value is noticed
299send_gdb "print w\n"
300gdb_expect {
301   -re "\\$\[0-9\]* = 45\r\n$gdb_prompt $" {
302      pass "print w 2"
303    }
304    -re ".*$gdb_prompt $" { fail "print w 2" }
305    timeout { fail "(timeout) print w 2" }
306}
307
308# See if z shows the same value
309send_gdb "print z\n"
310gdb_expect {
311   -re "\\$\[0-9\]* = 45\r\n$gdb_prompt $" {
312      pass "print z 2"
313    }
314    -re ".*$gdb_prompt $" { fail "print z 2" }
315    timeout { fail "(timeout) print z 2" }
316}
317
318# Set the anon union member
319send_gdb "set var z = 27\n"
320gdb_expect {
321   -re "\r\n$gdb_prompt $" {
322      pass "set var z"
323    }
324    timeout { fail "(timeout) set var z" }
325}
326
327# See if the change in value is noticed
328send_gdb "print w\n"
329gdb_expect {
330   -re "\\$\[0-9\]* = 27\r\n$gdb_prompt $" {
331      pass "print w 3"
332    }
333    -re ".*$gdb_prompt $" { fail "print w 3" }
334    timeout { fail "(timeout) print w 3" }
335}
336
337# See if z shows the same value
338send_gdb "print z\n"
339gdb_expect {
340   -re "\\$\[0-9\]* = 27\r\n$gdb_prompt $" {
341      pass "print z 3"
342    }
343    -re ".*$gdb_prompt $" { fail "print z 3" }
344    timeout { fail "(timeout) print z 3" }
345}
346