Example #1
0
    def test(self, f: Formula) -> None:
        self.case += 1
        self.tested += 1
        print("CASE %d: %s" % (self.case, f.toString()))
        cnf = Cnf()
        try:
            start = now()
            cnf = f.toCnf()
            duration = now() - start
        except KeyboardInterrupt:
            raise KeyboardInterrupt()
        except:
            printException()
            if stopOnError:
                raise FailedTestException()
            return

        deg = self.formulaDeg(f)
        size = self.cnfSize(cnf)
        fVars = self.formulaVars(f)
        cnfVars = self.cnfVars(cnf)

        print(
            'CNF: time: %12.9f   fVars: %3d  f.deg: %3d    cnfVars: %3d  cnf.size: %3d'
            % (duration, len(fVars), deg, len(cnfVars), size))
        print('fVars:   %r' % (sorted(fVars), ))
        print('cnfVars: %r' % (sorted(cnfVars), ))

        if not isinstance(cnf, Cnf):
            print('FAILED: not a CNF (must be an instanse of Cnf): %s' %
                  type(cnf))
            print()
            return

        for i, cls in enumerate(cnf):
            if not isinstance(cls, Clause):
                print(
                    'FAILED: not a CNF, %d-th clause is not an instance of Clause: %s'
                    % (i, type(cnf)))
                print()
                return
            for j, lit in enumerate(cls):
                if not isinstance(lit, Literal):
                    print(
                        'FAILED: not a CNF, %d-th literal of %d-th clause is not a Literal: %s'
                        % (j, i, type(cnf)))
                    print()
                    return

        start = now()
        equiSatisfiable = self.satisfiableFormula(f) == self.satisfiableCnf(
            cnf)
        allVars = fVars.union(cnfVars)
        equivalent = all(
            self.formulaIsSatisfied(f, v) == self.cnfIsSatisfied(cnf, v)
            for v in self.valuations(allVars))
        durationTest = now() - start

        if equivalent:
            self.equiv += 1

        self.size += size
        self.time += duration

        if equiSatisfiable:
            self.passed += 1
            print('PASSED: testTime: %12.9f    equiSatisfiable %s' %
                  (durationTest, 'equivalent' if equivalent else ''))
        else:
            if len(cnf) < 20:
                print('FAILED: \n-----CNF-----\n%s%s' % (str(cnf), '-' * 13))
            else:
                print('FAILED: \n-----CNF-----\n%s...\n%s' %
                      (str(cnf[:20]), '-' * 13))
            if stopOnError:
                raise FailedTestException()
        print()