Exemplo n.º 1
0
 def test_choice_constraint(self):
     self.assertEqual(ChoiceConstraint((1, )).choice_count, 1)
     self.assertTupleEqual(ChoiceConstraint((1, 2)).choices, (1, 2))
     with self.assertRaisesRegex(ValueError, 'possible choices.'):
         BoundedConstraint(1.2, 4).choices
     with self.assertRaisesRegex(ValueError, 'number of choices.'):
         BoundedConstraint(1.2, 1.3).choice_count
Exemplo n.º 2
0
 def test_array_generator_sorted(self):
     s = ArrayGenerator(BoundedConstraint(1, 50),
                        V=BoundedConstraint(1, 50),
                        type='sorted',
                        seed=self.SEED)
     for i in range(10):
         with self.subTest(i=i):
             arr = s.next()
             self.assertListEqual(arr, sorted(arr))
Exemplo n.º 3
0
    def test_generator_graph(self):
        def set_constraints(this):
            this.N = BoundedConstraint(1, 500)
            this.M = BoundedConstraint(1, 2000)
            this.E = CustomGeneratorConstraint(generator=GraphGenerator)

        def generate_input(self, **kwargs):
            n = self.N.next
            self.M.set_min(max(self.M.min, n - 1))
            self.M.set_max(max(self.M.max, n - 1))
            m = self.M.next
            yield n, m
            self.E.initialize(N=n,
                              type=2,
                              M=m,
                              duplicates=True,
                              self_loops=True)

            for i in range(m):
                s = self.E.next
                assert s is not None
                yield s
            assert self.E.next is None

        Case.SET_CONSTRAINTS = set_constraints
        Case.SET_INPUT = generate_input

        batches = [
            Batch(num=1, cases=[Case() for i in range(10)], start=1),
            Batch(num=2,
                  cases=[
                      Case(N=BoundedConstraint(5, 5),
                           M=BoundedConstraint(100, 100))
                  ]),
            Batch(num=3, cases=[Case(N=BoundedConstraint(1, 1))]),
        ]
        Generator(batches=batches).start()

        self.assertEqual(len(os.listdir(Batch.CASES_DIR)), len(batches))
        for x in batches:
            cases = set(os.listdir(x.location))
            for j in range(len(x.cases)):
                case = j + x.start_case
                self.assertIn('{num}.in'.format(num=case), cases)
                with open(os.path.join(x.location, '{}.in'.format(case))) as f:
                    data = f.read().split('\n')
                    self.assertEqual(
                        len(data),
                        int(data[0].split()[1]) +
                        2)  # account of ending newline + initial line
Exemplo n.º 4
0
    def test_generator_basic(self):
        def set_constraints(this):
            this.N = BoundedConstraint(1, 100)
            this.M = BoundedConstraint(1, 10, generator=random.uniform)

        def generate_input(self, **kwargs):
            yield self.N.next, self.M.next
            yield str(self.N.next)
            yield self.M.next

        Case.SET_CONSTRAINTS = set_constraints
        Case.SET_INPUT = generate_input

        batches = [
            Batch(num=1, cases=[Case(N=BoundedConstraint(1, 1))]),
            Batch(num=2, cases=[Case() for i in range(10)]),
        ]
        Generator(batches=batches).start()

        self.assertEqual(len(os.listdir(Batch.CASES_DIR)), len(batches))
        for x in batches:
            self.assertEqual(
                sorted(os.listdir(x.location)),
                sorted('{}.in'.format(i + x.start_case)
                       for i in range(len(x.cases))))
Exemplo n.º 5
0
 def test_string_generator_charset(self):
     s = StringGenerator(BoundedConstraint(1, 20),
                         V=ChoiceConstraint('azyc'),
                         seed=self.SEED)
     for i in range(10):
         with self.subTest(i=i):
             self.assertTrue(set(s.next()).issubset('azyc'))
