simple.exp revision 1.1.1.1
1# Copyright (C) 2016 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# Test expression parsing and evaluation that requires Rust compiler.
17
18load_lib rust-support.exp
19if {[skip_rust_tests]} {
20    continue
21}
22
23standard_testfile .rs
24if {[prepare_for_testing ${testfile}.exp $testfile $srcfile {debug rust}]} {
25    return -1
26}
27
28set line [gdb_get_line_number "set breakpoint here"]
29if {![runto ${srcfile}:$line]} {
30    untested $testfile
31    return -1
32}
33
34gdb_test "print a" " = \\(\\)"
35gdb_test "ptype a" " = \\(\\)"
36
37gdb_test "print b" " = \\\[\\\]"
38gdb_test "ptype b" " = \\\[i32; 0\\\]"
39gdb_test "print *(&b as *const \[i32; 0\])" " = \\\[\\\]"
40gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]"
41
42gdb_test "print c" " = 99"
43gdb_test "ptype c" " = i32"
44
45gdb_test "print c = 87" " = \\(\\)"
46gdb_test "print c" " = 87"
47gdb_test "print c += 3" " = \\(\\)"
48gdb_test "print c" " = 90"
49gdb_test "print c -= 90" " = \\(\\)"
50gdb_test "print c" " = 0"
51gdb_test "print *&c" " = 0"
52gdb_test "print *(&c as &i32)" " = 0"
53gdb_test "print *(&c as *const i32)" " = 0"
54gdb_test "print *(&c as *mut i32)" " = 0"
55
56gdb_test "print j" " = simple::Unit"
57gdb_test "ptype j" " = struct simple::Unit"
58gdb_test "print j2" " = simple::Unit"
59gdb_test "ptype j2" " = struct simple::Unit"
60gdb_test "print simple::Unit" " = simple::Unit"
61gdb_test "print simple::Unit{}" " = simple::Unit"
62
63gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\""
64gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]"
65
66gdb_test "print v" " = simple::Something::Three"
67gdb_test_sequence "ptype v" "" {
68    " = enum simple::Something \\{"
69    "  One,"
70    "  Two,"
71    "  Three,"
72    "\\}"
73}
74
75gdb_test "print w" " = \\\[1, 2, 3, 4\\\]"
76gdb_test "ptype w" " = \\\[i32; 4\\\]"
77gdb_test "print w\[2\]" " = 3"
78gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]"
79gdb_test "print w_ptr\[2\]" " = 3"
80gdb_test "print fromslice" " = 3"
81gdb_test "print slice\[0\]" " = 3"
82gdb_test "print slice as &\[i32\]\[0\]" " = 3"
83
84gdb_test "print x" " = \\(23, 25\\.5\\)"
85gdb_test "ptype x" " = \\(i32, f64\\)"
86gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"
87
88gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
89gdb_test_sequence "ptype y" "" {
90    " = struct simple::HiBob \\{"
91    "  field1: i32,"
92    "  field2: u64,"
93    "\\}"
94}
95gdb_test "print y.field2" " = 8"
96
97gdb_test "print z" " = simple::ByeBob \\(7, 8\\)"
98gdb_test_sequence "ptype z" "" {
99    " = struct simple::ByeBob \\("
100    "  i32,"
101    "  u64,"
102    "\\)"
103}
104gdb_test "print z.1" " = 8"
105
106gdb_test_sequence "ptype simple::ByeBob" "" {
107    " = struct simple::ByeBob \\("
108    "  i32,"
109    "  u64,"
110    "\\)"
111}
112gdb_test "print simple::ByeBob(0xff, 5)" \
113    " = simple::ByeBob \\(255, 5\\)"
114gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \
115    "Struct expression applied to non-struct type"
116
117gdb_test "print simple::HiBob(0xff, 5)" \
118    "Type simple::HiBob is not a tuple struct"
119gdb_test "print nosuchsymbol" \
120    "No symbol 'nosuchsymbol' in current context"
121
122gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
123gdb_test "print e2" \
124    " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
125
126gdb_test_sequence "ptype e" "" {
127    " = enum simple::MoreComplicated \\{"
128    "  One,"
129    "  Two\\(i32\\),"
130    "  Three\\(simple::HiBob\\),"
131    "  Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
132    "\\}"
133}
134
135gdb_test "print e.0" " = 73"
136gdb_test "print e.1" \
137    "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
138gdb_test "print e.foo" \
139    "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"
140
141gdb_test "print e2.variant" " = 10"
142gdb_test "print e2.notexist" \
143    "Could not find field notexist of struct variant simple::MoreComplicated::Four"
144gdb_test "print e2.0" \
145    "Variant simple::MoreComplicated::Four is not a tuple variant"
146
147gdb_test "print k" " = simple::SpaceSaver::Nothing"
148gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
149gdb_test "print *l.1" " = 1729"
150
151gdb_test "print diff2(3, 7)" " = -4"
152gdb_test "print self::diff2(8, 9)" " = -1"
153gdb_test "print ::diff2(23, -23)" " = 46"
154
155gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32"
156gdb_test "ptype empty" "fn \\(\\)"
157
158gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
159
160# We need the ".*" because currently we don't extract the length and
161# use it to intelligently print the string data.
162gdb_test "print \"hello rust\"" \
163    " = &str \\{data_ptr: $hex \"hello rust.*\", length: 10\\}"
164gdb_test "print \"hello" "Unexpected EOF in string"
165gdb_test "print r##\"hello \" rust\"##" \
166    " = &str \\{data_ptr: $hex \"hello \\\\\" rust.*\", length: 12\\}"
167gdb_test "print r\"hello" "Unexpected EOF in string"
168gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
169gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
170gdb_test "print r###\"hello###" "Unexpected EOF in string"
171
172gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
173gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
174gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
175gdb_test "print .." " = .*::ops::RangeFull"
176
177gdb_test "print str_some" \
178    " = core::option::Option<collections::string::String>::Some\\(collections::string::String .*"
179gdb_test "print str_none" " = core::option::Option<collections::string::String>::None"
180gdb_test "print int_some" " = core::option::Option::Some\\(1\\)"
181gdb_test "print int_none" " = core::option::Option::None"
182gdb_test "print box_some" " = core::option::Option<Box<u8>>::Some\\(.*\\)"
183gdb_test "print box_none" " = core::option::Option<Box<u8>>::None"
184gdb_test "print custom_some" \
185    " = simple::NonZeroOptimized::Value\\(collections::string::String .*"
186gdb_test "print custom_none" " = simple::NonZeroOptimized::Empty"
187
188proc test_one_slice {svar length base range} {
189    global hex
190
191    set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"
192
193    gdb_test "print $svar" $result
194    gdb_test "print &${base}\[${range}\]" $result
195}
196
197test_one_slice slice 1 w 2..3
198test_one_slice slice2 1 slice 0..1
199
200test_one_slice all1 4 w ..
201test_one_slice all2 1 slice ..
202
203test_one_slice from1 3 w 1..
204test_one_slice from2 0 slice 1..
205
206test_one_slice to1 3 w ..3
207test_one_slice to2 1 slice ..1
208
209gdb_test "print w\[2..3\]" "Can't take slice of array without '&'"
210
211
212gdb_test_sequence "complete print y.f" "" \
213    {"print y.field1" "print y.field2"}
214gdb_test_sequence "complete print y." "" \
215    {"print y.field1" "print y.field2"}
216
217# Unimplemented, but we can at least test the parser productions.
218gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
219gdb_test "print (1,)" "Tuple expressions not supported yet"
220gdb_test "print (1)" " = 1"
221
222gdb_test "print 23..97.0" "Range expression with different types"
223