Exemplo n.º 1
0
    def __new__(mcs, name, bases, dict):
        def gen_test_success(gens, config, transfunc):
            def test(self):
                config.num_generations = gens
                g = Grid1D(config, transfunc)
                self.assertIsInstance(g, Grid1D)

            return test

        def gen_test_valerr(gens, config, transfunc):
            def test(self):
                config.num_generations = gens
                self.assertRaises(ValueError,
                                  lambda c=config: Grid1D(config, transfunc))

            return test

        config = CAConfig("test/testdescriptions/1dbasic.py")
        config.states = 0, 1
        testgens = range(-1, 6)

        def transfunc(self, grid, neighbourcounts):
            return grid

        for g in testgens:
            testname = "test_1d_numgens_{i}".format(i=g)
            config.num_generations = g
            if g < 1:
                dict[testname] = gen_test_valerr(g, config, transfunc)
            else:
                dict[testname] = gen_test_success(g, config, transfunc)
        return type.__new__(mcs, name, bases, dict)
Exemplo n.º 2
0
    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)