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 |
|
30 | var myarray = :|1 2 3 4|
|
31 | pp test_ (myarray[1])
|
32 | pp test_ (myarray[1:3])
|
33 |
|
34 | echo 'implicit'
|
35 | pp test_ (myarray[:2])
|
36 | pp test_ (myarray[2:])
|
37 |
|
38 | echo 'out of bounds'
|
39 | pp test_ (myarray[:5])
|
40 | pp 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"]
|
50 | implicit
|
51 | (List) ["1","2"]
|
52 | (List) ["3","4"]
|
53 | out 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 |
|
60 | pp test_ (list('3' .. '6'))
|
61 |
|
62 | var i = '5'
|
63 |
|
64 | pp test_ (list(i .. 7))
|
65 | pp test_ (list(3 .. i))
|
66 |
|
67 | var i = '-5'
|
68 |
|
69 | pp test_ (list(i .. -3))
|
70 | pp test_ (list(-7 .. i))
|
71 |
|
72 | # Not allowed
|
73 | pp 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 |
|
86 | var a = list(0..10)
|
87 | #pp test_ (a)
|
88 |
|
89 | pp test_ (a['3': '6'])
|
90 |
|
91 | var i = '5'
|
92 |
|
93 | pp test_ (a[i : 7])
|
94 | pp test_ (a[3 : i])
|
95 |
|
96 | var i = '-5'
|
97 |
|
98 | pp test_ (a[i : -3])
|
99 | pp test_ (a[-7 : i])
|
100 |
|
101 | # Not allowed
|
102 | pp 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 |
|
116 | show-py() {
|
117 | python3 -c '
|
118 | import json, sys; a = [1, 2, 3, 4, 5]; print(json.dumps(eval(sys.argv[1])))' $1
|
119 | }
|
120 |
|
121 | show-ysh() {
|
122 | eval "var a = [1, 2, 3, 4, 5]; json write ($1, space=0)"
|
123 | }
|
124 |
|
125 | compare() {
|
126 | local expr=$1
|
127 | show-py "$1" | sed 's/ //g'
|
128 | show-ysh "$1"
|
129 | echo
|
130 | }
|
131 |
|
132 | compare 'a[1:3]'
|
133 | compare 'a[1:100]' # big number
|
134 | compare 'a[100:1]' # inverted
|
135 | compare 'a[1:-1]'
|
136 | compare 'a[-3:-1]'
|
137 | compare 'a[-100:-1]' # very negative
|
138 | compare 'a[-1:-100]' # inverted
|
139 | compare '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
|
170 | var mylist = [1,2,3,4]
|
171 | pp test_ (mylist[1])
|
172 | pp test_ (mylist[1:3])
|
173 |
|
174 | echo 'implicit'
|
175 | pp test_ (mylist[:2])
|
176 | pp test_ (mylist[2:])
|
177 | ## STDOUT:
|
178 | (Int) 2
|
179 | (List) [2,3]
|
180 | implicit
|
181 | (List) [1,2]
|
182 | (List) [3,4]
|
183 | ## END
|
184 |
|
185 | #### expressions and negative indices
|
186 | var myarray = :|1 2 3 4 5|
|
187 | pp test_ (myarray[-1])
|
188 | pp test_ (myarray[-4:-2])
|
189 |
|
190 | echo 'implicit'
|
191 | pp test_ (myarray[:-2])
|
192 | pp test_ (myarray[-2:])
|
193 | ## STDOUT:
|
194 | (Str) "5"
|
195 | (List) ["2","3"]
|
196 | implicit
|
197 | (List) ["1","2","3"]
|
198 | (List) ["4","5"]
|
199 | ## END
|
200 |
|
201 | #### Index with expression
|
202 | var mydict = {['5']: 3}
|
203 | var val = mydict["$[2+3]"]
|
204 | echo $val
|
205 | ## STDOUT:
|
206 | 3
|
207 | ## END
|
208 |
|
209 | #### Copy with a[:]
|
210 | var a = [1,2,3]
|
211 | var b = a[:]
|
212 | pp test_ (b)
|
213 | ## STDOUT:
|
214 | (List) [1,2,3]
|
215 | ## END
|
216 |
|
217 | #### Iterate over range
|
218 | for i in (1..5) {
|
219 | echo $[i]
|
220 | }
|
221 | for i, n in (1..4) {
|
222 | echo "$[i], $[n]"
|
223 | }
|
224 | ## STDOUT:
|
225 | 1
|
226 | 2
|
227 | 3
|
228 | 4
|
229 | 0, 1
|
230 | 1, 2
|
231 | 2, 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.
|
237 | for i in (5..1) {
|
238 | echo $[i]
|
239 | }
|
240 | ## STDOUT:
|
241 | ## END
|
242 |
|
243 | #### Slices with Multiple Dimensions (for TSV8?)
|
244 |
|
245 | qtt pretty :mytable <<< '''
|
246 | name age:Int
|
247 | alice 42
|
248 | bob 31
|
249 | carol 20
|
250 | '''
|
251 |
|
252 | # Cut off the first two rows
|
253 | var t1 = mytable[2:, :]
|
254 | = t1
|
255 |
|
256 | var t2 = mytable[:2, 3:4]
|
257 | = t2
|
258 |
|
259 | var t3 = mytable[:2, %(name age)]
|
260 | = t3
|
261 |
|
262 | ## STDOUT:
|
263 | (Str) 'TODO: Table Slicing'
|
264 | (Str) 'TODO: Table Slicing'
|
265 | ## END
|