spaces/discrete/
binary.rs1use crate::prelude::*;
2use std::{fmt, ops::Range};
3
4#[derive(Debug, PartialEq, Eq, Clone, Copy)]
6#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
7pub struct Binary;
8
9impl Space for Binary {
10 const DIM: usize = 1;
11
12 type Value = bool;
13
14 fn card(&self) -> Card { Card::Finite(2) }
15
16 fn contains(&self, _: &bool) -> bool { true }
17}
18
19impl OrderedSpace for Binary {
20 fn min(&self) -> Option<bool> { Some(false) }
21
22 fn max(&self) -> Option<bool> { Some(true) }
23}
24
25impl FiniteSpace for Binary {
26 fn to_ordinal(&self) -> Range<usize> { 0..1 }
27}
28
29impl_union_intersect!(Binary, Binary);
30
31impl fmt::Display for Binary {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 write!(f, "{{0, 1}}")
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[cfg(feature = "serialize")]
42 extern crate serde_test;
43 #[cfg(feature = "serialize")]
44 use self::serde_test::{assert_tokens, Token};
45
46 #[test]
47 fn test_dim() {
48 assert_eq!(Binary::DIM, 1);
49 }
50
51 #[test]
52 fn test_card() {
53 let d = Binary;
54
55 assert_eq!(d.card(), Card::Finite(2));
56 }
57
58 #[test]
59 fn test_bounds() {
60 let d = Binary;
61
62 assert_eq!(d.inf().unwrap(), false);
63 assert_eq!(d.sup().unwrap(), true);
64
65 assert!(d.contains(&false));
66 assert!(d.contains(&true));
67 }
68
69 #[test]
70 fn test_to_ordinal() {
71 assert_eq!(Binary.to_ordinal(), 0..1);
72 }
73
74 #[cfg(feature = "serialize")]
75 #[test]
76 fn test_serialisation() {
77 let d = Binary;
78
79 assert_tokens(&d, &[Token::UnitStruct { name: "Binary" }]);
80 }
81}