rna_array.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # Apache License, Version 2.0
  2. import unittest
  3. import random
  4. test = bpy.data.test
  5. # farr - 1-dimensional array of float
  6. # fdarr - dynamic 1-dimensional array of float
  7. # fmarr - 3-dimensional ([3][4][5]) array of float
  8. # fdmarr - dynamic 3-dimensional (ditto size) array of float
  9. # same as above for other types except that the first letter is "i" for int and "b" for bool
  10. class TestArray(unittest.TestCase):
  11. # test that assignment works by: assign -> test value
  12. # - rvalue = list of float
  13. # - rvalue = list of numbers
  14. # test.object
  15. # bpy.data.test.farr[3], iarr[3], barr[...], fmarr, imarr, bmarr
  16. def setUp(self):
  17. test.farr = (1.0, 2.0, 3.0)
  18. test.iarr = (7, 8, 9)
  19. test.barr = (False, True, False)
  20. # test access
  21. # test slice access, negative indices
  22. def test_access(self):
  23. rvals = ([1.0, 2.0, 3.0], [7, 8, 9], [False, True, False])
  24. for arr, rval in zip((test.farr, test.iarr, test.barr), rvals):
  25. self.assertEqual(prop_to_list(arr), rval)
  26. self.assertEqual(arr[0:3], rval)
  27. self.assertEqual(arr[1:2], rval[1:2])
  28. self.assertEqual(arr[-1], arr[2])
  29. self.assertEqual(arr[-2], arr[1])
  30. self.assertEqual(arr[-3], arr[0])
  31. # fail when index out of bounds
  32. def test_access_fail(self):
  33. for arr in (test.farr, test.iarr, test.barr):
  34. self.assertRaises(IndexError, lambda: arr[4])
  35. # test assignment of a whole array
  36. def test_assign_array(self):
  37. # should accept int as float
  38. test.farr = (1, 2, 3)
  39. # fail when: unexpected no. of items, invalid item type
  40. def test_assign_array_fail(self):
  41. def assign_empty_list(arr):
  42. setattr(test, arr, ())
  43. for arr in ("farr", "iarr", "barr"):
  44. self.assertRaises(ValueError, assign_empty_list, arr)
  45. def assign_invalid_float():
  46. test.farr = (1.0, 2.0, "3.0")
  47. def assign_invalid_int():
  48. test.iarr = ("1", 2, 3)
  49. def assign_invalid_bool():
  50. test.barr = (True, 0.123, False)
  51. for func in [assign_invalid_float, assign_invalid_int, assign_invalid_bool]:
  52. self.assertRaises(TypeError, func)
  53. # shouldn't accept float as int
  54. def assign_float_as_int():
  55. test.iarr = (1, 2, 3.0)
  56. self.assertRaises(TypeError, assign_float_as_int)
  57. # non-dynamic arrays cannot change size
  58. def assign_different_size(arr, val):
  59. setattr(test, arr, val)
  60. for arr, val in zip(("iarr", "farr", "barr"), ((1, 2), (1.0, 2.0), (True, False))):
  61. self.assertRaises(ValueError, assign_different_size, arr, val)
  62. # test assignment of specific items
  63. def test_assign_item(self):
  64. for arr, rand_func in zip((test.farr, test.iarr, test.barr), (rand_float, rand_int, rand_bool)):
  65. for i in range(len(arr)):
  66. val = rand_func()
  67. arr[i] = val
  68. self.assertEqual(arr[i], val)
  69. # float prop should accept also int
  70. for i in range(len(test.farr)):
  71. val = rand_int()
  72. test.farr[i] = val
  73. self.assertEqual(test.farr[i], float(val))
  74. #
  75. def test_assign_item_fail(self):
  76. def assign_bad_index(arr):
  77. arr[4] = 1.0
  78. def assign_bad_type(arr):
  79. arr[1] = "123"
  80. for arr in [test.farr, test.iarr, test.barr]:
  81. self.assertRaises(IndexError, assign_bad_index, arr)
  82. # not testing bool because bool allows not only (True|False)
  83. for arr in [test.farr, test.iarr]:
  84. self.assertRaises(TypeError, assign_bad_type, arr)
  85. def test_dynamic_assign_array(self):
  86. # test various lengths here
  87. for arr, rand_func in zip(("fdarr", "idarr", "bdarr"), (rand_float, rand_int, rand_bool)):
  88. for length in range(1, 64):
  89. rval = make_random_array(length, rand_func)
  90. setattr(test, arr, rval)
  91. self.assertEqual(prop_to_list(getattr(test, arr)), rval)
  92. def test_dynamic_assign_array_fail(self):
  93. # could also test too big length here
  94. def assign_empty_list(arr):
  95. setattr(test, arr, ())
  96. for arr in ("fdarr", "idarr", "bdarr"):
  97. self.assertRaises(ValueError, assign_empty_list, arr)
  98. class TestMArray(unittest.TestCase):
  99. def setUp(self):
  100. # reset dynamic array sizes
  101. for arr, func in zip(("fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool)):
  102. setattr(test, arr, make_random_3d_array((3, 4, 5), func))
  103. # test assignment
  104. def test_assign_array(self):
  105. for arr, func in zip(("fmarr", "imarr", "bmarr"), (rand_float, rand_int, rand_bool)):
  106. # assignment of [3][4][5]
  107. rval = make_random_3d_array((3, 4, 5), func)
  108. setattr(test, arr, rval)
  109. self.assertEqual(prop_to_list(getattr(test, arr)), rval)
  110. # test assignment of [2][4][5], [1][4][5] should work on dynamic arrays
  111. def test_assign_array_fail(self):
  112. def assign_empty_array():
  113. test.fmarr = ()
  114. self.assertRaises(ValueError, assign_empty_array)
  115. def assign_invalid_size(arr, rval):
  116. setattr(test, arr, rval)
  117. # assignment of 3,4,4 or 3,3,5 should raise ex
  118. for arr, func in zip(("fmarr", "imarr", "bmarr"), (rand_float, rand_int, rand_bool)):
  119. rval = make_random_3d_array((3, 4, 4), func)
  120. self.assertRaises(ValueError, assign_invalid_size, arr, rval)
  121. rval = make_random_3d_array((3, 3, 5), func)
  122. self.assertRaises(ValueError, assign_invalid_size, arr, rval)
  123. rval = make_random_3d_array((3, 3, 3), func)
  124. self.assertRaises(ValueError, assign_invalid_size, arr, rval)
  125. def test_assign_item(self):
  126. # arr[i] = x
  127. for arr, func in zip(("fmarr", "imarr", "bmarr", "fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool) * 2):
  128. rval = make_random_2d_array((4, 5), func)
  129. for i in range(3):
  130. getattr(test, arr)[i] = rval
  131. self.assertEqual(prop_to_list(getattr(test, arr)[i]), rval)
  132. # arr[i][j] = x
  133. for arr, func in zip(("fmarr", "imarr", "bmarr", "fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool) * 2):
  134. arr = getattr(test, arr)
  135. rval = make_random_array(5, func)
  136. for i in range(3):
  137. for j in range(4):
  138. arr[i][j] = rval
  139. self.assertEqual(prop_to_list(arr[i][j]), rval)
  140. def test_assign_item_fail(self):
  141. def assign_wrong_size(arr, i, rval):
  142. getattr(test, arr)[i] = rval
  143. # assign wrong size at level 2
  144. for arr, func in zip(("fmarr", "imarr", "bmarr"), (rand_float, rand_int, rand_bool)):
  145. rval1 = make_random_2d_array((3, 5), func)
  146. rval2 = make_random_2d_array((4, 3), func)
  147. for i in range(3):
  148. self.assertRaises(ValueError, assign_wrong_size, arr, i, rval1)
  149. self.assertRaises(ValueError, assign_wrong_size, arr, i, rval2)
  150. def test_dynamic_assign_array(self):
  151. for arr, func in zip(("fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool)):
  152. # assignment of [3][4][5]
  153. rval = make_random_3d_array((3, 4, 5), func)
  154. setattr(test, arr, rval)
  155. self.assertEqual(prop_to_list(getattr(test, arr)), rval)
  156. # [2][4][5]
  157. rval = make_random_3d_array((2, 4, 5), func)
  158. setattr(test, arr, rval)
  159. self.assertEqual(prop_to_list(getattr(test, arr)), rval)
  160. # [1][4][5]
  161. rval = make_random_3d_array((1, 4, 5), func)
  162. setattr(test, arr, rval)
  163. self.assertEqual(prop_to_list(getattr(test, arr)), rval)
  164. # test access
  165. def test_access(self):
  166. pass
  167. # test slice access, negative indices
  168. def test_access_fail(self):
  169. pass
  170. random.seed()
  171. def rand_int():
  172. return random.randint(-1000, 1000)
  173. def rand_float():
  174. return float(rand_int())
  175. def rand_bool():
  176. return bool(random.randint(0, 1))
  177. def make_random_array(len, rand_func):
  178. arr = []
  179. for i in range(len):
  180. arr.append(rand_func())
  181. return arr
  182. def make_random_2d_array(dimsize, rand_func):
  183. marr = []
  184. for i in range(dimsize[0]):
  185. marr.append([])
  186. for j in range(dimsize[1]):
  187. marr[-1].append(rand_func())
  188. return marr
  189. def make_random_3d_array(dimsize, rand_func):
  190. marr = []
  191. for i in range(dimsize[0]):
  192. marr.append([])
  193. for j in range(dimsize[1]):
  194. marr[-1].append([])
  195. for k in range(dimsize[2]):
  196. marr[-1][-1].append(rand_func())
  197. return marr
  198. def prop_to_list(prop):
  199. ret = []
  200. for x in prop:
  201. if type(x) not in {bool, int, float}:
  202. ret.append(prop_to_list(x))
  203. else:
  204. ret.append(x)
  205. return ret
  206. def suite():
  207. return unittest.TestSuite([unittest.TestLoader().loadTestsFromTestCase(TestArray), unittest.TestLoader().loadTestsFromTestCase(TestMArray)])
  208. if __name__ == "__main__":
  209. unittest.TextTestRunner(verbosity=2).run(suite())