def __new__(mcs, name, bases, dict): def gen_test_success(d, config, transfunc, expectedshape): def test(self): config.grid_dims = d g = Grid2D(config, transfunc) self.assertIsInstance(g, Grid2D) self.assertEqual(g.grid.shape, expectedshape) return test def gen_test_valerr(d, config, transfunc): def test(self): config.grid_dims = d l = lambda c=config: Grid2D(c, transfunc) self.assertRaises(ValueError, l) return test config = CAConfig('test/testdescriptions/2dbasic.py') dimensions = 2 config.states = 0,1,2 config.nhood_arr = [1,1,1] shapes = range(0, 5) def transfunc(grid, neighbourcounts): return grid for i in shapes: for j in shapes: testname = "test_grid_2d_{i}{j}".format(i=i, j=j) testdims = (i, j) if i < 3 or j < 3: dict[testname] = gen_test_valerr(testdims, config, transfunc) else: dict[testname] = gen_test_success(testdims, config, transfunc, (i,j)) return type.__new__(mcs, name, bases, dict)
def __new__(mcs, name, bases, dict): def gen_test_array_eq(a, b): def test(self): self.assertTrue(np.array_equal(a, b)) return test config = CAConfig("test/testdescriptions/1dbasic.py") config.num_generations = 20 config.states = 0, 1 config.dimensions = 1 config.nhood_arr = [1, 1, 1] def transfunc(self, grid, neighbourcounts): return grid[0] numgens = 20 sizes = range(numgens * 2, numgens * 2 + 3) for s in sizes: testname = "test_initial_grid_set_{s}".format(s=s) initialgrid = np.array([np.random.randint(0, 2, s)]) config.initial_grid = initialgrid g = Grid1D(config, transfunc) #check that the first lines match if s < g.grid.shape[1]: dict[testname] = gen_test_array_eq(g.grid[0, :s], initialgrid[0]) else: dict[testname] = gen_test_array_eq( g.grid[0], initialgrid[0, :g.grid.shape[1]]) return type.__new__(mcs, name, bases, dict)
def __new__(mcs, name, bases, dict): def gen_test_array_equal(a,b): def test(self): self.assertTrue(np.array_equal(a, b)) return test config = CAConfig('test/testdescriptions/2dbasic.py') dimensions = 2 config.states = 0,1,2 config.nhood_arr = [1,1,1] def transfunc(self, grid, neighbourcounts): return grid #matching dimensions config.grid_dims = (20,20) testname = "test_gridset_2d_{i}{j}".format(i=config.grid_dims[0], j=config.grid_dims[1]) config.initial_grid = np.random.randint(0, 3, config.grid_dims) g = Grid2D(config, transfunc) dict[testname] = gen_test_array_equal(g.grid, config.initial_grid) #non matching testsize = [(19,20), (20,21), (21,21), (19,19)] for s in testsize: testname = "test_gridset_2d_{i}{j}".format(i=s[0], j=s[1]) config.grid_dims = s ig = np.random.randint(0, 3, s) config.initial_grid = ig g = Grid2D(config, transfunc) if s < config.grid_dims: dict[testname] = gen_test_array_equal(g.grid[:s[0], :s[1]], ig) else: dict[testname] = gen_test_array_equal(g.grid, ig[:config.grid_dims[0], :config.grid_dims[1]]) return type.__new__(mcs, name, bases, dict)