Exemplo n.º 6
0
 def test_array_generator_fail_validation(self):
     with self.assertRaisesRegex(ValueError, 'Unknown type aa'):
         ArrayGenerator(5,
                        V=BoundedConstraint(1, 1),
                        type='aa',
                        seed=self.SEED)
     with self.assertRaisesRegex(ValueError, 'must be a '):
         ArrayGenerator(5, 1, seed=self.SEED)
Exemplo n.º 7
0
 def test_array_generator_palindrome(self):
     s = ArrayGenerator(100,
                        V=BoundedConstraint(1, 100),
                        type='palindrome',
                        seed=self.SEED)
     for i in range(10):
         with self.subTest(i=i):
             arr = s.next()
             self.assertListEqual(arr, arr[::-1])
Exemplo n.º 8
0
 def test_array_generator_distinct(self):
     self.assertListEqual(
         ArrayGenerator(10,
                        V=BoundedConstraint(1, 60),
                        type='distinct',
                        k=3,
                        seed=self.SEED).next(),
         [9, 37, 55, 52, 49, 5, 17, 8, 32, 49],
     )
     self.assertListEqual(
         sorted(
             ArrayGenerator(100,
                            V=BoundedConstraint(1, 100),
                            type='distinct',
                            seed=self.SEED).next()),
         list(range(1, 101)),
     )
     with self.assertRaisesRegex(ValueError, 'Impossible to generate.'):
         ArrayGenerator(10,
                        V=BoundedConstraint(1, 4),
                        type='distinct',
                        k=2,
                        seed=self.SEED).next()
Exemplo n.º 9
0
    def test_generator_tree(self):
        def set_constraints(this):
            this.N = BoundedConstraint(1, 10**5)
            this.M = BoundedConstraint(1, 10)
            this.E = CustomGeneratorConstraint(generator=GraphGenerator)
            this.T = BoundedConstraint(10, 14)

        def generate_input(self, **kwargs):
            n = self.N.next
            yield n, self.M.next
            self.E.initialize(N=n, type=self.T.next)

            for i in range(n - 1):
                s = self.E.next
                assert s is not None
                yield s
            assert self.E.next is None

        Case.SET_CONSTRAINTS = set_constraints
        Case.SET_INPUT = generate_input

        batches = [
            Batch(num=1, cases=[Case() for i in range(5)], start=5),
            Batch(num=2,
                  cases=[Case(N=BoundedConstraint(1, 100))
                         for i in range(20)]),
        ]
        Generator(batches=batches, exe='echo 0').start()

        self.assertEqual(len(os.listdir(Batch.CASES_DIR)), len(batches))
        for x in batches:
            cases = set(os.listdir(x.location))
            for j in range(len(x.cases)):
                case = j + x.start_case
                for ext in ('in', 'out'):
                    self.assertIn('{num}.{ext}'.format(num=case, ext=ext),
                                  cases)
                with open(os.path.join(x.location,
                                       '{}.out'.format(case))) as f:
                    self.assertEqual(f.read(), '0\n')
                with open(os.path.join(x.location, '{}.in'.format(case))) as f:
                    data = f.read().split('\n')
                    self.assertEqual(len(data),
                                     int(data[0].split()[0]) +
                                     1)  # account of ending newline
Exemplo n.º 10
0
 def set_constraints(this):
     this.N = BoundedConstraint(1, 10**5)
     this.M = BoundedConstraint(1, 10)
     this.E = CustomGeneratorConstraint(generator=GraphGenerator)
     this.T = BoundedConstraint(10, 14)
Exemplo n.º 11
0
 def set_constraints(this):
     this.N = BoundedConstraint(1, 100)
     this.M = BoundedConstraint(1, 10, generator=random.uniform)
Exemplo n.º 12
0
 def set_constraints(this):
     this.N = BoundedConstraint(1, 500)
     this.M = BoundedConstraint(1, 2000)
     this.E = CustomGeneratorConstraint(generator=GraphGenerator)
Exemplo n.º 13
0
 def test_bounded_constraint(self):
     with self.assertRaisesRegex(ValueError, 'takes exactly 2 arguments.'):
         BoundedConstraint(1, 1, 1)