diff --git a/Makefile b/Makefile index cf05f91b..6e0310dc 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,9 @@ doc: $(COMP) $(FLAGS) $(DIRS) $(DOC) test: $(TEST) - ./tests/main tests/test-0.d + +test-full: $(TEST) + ./tests/run $(TEST): $(LIB) $(COMP) $(FLAGS) $(DIRS) $(TEST) diff --git a/sat/sat.ml b/sat/sat.ml index c8149483..0ff23b98 100644 --- a/sat/sat.ml +++ b/sat/sat.ml @@ -11,7 +11,12 @@ module Fsat = struct let max_lit = min max_int (- min_int) let max_index = ref 0 - let make i = if i <> 0 then i else raise Dummy + let make i = + if i <> 0 then begin + max_index := max !max_index (abs i); + i + end else + raise Dummy let dummy = 0 diff --git a/sat/sat.mli b/sat/sat.mli index d3d98015..1ef2fdc4 100644 --- a/sat/sat.mli +++ b/sat/sat.mli @@ -18,7 +18,7 @@ module Make(Dummy: sig end) : sig val make : int -> atom (** Returns the literal corresponding to the integer. - @raise bad_atom if given [0] as argument.*) + @raise Bad_atom if given [0] as argument.*) val neg : atom -> atom (** [neg a] returns the negation of a literal. Involutive, i.e [neg (neg a) = a]. *) diff --git a/tests/run b/tests/run new file mode 100755 index 00000000..e23576c3 --- /dev/null +++ b/tests/run @@ -0,0 +1,19 @@ +#!/bin/bash + +solvertest () { + for f in `find $1 -name *.d -type f` + do + echo -ne "\r\033[K Testing $f " + tests/main $f | grep $2 > /dev/null 2> /dev/null + RET=$? + if [ $RET -ne 0 ]; + then + echo -e "\r\033[K\e[31m[KO]\e[0m $f" + exit 2 + fi + done + echo -e "\r\033[K\e[32m[OK]\e[0m $2" +} + +solvertest "tests/sat/" "Sat" +solvertest "tests/unsat/" "Unsat" diff --git a/tests/test-0.d b/tests/sat/test-000.d similarity index 53% rename from tests/test-0.d rename to tests/sat/test-000.d index 527f6034..b64785ea 100644 --- a/tests/test-0.d +++ b/tests/sat/test-000.d @@ -1,3 +1,3 @@ -c First test +c Status: SAT p cnf 2 1 1 2 0 diff --git a/tests/unsat/test-001.d b/tests/unsat/test-001.d new file mode 100644 index 00000000..d4c3d433 --- /dev/null +++ b/tests/unsat/test-001.d @@ -0,0 +1,8 @@ +c First test +p cnf 5 6 +1 2 0 +-1 3 0 +-2 3 0 +4 5 0 +-4 -3 0 +-5 -3 0 diff --git a/util/lexdimacs.mll b/util/lexdimacs.mll index d2f544b2..7824dd3f 100644 --- a/util/lexdimacs.mll +++ b/util/lexdimacs.mll @@ -11,6 +11,6 @@ rule token = parse | 'p' { P } | "cnf" { CNF } | '\n' { EOL } - | "c " [^ '\n']* '\n' { token lexbuf } + | "c " [^ '\n']* '\n' { token lexbuf } | ['-']? number { LIT (int_of_string (Lexing.lexeme lexbuf)) } | eof { EOF } diff --git a/util/parsedimacs.mly b/util/parsedimacs.mly index 147ff6ab..35be9555 100644 --- a/util/parsedimacs.mly +++ b/util/parsedimacs.mly @@ -15,15 +15,16 @@ /* DIMACS syntax */ file: - | EOF { [] } + | EOF { [] } | P CNF LIT LIT EOL clause_list { $6 } ; clause_list: | EOF { [] } + | EOL clause_list { $2 } | clause clause_list { $1 :: $2 } ; clause: - /* Clause always ends with a '0' */ + /* clauses always ends with a '0' */ | LIT EOL { if $1 = 0 then [] else raise (Clause_ending $1) } | LIT clause { $1 :: $2 } ; diff --git a/util/test.ml b/util/test.ml index 60a69823..2c92cbc0 100644 --- a/util/test.ml +++ b/util/test.ml @@ -3,12 +3,15 @@ module S = Sat.Make(struct end) (* Arguments parsing *) let file = ref "" +let p_assign = ref false let input_file = fun s -> file := s let usage = "Usage : main [options] " let argspec = Arg.align [ "-v", Arg.Int (fun i -> Log.set_debug i), - " Sets the debug verbose level"; + " Sets the debug verbose level"; + "-model", Arg.Set p_assign, + " Outputs the boolean model found if sat"; ] (* Entry file parsing *) @@ -33,7 +36,7 @@ let print_assign fmt () = S.iter_atoms (fun a -> Format.fprintf fmt "%a -> %s,@ " S.print_atom a - (if S.eval a then "true" else "false") + (if S.eval a then "T" else "F") ) let main () = @@ -43,8 +46,14 @@ let main () = exit 2 end; let cnf = get_cnf () in - print_cnf cnf; - () + S.assume cnf; + match S.solve () with + | S.Sat -> + Format.printf "Sat@."; + if !p_assign then + print_assign Format.std_formatter () + | S.Unsat -> + Format.printf "Unsat@." ;; main ()