def test_generate(self):
        """
        Ensures that the generate iterable is used to create new generations.
        """
        start_pop = [1, 2, 3]

        def fitness(genome):
            """
            For illustrative purposes.
            """
            return genome

        def halt(population, generation_count):
            """
            Halts the algorithm after ten populations.
            """
            return generation_count == 10

        class Generate(object):
            """
            An iterable for producing new offspring.
            """

            def __init__(self, parent_population):
                """
                @param parent_population: the parent genomes.
                """
                self.counter = 0
                self.size = len(parent_population)
                self.parents = parent_population

            def __iter__(self):
                return self

            def next(self):
                """
                For testing purposes only.
                """
                if self.counter < self.size:
                    pos, self.counter = self.counter, self.counter + 1
                    return self.parents[pos] + 1
                else:
                    raise StopIteration()

        ga = genetic_algorithm(start_pop, fitness, Generate, halt)
        actual = [p for p in ga]
        expected = [[3 + i, 2 + i, 1 + i] for i in range(10)]
        self.assertEqual(actual, expected,
            "Actual: %r Expected: %r" % (actual, expected))
dataset/ETHPy150Open ntoll/foox/test/test_ga.py/TestGeneticAlgorithm.test_generate/original