]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.rust/expr.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.rust / expr.exp
1 # Copyright (C) 2016-2023 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 basic expression parsing and evaluation, without requiring a
17 # Rust compiler. This serves as a smoke test.
18
19 load_lib "rust-support.exp"
20 if {[skip_rust_tests]} { return }
21
22 gdb_start
23
24 gdb_test_no_output "set var \$something = 27"
25
26 if {![set_lang_rust]} {
27 warning "Rust expression tests suppressed."
28 return
29 }
30
31 gdb_test "print 9__97" " = 997"
32 gdb_test "print -5" " = -5"
33 gdb_test "print +5" " = 5"
34 gdb_test "print +-+-5" " = 5"
35 gdb_test "print 3_2i32" " = 32"
36 gdb_test "print 32i64" " = 32"
37 gdb_test "print 8u8" " = 8"
38 gdb_test "print 0x1f" " = 31"
39 gdb_test "print 0o07" " = 7"
40 gdb_test "print 0o70" " = 56"
41 gdb_test "print 0b1_111" " = 15"
42 gdb_test "print 32usize" " = 32"
43 gdb_test "print 0x_4" " = 4"
44
45 gdb_test "print 'z'" " = 122 'z'"
46 gdb_test "print '\\t'" " = 9 '\\\\t'"
47 gdb_test "print '\\n'" " = 10 '\\\\n'"
48 gdb_test "print '\\r'" " = 13 '\\\\r'"
49 gdb_test "print '\\\\'" " = 92 '\\\\\\\\'"
50 gdb_test "print '\\0'" " = 0 '\\\\0'"
51 gdb_test "print '\\''" " = 39 '\\\\''"
52 gdb_test "print '\\\"'" " = 34 '\"'"
53 gdb_test "print '\\xff'" " = 255 '\\\\xff'"
54 gdb_test "print '\\xFF'" " = 255 '\\\\xff'"
55 gdb_test "print '\\u\{F0eF\}'" " = 61679 '\\\\u\\{00f0ef\\}'"
56
57 gdb_test "print b'z'" " = 122"
58 gdb_test "print b'\\xfe'" " = 254"
59 gdb_test "print b'\\t'" " = 9"
60 gdb_test "print b'\\n'" " = 10"
61 gdb_test "print b'\\r'" " = 13"
62 gdb_test "print b'\\\\'" " = 92"
63 gdb_test "print b'\\0'" " = 0"
64 gdb_test "print b'\\''" " = 39"
65 gdb_test "print b'\\\"'" " = 34"
66 gdb_test "print b'\\xff'" " = 255"
67
68 gdb_test "print 23.5" " = 23.5"
69 gdb_test "print 23.5e1" " = 235"
70 gdb_test "print 2e4" " = 20000"
71 gdb_test "print 2_E+4_f64" " = 20000"
72 gdb_test "print 5e-1" " = 0.5"
73 gdb_test "print 5e-1f32" " = 0.5"
74
75 gdb_test "print false" " = false"
76 gdb_test "print true" " = true"
77
78 gdb_test "print 1+2" " = 3"
79 gdb_test "print 1i32 + 2i32" " = 3"
80 gdb_test "print 2.0 - 1.0" " = 1"
81 gdb_test "print !false" " = true"
82 gdb_test "print !true" " = false"
83 gdb_test "print !0u8" " = 255"
84 gdb_test "print 7 * 7" " = 49"
85 gdb_test "print 7usize * 7usize" " = 49"
86 gdb_test "print 42 / 7" " = 6"
87 gdb_test "print 42 % 7" " = 0"
88 gdb_test "print 1.0 / 2.0" " = 0.5"
89 gdb_test "print 1 < 2" " = true"
90 gdb_test "print !(1 < 2)" " = false"
91 gdb_test "print 3 + 4 * 7" " = 31"
92 gdb_test "print 1 > 2" " = false"
93 gdb_test "print 1 | 2" " = 3"
94 gdb_test "print 1 & 2" " = 0"
95 gdb_test "print 3 & 2" " = 2"
96 gdb_test "print 3 ^ 2" " = 1"
97 gdb_test "print (1 < 0) || true" " = true"
98 gdb_test "print (1 > 0) && false" " = false"
99 gdb_test "print 'z' == 'z'" " = true"
100 gdb_test "print '\\u{1016f}' != 'q'" " = true"
101 gdb_test "print 32 <= 32" " = true"
102 gdb_test "print 32 >= 32" " = true"
103 gdb_test "print 1 << 5" " = 32"
104 gdb_test "print 32usize >> 5" " = 1"
105 gdb_test "ptype 32i32 as f64" "type = f64"
106
107 gdb_test "ptype 0xf9f9f9f90000" "type = i64"
108
109 gdb_test "print ()" " = \\(\\)"
110
111 gdb_test "print \[1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]"
112 gdb_test "ptype \[1,2,3,4\]" "type = \\\[i32; 4\\\]"
113 gdb_test "print \[mut 1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]"
114 gdb_test "print \[1,2 3" "',' or ']' expected"
115 gdb_test "print \[1 2" "',', ';', or ']' expected"
116
117 gdb_test "print b\"hi rust\"" " = b\"hi rust\""
118 # This isn't rusty syntax yet, but that's another bug -- this is just
119 # testing that byte escapes work properly.
120 gdb_test "print b\"\\xddhi bob\"" " = b\"\\\\335hi bob\""
121 gdb_test "print b\"has\\0nul\"" " = b\"has\\\\000nul\""
122
123 gdb_test "print br##\"hi\"##" " = b\"hi\""
124 gdb_test "print br##\"hi" "Unexpected EOF in string"
125 gdb_test "print br##\"hi\"" "Unexpected EOF in string"
126 gdb_test "print br##\"hi\"#" "Unexpected EOF in string"
127
128 # Test that convenience variables and functions work with the Rust
129 # parser.
130 gdb_test "print \$something" " = 27"
131 gdb_test "print \$_isvoid(\$nosuchvariable)" " = 1"
132 gdb_test "print \$_isvoid(\$something)" " = 0"
133
134 gdb_test "print \[23usize; 4\]" " = \\\[23, 23, 23, 23\\\]"
135 gdb_test "ptype \[23usize; 4\]" " = \\\[usize; 4\\\]"
136 gdb_test "print \[mut 23usize; 4\]" " = \\\[23, 23, 23, 23\\\]"
137
138 # Test lexer corner cases.
139 gdb_test "print 0x0 as *mut ()" " = \\\(\\*mut \\\(\\\)\\\) 0x0"
140 gdb_test "print 0x0 as fn(i64) -> ()" " = \\\(\\*mut fn \\\(i64\\\) -> \\\(\\\)\\\) 0x0"
141
142 # The lexer doesn't treat this as a failure, but rather as two tokens,
143 # and we error out while trying to look up 'r'. This is fine, though
144 # -- what's important is that it isn't accepted.
145 gdb_test "print r#" "No symbol 'r' in current context"
146
147 gdb_test "printf \"%d %d\\n\", 23+1, 23-1" "24 22"
148
149 gdb_test "print 5," "Syntax error near ','"