-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathtest_array.py
224 lines (175 loc) · 8.2 KB
/
test_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.
##
## Test array support by IronPython (System.Array)
##
import unittest
from iptest import IronPythonTestCase, is_cli, is_posix, run_test
if is_cli:
import System
class ArrayTest(IronPythonTestCase):
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_sanity(self):
# 1-dimension array
array1 = System.Array.CreateInstance(int, 2)
for i in range(2): array1[i] = i * 10
self.assertRaises(IndexError, lambda: array1[2])
array2 = System.Array.CreateInstance(int, 4)
for i in range(2, 6): array2[i - 2] = i * 10
array3 = System.Array.CreateInstance(float, 3)
array3[0] = 2.1
array3[1] = 3.14
array3[2] = 0.11
## __setitem__/__getitem__
System.Array.__setitem__(array3, 2, 0.14)
self.assertEqual(System.Array.__getitem__(array3, 1), 3.14)
self.assertEqual([x for x in System.Array.__getitem__(array3, slice(2))], [2.1, 3.14])
## __repr__
# 2-dimension array
array4 = System.Array.CreateInstance(int, 2, 2)
array4[0, 1] = 1
self.assertTrue(repr(array4).startswith("<2 dimensional Array[int] at"), "bad repr for 2-dimensional array")
# 3-dimension array
array5 = System.Array.CreateInstance(object, 2, 2, 2)
array5[0, 1, 1] = int
self.assertTrue(repr(array5).startswith("<3 dimensional Array[object] at "), "bad repr for 3-dimensional array")
## index access
self.assertRaises(TypeError, lambda : array5['s'])
def f1(): array5[0, 1] = 0
self.assertRaises(ValueError, f1)
def f2(): array5['s'] = 0
self.assertRaises(TypeError, f2)
## __add__/__mul__
for f in (
lambda a, b : System.Array.__add__(a, b),
lambda a, b : a + b
) :
temp = System.Array.__add__(array1, array2)
result = f(array1, array2)
for i in range(6): self.assertEqual(i * 10, result[i])
self.assertEqual(repr(result), "Array[int]((0, 10, 20, 30, 40, 50))")
result = f(array1, array3)
self.assertEqual(len(result), 2 + 3)
self.assertEqual([x for x in result], [0, 10, 2.1, 3.14, 0.14])
self.assertRaises(NotImplementedError, f, array1, array4)
for f in [
lambda a, x: System.Array.__mul__(a, x),
lambda a, x: array1 * x
]:
self.assertEqual([x for x in f(array1, 4)], [0, 10, 0, 10, 0, 10, 0, 10])
self.assertEqual([x for x in f(array1, 5)], [0, 10, 0, 10, 0, 10, 0, 10, 0, 10])
self.assertEqual([x for x in f(array1, 0)], [])
self.assertEqual([x for x in f(array1, -10)], [])
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_slice(self):
array1 = System.Array.CreateInstance(int, 20)
for i in range(20): array1[i] = i * i
# positive
array1[::2] = [x * 2 for x in range(10)]
for i in range(0, 20, 2):
self.assertEqual(array1[i], i)
for i in range(1, 20, 2):
self.assertEqual(array1[i], i * i)
# negative: not-same-length
def f(): array1[::2] = [x * 2 for x in range(11)]
self.assertRaises(ValueError, f)
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_creation(self):
t = System.Array
ti = type(System.Array.CreateInstance(int, 1))
self.assertRaises(TypeError, t, [1, 2])
for x in (ti([1,2]), t[int]([1, 2]), ti([1.5, 2.3])):
self.assertEqual([i for i in x], [1, 2])
t.Reverse(x)
self.assertEqual([i for i in x], [2, 1])
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_nonzero_lowerbound(self):
a = System.Array.CreateInstance(int, (5,), (5,))
for i in xrange(5): a[i] = i
self.assertArrayEqual(a[:2], System.Array[int]((0,1)))
self.assertArrayEqual(a[2:], System.Array[int]((2,3,4)))
self.assertArrayEqual(a[2:4], System.Array[int]((2,3)))
self.assertEqual(a[-1], 4)
self.assertEqual(repr(a), 'Array[int]((0, 1, 2, 3, 4))')
a = System.Array.CreateInstance(int, (5,), (15,))
b = System.Array.CreateInstance(int, (5,), (20,))
self.assertArrayEqual(a,b)
## 5-dimension
a = System.Array.CreateInstance(int, (2,2,2,2,2), (1,2,3,4,5))
self.assertEqual(a[0,0,0,0,0], 0)
for i in range(5):
index = [0,0,0,0,0]
index[i] = 1
a[index[0], index[1], index[2], index[3], index[4]] = i
self.assertEqual(a[index[0], index[1], index[2], index[3], index[4]], i)
for i in range(5):
index = [0,0,0,0,0]
index[i] = 0
a[index[0], index[1], index[2], index[3], index[4]] = i
self.assertEqual(a[index[0], index[1], index[2], index[3], index[4]], i)
def sliceArray(arr, index):
arr[:index]
def sliceArrayAssign(arr, index, val):
arr[:index] = val
self.assertRaises(NotImplementedError, sliceArray, a, 1)
self.assertRaises(NotImplementedError, sliceArray, a, 200)
self.assertRaises(NotImplementedError, sliceArray, a, -200)
self.assertRaises(NotImplementedError, sliceArrayAssign, a, -200, 1)
self.assertRaises(NotImplementedError, sliceArrayAssign, a, 1, 1)
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_array_type(self):
def type_helper(array_type, instance):
#create the array type
AT = System.Array[array_type]
a0 = AT([])
a1 = AT([instance])
a2 = AT([instance, instance])
a_normal = System.Array.CreateInstance(array_type, 3)
self.assertTrue(str(AT)==str(type(a_normal)))
for i in xrange(3):
a_normal[i] = instance
self.assertTrue(str(AT)==str(type(a_normal)))
a_multi = System.Array.CreateInstance(array_type, 2, 3)
self.assertTrue(str(AT)==str(type(a_multi)))
for i in xrange(2):
for j in xrange(3):
self.assertTrue(str(AT)==str(type(a_multi)))
a_multi[i, j]=instance
self.assertTrue(str(AT)==str(type(a0)))
self.assertTrue(str(AT)==str(type(a0[0:])))
self.assertTrue(str(AT)==str(type(a0[:0])))
self.assertTrue(str(AT)==str(type(a1)))
self.assertTrue(str(AT)==str(type(a1[1:])))
self.assertTrue(str(AT)==str(type(a1[:0])))
self.assertTrue(str(AT)==str(type(a_normal)))
self.assertTrue(str(AT)==str(type(a_normal[:0])))
self.assertTrue(str(AT)==str(type(a_normal[3:])))
self.assertTrue(str(AT)==str(type(a_normal[4:])))
self.assertTrue(str(AT)==str(type(a_normal[1:])))
self.assertTrue(str(AT)==str(type(a_normal[1:1:50])))
self.assertTrue(str(AT)==str(type(a_multi)))
def silly(): a_multi[0:][1:0]
self.assertRaises(NotImplementedError, silly)
self.assertTrue(str(AT)==str(type((a0+a1)[:0])))
type_helper(int, 0)
type_helper(int, 1)
type_helper(int, 100)
type_helper(bool, False)
type_helper(bool, True)
#type_helper(bool, 1)
type_helper(long, 0L)
type_helper(long, 1L)
type_helper(long, 100L)
type_helper(float, 0.0)
type_helper(float, 1.0)
type_helper(float, 3.14)
type_helper(str, "")
type_helper(str, " ")
type_helper(str, "abc")
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_tuple_indexer(self):
array1 = System.Array.CreateInstance(int, 20, 20)
array1[0,0] = 5
self.assertEqual(array1[0,0], array1[(0,0)])
run_test(__name__)