Blender V2.61 - r43446
|
00001 # ##### BEGIN GPL LICENSE BLOCK ##### 00002 # 00003 # This program is free software; you can redistribute it and/or 00004 # modify it under the terms of the GNU General Public License 00005 # as published by the Free Software Foundation; either version 2 00006 # of the License, or (at your option) any later version. 00007 # 00008 # This program is distributed in the hope that it will be useful, 00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 # GNU General Public License for more details. 00012 # 00013 # You should have received a copy of the GNU General Public License 00014 # along with this program; if not, write to the Free Software Foundation, 00015 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00016 # 00017 # ##### END GPL LICENSE BLOCK ##### 00018 00019 import unittest 00020 import random 00021 00022 test= bpy.data.test 00023 00024 # farr - 1-dimensional array of float 00025 # fdarr - dynamic 1-dimensional array of float 00026 # fmarr - 3-dimensional ([3][4][5]) array of float 00027 # fdmarr - dynamic 3-dimensional (ditto size) array of float 00028 00029 # same as above for other types except that the first letter is "i" for int and "b" for bool 00030 00031 class TestArray(unittest.TestCase): 00032 # test that assignment works by: assign -> test value 00033 # - rvalue = list of float 00034 # - rvalue = list of numbers 00035 # test.object 00036 # bpy.data.test.farr[3], iarr[3], barr[...], fmarr, imarr, bmarr 00037 00038 def setUp(self): 00039 test.farr= (1.0, 2.0, 3.0) 00040 test.iarr= (7, 8, 9) 00041 test.barr= (False, True, False) 00042 00043 # test access 00044 # test slice access, negative indices 00045 def test_access(self): 00046 rvals= ([1.0, 2.0, 3.0], [7, 8, 9], [False, True, False]) 00047 for arr, rval in zip((test.farr, test.iarr, test.barr), rvals): 00048 self.assertEqual(prop_to_list(arr), rval) 00049 self.assertEqual(arr[0:3], rval) 00050 self.assertEqual(arr[1:2], rval[1:2]) 00051 self.assertEqual(arr[-1], arr[2]) 00052 self.assertEqual(arr[-2], arr[1]) 00053 self.assertEqual(arr[-3], arr[0]) 00054 00055 # fail when index out of bounds 00056 def test_access_fail(self): 00057 for arr in (test.farr, test.iarr, test.barr): 00058 self.assertRaises(IndexError, lambda : arr[4]) 00059 00060 # test assignment of a whole array 00061 def test_assign_array(self): 00062 # should accept int as float 00063 test.farr= (1, 2, 3) 00064 00065 # fail when: unexpected no. of items, invalid item type 00066 def test_assign_array_fail(self): 00067 def assign_empty_list(arr): 00068 setattr(test, arr, ()) 00069 00070 for arr in ("farr", "iarr", "barr"): 00071 self.assertRaises(ValueError, assign_empty_list, arr) 00072 00073 def assign_invalid_float(): 00074 test.farr= (1.0, 2.0, "3.0") 00075 00076 def assign_invalid_int(): 00077 test.iarr= ("1", 2, 3) 00078 00079 def assign_invalid_bool(): 00080 test.barr= (True, 0.123, False) 00081 00082 for func in [assign_invalid_float, assign_invalid_int, assign_invalid_bool]: 00083 self.assertRaises(TypeError, func) 00084 00085 # shouldn't accept float as int 00086 def assign_float_as_int(): 00087 test.iarr= (1, 2, 3.0) 00088 self.assertRaises(TypeError, assign_float_as_int) 00089 00090 # non-dynamic arrays cannot change size 00091 def assign_different_size(arr, val): 00092 setattr(test, arr, val) 00093 for arr, val in zip(("iarr", "farr", "barr"), ((1, 2), (1.0, 2.0), (True, False))): 00094 self.assertRaises(ValueError, assign_different_size, arr, val) 00095 00096 # test assignment of specific items 00097 def test_assign_item(self): 00098 for arr, rand_func in zip((test.farr, test.iarr, test.barr), (rand_float, rand_int, rand_bool)): 00099 for i in range(len(arr)): 00100 val= rand_func() 00101 arr[i]= val 00102 00103 self.assertEqual(arr[i], val) 00104 00105 # float prop should accept also int 00106 for i in range(len(test.farr)): 00107 val= rand_int() 00108 test.farr[i]= val 00109 self.assertEqual(test.farr[i], float(val)) 00110 00111 # 00112 00113 def test_assign_item_fail(self): 00114 def assign_bad_index(arr): 00115 arr[4] = 1.0 00116 00117 def assign_bad_type(arr): 00118 arr[1]= "123" 00119 00120 for arr in [test.farr, test.iarr, test.barr]: 00121 self.assertRaises(IndexError, assign_bad_index, arr) 00122 00123 # not testing bool because bool allows not only (True|False) 00124 for arr in [test.farr, test.iarr]: 00125 self.assertRaises(TypeError, assign_bad_type, arr) 00126 00127 def test_dynamic_assign_array(self): 00128 # test various lengths here 00129 for arr, rand_func in zip(("fdarr", "idarr", "bdarr"), (rand_float, rand_int, rand_bool)): 00130 for length in range(1, 64): 00131 rval= make_random_array(length, rand_func) 00132 setattr(test, arr, rval) 00133 self.assertEqual(prop_to_list(getattr(test, arr)), rval) 00134 00135 def test_dynamic_assign_array_fail(self): 00136 # could also test too big length here 00137 00138 def assign_empty_list(arr): 00139 setattr(test, arr, ()) 00140 00141 for arr in ("fdarr", "idarr", "bdarr"): 00142 self.assertRaises(ValueError, assign_empty_list, arr) 00143 00144 00145 class TestMArray(unittest.TestCase): 00146 def setUp(self): 00147 # reset dynamic array sizes 00148 for arr, func in zip(("fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool)): 00149 setattr(test, arr, make_random_3d_array((3, 4, 5), func)) 00150 00151 # test assignment 00152 def test_assign_array(self): 00153 for arr, func in zip(("fmarr", "imarr", "bmarr"), (rand_float, rand_int, rand_bool)): 00154 # assignment of [3][4][5] 00155 rval= make_random_3d_array((3, 4, 5), func) 00156 setattr(test, arr, rval) 00157 self.assertEqual(prop_to_list(getattr(test, arr)), rval) 00158 00159 # test assignment of [2][4][5], [1][4][5] should work on dynamic arrays 00160 00161 def test_assign_array_fail(self): 00162 def assign_empty_array(): 00163 test.fmarr= () 00164 self.assertRaises(ValueError, assign_empty_array) 00165 00166 def assign_invalid_size(arr, rval): 00167 setattr(test, arr, rval) 00168 00169 # assignment of 3,4,4 or 3,3,5 should raise ex 00170 for arr, func in zip(("fmarr", "imarr", "bmarr"), (rand_float, rand_int, rand_bool)): 00171 rval= make_random_3d_array((3, 4, 4), func) 00172 self.assertRaises(ValueError, assign_invalid_size, arr, rval) 00173 00174 rval= make_random_3d_array((3, 3, 5), func) 00175 self.assertRaises(ValueError, assign_invalid_size, arr, rval) 00176 00177 rval= make_random_3d_array((3, 3, 3), func) 00178 self.assertRaises(ValueError, assign_invalid_size, arr, rval) 00179 00180 def test_assign_item(self): 00181 # arr[i] = x 00182 for arr, func in zip(("fmarr", "imarr", "bmarr", "fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool) * 2): 00183 rval= make_random_2d_array((4, 5), func) 00184 00185 for i in range(3): 00186 getattr(test, arr)[i]= rval 00187 self.assertEqual(prop_to_list(getattr(test, arr)[i]), rval) 00188 00189 # arr[i][j] = x 00190 for arr, func in zip(("fmarr", "imarr", "bmarr", "fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool) * 2): 00191 00192 arr= getattr(test, arr) 00193 rval= make_random_array(5, func) 00194 00195 for i in range(3): 00196 for j in range(4): 00197 arr[i][j]= rval 00198 self.assertEqual(prop_to_list(arr[i][j]), rval) 00199 00200 00201 def test_assign_item_fail(self): 00202 def assign_wrong_size(arr, i, rval): 00203 getattr(test, arr)[i]= rval 00204 00205 # assign wrong size at level 2 00206 for arr, func in zip(("fmarr", "imarr", "bmarr"), (rand_float, rand_int, rand_bool)): 00207 rval1= make_random_2d_array((3, 5), func) 00208 rval2= make_random_2d_array((4, 3), func) 00209 00210 for i in range(3): 00211 self.assertRaises(ValueError, assign_wrong_size, arr, i, rval1) 00212 self.assertRaises(ValueError, assign_wrong_size, arr, i, rval2) 00213 00214 def test_dynamic_assign_array(self): 00215 for arr, func in zip(("fdmarr", "idmarr", "bdmarr"), (rand_float, rand_int, rand_bool)): 00216 # assignment of [3][4][5] 00217 rval= make_random_3d_array((3, 4, 5), func) 00218 setattr(test, arr, rval) 00219 self.assertEqual(prop_to_list(getattr(test, arr)), rval) 00220 00221 # [2][4][5] 00222 rval= make_random_3d_array((2, 4, 5), func) 00223 setattr(test, arr, rval) 00224 self.assertEqual(prop_to_list(getattr(test, arr)), rval) 00225 00226 # [1][4][5] 00227 rval= make_random_3d_array((1, 4, 5), func) 00228 setattr(test, arr, rval) 00229 self.assertEqual(prop_to_list(getattr(test, arr)), rval) 00230 00231 00232 # test access 00233 def test_access(self): 00234 pass 00235 00236 # test slice access, negative indices 00237 def test_access_fail(self): 00238 pass 00239 00240 random.seed() 00241 00242 def rand_int(): 00243 return random.randint(-1000, 1000) 00244 00245 def rand_float(): 00246 return float(rand_int()) 00247 00248 def rand_bool(): 00249 return bool(random.randint(0, 1)) 00250 00251 def make_random_array(len, rand_func): 00252 arr= [] 00253 for i in range(len): 00254 arr.append(rand_func()) 00255 00256 return arr 00257 00258 def make_random_2d_array(dimsize, rand_func): 00259 marr= [] 00260 for i in range(dimsize[0]): 00261 marr.append([]) 00262 00263 for j in range(dimsize[1]): 00264 marr[-1].append(rand_func()) 00265 00266 return marr 00267 00268 def make_random_3d_array(dimsize, rand_func): 00269 marr= [] 00270 for i in range(dimsize[0]): 00271 marr.append([]) 00272 00273 for j in range(dimsize[1]): 00274 marr[-1].append([]) 00275 00276 for k in range(dimsize[2]): 00277 marr[-1][-1].append(rand_func()) 00278 00279 return marr 00280 00281 def prop_to_list(prop): 00282 ret= [] 00283 00284 for x in prop: 00285 if type(x) not in (bool, int, float): 00286 ret.append(prop_to_list(x)) 00287 else: 00288 ret.append(x) 00289 00290 return ret 00291 00292 def suite(): 00293 return unittest.TestSuite([unittest.TestLoader().loadTestsFromTestCase(TestArray), unittest.TestLoader().loadTestsFromTestCase(TestMArray)]) 00294 00295 if __name__ == "__main__": 00296 unittest.TextTestRunner(verbosity=2).run(suite()) 00297