Browse Source

Added testing and docs for the clause generator

clause_generator
Cameron Weinfurt 3 years ago
parent
commit
64a356b717
  1. 43
      src/clause_gen.rs

43
src/clause_gen.rs

@ -1,7 +1,12 @@
use crate::clause::*;
/*
* Produces a unique variable name from a given usize. The variable name is
* always lower case and will extend to a longer name should the number be
* larger than 25. For reference, 0 maps to 'a' and 26 maps to 'aa'.
*/
fn get_var_name(n: usize) -> String {
let ch = std::char::from_u32((n as u32) % 26 + 0x60).unwrap().to_string();
let ch = std::char::from_u32((n as u32) % 26 + 0x61).unwrap().to_string();
if n >= 26 {
get_var_name(n / 26) + &ch
} else {
@ -9,7 +14,10 @@ fn get_var_name(n: usize) -> String {
}
}
fn gen_clause(var_count: usize) -> Clause {
/*
* Generates a clause with [var_count] literals that are randomly negated.
*/
pub fn gen_clause(var_count: usize) -> Clause {
let mut vars : Vec<Literal> = (0..var_count)
.map(get_var_name)
.map(Variable::from_name)
@ -23,12 +31,16 @@ fn gen_clause(var_count: usize) -> Clause {
.collect();
let mut cl = Clause::new();
for _ in 0..var_count {
let idx : usize = rand::random::<usize>() % var_count;
let idx : usize = rand::random::<usize>() % vars.len();
cl.push(vars.remove(idx));
}
cl
}
/*
* Generates a list of clauses that has [count] clauses that will have up to
* [max_vars] literals in them. No empty clauses are generated.
*/
pub fn gen_clause_list(count: usize, max_vars: usize) -> ClauseList {
let mut cls = ClauseList::new();
for _ in 0..count {
@ -36,3 +48,28 @@ pub fn gen_clause_list(count: usize, max_vars: usize) -> ClauseList {
}
cls
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_clause_gen() {
let test_var_short = get_var_name(1);
let test_var_long = get_var_name(27);
println!("{}, {}", test_var_short, test_var_long);
assert!(test_var_short == "b");
assert!(test_var_long == "bb");
let test_clause = gen_clause(10);
println!("{}", test_clause);
assert!(test_clause.len() == 10);
let test_clause_list = gen_clause_list(5, 4);
assert!(test_clause_list.len() == 5);
for cl in test_clause_list.iter() {
println!("{}", cl);
assert!(cl.len() <= 4 && cl.len() > 0);
}
}
}
Loading…
Cancel
Save