1pub fn is_in_range<T: PartialOrd>(value: T, min: T, max: T) -> bool {
14 value >= min && value <= max
15}
16
17pub fn is_positive<T: PartialOrd + Default>(value: T) -> bool {
28 value > T::default()
29}
30
31pub fn is_negative<T: PartialOrd + Default>(value: T) -> bool {
42 value < T::default()
43}
44
45pub fn is_zero<T: PartialEq + Default>(value: T) -> bool {
47 value == T::default()
48}
49
50pub fn is_even(value: i64) -> bool {
52 value % 2 == 0
53}
54
55pub fn is_odd(value: i64) -> bool {
57 value % 2 != 0
58}
59
60pub fn is_multiple_of(value: i64, divisor: i64) -> bool {
62 if divisor == 0 {
63 return false;
64 }
65 value % divisor == 0
66}
67
68pub fn is_close_to(value: f64, target: f64, tolerance: f64) -> bool {
70 (value - target).abs() <= tolerance
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_range() {
79 assert!(is_in_range(5, 1, 10));
80 assert!(is_in_range(1, 1, 10));
81 assert!(is_in_range(10, 1, 10));
82 assert!(!is_in_range(0, 1, 10));
83 assert!(!is_in_range(11, 1, 10));
84 }
85
86 #[test]
87 fn test_positive_negative() {
88 assert!(is_positive(5));
89 assert!(is_positive(0.1));
90 assert!(!is_positive(0));
91 assert!(!is_positive(-5));
92
93 assert!(is_negative(-5));
94 assert!(is_negative(-0.1));
95 assert!(!is_negative(0));
96 assert!(!is_negative(5));
97 }
98
99 #[test]
100 fn test_zero() {
101 assert!(is_zero(0));
102 assert!(is_zero(0.0));
103 assert!(!is_zero(1));
104 assert!(!is_zero(-1));
105 }
106
107 #[test]
108 fn test_even_odd() {
109 assert!(is_even(2));
110 assert!(is_even(0));
111 assert!(is_even(-4));
112 assert!(!is_even(3));
113
114 assert!(is_odd(3));
115 assert!(is_odd(-5));
116 assert!(!is_odd(2));
117 assert!(!is_odd(0));
118 }
119
120 #[test]
121 fn test_multiple_of() {
122 assert!(is_multiple_of(10, 5));
123 assert!(is_multiple_of(15, 3));
124 assert!(is_multiple_of(0, 5));
125 assert!(!is_multiple_of(10, 3));
126 assert!(!is_multiple_of(10, 0));
127 }
128
129 #[test]
130 fn test_close_to() {
131 assert!(is_close_to(1.0, 1.01, 0.02));
132 assert!(is_close_to(1.0, 1.0, 0.0));
133 assert!(!is_close_to(1.0, 1.1, 0.05));
134 }
135}
136