OILS / spec / ysh-slice-range.test.sh View on Github | oilshell.org

265 lines, 140 significant
1## oils_failures_allowed: 1
2
3# Test a[1]
4
5#### precedence of 1:3 vs comparison
6
7# This test exposed nondeterminism in CPython itself! Gah. Is this because of
8# the hashing?
9# Python xrange objects probably shouldn't even be comparable!
10#
11# = 1..3 < 1..4
12# >>> xrange(1,3) < xrange(1,4)
13# False
14# >>> xrange(1,3) < xrange(1,4)
15# True
16
17= 1..3
18
19## STDOUT:
20(Range 1 .. 3)
21## END
22
23#### precedence of 1:3 vs bitwise operator
24= 3..3|4
25## STDOUT:
26(Range 3 .. 7)
27## END
28
29#### subscript and slice :| 1 2 3 4 |
30var myarray = :|1 2 3 4|
31pp test_ (myarray[1])
32pp test_ (myarray[1:3])
33
34echo 'implicit'
35pp test_ (myarray[:2])
36pp test_ (myarray[2:])
37
38echo 'out of bounds'
39pp test_ (myarray[:5])
40pp test_ (myarray[-5:])
41
42# Stride not supported
43#= myarray[1:4:2]
44
45# Now try omitting some
46#= myarray[1:4:2]
47## STDOUT:
48(Str) "2"
49(List) ["2","3"]
50implicit
51(List) ["1","2"]
52(List) ["3","4"]
53out of bounds
54(List) ["1","2","3","4"]
55(List) ["1","2","3","4"]
56## END
57
58#### Range end points can be int-looking Strings
59
60pp test_ (list('3' .. '6'))
61
62var i = '5'
63
64pp test_ (list(i .. 7))
65pp test_ (list(3 .. i))
66
67var i = '-5'
68
69pp test_ (list(i .. -3))
70pp test_ (list(-7 .. i))
71
72# Not allowed
73pp test_ ('a' .. 'z')
74
75## status: 3
76## STDOUT:
77(List) [3,4,5]
78(List) [5,6]
79(List) [3,4]
80(List) [-5,-4]
81(List) [-7,-6]
82## END
83
84#### Slice indices can be int-looking strings
85
86var a = list(0..10)
87#pp test_ (a)
88
89pp test_ (a['3': '6'])
90
91var i = '5'
92
93pp test_ (a[i : 7])
94pp test_ (a[3 : i])
95
96var i = '-5'
97
98pp test_ (a[i : -3])
99pp test_ (a[-7 : i])
100
101# Not allowed
102pp test_ (a['a' : 'z'])
103
104## status: 3
105## STDOUT:
106(List) [3,4,5]
107(List) [5,6]
108(List) [3,4]
109(List) [5,6]
110(List) [3,4]
111## END
112
113
114#### slice subscripts are adjusted like Python
115
116show-py() {
117 python3 -c '
118import json, sys; a = [1, 2, 3, 4, 5]; print(json.dumps(eval(sys.argv[1])))' $1
119}
120
121show-ysh() {
122 eval "var a = [1, 2, 3, 4, 5]; json write ($1, space=0)"
123}
124
125compare() {
126 local expr=$1
127 show-py "$1" | sed 's/ //g'
128 show-ysh "$1"
129 echo
130}
131
132compare 'a[1:3]'
133compare 'a[1:100]' # big number
134compare 'a[100:1]' # inverted
135compare 'a[1:-1]'
136compare 'a[-3:-1]'
137compare 'a[-100:-1]' # very negative
138compare 'a[-1:-100]' # inverted
139compare 'a[4:5]'
140
141## STDOUT:
142[2,3]
143[2,3]
144
145[2,3,4,5]
146[2,3,4,5]
147
148[]
149[]
150
151[2,3,4]
152[2,3,4]
153
154[3,4]
155[3,4]
156
157[1,2,3,4]
158[1,2,3,4]
159
160[]
161[]
162
163[5]
164[5]
165
166## END
167
168
169#### subscript and slice of List
170var mylist = [1,2,3,4]
171pp test_ (mylist[1])
172pp test_ (mylist[1:3])
173
174echo 'implicit'
175pp test_ (mylist[:2])
176pp test_ (mylist[2:])
177## STDOUT:
178(Int) 2
179(List) [2,3]
180implicit
181(List) [1,2]
182(List) [3,4]
183## END
184
185#### expressions and negative indices
186var myarray = :|1 2 3 4 5|
187pp test_ (myarray[-1])
188pp test_ (myarray[-4:-2])
189
190echo 'implicit'
191pp test_ (myarray[:-2])
192pp test_ (myarray[-2:])
193## STDOUT:
194(Str) "5"
195(List) ["2","3"]
196implicit
197(List) ["1","2","3"]
198(List) ["4","5"]
199## END
200
201#### Index with expression
202var mydict = {['5']: 3}
203var val = mydict["$[2+3]"]
204echo $val
205## STDOUT:
2063
207## END
208
209#### Copy with a[:]
210var a = [1,2,3]
211var b = a[:]
212pp test_ (b)
213## STDOUT:
214(List) [1,2,3]
215## END
216
217#### Iterate over range
218for i in (1..5) {
219 echo $[i]
220}
221for i, n in (1..4) {
222 echo "$[i], $[n]"
223}
224## STDOUT:
2251
2262
2273
2284
2290, 1
2301, 2
2312, 3
232## END
233
234#### Loops over bogus ranges terminate
235# Regression test for bug found during dev. Loops over backwards ranges should
236# terminate immediately.
237for i in (5..1) {
238 echo $[i]
239}
240## STDOUT:
241## END
242
243#### Slices with Multiple Dimensions (for TSV8?)
244
245qtt pretty :mytable <<< '''
246name age:Int
247alice 42
248bob 31
249carol 20
250'''
251
252# Cut off the first two rows
253var t1 = mytable[2:, :]
254= t1
255
256var t2 = mytable[:2, 3:4]
257= t2
258
259var t3 = mytable[:2, %(name age)]
260= t3
261
262## STDOUT:
263(Str) 'TODO: Table Slicing'
264(Str) 'TODO: Table Slicing'
265## END