/DLR_Main/Languages/IronPython/Tests/test_array.py

https://bitbucket.org/mdavid/dlr · Python · 237 lines · 154 code · 48 blank · 35 comment · 27 complexity · f45427b39d2bcb7da0520e8b70a8ff5e MD5 · raw file

  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. ##
  16. ## Test array support by IronPython (System.Array)
  17. ##
  18. from iptest.assert_util import *
  19. if not is_cpython:
  20. import System
  21. @skip("win32")
  22. def test_sanity():
  23. # 1-dimension array
  24. array1 = System.Array.CreateInstance(int, 2)
  25. for i in range(2): array1[i] = i * 10
  26. AssertError(IndexError, lambda: array1[2])
  27. array2 = System.Array.CreateInstance(int, 4)
  28. for i in range(2, 6): array2[i - 2] = i * 10
  29. array3 = System.Array.CreateInstance(float, 3)
  30. array3[0] = 2.1
  31. array3[1] = 3.14
  32. array3[2] = 0.11
  33. ## __setitem__/__getitem__
  34. System.Array.__setitem__(array3, 2, 0.14)
  35. AreEqual(System.Array.__getitem__(array3, 1), 3.14)
  36. AreEqual([x for x in System.Array.__getitem__(array3, slice(2))], [2.1, 3.14])
  37. ## __repr__
  38. # 2-dimension array
  39. array4 = System.Array.CreateInstance(int, 2, 2)
  40. array4[0, 1] = 1
  41. Assert(repr(array4).startswith("<2 dimensional Array[int] at"), "bad repr for 2-dimensional array")
  42. # 3-dimension array
  43. array5 = System.Array.CreateInstance(object, 2, 2, 2)
  44. array5[0, 1, 1] = int
  45. Assert(repr(array5).startswith("<3 dimensional Array[object] at "), "bad repr for 3-dimensional array")
  46. ## index access
  47. AssertError(TypeError, lambda : array5['s'])
  48. def f1(): array5[0, 1] = 0
  49. AssertError(ValueError, f1)
  50. def f2(): array5['s'] = 0
  51. AssertError(TypeError, f2)
  52. ## __add__/__mul__
  53. for f in (
  54. lambda a, b : System.Array.__add__(a, b),
  55. lambda a, b : a + b
  56. ) :
  57. temp = System.Array.__add__(array1, array2)
  58. result = f(array1, array2)
  59. for i in range(6): AreEqual(i * 10, result[i])
  60. AreEqual(repr(result), "Array[int]((0, 10, 20, 30, 40, 50))")
  61. result = f(array1, array3)
  62. AreEqual(len(result), 2 + 3)
  63. AreEqual([x for x in result], [0, 10, 2.1, 3.14, 0.14])
  64. AssertError(NotImplementedError, f, array1, array4)
  65. for f in [
  66. lambda a, x: System.Array.__mul__(a, x),
  67. lambda a, x: array1 * x
  68. ]:
  69. AreEqual([x for x in f(array1, 4)], [0, 10, 0, 10, 0, 10, 0, 10])
  70. AreEqual([x for x in f(array1, 5)], [0, 10, 0, 10, 0, 10, 0, 10, 0, 10])
  71. AreEqual([x for x in f(array1, 0)], [])
  72. AreEqual([x for x in f(array1, -10)], [])
  73. @skip("win32")
  74. def test_slice():
  75. array1 = System.Array.CreateInstance(int, 20)
  76. for i in range(20): array1[i] = i * i
  77. # positive
  78. array1[::2] = [x * 2 for x in range(10)]
  79. for i in range(0, 20, 2):
  80. AreEqual(array1[i], i)
  81. for i in range(1, 20, 2):
  82. AreEqual(array1[i], i * i)
  83. # negative: not-same-length
  84. def f(): array1[::2] = [x * 2 for x in range(11)]
  85. AssertError(ValueError, f)
  86. @skip("win32")
  87. def test_creation():
  88. t = System.Array
  89. ti = type(System.Array.CreateInstance(int, 1))
  90. AssertError(TypeError, t, [1, 2])
  91. for x in (ti([1,2]), t[int]([1, 2]), ti([1.5, 2.3])):
  92. AreEqual([i for i in x], [1, 2])
  93. t.Reverse(x)
  94. AreEqual([i for i in x], [2, 1])
  95. def _ArrayEqual(a,b):
  96. AreEqual(a.Length, b.Length)
  97. for x in xrange(a.Length):
  98. AreEqual(a[x], b[x])
  99. ## public static Array CreateInstance (
  100. ## Type elementType,
  101. ## int[] lengths,
  102. ## int[] lowerBounds
  103. ##)
  104. @skip('silverlight', 'win32')
  105. def test_nonzero_lowerbound():
  106. a = System.Array.CreateInstance(int, (5,), (5,))
  107. for i in xrange(5): a[i] = i
  108. _ArrayEqual(a[:2], System.Array[int]((0,1)))
  109. _ArrayEqual(a[2:], System.Array[int]((2,3,4)))
  110. _ArrayEqual(a[2:4], System.Array[int]((2,3)))
  111. AreEqual(a[-1], 4)
  112. AreEqual(repr(a), 'Array[int]((0, 1, 2, 3, 4))')
  113. a = System.Array.CreateInstance(int, (5,), (15,))
  114. b = System.Array.CreateInstance(int, (5,), (20,))
  115. _ArrayEqual(a,b)
  116. ## 5-dimension
  117. a = System.Array.CreateInstance(int, (2,2,2,2,2), (1,2,3,4,5))
  118. AreEqual(a[0,0,0,0,0], 0)
  119. for i in range(5):
  120. index = [0,0,0,0,0]
  121. index[i] = 1
  122. a[index[0], index[1], index[2], index[3], index[4]] = i
  123. AreEqual(a[index[0], index[1], index[2], index[3], index[4]], i)
  124. for i in range(5):
  125. index = [0,0,0,0,0]
  126. index[i] = 0
  127. a[index[0], index[1], index[2], index[3], index[4]] = i
  128. AreEqual(a[index[0], index[1], index[2], index[3], index[4]], i)
  129. def sliceArray(arr, index):
  130. arr[:index]
  131. def sliceArrayAssign(arr, index, val):
  132. arr[:index] = val
  133. AssertError(NotImplementedError, sliceArray, a, 1)
  134. AssertError(NotImplementedError, sliceArray, a, 200)
  135. AssertError(NotImplementedError, sliceArray, a, -200)
  136. AssertError(NotImplementedError, sliceArrayAssign, a, -200, 1)
  137. AssertError(NotImplementedError, sliceArrayAssign, a, 1, 1)
  138. @skip("win32")
  139. def test_array_type():
  140. def type_helper(array_type, instance):
  141. #create the array type
  142. AT = System.Array[array_type]
  143. a0 = AT([])
  144. a1 = AT([instance])
  145. a2 = AT([instance, instance])
  146. a_normal = System.Array.CreateInstance(array_type, 3)
  147. Assert(str(AT)==str(type(a_normal)))
  148. for i in xrange(3):
  149. a_normal[i] = instance
  150. Assert(str(AT)==str(type(a_normal)))
  151. a_multi = System.Array.CreateInstance(array_type, 2, 3)
  152. Assert(str(AT)==str(type(a_multi)))
  153. for i in xrange(2):
  154. for j in xrange(3):
  155. Assert(str(AT)==str(type(a_multi)))
  156. a_multi[i, j]=instance
  157. Assert(str(AT)==str(type(a0)))
  158. Assert(str(AT)==str(type(a0[0:])))
  159. Assert(str(AT)==str(type(a0[:0])))
  160. Assert(str(AT)==str(type(a1)))
  161. Assert(str(AT)==str(type(a1[1:])))
  162. Assert(str(AT)==str(type(a1[:0])))
  163. Assert(str(AT)==str(type(a_normal)))
  164. Assert(str(AT)==str(type(a_normal[:0])))
  165. Assert(str(AT)==str(type(a_normal[3:])))
  166. Assert(str(AT)==str(type(a_normal[4:])))
  167. Assert(str(AT)==str(type(a_normal[1:])))
  168. Assert(str(AT)==str(type(a_normal[1:1:50])))
  169. Assert(str(AT)==str(type(a_multi)))
  170. def silly(): a_multi[0:][1:0]
  171. AssertError(NotImplementedError, silly)
  172. Assert(str(AT)==str(type((a0+a1)[:0])))
  173. type_helper(int, 0)
  174. type_helper(int, 1)
  175. type_helper(int, 100)
  176. type_helper(bool, False)
  177. type_helper(bool, True)
  178. #type_helper(bool, 1)
  179. type_helper(long, 0L)
  180. type_helper(long, 1L)
  181. type_helper(long, 100L)
  182. type_helper(float, 0.0)
  183. type_helper(float, 1.0)
  184. type_helper(float, 3.14)
  185. type_helper(str, "")
  186. type_helper(str, " ")
  187. type_helper(str, "abc")
  188. #--MAIN------------------------------------------------------------------------
  189. run_test(__name__)