1use crate::types::UInteger;
22
23#[repr(C, packed)]
27#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
28pub struct Range {
29 pub location: UInteger,
31 pub length: UInteger,
33}
34
35impl Range {
36 #[inline]
40 pub const fn new(location: UInteger, length: UInteger) -> Self {
41 Self { location, length }
42 }
43
44 #[inline]
48 pub const fn make(location: UInteger, length: UInteger) -> Self {
49 Self::new(location, length)
50 }
51
52 #[inline]
56 pub const fn equal(&self, other: &Range) -> bool {
57 self.location == other.location && self.length == other.length
58 }
59
60 #[inline]
64 pub const fn location_in_range(&self, loc: UInteger) -> bool {
65 loc >= self.location && (loc - self.location) < self.length
66 }
67
68 #[inline]
72 pub const fn max(&self) -> UInteger {
73 self.location + self.length
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_range_new() {
83 let range = Range::new(10, 5);
84 let location = { range.location };
85 let length = { range.length };
86 assert_eq!(location, 10);
87 assert_eq!(length, 5);
88 }
89
90 #[test]
91 fn test_range_equal() {
92 let r1 = Range::new(10, 5);
93 let r2 = Range::new(10, 5);
94 let r3 = Range::new(10, 6);
95
96 assert!(r1.equal(&r2));
97 assert!(!r1.equal(&r3));
98 }
99
100 #[test]
101 fn test_location_in_range() {
102 let range = Range::new(10, 5);
103
104 assert!(!range.location_in_range(9));
105 assert!(range.location_in_range(10));
106 assert!(range.location_in_range(14));
107 assert!(!range.location_in_range(15));
108 }
109
110 #[test]
111 fn test_range_max() {
112 let range = Range::new(10, 5);
113 assert_eq!(range.max(), 15);
114 }
115}