@@ -4,50 +4,47 @@ using namespace std;
44
55#include " 2sat.h"
66
7- void setup (int size) {
8- n = 2 * size;
9- adj.clear ();
10- adj.resize (n);
11- adj_t .clear ();
12- adj_t .resize (n);
7+ void test_2sat_example_usage () {
8+ TwoSatSolver::example_usage ();
139}
1410
1511void test_2sat_article_example () {
16- setup (3 );
17- add_disjunction (0 , 0 , 1 , 1 ); // a v not b
18- add_disjunction (0 , 1 , 1 , 0 ); // not a v b
19- add_disjunction (0 , 1 , 1 , 1 ); // not a v not b
20- add_disjunction (0 , 0 , 2 , 1 ); // a v not c
21- assert (solve_2SAT () == true );
22- auto expected = vector<bool >{{false , false , false }};
23- assert (assignment == expected);
12+ TwoSatSolver solver (3 ); // a, b, c
13+ solver. add_disjunction (0 , false , 1 , true ); // a v not b
14+ solver. add_disjunction (0 , true , 1 , false ); // not a v b
15+ solver. add_disjunction (0 , true , 1 , true ); // not a v not b
16+ solver. add_disjunction (0 , false , 2 , true ); // a v not c
17+ assert (solver. solve_2SAT () == true );
18+ auto expected = vector<bool >{{false , false , false }};
19+ assert (solver. assignment == expected);
2420}
2521
2622void test_2sat_unsatisfiable () {
27- setup (2 );
28- add_disjunction (0 , 0 , 1 , 0 ); // x v y
29- add_disjunction (0 , 0 , 1 , 1 ); // x v not y
30- add_disjunction (0 , 1 , 1 , 0 ); // not x v y
31- add_disjunction (0 , 1 , 1 , 1 ); // not x v not y
32- assert (solve_2SAT () == false );
23+ TwoSatSolver solver (2 ); // a, b
24+ solver. add_disjunction (0 , false , 1 , false ); // a v b
25+ solver. add_disjunction (0 , false , 1 , true ); // a v not b
26+ solver. add_disjunction (0 , true , 1 , false ); // not a v b
27+ solver. add_disjunction (0 , true , 1 , true ); // not a v not b
28+ assert (solver. solve_2SAT () == false );
3329}
3430
3531void test_2sat_other_satisfiable_example () {
36- setup (4 );
37- add_disjunction (0 , 0 , 1 , 1 ); // a v not b
38- add_disjunction (0 , 1 , 2 , 1 ); // not a v not c
39- add_disjunction (0 , 0 , 1 , 0 ); // a v b
40- add_disjunction (3 , 0 , 2 , 1 ); // d v not c
41- add_disjunction (3 , 0 , 0 , 1 ); // d v not a
42- assert (solve_2SAT () == true );
43- // two solutions
44- auto expected_1 = vector<bool >{{true , true , false , true }};
45- auto expected_2 = vector<bool >{{true , false , false , true }};
46- assert (assignment == expected_1 || assignment == expected_2);
32+ TwoSatSolver solver (4 ); // a, b, c, d
33+ solver. add_disjunction (0 , false , 1 , true ); // a v not b
34+ solver. add_disjunction (0 , true , 2 , true ); // not a v not c
35+ solver. add_disjunction (0 , false , 1 , false ); // a v b
36+ solver. add_disjunction (3 , false , 2 , true ); // d v not c
37+ solver. add_disjunction (3 , false , 0 , true ); // d v not a
38+ assert (solver. solve_2SAT () == true );
39+ // two solutions
40+ auto expected_1 = vector<bool >{{true , true , false , true }};
41+ auto expected_2 = vector<bool >{{true , false , false , true }};
42+ assert (solver. assignment == expected_1 || solver. assignment == expected_2);
4743}
4844
4945int main () {
50- test_2sat_article_example ();
51- test_2sat_unsatisfiable ();
52- test_2sat_other_satisfiable_example ();
46+ test_2sat_example_usage ();
47+ test_2sat_article_example ();
48+ test_2sat_unsatisfiable ();
49+ test_2sat_other_satisfiable_example ();
5350}
0 commit comments