validator_rs/
numeric.rs

1//! Numeric validation functions
2
3/// Validates if a number is within a specified range (inclusive)
4///
5/// # Examples
6///
7/// ```
8/// use validator_rs::numeric::is_in_range;
9///
10/// assert!(is_in_range(5, 1, 10));
11/// assert!(!is_in_range(15, 1, 10));
12/// ```
13pub fn is_in_range<T: PartialOrd>(value: T, min: T, max: T) -> bool {
14    value >= min && value <= max
15}
16
17/// Validates if a number is positive
18///
19/// # Examples
20///
21/// ```
22/// use validator_rs::numeric::is_positive;
23///
24/// assert!(is_positive(5));
25/// assert!(!is_positive(-5));
26/// ```
27pub fn is_positive<T: PartialOrd + Default>(value: T) -> bool {
28    value > T::default()
29}
30
31/// Validates if a number is negative
32///
33/// # Examples
34///
35/// ```
36/// use validator_rs::numeric::is_negative;
37///
38/// assert!(is_negative(-5));
39/// assert!(!is_negative(5));
40/// ```
41pub fn is_negative<T: PartialOrd + Default>(value: T) -> bool {
42    value < T::default()
43}
44
45/// Validates if a number is zero
46pub fn is_zero<T: PartialEq + Default>(value: T) -> bool {
47    value == T::default()
48}
49
50/// Validates if a number is even
51pub fn is_even(value: i64) -> bool {
52    value % 2 == 0
53}
54
55/// Validates if a number is odd
56pub fn is_odd(value: i64) -> bool {
57    value % 2 != 0
58}
59
60/// Validates if a number is a multiple of another number
61pub fn is_multiple_of(value: i64, divisor: i64) -> bool {
62    if divisor == 0 {
63        return false;
64    }
65    value % divisor == 0
66}
67
68/// Validates if a floating point number is within a tolerance of a target
69pub 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