ctb_formats_eite/util/
math.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Numeric predicates for i32
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29pub fn is_nonnegative_i32(n: i32) -> bool {
30    n >= 0
31}
32pub fn is_negative_i32(n: i32) -> bool {
33    n < 0
34}
35pub fn is_positive_i32(n: i32) -> bool {
36    n > 0
37}
38pub fn is_nonpositive_i32(n: i32) -> bool {
39    n <= 0
40}
41pub fn is_even_i32(n: i32) -> bool {
42    n % 2 == 0
43}
44pub fn is_odd_i32(n: i32) -> bool {
45    n % 2 != 0
46}
47
48pub fn int_is_between_i32(n: i32, a: i32, b: i32) -> bool {
49    n >= a && n <= b
50}
51
52// Numeric predicates for u32
53pub fn is_nonnegative_u32(_n: u32) -> bool {
54    true
55} // Always true for u32
56pub fn is_negative_u32(_n: u32) -> bool {
57    false
58} // Always false for u32
59pub fn is_positive_u32(n: u32) -> bool {
60    n > 0
61}
62pub fn is_nonpositive_u32(n: u32) -> bool {
63    n == 0
64}
65pub fn is_even_u32(n: u32) -> bool {
66    n.is_multiple_of(2)
67}
68pub fn is_odd_u32(n: u32) -> bool {
69    !n.is_multiple_of(2)
70}
71
72pub fn int_is_between_u32(n: u32, a: u32, b: u32) -> bool {
73    n >= a && n <= b
74}
75
76// ---------------
77// Inequality wrappers
78// ---------------
79
80pub fn ge_int(a: i32, b: i32) -> bool {
81    a >= b
82}
83pub fn le_int(a: i32, b: i32) -> bool {
84    a <= b
85}
86pub fn ngt_int(a: i32, b: i32) -> bool {
87    a <= b
88}
89pub fn nlt_int(a: i32, b: i32) -> bool {
90    a >= b
91}
92pub fn nge_int(a: i32, b: i32) -> bool {
93    a < b
94}
95pub fn nle_int(a: i32, b: i32) -> bool {
96    a > b
97}
98
99#[cfg(test)]
100#[allow(
101    clippy::panic,
102    clippy::expect_used,
103    clippy::unwrap_used,
104    clippy::unwrap_in_result,
105    clippy::panic_in_result_fn,
106    clippy::indexing_slicing,
107    clippy::arithmetic_side_effects,
108    reason = "Standard repository test boilerplate"
109)]
110mod tests {
111    use super::*;
112
113    #[crate::ctb_test]
114    fn test_number_predicates() {
115        assert!(is_nonnegative_u32(0));
116        assert!(!is_negative_u32(0));
117        assert!(is_positive_u32(5));
118        assert!(is_nonpositive_u32(0));
119        assert!(is_even_u32(4));
120        assert!(is_odd_u32(5));
121        assert!(int_is_between_u32(5, 1, 10));
122        assert!(!int_is_between_u32(0, 1, 10));
123
124        assert!(is_nonnegative_i32(0));
125        assert!(is_negative_i32(-1));
126        assert!(!is_negative_i32(0));
127        assert!(is_positive_i32(5));
128        assert!(is_nonpositive_i32(0));
129        assert!(is_even_i32(4));
130        assert!(is_odd_i32(5));
131        assert!(int_is_between_i32(5, 1, 10));
132        assert!(!int_is_between_i32(0, 1, 10));
133    }
134}