90_array2d.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from array2d import array2d
  2. from linalg import vec2i
  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, 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) and a.is_valid(vec2i(0, 0))
  16. assert a.is_valid(1, 3) and a.is_valid(vec2i(1, 3))
  17. assert not a.is_valid(2, 0) and not a.is_valid(vec2i(2, 0))
  18. assert not a.is_valid(0, 4) and not a.is_valid(vec2i(0, 4))
  19. assert not a.is_valid(-1, 0) and not a.is_valid(vec2i(-1, 0))
  20. assert not a.is_valid(0, -1) and not a.is_valid(vec2i(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, '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(2, 4, default=0)
  77. x.copy_(d)
  78. assert x == d and x is not d
  79. x.copy_([1, 2, 3, 4, 5, 6, 7, 8])
  80. assert x.tolist() == [[1, 2], [3, 4], [5, 6], [7, 8]]
  81. # test alive_neighbors
  82. a = array2d(3, 3, default=0)
  83. a[1, 1] = 1
  84. """ Moore von Neumann
  85. 0 0 0 1 1 1 0 1 0
  86. 0 1 0 1 0 1 1 0 1
  87. 0 0 0 1 1 1 0 1 0
  88. """
  89. moore_result = array2d(3, 3, default=1)
  90. moore_result[1, 1] = 0
  91. von_neumann_result = array2d(3, 3, default=0)
  92. von_neumann_result[0, 1] = von_neumann_result[1, 0] = von_neumann_result[1, 2] = von_neumann_result[2, 1] = 1
  93. _0 = a.count_neighbors(1, 'Moore')
  94. assert _0 == moore_result
  95. _1 = a.count_neighbors(1, 'von Neumann')
  96. assert _1 == von_neumann_result
  97. # test slice get
  98. a = array2d(5, 5, default=0)
  99. b = array2d(3, 2, default=1)
  100. assert a[1:4, 1:4] == array2d(3, 3, default=0)
  101. assert a[1:4, 1:3] == array2d(3, 2, default=0)
  102. assert a[1:4, 1:3] != b
  103. a[1:4, 1:3] = b
  104. assert a[1:4, 1:3] == b
  105. """
  106. 0 0 0 0 0
  107. 0 1 1 1 0
  108. 0 1 1 1 0
  109. 0 0 0 0 0
  110. 0 0 0 0 0
  111. """
  112. assert a.count(1) == 3*2
  113. assert a.find_bounding_rect(1) == (1, 1, 3, 2)
  114. assert a.find_bounding_rect(0) == (0, 0, 5, 5)
  115. try:
  116. a.find_bounding_rect(2)
  117. exit(1)
  118. except ValueError:
  119. pass
  120. a = array2d(3, 2, default='?')
  121. # int/float/str/bool/None
  122. for value in [0, 0.0, '0', False, None]:
  123. a[0:2, 0:1] = value
  124. assert a[2, 1] == '?'
  125. assert a[0, 0] == value
  126. a[:, :] = 3
  127. assert a == array2d(3, 2, default=3)
  128. try:
  129. a[:, :] = array2d(1, 1)
  130. exit(1)
  131. except ValueError:
  132. pass
  133. try:
  134. a[:, :] = ...
  135. exit(1)
  136. except TypeError:
  137. pass
  138. a = array2d(3, 4, default=1)
  139. for i, j, x in a:
  140. assert a[i, j] == x
  141. assert len(a) == a.numel
  142. # test _get and _set
  143. a = array2d(3, 4, default=1)
  144. assert a.unsafe_get(0, 0) == 1
  145. a.unsafe_set(0, 0, 2)
  146. assert a.unsafe_get(0, 0) == 2
  147. # stackoverflow bug due to recursive mark-and-sweep
  148. # class Cell:
  149. # neighbors: list['Cell']
  150. # cells: array2d[Cell] = array2d(192, 108, default=Cell)
  151. # OutOfBounds = Cell()
  152. # for x, y, cell in cells:
  153. # cell.neighbors = [
  154. # cells.get(x-1, y-1, OutOfBounds),
  155. # cells.get(x , y-1, OutOfBounds),
  156. # cells.get(x+1, y-1, OutOfBounds),
  157. # cells.get(x-1, y , OutOfBounds),
  158. # cells.get(x+1, y , OutOfBounds),
  159. # cells.get(x , y+1, OutOfBounds),
  160. # cells.get(x+1, y+1, OutOfBounds),
  161. # ]
  162. # import gc
  163. # gc.collect()