spaces/discrete/
naturals.rs

1use crate::prelude::*;
2use std::fmt;
3
4/// Type representing the set of unsigned 64-bit integers.
5#[derive(Debug, Clone, Copy)]
6#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
7pub struct Naturals;
8
9impl Space for Naturals {
10    const DIM: usize = 1;
11
12    type Value = u64;
13
14    fn card(&self) -> Card { Card::Infinite }
15
16    fn contains(&self, _: &u64) -> bool { true }
17}
18
19impl OrderedSpace for Naturals {
20    fn min(&self) -> Option<u64> { Some(0) }
21
22    fn max(&self) -> Option<u64> { None }
23}
24
25impl_union_intersect!(Naturals, Naturals);
26
27impl fmt::Display for Naturals {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        write!(f, "\u{2124}(≥0)")
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[cfg(feature = "serialize")]
38    extern crate serde_test;
39    #[cfg(feature = "serialize")]
40    use self::serde_test::{assert_tokens, Token};
41
42    #[test]
43    fn test_dim() {
44        assert_eq!(Naturals::DIM, 1);
45    }
46
47    #[test]
48    fn test_card() {
49        let d = Naturals;
50
51        assert_eq!(d.card(), Card::Infinite);
52    }
53
54    #[test]
55    fn test_bounds() {
56        let d = Naturals;
57
58        assert_eq!(d.inf().unwrap(), 0);
59        assert!(d.sup().is_none());
60
61        assert!(d.contains(&0));
62        assert!(d.contains(&1));
63    }
64
65    #[cfg(feature = "serialize")]
66    #[test]
67    fn test_serialisation() {
68        let d = Naturals;
69
70        assert_tokens(&d, &[Token::UnitStruct { name: "Naturals" }]);
71    }
72}