gym_rs/spaces/
discrete.rs

1use serde::Serialize;
2
3use super::Space;
4
5/// Defines a set of discrete integers starting at 0.
6///
7/// The value held by this structure defines the largest inclusive value that
8/// exists within the derived set.  
9///
10/// TODO: Update to support negative values.
11#[derive(Debug, Serialize, PartialEq, PartialOrd, Eq, Ord, Clone)]
12pub struct Discrete(pub usize);
13
14impl Space<usize> for Discrete {
15    fn contains(&self, value: usize) -> bool {
16        match *self {
17            Discrete(upper_bound) => value < upper_bound,
18        }
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::Discrete;
25    use crate::spaces::Space;
26
27    #[test]
28    fn given_value_greater_or_eq_than_upper_bound_when_contains_called_then_returns_false() {
29        let obj = Discrete(3);
30
31        assert!(!obj.contains(3));
32        assert!(!obj.contains(4));
33    }
34
35    #[test]
36    fn given_value_less_than_upper_bound_when_contains_then_returns_true() {
37        let obj = Discrete(3);
38
39        assert!(obj.contains(1));
40        assert!(obj.contains(2));
41    }
42}