-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.rs
74 lines (71 loc) · 2.08 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
pub fn table(argsize: usize, expr: fn(&[bool]) -> bool) -> Vec<Vec<bool>> {
var_list(argsize)
.into_iter()
.map(|vars| {
let mut res = vars.clone();
res.push(expr(&vars));
res
})
.collect()
}
// generate bool matrix for given size
fn var_list(argsize: usize) -> Vec<Vec<bool>> {
fn var_list_rec(k: usize, acc: &Vec<bool>) -> Vec<Vec<bool>> {
if k == 0 {
vec![acc.clone()]
} else {
let mut res = vec![];
let mut acc2_true = acc.clone();
acc2_true.push(true);
res.extend(var_list_rec(k - 1, &acc2_true));
let mut acc2_false = acc.clone();
acc2_false.push(false);
res.extend(var_list_rec(k - 1, &acc2_false));
res
}
}
var_list_rec(argsize, &vec![])
}
#[cfg(test)]
mod tests {
use super::*;
use P47::*;
#[test]
fn test_var_list() {
assert_eq!(
var_list(3),
vec![
vec![true, true, true],
vec![true, true, false],
vec![true, false, true],
vec![true, false, false],
vec![false, true, true],
vec![false, true, false],
vec![false, false, true],
vec![false, false, false]
]
);
}
#[test]
fn test_table() {
let f = |args: &[bool]| -> bool {
let a = args.get(0).unwrap();
let b = args.get(1).unwrap();
let c = args.get(2).unwrap();
a.and(&b.or(c)).equ(&a.and(&b).or(&a.and(&c)))
};
assert_eq!(
table(3, f),
vec![
vec![true, true, true, true],
vec![true, true, false, true],
vec![true, false, true, true],
vec![true, false, false, true],
vec![false, true, true, true],
vec![false, true, false, true],
vec![false, false, true, true],
vec![false, false, false, true]
]
);
}
}