Skip to content

Commit

Permalink
Roundtrip tests for wcnf and gcnf
Browse files Browse the repository at this point in the history
  • Loading branch information
jix committed Dec 26, 2021
1 parent 19be237 commit d0b50c6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
31 changes: 16 additions & 15 deletions flussab-cnf/src/cnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,22 @@ mod tests {

#[test]
fn roundtrip() -> Result<()> {
let input = concat!(
"p cnf 5 12\n",
"-1 -2 0\n",
"-1 -3 0\n",
"-1 -4 0\n",
"-1 -5 0\n",
"-2 -3 0\n",
"-2 -4 0\n",
"-2 -5 0\n",
"-3 -4 0\n",
"-3 -5 0\n",
"-4 -5 0\n",
"2 5 3 4 1 0\n",
"4 2 3 1 5 0\n"
);
let input = &r"
p cnf 5 12
-1 -2 0
-1 -3 0
-1 -4 0
-1 -5 0
-2 -3 0
-2 -4 0
-2 -5 0
-3 -4 0
-3 -5 0
-4 -5 0
2 5 3 4 1 0
4 2 3 1 5 0
"[1..];

let mut output = vec![];
let mut parser = Parser::<i32>::from_read(input.as_bytes(), true)?;

Expand Down
45 changes: 45 additions & 0 deletions flussab-cnf/src/gcnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,48 @@ pub fn write_clause<L: Dimacs>(writer: &mut DeferredWriter, group: usize, clause
}
writer.write_all_defer_err(b"0\n");
}

#[cfg(test)]
mod tests {
use super::*;

type Result<T> = std::result::Result<T, ParseError>;

#[test]
fn roundtrip() -> Result<()> {
let input = &r"
p gcnf 5 12 3
{0} -1 -2 0
{0} -1 -3 0
{0} -1 -4 0
{0} -1 -5 0
{0} -2 -3 0
{0} -2 -4 0
{0} -2 -5 0
{1} -3 -4 0
{1} -3 -5 0
{2} -4 -5 0
{3} 2 5 3 4 1 0
{3} 4 2 3 1 5 0
"[1..];

let mut output = vec![];
let mut parser = Parser::<i32>::from_read(input.as_bytes(), true)?;

{
let mut writer = DeferredWriter::from_write(&mut output);

write_header(&mut writer, parser.header().unwrap());

while let Some((group, clause)) = parser.next_clause()? {
write_clause(&mut writer, group, clause);
}

writer.flush()?;
}

assert_eq!(input.as_bytes(), output);

Ok(())
}
}
45 changes: 45 additions & 0 deletions flussab-cnf/src/wcnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,48 @@ pub fn write_clause<L: Dimacs>(writer: &mut DeferredWriter, weight: u64, clause_
}
writer.write_all_defer_err(b" 0\n");
}

#[cfg(test)]
mod tests {
use super::*;

type Result<T> = std::result::Result<T, ParseError>;

#[test]
fn roundtrip() -> Result<()> {
let input = &r"
p wcnf 5 12 67
1 -1 -2 0
2 -1 -3 0
3 -1 -4 0
4 -1 -5 0
5 -2 -3 0
6 -2 -4 0
7 -2 -5 0
8 -3 -4 0
9 -3 -5 0
10 -4 -5 0
11 2 5 3 4 1 0
67 4 2 3 1 5 0
"[1..];

let mut output = vec![];
let mut parser = Parser::<i32>::from_read(input.as_bytes(), true)?;

{
let mut writer = DeferredWriter::from_write(&mut output);

write_header(&mut writer, parser.header().unwrap());

while let Some((weight, clause)) = parser.next_clause()? {
write_clause(&mut writer, weight, clause);
}

writer.flush()?;
}

assert_eq!(input.as_bytes(), output);

Ok(())
}
}

0 comments on commit d0b50c6

Please sign in to comment.