aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.fortran/array-slices-sub-slices.exp
blob: 5a0f0406afe424bc9749483ea6d8aa1632f9357e (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
# Copyright 2020-2024 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/> .

# Create a slice of an array, then take a slice of that slice.

require allow_fortran_tests

standard_testfile ".f90"
load_lib fortran.exp

if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
	 {debug f90}]} {
    return -1
}

if ![fortran_runto_main] {
    return -1
}

# Avoid libc symbols, in particular the 'array' type.
gdb_test_no_output "nosharedlibrary"

# gdb_breakpoint [gdb_get_line_number "Display Message Breakpoint"]
gdb_breakpoint [gdb_get_line_number "Stop Here"]
gdb_breakpoint [gdb_get_line_number "Final Breakpoint"]

# We're going to print some reasonably large arrays.
gdb_test_no_output "set print elements unlimited"

gdb_continue_to_breakpoint "Stop Here"

# Print a slice, capture the convenience variable name created.
set cmd "print array (1:10:2, 1:10:2)"
gdb_test_multiple $cmd $cmd {
    -re "\r\n\\\$(\\d+) = .*\r\n$gdb_prompt $" {
	set varname "\$$expect_out(1,string)"
    }
}

# Now check that we can correctly extract all the elements from this
# slice.
for { set j 1 } { $j < 6 } { incr j } {
    for { set i 1 } { $i < 6 } { incr i } {
	set val [expr ((($i - 1) * 2) + (($j - 1) * 20)) + 1]
	gdb_test "print ${varname} ($i,$j)" " = $val"
    }
}

# Now take a slice of the slice.
gdb_test "print ${varname} (3:5, 3:5)" \
    " = \\(\\(45, 47, 49\\) \\(65, 67, 69\\) \\(85, 87, 89\\)\\)"

# Now take a different slice of a slice.
set cmd "print ${varname} (1:5:2, 1:5:2)"
gdb_test_multiple $cmd $cmd {
    -re "\r\n\\\$(\\d+) = \\(\\(1, 5, 9\\) \\(41, 45, 49\\) \\(81, 85, 89\\)\\)\r\n$gdb_prompt $" {
	set varname "\$$expect_out(1,string)"
	pass $gdb_test_name
    }
}

# Now take a slice from the slice, of a slice!
set cmd "print ${varname} (1:3:2, 1:3:2)"
gdb_test_multiple $cmd $cmd {
    -re "\r\n\\\$(\\d+) = \\(\\(1, 9\\) \\(81, 89\\)\\)\r\n$gdb_prompt $" {
	set varname "\$$expect_out(1,string)"
	pass $gdb_test_name
    }
}

# And again!
set cmd "print ${varname} (1:2:2, 1:2:2)"
gdb_test_multiple $cmd $cmd {
    -re "\r\n\\\$(\\d+) = \\(\\(1\\)\\)\r\n$gdb_prompt $" {
	set varname "\$$expect_out(1,string)"
	pass $gdb_test_name
    }
}

# Test taking a slice with stride of a string.  This isn't actually
# supported within gfortran (at least), but naturally drops out of how
# GDB models arrays and strings in a similar way, so we may as well
# test that this is still working.
gdb_test "print str (1:26:2)" " = 'acegikmoqsuwy'"
gdb_test "print str (26:1:-1)" " = 'zyxwvutsrqponmlkjihgfedcba'"
gdb_test "print str (26:1:-2)" " = 'zxvtrpnljhfdb'"

# Now test the memory requirements of taking a slice from an array.
# The idea is that we shouldn't require more memory to extract a slice
# than the size of the slice.
#
# This will only work if array repacking is turned on, otherwise GDB
# will create the slice by generating a new type that sits over the
# existing value in memory.
gdb_test_no_output "set fortran repack-array-slices on"
set element_size [get_integer_valueof "sizeof (array (1,1))" "unknown"]
set slice_size [expr $element_size * 4]
gdb_test_no_output "set max-value-size $slice_size"
gdb_test "print array (1:2, 1:2)" "= \\(\\(1, 2\\) \\(11, 12\\)\\)"
gdb_test "print array (2:3, 2:3)" "= \\(\\(12, 13\\) \\(22, 23\\)\\)"
gdb_test "print array (2:5:2, 2:5:2)" "= \\(\\(12, 14\\) \\(32, 34\\)\\)"