strftime/format/
assert.rs

1#![expect(clippy::indexing_slicing, reason = "const index is always in bounds")]
2
3//! Compile-time assert functions.
4
5/// Helper macro for implementing asserts.
6macro_rules! assert_sorted_by_key {
7    ($s:expr, $f:expr) => {{
8        let mut i = 0;
9        while i + 1 < $s.len() {
10            assert!(
11                *$f(&$s[i]) < *$f(&$s[i + 1]),
12                concat!("table is not sorted by ", stringify!($f))
13            );
14            i += 1;
15        }
16        $s
17    }};
18}
19
20/// Returns the first element of a tuple.
21const fn elem_0<T>(x: &(u8, T)) -> &u8 {
22    &x.0
23}
24
25/// Asserts that a slice is sorted and has no duplicates.
26pub(crate) const fn assert_sorted(s: &[u8]) -> &[u8] {
27    assert_sorted_by_key!(s, core::convert::identity)
28}
29
30/// Asserts that a slice is sorted by its first element and has no duplicates.
31pub(crate) const fn assert_sorted_elem_0<T>(s: &[(u8, T)]) -> &[(u8, T)] {
32    assert_sorted_by_key!(s, elem_0)
33}
34
35/// Asserts that converting the first input to uppercase yields the second input.
36#[allow(dead_code)]
37pub(crate) const fn assert_to_ascii_uppercase(table: &[&str], upper_table: &[&str]) {
38    assert!(
39        table.len() == upper_table.len(),
40        "tables must have the same length"
41    );
42
43    let mut index = 0;
44    while index < table.len() {
45        let (s, upper_s) = (table[index].as_bytes(), upper_table[index].as_bytes());
46        assert!(s.len() == upper_s.len(), "keys must have the same length");
47
48        let mut i = 0;
49        while i < s.len() {
50            assert!(s[i].is_ascii(), "table key must be ascii");
51            assert!(upper_s[i].is_ascii(), "upper key must be ascii");
52            assert!(
53                upper_s[i] == s[i].to_ascii_uppercase(),
54                "upper key is not the uppercase of key"
55            );
56            i += 1;
57        }
58
59        index += 1;
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_assert_sorted() {
69        assert_sorted(&[1, 2, 3]);
70    }
71
72    #[test]
73    #[should_panic = "table is not sorted by core::convert::identity"]
74    fn test_assert_sorted_invalid() {
75        assert_sorted(&[1, 3, 2]);
76    }
77
78    #[test]
79    fn test_assert_sorted_elem_0() {
80        assert_sorted_elem_0(&[(1, 3), (2, 2), (3, 1)]);
81    }
82
83    #[test]
84    #[should_panic = "table is not sorted by elem_0"]
85    fn test_assert_sorted_elem_0_invalid() {
86        assert_sorted_elem_0(&[(1, 3), (3, 2), (2, 1)]);
87    }
88
89    #[test]
90    fn test_assert_to_ascii_uppercase() {
91        assert_to_ascii_uppercase(&["aaa"], &["AAA"]);
92    }
93
94    #[test]
95    #[should_panic = "tables must have the same length"]
96    fn assert_to_ascii_uppercase_requires_equal_length_tables() {
97        assert_to_ascii_uppercase(&["aaa", "bbb"], &["AAA"]);
98    }
99
100    #[test]
101    #[should_panic = "upper key is not the uppercase of key"]
102    fn test_assert_to_ascii_uppercase_invalid() {
103        assert_to_ascii_uppercase(&["aaa"], &["AaA"]);
104    }
105
106    #[test]
107    #[should_panic = "key must be ascii"]
108    fn test_assert_to_ascii_uppercase_invalid_ascii() {
109        assert_to_ascii_uppercase(&["€"], &["€"]);
110    }
111
112    #[test]
113    #[should_panic = "table key must be ascii"]
114    fn test_assert_to_ascii_uppercase_invalid_ascii_left() {
115        assert_to_ascii_uppercase(&["€"], &["$$$"]);
116    }
117
118    #[test]
119    #[should_panic = "upper key must be ascii"]
120    fn test_assert_to_ascii_uppercase_invalid_ascii_right() {
121        assert_to_ascii_uppercase(&["$$$"], &["€"]);
122    }
123}