Skip to content

Commit 06cfb08

Browse files
Throw RuntimeExceptions for array functions
1 parent 111be3f commit 06cfb08

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

pixie/vm/array.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def reduce_small(self, f, init):
2626
init = f.invoke([init, self._list[x]])
2727
return init
2828

29+
def list(self):
30+
return self._list
2931

3032
def reduce_large(self, f, init):
3133
for x in range(len(self._list)):
@@ -37,30 +39,30 @@ def reduce_large(self, f, init):
3739
@extend(proto._count, Array)
3840
def _count(self):
3941
assert isinstance(self, Array)
40-
return rt.wrap(len(self._list))
42+
return rt.wrap(len(self.list()))
4143

4244
@extend(proto._nth, Array)
4345
def _nth(self, idx):
4446
assert isinstance(self, Array)
4547
ival = idx.int_val()
46-
if ival < len(self._list):
47-
return self._list[ival]
48+
if ival < len(self.list()):
49+
return self.list()[ival]
4850
else:
4951
affirm(False, u"Index out of Range")
5052

5153
@extend(proto._nth_not_found, Array)
5254
def _nth_not_found(self, idx, not_found):
5355
assert isinstance(self, Array)
5456
ival = idx.int_val()
55-
if ival < len(self._list):
56-
return self._list[ival]
57+
if ival < len(self.list()):
58+
return self.list()[ival]
5759
else:
5860
return not_found
5961

6062
@extend(proto._reduce, Array)
6163
def reduce(self, f, init):
6264
assert isinstance(self, Array)
63-
if len(self._list) > UNROLL_IF_SMALLER_THAN:
65+
if len(self.list()) > UNROLL_IF_SMALLER_THAN:
6466
return self.reduce_large(f, init)
6567
return self.reduce_small(f, init)
6668

@@ -122,34 +124,39 @@ def array(lst):
122124

123125
@as_var("aget")
124126
def aget(self, idx):
125-
assert isinstance(self, Array)
126-
return self._list[idx.int_val()]
127+
affirm(isinstance(self, Array), u"aget expects an Array as the first argument")
128+
affirm(isinstance(idx, Integer), u"aget expects an Integer as the second argument")
129+
return self.list()[idx.int_val()]
127130

128131
@as_var("aset")
129132
def aset(self, idx, val):
130-
assert isinstance(self, Array)
131-
self._list[idx.int_val()] = val
133+
affirm(isinstance(self, Array), u"aset expects an Array as the first argument")
134+
affirm(isinstance(idx, Integer), u"aset expects an Integer as the second argument")
135+
self.list()[idx.int_val()] = val
132136
return val
133137

134138
@as_var("aslice")
135139
def aslice(self, offset):
136-
assert isinstance(self, Array) and isinstance(offset, Integer)
140+
affirm(isinstance(self, Array), u"aset expects an Array as the first argument")
141+
affirm(isinstance(offset, Integer), u"aset expects an Integer as the second argument")
137142

138143
offset = offset.int_val()
139144
if offset >= 0:
140-
return Array(self._list[offset:])
145+
return Array(self.list()[offset:])
141146
else:
142147
rt.throw(rt.wrap(u"offset must be an Integer >= 0"))
143148

144149
@as_var("aconcat")
145150
def aconcat(self, other):
146-
assert isinstance(self, Array) and isinstance(other, Array)
147-
return Array(self._list + other._list)
151+
affirm(isinstance(self, Array) and isinstance(other, Array),
152+
u"aconcat expects 2 Arrays")
153+
return Array(self.list() + other.list())
148154

149155
@as_var("alength")
150156
def alength(self):
151-
assert isinstance(self, Array)
152-
return rt.wrap(len(self._list))
157+
affirm(isinstance(self, Array), u"alength expects an Array")
158+
159+
return rt.wrap(len(self.list()))
153160

154161
@as_var("make-array")
155162
def make_array(l):

tests/pixie/tests/test-arrays.pxi

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
(foreach [x a]
99
(t/assert= x nil))))
1010

11+
(t/deftest test-alength
12+
(let [a (make-array 10)]
13+
(t/assert= (alength a) 10)
14+
(t/assert-throws? RuntimeException
15+
(alength []))))
16+
1117
(t/deftest test-aget-and-aset
1218
(let [a (make-array 10)]
1319
(dotimes [i 10]
@@ -17,7 +23,13 @@
1723
(aset a i i))
1824

1925
(dotimes [i 10]
20-
(t/assert= (aget a i) i))))
26+
(t/assert= (aget a i) i))
27+
28+
(t/assert-throws? RuntimeException
29+
(aget a 1.0))
30+
31+
(t/assert-throws? RuntimeException
32+
(aset a 1.0 :foo))))
2133

2234
(t/deftest test-aconcat
2335
(let [a1 (make-array 10)
@@ -30,7 +42,13 @@
3042

3143
(let [a3 (aconcat a1 a2)]
3244
(dotimes [i 20]
33-
(t/assert= (aget a3 i) i)))))
45+
(t/assert= (aget a3 i) i)))
46+
47+
(t/assert-throws? RuntimeException
48+
(t/aconcat a1 []))
49+
50+
(t/assert-throws? RuntimeException
51+
(t/aconcat a1 '()))))
3452

3553
(t/deftest test-aslice
3654
(let [a (make-array 10)]
@@ -42,10 +60,19 @@
4260
(foreach [i (range 0 7)]
4361
(t/assert= (aget a1 i) (+ i 3)))
4462
(foreach [i (range 0 3)]
45-
(t/assert= (aget a2 i) (+ i 7))))))
63+
(t/assert= (aget a2 i) (+ i 7))))
64+
65+
(t/assert-throws? RuntimeException
66+
(aslice [1 2 3 4] 0 2))
67+
68+
(t/assert-throws? RuntimeException
69+
(aslice '() 0 2))
70+
71+
(t/assert-throws? RuntimeException
72+
(aslice a 1.0 2))))
4673

4774

4875
(t/deftest test-byte-array-creation
4976
(let [ba (byte-array 10)]
5077
(t/assert= (vec ba) [0 0 0 0 0 0 0 0 0 0])
51-
(t/assert= (count ba) 10)))
78+
(t/assert= (count ba) 10)))

0 commit comments

Comments
 (0)