-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.rs
98 lines (84 loc) · 2.12 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
pub fn and(a: bool, b: bool) -> bool {
a && b
}
pub fn or(a: bool, b: bool) -> bool {
a || b
}
pub fn not(a: bool) -> bool {
!a
}
pub fn nand(a: bool, b: bool) -> bool {
!(a && b)
}
pub fn nor(a: bool, b: bool) -> bool {
!(a || b)
}
pub fn xor(a: bool, b: bool) -> bool {
(a || b) && (!a || !b)
}
pub fn impli(a: bool, b: bool) -> bool {
(a && b) || !a
}
pub fn equ(a: bool, b: bool) -> bool {
impli(a, b) && impli(b, a)
}
pub fn table2(f: fn(bool, bool) -> bool) -> Vec<(bool, bool, bool)> {
vec![
(true, true, f(true, true)),
(true, false, f(true, false)),
(false, true, f(false, true)),
(false, false, f(false, false)),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nand() {
assert_eq!(nand(true, true), false);
assert_eq!(nand(true, false), true);
assert_eq!(nand(false, true), true);
assert_eq!(nand(false, false), true);
}
#[test]
fn test_nor() {
assert_eq!(nor(true, true), false);
assert_eq!(nor(true, false), false);
assert_eq!(nor(false, true), false);
assert_eq!(nor(false, false), true);
}
#[test]
fn test_xor() {
assert_eq!(xor(true, true), false);
assert_eq!(xor(true, false), true);
assert_eq!(xor(false, true), true);
assert_eq!(xor(false, false), false);
}
#[test]
fn test_impli() {
assert_eq!(impli(true, true), true);
assert_eq!(impli(true, false), false);
assert_eq!(impli(false, true), true);
assert_eq!(impli(false, false), true);
}
#[test]
fn test_equ() {
assert_eq!(equ(true, true), true);
assert_eq!(equ(true, false), false);
assert_eq!(equ(false, true), false);
assert_eq!(equ(false, false), true);
}
#[test]
fn test_table2() {
let f = |a, b| and(a, or(a, b));
assert_eq!(
table2(f),
vec![
(true, true, true),
(true, false, true),
(false, true, false),
(false, false, false)
]
);
}
}