1# Copyright (C) 2007, 2008, 2009, 2010, 2011 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 file was written by Wu Zhou. (woodzltc@cn.ibm.com)
17
18# This file is part of the gdb testsuite.  It is intended to test that
19# gdb could correctly handle decimal floating point introduced in IEEE 754R.
20
21if $tracelevel then {
22  strace $tracelevel
23}
24
25set testfile "dfp-test"
26set srcfile ${testfile}.c
27set binfile ${objdir}/${subdir}/${testfile}
28
29# Try to compile the test case.  If we can't, assume the
30# toolchain does not yet provide DFP support and bail out.
31if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {quiet debug}] != "" } {
32    verbose "Skipping DFP tests."
33    return -1
34}
35
36gdb_exit
37gdb_start
38gdb_reinitialize_dir $srcdir/$subdir
39gdb_load ${binfile}
40
41if ![runto_main] then {
42    perror "couldn't run to breakpoint"
43    continue
44}
45
46set sizeof_long [get_sizeof "long" 4]
47
48proc d32_set_tests {} {
49
50    gdb_test "p d32=123.45df" " = 123.45"
51    gdb_test "p d32=12345.df" " = 12345"
52    gdb_test "p d32=12345.67df" " = 12345.67"
53    gdb_test "p d32=1234567.df" " = 1234567"
54
55    gdb_test "p d32=1.234567E0df" " = 1.234567"
56    gdb_test "p d32=1.234567E10df" " = 1.234567E\\+10"
57    gdb_test "p d32=1.234567E+96df" " = 1.234567E\\+96"
58
59    # Test that gdb could handle the max, normalized min and subnormalized min.
60    gdb_test "p d32=9.999999E96df" " = 9.999999E\\+96"
61    gdb_test "p d32=1.0E-95df" " = 1.0E\\-95"
62    gdb_test "p d32=1.E-101df" " = 1E\\-101"
63    gdb_test "p d32=0.000001E-95df" " = 1E\\-101"
64
65    # Test that gdb could detect coefficient/exponent out of range.
66    # The coefficient out of range will be rounded to its nearest value.
67    # And the exponent out of range will be handled as infinity.
68    gdb_test "p d32=1.2345678df" " = 1.234568" "1.2345678 is rounded to 1.234568"
69    gdb_test "p d32=1.0E-101df" " = 1E-101" "1.0E-101 is rounded to 1E-101"
70    gdb_test "p d32=1.234567E+97df" " = Infinity" "1.234567E+97 is Infinity"
71
72    # Test that gdb could detect the errors in the string representation of _Decimal32
73    gdb_test "p d32=12345.df" " = 12345" "12345. is a valid number"
74    gdb_test "p d32=12345df" ".*Invalid number.*" "12345 is an invalid number"
75    gdb_test "p d32=1.23Edf" ".*Conversion syntax.*" "1.23E is an invalid number"
76    gdb_test "p d32=1.23E45Adf" ".*Conversion syntax.*" "1.23E45A is an invalid number"
77}
78
79proc d64_set_tests {} {
80
81    gdb_test "p d64=123.45dd" " = 123.45"
82    gdb_test "p d64=12345.dd" " = 12345"
83    gdb_test "p d64=12345.67dd" " = 12345.67"
84    gdb_test "p d64=1.234567890123456dd" " = 1.234567890123456"
85
86    gdb_test "p d64=1.234567890123456E10dd" " = 12345678901.23456"
87    gdb_test "p d64=1.234567890123456E100dd" " = 1.234567890123456E\\+100"
88    gdb_test "p d64=1.234567890123456E384dd" " = 1.234567890123456E\\+384"
89
90    # Test that gdb could handle the max, normalized min and subnormalized min.
91    gdb_test "p d64=9.999999999999999E384dd" " = 9.999999999999999E\\+384"
92    gdb_test "p d64=1.E-383dd" " = 1E\\-383"
93    gdb_test "p d64=1.E-398dd" " = 1E\\-398"
94    gdb_test "p d64=0.000000000000001E-383dd" " = 1E\\-398"
95
96    # Test that gdb could detect coefficient/exponent out of range.
97    # The coefficient out of range will be rounded to its nearest value.
98    # And the exponent out of range will be handled as infinity.
99    gdb_test "p d64=1.2345678901234567dd" " = 1.234567890123457" "1.2345678901234567 is rounded to 1.234567890123457"
100    gdb_test "p d64=9.9999999999999999E384dd" " = Infinity" "d64=9.9999999999999999E384 is Infinity"
101    gdb_test "p d64=1.234567890123456E385dd" " = Infinity" "d64=1.234567890123456E385 is Infinity"
102
103    # Test that gdb could detect the errors in the string representation of _Decimal64
104    gdb_test "p d64=12345dd" ".*Invalid number.*" "12345dd is an invalid number"
105    gdb_test "p d64=1.23Edd" ".*Conversion syntax.*" "1.23E is an invalid number"
106    gdb_test "p d64=1.23E45Add" ".*Conversion syntax.*" "1.23E45A is an invalid number"
107}
108
109proc d128_set_tests {} {
110
111    gdb_test "p d128=123.45dl" " = 123.45"
112    gdb_test "p d128=12345.dl" " = 12345"
113    gdb_test "p d128=12345.67dl" " = 12345.67"
114    gdb_test "p d128=1.234567890123456789012345678901234dl" " = 1.234567890123456789012345678901234"
115
116    gdb_test "p d128=1.234567890123456E10dl" " = 12345678901.23456"
117    gdb_test "p d128=1.234567890123456E100dl" " = 1.234567890123456E\\+100"
118    gdb_test "p d128=1.234567890123456E1000dl" " = 1.234567890123456E\\+1000"
119
120    # Test that gdb could handle the max, normalized min and subnormalized min.
121    gdb_test "p d128=9.999999999999999999999999999999999E6144dl" " = 9.999999999999999999999999999999999E\\+6144"
122    gdb_test "p d128=1.E-6143dl" " = 1E\\-6143"
123    gdb_test "p d128=1.E-6176dl" " = 1E\\-6176"
124    gdb_test "p d128=0.000000000000000000000000000000001E-6143dl" " = 1E\\-6176"
125
126    # Test that gdb could detect coefficient/exponent out of range.
127    # The coefficient out of range will be rounded to its nearest value.
128    # And the exponent out of range will be handled as infinity.
129    gdb_test "p d128=1.2345678901234567890123456789012345dl" "1.234567890123456789012345678901234" "1.2345678901234567890123456789012345 is rounded to 1.234567890123456789012345678901234"
130    gdb_test "p d128=1.234567890123456E6145dl" "Infinity" "d128=1.234567890123456E6145 is Infinity"
131
132    # Test that gdb could detect the errors in the string representation of _Decimal128
133    gdb_test "p d128=12345dl" ".*Invalid number.*" "12345dl is an invalid number"
134    gdb_test "p d128=1.23Edl" ".*Conversion syntax.*" "1.23E is an invalid number"
135    gdb_test "p d128=1.23E45Adl" ".*Conversion syntax.*" "1.23E45A is an invalid number"
136}
137
138# Different tests on 32-bits decimal floating point, including the printing
139# of finite numbers, infinite and NaN, and also the setting of different
140# decimal value.
141
142if [gdb_test "next" \
143    ".*Positive infd32.*" \
144    "next after initializing d32"] then { gdb_suppress_tests }
145gdb_test "print d32" "1.2345" "d32 is initialized to 1.2345"
146
147if [gdb_test "next" \
148    ".*Negative infd32.*" \
149    "next after assigning builtin infinity to d32"] then { gdb_suppress_tests }
150gdb_test "print d32" "Infinity" "d32 is positive Infinity"
151
152if [gdb_test "next" \
153    ".*__builtin_nand32.*" \
154    "next after assigning negative builtin infinity to d32"] then { gdb_suppress_tests }
155gdb_test "print d32" "-Infinity" "d32 is negative Infinity"
156
157if [gdb_test "next" \
158    ".*d64 = 1.2345.*" \
159    "next after assigning builtin NaN to d32"] then { gdb_suppress_tests }
160gdb_test "print d32" "NaN" "d32 is NaN"
161
162d32_set_tests
163
164
165# Different tests on 64-bits decimal floating point, including the display
166# of finite number, infinite and NaN, and also the setting of different
167# decimal value.
168
169if [gdb_test "next" \
170    ".*Positive infd64.*" \
171    "next after initializing d64"] then { gdb_suppress_tests }
172gdb_test "print d64" "1.2345" "d64 is initialized to 1.2345"
173
174if [gdb_test "next" \
175    ".*Negative infd64.*" \
176    "next after assigning builtin infinity to d64"] then { gdb_suppress_tests }
177gdb_test "print d64" "Infinity" "d64 is positive Infinity"
178
179if [gdb_test "next" \
180    ".*__builtin_nand64.*" \
181    "next after assigning negative builtin infinity to d64"] then { gdb_suppress_tests }
182gdb_test "print d64" "-Infinity" "d64 is negative Infinity"
183
184if [gdb_test "next" \
185    ".*d128 = 1.2345.*" \
186    "next after assigning builtin NaN to d64"] then { gdb_suppress_tests }
187gdb_test "print d64" "NaN" "d64 is NaN"
188
189d64_set_tests
190
191
192# Different tests on 128-bits decimal floating point, including the display
193# of finite number, infinite and NaN, and also the setting of different
194# decimal value.
195
196if [gdb_test "next" \
197    ".*Positive infd128.*" \
198    "next after initializing d128"] then { gdb_suppress_tests }
199gdb_test "print d128" "1.2345" "d128 is initialized to 1.2345"
200
201d128_set_tests
202
203if [gdb_test "next" \
204    ".*Negative infd128.*" \
205    "next after assigning builtin infinity to d128"] then { gdb_suppress_tests }
206gdb_test "print d128" "Infinity" "d128 is positive Infinity"
207
208if [gdb_test "next" \
209    ".*__builtin_nand128.*" \
210    "next after assigning negative builtin infinity to d128"] then { gdb_suppress_tests }
211gdb_test "print d128" "-Infinity" "d128 is negative Infinity"
212
213if [gdb_test "next" \
214    ".*arg0_32.*" \
215    "next after assigning builtin NaN to d128"] then { gdb_suppress_tests }
216gdb_test "print d128" "NaN" "d128 is NaN"
217
218# The following tests are intended to verify that gdb can correctly handle
219# DFP types in function arguments.
220
221gdb_breakpoint arg0_32
222gdb_continue_to_breakpoint "entry to arg0_32"
223gdb_test "backtrace" ".*arg0_32 \\(arg0=0.1, arg1=1.0, arg2=2.0, arg3=3.0, arg4=4.0, arg5=5.0\\).*" "backtrace at arg0_32"
224
225gdb_breakpoint arg0_64
226gdb_continue_to_breakpoint "entry to arg0_64"
227gdb_test "backtrace" ".*arg0_64 \\(arg0=0.1, arg1=1.0, arg2=2.0, arg3=3.0, arg4=4.0, arg5=5.0\\).*" "backtrace at arg0_64"
228
229gdb_breakpoint arg0_128
230gdb_continue_to_breakpoint "entry to arg0_128"
231gdb_test "backtrace" ".*arg0_128 \\(arg0=0.1, arg1=1.0, arg2=2.0, arg3=3.0, arg4=4.0, arg5=5.0\\).*" "backtrace at arg0_128"
232
233# Test calling inferior function with DFP arguments or return value.
234
235gdb_test "call arg0_32 (1.2df, 2.2df, 3.2df, 4.2df, 5.2df, 6.2df)" "Breakpoint.*arg0_32.*" "Call function with correct _Decimal32 arguments."
236gdb_test "backtrace 1" "\n#\[0-9\]+  arg0_32 \\(arg0=1.2, arg1=2.2, arg2=3.2, arg3=4.2, arg4=5.2, arg5=6.2\\).*" "Backtrace function with correct _Decimal32 arguments."
237gdb_test "finish" " = 1.2" "Correct _Decimal32 return value from called function."
238
239gdb_test "call arg0_64 (1.2dd, 2.2dd, 3.2dd, 4.2dd, 5.2dd, 6.2dd)" "Breakpoint.*arg0_64.*" "Call function with correct _Decimal64 arguments."
240gdb_test "backtrace 1" "\n#\[0-9\]+  arg0_64 \\(arg0=1.2, arg1=2.2, arg2=3.2, arg3=4.2, arg4=5.2, arg5=6.2\\).*" "Backtrace function with correct _Decimal64 arguments."
241gdb_test "finish" " = 1.2" "Correct _Decimal64 return value from called function."
242
243gdb_test "call arg0_128 (1.2dl, 2.2dl, 3.2dl, 4.2dl, 5.2dl, 6.2dl)" "Breakpoint.*arg0_128.*" "Call function with correct _Decimal128 arguments."
244gdb_test "backtrace 1" "\n#\[0-9\]+  arg0_128 \\(arg0=1.2, arg1=2.2, arg2=3.2, arg3=4.2, arg4=5.2, arg5=6.2\\).*" "Backtrace function with correct _Decimal128 arguments."
245gdb_test "finish" " = 1.2" "Correct _Decimal128 return value from called function."
246
247gdb_test "call decimal_dec128_align (double_val1, dec128_val2, double_val3, double_val4, double_val5, double_val6, double_val7, double_val8, double_val9, double_val10, double_val11, double_val12, double_val13, double_val14)" " = 1" \
248  "Call function with mixed decimal float arguments TEST."
249
250gdb_test "call decimal_mixed (dec32_val1, dec64_val1, dec128_val1)" " = 1" \
251  "Call function with mixed decimal float arguments."
252
253gdb_test "call decimal_many_args_dec32 (dec32_val1, dec32_val2, dec32_val3, dec32_val4, dec32_val5, dec32_val6, dec32_val7, dec32_val8, dec32_val9, dec32_val10, dec32_val11, dec32_val12, dec32_val13, dec32_val14, dec32_val15, dec32_val16)" " = 1" \
254  "Call function with many _Decimal32 arguments."
255
256gdb_test "call decimal_many_args_dec64 (dec64_val1, dec64_val2, dec64_val3, dec64_val4, dec64_val5, dec64_val6, dec64_val7, dec64_val8, dec64_val9, dec64_val10, dec64_val11, dec64_val12, dec64_val13, dec64_val14, dec64_val15, dec64_val16)" " = 1" \
257  "Call function with many _Decimal64 arguments."
258
259gdb_test "call decimal_many_args_dec128 (dec128_val1, dec128_val2, dec128_val3, dec128_val4, dec128_val5, dec128_val6, dec128_val7, dec128_val8, dec128_val9, dec128_val10, dec128_val11, dec128_val12, dec128_val13, dec128_val14, dec128_val15, dec128_val16)" " = 1" \
260  "Call function with many _Decimal128 arguments."
261
262gdb_test "call decimal_many_args_mixed (dec32_val1, dec32_val2, dec32_val3, dec64_val4, dec64_val5, dec64_val6, dec64_val7, dec128_val8, dec128_val9, dec128_val10, dec32_val11, dec64_val12, dec32_val13, dec64_val14, dec128_val15)" " = 1" \
263  "Call function with many mixed decimal float arguments."
264
265# The following tests are intended to verify that gdb can handle DFP types
266# correctly in struct.
267
268gdb_breakpoint [gdb_get_line_number "Exit point"]
269gdb_continue_to_breakpoint "Setting a decimal struct"
270gdb_test "print ds.dec32" " = 1.2345"
271gdb_test "print ds.dec64" " = 1.2345"
272gdb_test "print ds.dec128" " = 1.2345"
273
274# Test expressions with DFP variables.
275
276gdb_test "print d32 + ds.dec32" " = 1.3345"
277gdb_test "print d64 + ds.dec64" " = 1.3345"
278gdb_test "print d128 + ds.dec128" " = 1.3345"
279
280# Test conversion between different _Decimal sizes.
281
282gdb_test "ptype d64 + ds.dec32" " = volatile _Decimal64"
283gdb_test "ptype d128 + ds.dec32" " = volatile _Decimal128"
284gdb_test "ptype d128 + ds.dec64" " = volatile _Decimal128"
285
286# Mixture of Decimal and integral operands
287gdb_test "p d32 + 1" " = 1.1"
288gdb_test "p 2 + d64" " = 2.1"
289gdb_test "p ds.int4 + d128" " = 1.1"
290gdb_test "ptype d32 + 1" " = volatile _Decimal32"
291gdb_test "ptype ds.int4 + d128" " = volatile _Decimal128"
292
293# Test other operations with DFP operands
294gdb_test "p !d32" " = 0"
295gdb_test "p !d64" " = 0"
296gdb_test "p !d128" " = 0"
297gdb_test "p +d32" " = 0.1"
298gdb_test "p +d64" " = 0.1"
299gdb_test "p +d128" " = 0.1"
300gdb_test "p d64 == d128" " = 1"
301gdb_test "p d128 == ds.dec32" " = 0"
302gdb_test "p d128 == d32" " = 1"
303gdb_test "p ds.dec32 == ds.dec64" " = 1"
304gdb_test "p d32 < ds.dec32" " = 1"
305gdb_test "p d64 < ds.dec64" " = 1"
306gdb_test "p d128 < ds.dec128" " = 1"
307gdb_test "p ds.dec32 < d32" " = 0"
308gdb_test "p d64 > ds.dec64" " = 0"
309gdb_test "p ds.dec128 > d128 " " = 1"
310gdb_test "p d32 < ds.int4" " = 1"
311gdb_test "p ds.int4 > d32" " = 1"
312gdb_test "p ds.dec32 < ds.int4" " = 0"
313gdb_test "p ds.int4 > ds.dec64" " = 0"
314gdb_test "p ds.dec128 > ds.int4" " = 1"
315
316# Reject operation with integral larger than 32-bits
317if { ${sizeof_long} > 4 } {
318  gdb_test "p d32 + ds.long8" "Conversion of large integer to a decimal floating type is not supported."
319}
320
321# Reject operation with DFP and Binary FP
322gdb_test "p d64 + ds.float4" "Mixing decimal floating types with other floating types is not allowed."
323gdb_test "p ds.double8 + d128" "Mixing decimal floating types with other floating types is not allowed."
324
325# The following tests are intended to verify that gdb can handle "d1=d2"
326# and "d1=-d2" correctly.
327
328gdb_test "print ds.dec32=d32" " = 0.1"
329gdb_test "print ds.dec64=d64" " = 0.1"
330gdb_test "print ds.dec128=d128" " = 0.1"
331gdb_test "print ds.dec32 = -d32" " = -0.1"
332gdb_test "print ds.dec64 = -d64" " = -0.1"
333gdb_test "print ds.dec128 = -d128" " = -0.1"
334
335# Test cast to and from DFP values
336
337gdb_test "print ds.double8 = ds.dec64" " = -0.(0999.*|1000.*)"
338gdb_test "print ds.dec64 = ds.float4" " = 3.(0999.*|1000.*)"
339gdb_test "print ds.dec128 = -ds.double8" " = 0.(0999.*|1000.*)"
340gdb_test "print ds.dec128 = ds.dec32" " = -0.1"
341gdb_test "print ds.dec32 = ds.int4" " = 1"
342gdb_test "print ds.int4 = 7.3dl" " = 7"
343