aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.guile/scm-math.exp
blob: 12caa71820dd28cc071d1fd22947440f0f95a6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Copyright (C) 2008-2014 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the GDB testsuite.
# It tests <gdb:value> math operations.

load_lib gdb-guile.exp

standard_testfile

proc test_value_numeric_ops {} {
    global gdb_prompt

    gdb_scm_test_silent_cmd "gu (define i (make-value 5))" \
	"create first integer value"
    gdb_scm_test_silent_cmd "gu (define j (make-value 2))" \
	"create second integer value"
    gdb_test "gu (print (value-add i j))" \
	"= 7" "add two integer values"
    gdb_test "gu (raw-print (value-add i j))" \
	"= #<gdb:value 7>" "verify type of integer add result"

    gdb_scm_test_silent_cmd "gu (define f (make-value  1.25))" \
	"create first double value"
    gdb_scm_test_silent_cmd "gu (define g (make-value 2.5))" \
	"create second double value"
    gdb_test "gu (print (value-add f g))" \
	"= 3.75" "add two double values"
    gdb_test "gu (raw-print (value-add f g))" \
	"= #<gdb:value 3.75>" "verify type of double add result"

    gdb_test "gu (print (value-sub i j))" \
	"= 3" "subtract two integer values"
    gdb_test "gu (print (value-sub f g))" \
	"= -1.25" "subtract two double values"

    gdb_test "gu (print (value-mul i j))" \
	"= 10" "multiply two integer values"
    gdb_test "gu (print (value-mul f g))" \
	"= 3.125" "multiply two double values"

    gdb_test "gu (print (value-div i j))" \
	"= 2" "divide two integer values"
    gdb_test "gu (print (value-div f g))" \
	"= 0.5" "divide two double values"
    gdb_test "gu (print (value-rem i j))" \
	"= 1" "take remainder of two integer values"
    gdb_test "gu (print (value-mod i j))" \
	"= 1" "take modulus of two integer values"

    gdb_test "gu (print (value-pow i j))" \
	"= 25" "integer value raised to the power of another integer value"
    gdb_test "gu (print (value-pow g j))" \
	"= 6.25" "double value raised to the power of integer value"

    gdb_test "gu (print (value-neg i))" \
	"= -5" "negated integer value"
    gdb_test "gu (print (value-pos i))" \
	"= 5" "positive integer value"
    gdb_test "gu (print (value-neg f))" \
	"= -1.25" "negated double value"
    gdb_test "gu (print (value-pos f))" \
	"= 1.25" "positive double value"
    gdb_test "gu (print (value-abs (value-sub j i)))" \
	"= 3" "absolute of integer value"
    gdb_test "gu (print (value-abs (value-sub f g)))" \
	"= 1.25" "absolute of double value"

    gdb_test "gu (print (value-lsh i j))" \
	"= 20" "left shift"
    gdb_test "gu (print (value-rsh i j))" \
	"= 1" "right shift"

    gdb_test "gu (print (value-min i j))" \
	"= 2" "min"
    gdb_test "gu (print (value-max i j))" \
	"= 5" "max"

    gdb_test "gu (print (value-lognot i))" \
	"= -6" "lognot"
    gdb_test "gu (print (value-logand i j))" \
	"= 0" "logand i j"
    gdb_test "gu (print (value-logand 5 1))" \
	"= 1" "logand 5 1"
    gdb_test "gu (print (value-logior i j))" \
	"= 7" "logior i j"
    gdb_test "gu (print (value-logior 5 1))" \
	"= 5" "logior 5 1"
    gdb_test "gu (print (value-logxor i j))" \
	"= 7" "logxor i j"
    gdb_test "gu (print (value-logxor 5 1))" \
	"= 4" "logxor 5 1"

    # Test <gdb:value> mixed with Guile types.

    gdb_test "gu (print (value-sub i 1))" \
	"= 4" "subtract integer value from guile integer"
    gdb_test "gu (raw-print (value-sub i 1))" \
	"#<gdb:value 4>" \
	"verify type of mixed integer subtraction result"
    gdb_test "gu (print (value-add f 1.5))" \
	"= 2.75" "add double value with guile float"

    gdb_test "gu (print (value-sub 1 i))" \
	"= -4" "subtract guile integer from integer value"
    gdb_test "gu (print (value-add 1.5 f))" \
	"= 2.75" "add guile float with double value"

    # Enum conversion test.
    gdb_test "print evalue" "= TWO"
    gdb_test "gu (print (value->integer (history-ref 0)))" "= 2"

    # Test pointer arithmetic.

    # First, obtain the pointers.
    gdb_test "print (void *) 2" ".*" ""
    gdb_test_no_output "gu (define a (history-ref 0))"
    gdb_test "print (void *) 5" ".*" ""
    gdb_test_no_output "gu (define b (history-ref 0))"

    gdb_test "gu (print (value-add a 5))" \
	"= 0x7( <.*>)?" "add pointer value with guile integer"
    gdb_test "gu (print (value-sub b 2))" \
	"= 0x3( <.*>)?" "subtract guile integer from pointer value"
    gdb_test "gu (print (value-sub b a))" \
	"= 3" "subtract two pointer values"

    # Test some invalid operations.

    gdb_test_multiple "gu (print (value-add i '()))" "catch error in guile type conversion" {
	-re "Wrong type argument in position 2.*$gdb_prompt $" {pass "catch error in guile type conversion"}
	-re "= .*$gdb_prompt $"  {fail "catch error in guile type conversion"}
	-re "$gdb_prompt $"      {fail "catch error in guile type conversion"}
    }

    gdb_test_multiple "gu (print (value-add i \"foo\"))" "catch throw of GDB error" {
	-re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch throw of GDB error"}
	-re "= .*$gdb_prompt $"         {fail "catch throw of GDB error"}
	-re "$gdb_prompt $"             {fail "catch throw of GDB error"}
    }
}

