Skip to content

Commit 3bba0ca

Browse files
committed
Add tests
1 parent dbacf02 commit 3bba0ca

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/twosat.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,75 @@ impl TwoSAT {
3434
&self.answer
3535
}
3636
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
#[test]
42+
fn solve_alpc_h_sample1() {
43+
// https://atcoder.jp/contests/practice2/tasks/practice2_h
44+
45+
let (n, d) = (3, 2);
46+
let x = [1, 2, 0i32];
47+
let y = [4, 5, 6];
48+
49+
let mut t = TwoSAT::new(n);
50+
51+
for i in 0..n {
52+
for j in i + 1..n {
53+
if (x[i] - x[j]).abs() < d {
54+
t.add_clause(i, false, j, false);
55+
}
56+
if (x[i] - y[j]).abs() < d {
57+
t.add_clause(i, false, j, true);
58+
}
59+
if (y[i] - x[j]).abs() < d {
60+
t.add_clause(i, true, j, false);
61+
}
62+
if (y[i] - y[j]).abs() < d {
63+
t.add_clause(i, true, j, true);
64+
}
65+
}
66+
}
67+
assert!(t.satisfiable());
68+
let answer = t.answer();
69+
let mut res = vec![];
70+
for (i, &v) in answer.iter().enumerate() {
71+
if v {
72+
res.push(x[i])
73+
} else {
74+
res.push(y[i]);
75+
}
76+
}
77+
assert_eq!(res, [4, 2, 0]);
78+
}
79+
80+
#[test]
81+
fn solve_alpc_h_sample2() {
82+
// https://atcoder.jp/contests/practice2/tasks/practice2_h
83+
84+
let (n, d) = (3, 3);
85+
let x = [1, 2, 0i32];
86+
let y = [4, 5, 6];
87+
88+
let mut t = TwoSAT::new(n);
89+
90+
for i in 0..n {
91+
for j in i + 1..n {
92+
if (x[i] - x[j]).abs() < d {
93+
t.add_clause(i, false, j, false);
94+
}
95+
if (x[i] - y[j]).abs() < d {
96+
t.add_clause(i, false, j, true);
97+
}
98+
if (y[i] - x[j]).abs() < d {
99+
t.add_clause(i, true, j, false);
100+
}
101+
if (y[i] - y[j]).abs() < d {
102+
t.add_clause(i, true, j, true);
103+
}
104+
}
105+
}
106+
assert!(!t.satisfiable());
107+
}
108+
}

0 commit comments

Comments
 (0)