90_array2d.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. exit()
  2. from array2d import array2d
  3. # test error args for __init__
  4. try:
  5. a = array2d(0, 0)
  6. exit(0)
  7. except ValueError:
  8. pass
  9. # test callable constructor
  10. a = array2d(2, 4, default=lambda: 0)
  11. assert a.width == a.n_cols == 2
  12. assert a.height == a.n_rows == 4
  13. assert a.numel == 8
  14. # test is_valid
  15. assert a.is_valid(0, 0)
  16. assert a.is_valid(1, 3)
  17. assert not a.is_valid(2, 0)
  18. assert not a.is_valid(0, 4)
  19. assert not a.is_valid(-1, 0)
  20. assert not a.is_valid(0, -1)
  21. # test get
  22. assert a.get(0, 0) == 0
  23. assert a.get(1, 3) == 0
  24. assert a.get(2, 0) is None
  25. assert a.get(0, 4, default='S') == 'S'
  26. # test __getitem__
  27. assert a[0, 0] == 0
  28. assert a[1, 3] == 0
  29. try:
  30. a[2, 0]
  31. exit(1)
  32. except IndexError:
  33. pass
  34. # test __setitem__
  35. a[0, 0] = 5
  36. assert a[0, 0] == 5
  37. a[1, 3] = 6
  38. assert a[1, 3] == 6
  39. try:
  40. a[0, -1] = 7
  41. exit(1)
  42. except IndexError:
  43. pass
  44. # test __iter__
  45. a_list = [[5, 0], [0, 0], [0, 0], [0, 6]]
  46. assert a_list == a.tolist()
  47. # test __len__
  48. assert len(a) == 4*2
  49. # test __eq__
  50. x = array2d(2, 4, default=0)
  51. b = array2d(2, 4, default=0)
  52. assert x == b
  53. b[0, 0] = 1
  54. assert x != b
  55. # test __repr__
  56. assert repr(a) == f'array2d(2, 4)'
  57. # test map
  58. c = a.map(lambda x: x + 1)
  59. assert c.tolist() == [[6, 1], [1, 1], [1, 1], [1, 7]]
  60. assert a.tolist() == [[5, 0], [0, 0], [0, 0], [0, 6]]
  61. assert c.width == c.n_cols == 2
  62. assert c.height == c.n_rows == 4
  63. assert c.numel == 8
  64. # test copy
  65. d = c.copy()
  66. assert d == c and d is not c
  67. # test fill_
  68. d.fill_(-3)
  69. assert d == array2d(2, 4, default=-3)
  70. # test apply_
  71. d.apply_(lambda x: x + 3)
  72. assert d == array2d(2, 4, default=0)
  73. # test copy_
  74. a.copy_(d)
  75. assert a == d and a is not d
  76. x = array2d(4, 4, default=0)
  77. x.copy_(d)
  78. assert x == d and x is not d
  79. x.copy_(['a']*d.numel)
  80. assert x == array2d(d.width, d.height, default='a')
  81. # test subclass array2d
  82. class A(array2d):
  83. def __init__(self):
  84. super().__init__(2, 4, default=0)
  85. assert A().width == 2
  86. assert A().height == 4
  87. assert A().numel == 8
  88. assert A().get(0, 0, default=2) == 0
  89. # test alive_neighbors
  90. a = array2d(3, 3, default=0)
  91. a[1, 1] = 1
  92. """ Moore von Neumann
  93. 0 0 0 1 1 1 0 1 0
  94. 0 1 0 1 0 1 1 0 1
  95. 0 0 0 1 1 1 0 1 0
  96. """
  97. moore_result = array2d(3, 3, default=1)
  98. moore_result[1, 1] = 0
  99. von_neumann_result = array2d(3, 3, default=0)
  100. von_neumann_result[0, 1] = von_neumann_result[1, 0] = von_neumann_result[1, 2] = von_neumann_result[2, 1] = 1
  101. a.count_neighbors(0, 'Moore') == moore_result
  102. a.count_neighbors(0, 'von Neumann') == von_neumann_result
  103. # test slice get
  104. a = array2d(5, 5, default=0)
  105. b = array2d(3, 2, default=1)
  106. assert a[1:4, 1:4] == array2d(3, 3, default=0)
  107. assert a[1:4, 1:3] == array2d(3, 2, default=0)
  108. assert a[1:4, 1:3] != b
  109. a[1:4, 1:3] = b
  110. assert a[1:4, 1:3] == b
  111. """
  112. 0 0 0 0 0
  113. 0 1 1 1 0
  114. 0 1 1 1 0
  115. 0 0 0 0 0
  116. 0 0 0 0 0
  117. """
  118. assert a.count(1) == 3*2
  119. assert a.find_bounding_rect(1) == (1, 1, 3, 2)
  120. assert a.find_bounding_rect(0) == (0, 0, 5, 5)
  121. assert a.find_bounding_rect(2) == None
  122. a = array2d(3, 2, default='?')
  123. # int/float/str/bool/None
  124. for value in [0, 0.0, '0', False, None]:
  125. a[0:2, 0:1] = value
  126. assert a[2, 1] == '?'
  127. assert a[0, 0] == value
  128. a[:, :] = 3
  129. assert a == array2d(3, 2, default=3)
  130. try:
  131. a[:, :] = array2d(1, 1)
  132. exit(1)
  133. except ValueError:
  134. pass
  135. try:
  136. a[:, :] = []
  137. exit(1)
  138. except TypeError:
  139. pass
  140. a = array2d(3, 4, default=1)
  141. for i, j, x in a:
  142. assert a[i, j] == x
  143. assert len(a) == a.numel
  144. # test _get and _set
  145. a = array2d(3, 4, default=1)
  146. assert a._get(0, 0) == 1
  147. a._set(0, 0, 2)
  148. assert a._get(0, 0) == 2
  149. # stackoverflow bug due to recursive mark-and-sweep
  150. # class Cell:
  151. # neighbors: list['Cell']
  152. # cells: array2d[Cell] = array2d(192, 108, default=Cell)
  153. # OutOfBounds = Cell()
  154. # for x, y, cell in cells:
  155. # cell.neighbors = [
  156. # cells.get(x-1, y-1, OutOfBounds),
  157. # cells.get(x , y-1, OutOfBounds),
  158. # cells.get(x+1, y-1, OutOfBounds),
  159. # cells.get(x-1, y , OutOfBounds),
  160. # cells.get(x+1, y , OutOfBounds),
  161. # cells.get(x , y+1, OutOfBounds),
  162. # cells.get(x+1, y+1, OutOfBounds),
  163. # ]
  164. # import gc
  165. # gc.collect()