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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]

use std::fmt;

/// Bint: A bounded integer.
///
/// Returns a struct that represents an unsigned integer and a boundary that represents when
//  the value will be reset to 0.
pub struct Bint {
    pub value: u8,
    pub boundary: u8,
}

impl Bint {
    pub fn new(boundary: u8) -> Bint {
        Bint {
            value: 0,
            boundary: boundary,
        }
    }

    pub fn up(&self) -> Bint {
        let v = (self.value + 1) % self.boundary;
        Bint {
            value: v,
            boundary: self.boundary,
        }
    }

    pub fn down(&self) -> Bint {
        if self.value == 0 {
            return Bint {
                value: self.boundary - 1,
                boundary: self.boundary,
            };
        }
        let v = (self.value - 1) % self.boundary;
        Bint {
            value: v,
            boundary: self.boundary,
        }
    }
}

impl fmt::Display for Bint {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

///
/// # Usage:
/// ```
/// let b: Bint = Bint {value: 5, boundary: 6 };
/// let c: Bint = b.up();
/// let d: Bint = c.up();
/// println!("{} {} {}", b, c, d); // Prints 5 0 1
///
/// assert_eq!(1, d.value);
/// ```

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn new() {
        let b = Bint::new(6);
        assert_eq!(0, b.value);
        assert_eq!(6, b.boundary);
    }

    #[test]
    fn format() {
        let b: Bint = Bint {
            value: 4,
            boundary: 6,
        };
        assert_eq!("4", format!("{}", b));
    }

    #[test]
    fn init() {
        let b = Bint {
            value: 7,
            boundary: 10,
        };
        assert_eq!(7, b.value);
        assert_eq!(10, b.boundary);
    }

    #[test]
    fn up() {
        let b: Bint = Bint {
            value: 4,
            boundary: 6,
        };
        let b: Bint = b.up();
        assert_eq!(5, b.value);

        let b: Bint = b.up();
        assert_eq!(0, b.value);
    }

    #[test]
    fn down() {
        let b: Bint = Bint {
            value: 1,
            boundary: 6,
        };
        let b: Bint = b.down();
        assert_eq!(0, b.value);

        let b: Bint = b.down();
        assert_eq!(5, b.value);
    }

    #[test]
    fn up_bint_outside() {
        let b: Bint = Bint {
            value: 50,
            boundary: 10,
        };
        let b: Bint = b.up();
        assert_eq!(1, b.value);

        let b: Bint = b.down();
        let b: Bint = b.down();
        assert_eq!(9, b.value);
    }
}