Exemplo n.º 1
0
    def test_compare(self):
        print(">> LogicProgram.compare(other)")

        # Equal programs
        for i in range(self.__nb_unit_test):
            p1 = self.random_program(self.__nb_variables, self.__nb_values,
                                     self.__body_size)
            p2 = LogicProgram(p1.get_variables(), p1.get_values(),
                              p1.get_rules())
            common, missing, over = p1.compare(p2)

            self.assertEqual(len(common), len(p1.get_rules()))
            self.assertEqual(len(missing), 0)
            self.assertEqual(len(over), 0)

        # Equal programs reverse call
        for i in range(self.__nb_unit_test):
            p1 = self.random_program(self.__nb_variables, self.__nb_values,
                                     self.__body_size)
            p2 = LogicProgram(p1.get_variables(), p1.get_values(),
                              p1.get_rules())
            common, missing, over = p2.compare(p1)

            self.assertEqual(len(common), len(p1.get_rules()))
            self.assertEqual(len(missing), 0)
            self.assertEqual(len(over), 0)

        # Random programs
        for i in range(self.__nb_unit_test):
            p1 = self.random_program(self.__nb_variables, self.__nb_values,
                                     self.__body_size)
            p2 = self.random_program(self.__nb_variables, self.__nb_values,
                                     self.__body_size)
            common, missing, over = p1.compare(p2)

            # All rules appear in a one of the set
            for r in p1.get_rules():
                self.assertTrue(r in common or r in missing)
            for r in p2.get_rules():
                self.assertTrue(r in common or r in over)

            # All rules are correctly placed
            for r in common:
                self.assertTrue(r in p1.get_rules() and r in p2.get_rules())
            for r in missing:
                self.assertTrue(r in p1.get_rules()
                                and r not in p2.get_rules())
            for r in over:
                self.assertTrue(r not in p1.get_rules()
                                and r in p2.get_rules())
Exemplo n.º 2
0
    def test___init__(self):
        print(">> LogicProgram.__init__(self, variables, values, rules)")

        for i in range(self.__nb_unit_test):
            variables = [
                "x" + str(i)
                for i in range(random.randint(1, self.__nb_variables))
            ]
            values = []
            rules = []

            for var in range(len(variables)):
                values.append([
                    val
                    for val in range(0, random.randint(2, self.__nb_values))
                ])

            for j in range(random.randint(0, self.__nb_rules)):
                r = self.random_rule(variables, values, self.__body_size)
                rules.append(r)

            p = LogicProgram(variables, values, rules)

            self.assertEqual(p.get_variables(), variables)
            self.assertEqual(p.get_values(), values)
            self.assertEqual(p.get_rules(), rules)