83_array2d.py 3.9 KB

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