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

209 lines, 113 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#### slice subscripts are adjusted like Python
59
60show-py() {
61 python3 -c '
62import json, sys; a = [1, 2, 3, 4, 5]; print(json.dumps(eval(sys.argv[1])))' $1
63}
64
65show-ysh() {
66 eval "var a = [1, 2, 3, 4, 5]; json write ($1, space=0)"
67}
68
69compare() {
70 local expr=$1
71 show-py "$1" | sed 's/ //g'
72 show-ysh "$1"
73 echo
74}
75
76compare 'a[1:3]'
77compare 'a[1:100]' # big number
78compare 'a[100:1]' # inverted
79compare 'a[1:-1]'
80compare 'a[-3:-1]'
81compare 'a[-100:-1]' # very negative
82compare 'a[-1:-100]' # inverted
83compare 'a[4:5]'
84
85## STDOUT:
86[2,3]
87[2,3]
88
89[2,3,4,5]
90[2,3,4,5]
91
92[]
93[]
94
95[2,3,4]
96[2,3,4]
97
98[3,4]
99[3,4]
100
101[1,2,3,4]
102[1,2,3,4]
103
104[]
105[]
106
107[5]
108[5]
109
110## END
111
112
113#### subscript and slice of List
114var mylist = [1,2,3,4]
115pp test_ (mylist[1])
116pp test_ (mylist[1:3])
117
118echo 'implicit'
119pp test_ (mylist[:2])
120pp test_ (mylist[2:])
121## STDOUT:
122(Int) 2
123(List) [2,3]
124implicit
125(List) [1,2]
126(List) [3,4]
127## END
128
129#### expressions and negative indices
130var myarray = :|1 2 3 4 5|
131pp test_ (myarray[-1])
132pp test_ (myarray[-4:-2])
133
134echo 'implicit'
135pp test_ (myarray[:-2])
136pp test_ (myarray[-2:])
137## STDOUT:
138(Str) "5"
139(List) ["2","3"]
140implicit
141(List) ["1","2","3"]
142(List) ["4","5"]
143## END
144
145#### Index with expression
146var mydict = {['5']: 3}
147var val = mydict["$[2+3]"]
148echo $val
149## STDOUT:
1503
151## END
152
153#### Copy with a[:]
154var a = [1,2,3]
155var b = a[:]
156pp test_ (b)
157## STDOUT:
158(List) [1,2,3]
159## END
160
161#### Iterate over range
162for i in (1..5) {
163 echo $[i]
164}
165for i, n in (1..4) {
166 echo "$[i], $[n]"
167}
168## STDOUT:
1691
1702
1713
1724
1730, 1
1741, 2
1752, 3
176## END
177
178#### Loops over bogus ranges terminate
179# Regression test for bug found during dev. Loops over backwards ranges should
180# terminate immediately.
181for i in (5..1) {
182 echo $[i]
183}
184## STDOUT:
185## END
186
187#### Slices with Multiple Dimensions (for TSV8?)
188
189qtt pretty :mytable <<< '''
190name age:Int
191alice 42
192bob 31
193carol 20
194'''
195
196# Cut off the first two rows
197var t1 = mytable[2:, :]
198= t1
199
200var t2 = mytable[:2, 3:4]
201= t2
202
203var t3 = mytable[:2, %(name age)]
204= t3
205
206## STDOUT:
207(Str) 'TODO: Table Slicing'
208(Str) 'TODO: Table Slicing'
209## END