bnum/int/
fmt.rs

1#[cfg(test)]
2macro_rules! format_trait {
3    ($($method: ident), *) => {
4        // This trait allows us to use the default tester macro instead of creating a custom one
5        pub trait Format {
6            $(
7                fn $method(&self, width: Option<u8>, extra: bool) -> alloc::string::String;
8            )*
9        }
10    };
11}
12
13#[cfg(test)]
14format_trait!(binary, lower_hex, upper_hex, octal, display, debug, lower_exp, upper_exp);
15
16#[cfg(test)]
17macro_rules! impl_format_method {
18    { $($name: ident : $format: literal), * } => {
19        $(
20            fn $name(&self, width: Option<u8>, extra: bool) -> alloc::string::String {
21                if let Some(width) = width {
22                    if extra {
23                        format!(concat!("{:+#0width$", $format, "}"), self, width = width as usize)
24                    } else {
25                        format!(concat!("{:width$", $format, "}"), self, width = width as usize)
26                    }
27                } else if extra {
28                    format!(concat!("{:+#", $format, "}"), self)
29                } else {
30                    format!(concat!("{:", $format, "}"), self)
31                }
32            }
33        )*
34    };
35}
36
37#[cfg(test)]
38pub(crate) use impl_format_method;
39
40#[cfg(test)]
41macro_rules! impl_format {
42    ($($ty: ty), *) => {
43        $(
44            impl Format for $ty {
45                crate::int::fmt::impl_format_method! {
46                    binary: "b",
47                    lower_hex: "x",
48                    upper_hex: "X",
49                    octal: "o",
50                    display: "",
51                    debug: "?",
52                    lower_exp: "e",
53                    upper_exp: "E"
54                }
55            }
56        )*
57    };
58}
59
60#[cfg(test)]
61pub(crate) use impl_format;
62
63#[cfg(test)]
64macro_rules! test_formats {
65    ($ty: ty; $($name: ident), *) => {
66        $(
67            test_bignum! {
68                function: <$ty as Format>::$name(a: ref &$ty, width: Option<u8>, extra: bool)
69            }
70        )*
71    };
72}
73
74#[cfg(test)]
75pub(crate) use test_formats;
76
77#[cfg(test)]
78macro_rules! tests {
79    ($ty: ty) => {
80        use crate::int::fmt::{Format, self};
81        use crate::test::{test_bignum, types::*};
82
83        paste::paste! {
84            fmt::impl_format!([<$ty:upper>]);
85        }
86
87        fmt::test_formats!($ty; binary, lower_hex, upper_hex, octal, display, debug, lower_exp, upper_exp);
88    };
89}
90
91#[cfg(test)]
92pub(crate) use tests;
93
94#[cfg(test)]
95crate::int::fmt::impl_format!(u128, i128, u64, i64, u32, i32, u16, i16);