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 line (myarray[1])
|
32 | pp line (myarray[1:3])
|
33 |
|
34 | echo 'implicit'
|
35 | pp line (myarray[:2])
|
36 | pp line (myarray[2:])
|
37 |
|
38 | echo 'out of bounds'
|
39 | pp line (myarray[:5])
|
40 | pp line (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 | #### slice subscripts are adjusted like Python
|
59 |
|
60 | show-py() {
|
61 | python3 -c '
|
62 | import json, sys; a = [1, 2, 3, 4, 5]; print(json.dumps(eval(sys.argv[1])))' $1
|
63 | }
|
64 |
|
65 | show-ysh() {
|
66 | eval "var a = [1, 2, 3, 4, 5]; json write ($1, space=0)"
|
67 | }
|
68 |
|
69 | compare() {
|
70 | local expr=$1
|
71 | show-py "$1" | sed 's/ //g'
|
72 | show-ysh "$1"
|
73 | echo
|
74 | }
|
75 |
|
76 | compare 'a[1:3]'
|
77 | compare 'a[1:100]' # big number
|
78 | compare 'a[100:1]' # inverted
|
79 | compare 'a[1:-1]'
|
80 | compare 'a[-3:-1]'
|
81 | compare 'a[-100:-1]' # very negative
|
82 | compare 'a[-1:-100]' # inverted
|
83 | compare '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
|
114 | var mylist = [1,2,3,4]
|
115 | pp line (mylist[1])
|
116 | pp line (mylist[1:3])
|
117 |
|
118 | echo 'implicit'
|
119 | pp line (mylist[:2])
|
120 | pp line (mylist[2:])
|
121 | ## STDOUT:
|
122 | (Int) 2
|
123 | (List) [2,3]
|
124 | implicit
|
125 | (List) [1,2]
|
126 | (List) [3,4]
|
127 | ## END
|
128 |
|
129 | #### expressions and negative indices
|
130 | var myarray = :|1 2 3 4 5|
|
131 | pp line (myarray[-1])
|
132 | pp line (myarray[-4:-2])
|
133 |
|
134 | echo 'implicit'
|
135 | pp line (myarray[:-2])
|
136 | pp line (myarray[-2:])
|
137 | ## STDOUT:
|
138 | (Str) "5"
|
139 | (List) ["2","3"]
|
140 | implicit
|
141 | (List) ["1","2","3"]
|
142 | (List) ["4","5"]
|
143 | ## END
|
144 |
|
145 | #### Index with expression
|
146 | var mydict = {['5']: 3}
|
147 | var val = mydict["$[2+3]"]
|
148 | echo $val
|
149 | ## STDOUT:
|
150 | 3
|
151 | ## END
|
152 |
|
153 | #### Copy with a[:]
|
154 | var a = [1,2,3]
|
155 | var b = a[:]
|
156 | pp line (b)
|
157 | ## STDOUT:
|
158 | (List) [1,2,3]
|
159 | ## END
|
160 |
|
161 | #### Iterate over range
|
162 | for i in (1..5) {
|
163 | echo $[i]
|
164 | }
|
165 | for i, n in (1..4) {
|
166 | echo "$[i], $[n]"
|
167 | }
|
168 | ## STDOUT:
|
169 | 1
|
170 | 2
|
171 | 3
|
172 | 4
|
173 | 0, 1
|
174 | 1, 2
|
175 | 2, 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.
|
181 | for i in (5..1) {
|
182 | echo $[i]
|
183 | }
|
184 | ## STDOUT:
|
185 | ## END
|
186 |
|
187 | #### Slices with Multiple Dimensions (for TSV8?)
|
188 |
|
189 | qtt pretty :mytable <<< '''
|
190 | name age:Int
|
191 | alice 42
|
192 | bob 31
|
193 | carol 20
|
194 | '''
|
195 |
|
196 | # Cut off the first two rows
|
197 | var t1 = mytable[2:, :]
|
198 | = t1
|
199 |
|
200 | var t2 = mytable[:2, 3:4]
|
201 | = t2
|
202 |
|
203 | var t3 = mytable[:2, %(name age)]
|
204 | = t3
|
205 |
|
206 | ## STDOUT:
|
207 | (Str) 'TODO: Table Slicing'
|
208 | (Str) 'TODO: Table Slicing'
|
209 | ## END
|