# Return the max signed int of size SIZE.
# TCL 8.5 required here.  Use lookup table instead?

proc get_max_int { size } {
    return [expr "(1 << ($size - 1)) - 1"]
}

# Return the min signed int of size SIZE.
# TCL 8.5 required here.  Use lookup table instead?

proc get_min_int { size } {
    return [expr "-(1 << ($size - 1))"]
}

# Return the max unsigned int of size SIZE.
# TCL 8.5 required here.  Use lookup table instead?

proc get_max_uint { size } {
    return [expr "(1 << $size) - 1"]
}

# Helper routine for test_value_numeric_ranges.

proc test_make_int_value { name size } {
    set max [get_max_int $size]
    set min [get_min_int $size]
    set umax [get_max_uint $size]
    gdb_test "gu (print (value-type (make-value $max)))" \
	"= $name" "test make-value $name $size max"
    gdb_test "gu (print (value-type (make-value $min)))" \
	"= $name" "test make-value $name $size min"
    gdb_test "gu (print (value-type (make-value $umax)))" \
	"= unsigned $name" "test make-value unsigned $name $size umax"
}

# Helper routine for test_value_numeric_ranges.

proc test_make_typed_int_value { size } {
    set name "int$size"
    set uname "uint$size"
    set max [get_max_int $size]
    set min [get_min_int $size]
    set umax [get_max_uint $size]

    gdb_test "gu (print (make-value $max #:type (arch-${name}-type arch)))" \
	"= $max" "test make-value $name $size max"
    gdb_test "gu (print (make-value $min #:type (arch-${name}-type arch)))" \
	"= $min" "test make-value $name $size min"
    gdb_test "gu (print (make-value $umax #:type (arch-${uname}-type arch)))" \
	"= $umax" "test make-value $uname $size umax"

    gdb_test "gu (print (make-value (+ $max 1) #:type (arch-${name}-type arch)))" \
	"ERROR.*Out of range.*" "test make-value $name $size max+1"
    gdb_test "gu (print (make-value (- $min 1) #:type (arch-${name}-type arch)))" \
	"ERROR.*Out of range.*" "test make-value $name $size min-1"
    gdb_test "gu (print (make-value (+ $umax 1) #:type (arch-${uname}-type arch)))" \
	"ERROR.*Out of range.*" "test make-value $uname $size umax+1"
}

proc test_value_numeric_ranges {} {
    # We can't assume anything about sizeof (int), etc. on the target.
    # Keep it simple for now, this will cover everything important for
    # the major targets.
    set int_size [get_sizeof "int" 0]
    set long_size [get_sizeof "long" 0]
    gdb_test_no_output "gu (define arch (current-arch))"

    if { $int_size == 4 } {
	test_make_int_value int 32
    }
    if { $long_size == 8} {
	test_make_int_value long 64
    }
    gdb_test "gu (print (value-type (make-value (ash 1 64))))" \
	"ERROR:.*value not a number representable.*" \
	"test make-value, number too large"

    foreach size { 8 16 32 } {
	test_make_typed_int_value $size
    }
    if { $long_size == 8 } {
	test_make_typed_int_value 64
    }
}

proc test_value_boolean {} {
    # Note: Boolean values print as 0,1 because they are printed in the
    # current language (in this case C).

    gdb_test "gu (print (make-value #t))" "= 1" "create boolean true"
    gdb_test "gu (print (make-value #f))" "= 0" "create boolean false"

    gdb_test "gu (print (value-not (make-value #t)))" \
	"= 0" "not true"
    gdb_test "gu (print (value-not (make-value #f)))" \
	"= 1" "not false"

    gdb_test "gu (raw-print (make-value #t))" \
	"#<gdb:value 1>" "verify type of boolean"
}

proc test_value_compare {} {
    gdb_test "gu (print (value<? 1 1))" \
	"#f" "less than, equal"
    gdb_test "gu (print (value<? 1 2))" \
	"#t" "less than, less"
    gdb_test "gu (print (value<? 2 1))" \
	"#f" "less than, greater"

    gdb_test "gu (print (value<=? 1 1))" \
	"#t" "less or equal, equal"
    gdb_test "gu (print (value<=? 1 2))" \
	"#t" "less or equal, less"
    gdb_test "gu (print (value<=? 2 1))" \
	"#f" "less or equal, greater"

    gdb_test "gu (print (value=? 1 1))" \
	"#t" "equality"
    gdb_test "gu (print (value=? 1 2))" \
	"#f" "inequality"
    gdb_test "gu (print (value=? (make-value 1) 1.0))" \
	"#t" "equality of gdb:value with Guile value"
    gdb_test "gu (print (value=? (make-value 1) 2))" \
	"#f" "inequality of gdb:value with Guile value"

    gdb_test "gu (print (value>? 1 1))" \
	"#f" "greater than, equal"
    gdb_test "gu (print (value>? 1 2))" \
	"#f" "greater than, less"
    gdb_test "gu (print (value>? 2 1))" \
	"#t" "greater than, greater"

    gdb_test "gu (print (value>=? 1 1))" \
	"#t" "greater or equal, equal"
    gdb_test "gu (print (value>=? 1 2))" \
	"#f" "greater or equal, less"
    gdb_test "gu (print (value>=? 2 1))" \
	"#t" "greater or equal, greater"
}

if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c}]} {
    return
}

# Skip all tests if Guile scripting is not enabled.
if { [skip_guile_tests] } { continue }

if ![gdb_guile_runto_main] {
   return
}

test_value_numeric_ops
test_value_numeric_ranges
test_value_boolean
test_value_compare