Skip to main content

round_multiple/
lib.rs

1#[macro_export]
2macro_rules! round_multiple_checked {
3    ($x:expr, $m:expr) => {{
4        let m = $m;
5        assert!(m > 0, "m must be positive");
6        (($x + m - 1) / m) * m
7    }};
8}
9
10#[cfg(test)]
11mod tests {
12    use super::*;
13
14    #[test]
15    fn round_multiple_checked_works() {
16        assert_eq!(round_multiple_checked!(7, 4), 8); // 7 → 8 (4×2)
17        assert_eq!(round_multiple_checked!(10, 5), 10); // 10 → 10 (5×2)
18        assert_eq!(round_multiple_checked!(11, 3), 12); // 11 → 12 (3×4)
19    }
20
21    #[test]
22    #[should_panic(expected = "m must be positive")]
23    fn round_multiple_checked_panic_with_zero_works() {
24        round_multiple_checked!(10, 0);
25    }
26}