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
extern crate num;
use super::single::{and_perceptron, nand_perceptron, or_perceptron};
pub fn xor_perceptron() -> Box<dyn Fn(bool, bool) -> bool> {
Box::new(|x1: bool, x2: bool| -> bool {
and_perceptron()(nand_perceptron()(x1, x2), or_perceptron()(x1, x2))
})
}
#[cfg(test)]
mod tests {
use super::*;
fn test_logical_gates_impl(
logical_gate: &Box<dyn Fn(bool, bool) -> bool>,
expected_answer: &[bool; 4],
) {
let mut i: usize = 0;
for b1 in [true, false].iter() {
for b2 in [true, false].iter() {
assert_eq!(logical_gate(*b1, *b2), expected_answer[i]);
i += 1;
}
}
}
#[test]
fn test_logical_gates() {
test_logical_gates_impl(&xor_perceptron(), &[false, true, true, false]);
}
}