1use core::fmt;
8
9use num_traits::{PrimInt, Signed};
10
11use super::BitInt;
12
13macro_rules! impl_fmt {
14 ($trait:ident) => {
15 impl<T: Signed + PrimInt + fmt::$trait, const N: u32> fmt::$trait for BitInt<T, N> {
16 #[inline]
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 self.get().fmt(f)
19 }
20 }
21 };
22}
23impl_fmt!(Display);
24impl_fmt!(Octal);
25impl_fmt!(LowerHex);
26impl_fmt!(UpperHex);
27impl_fmt!(Binary);
28impl_fmt!(LowerExp);
29impl_fmt!(UpperExp);
30
31#[cfg(test)]
32mod tests {
33 use super::super::BitI8;
34
35 #[test]
36 fn debug() {
37 assert_eq!(format!("{:?}", BitI8::<7>::MIN), "BitInt(-64)");
38 assert_eq!(format!("{:?}", BitI8::<7>::MAX), "BitInt(63)");
39 }
40
41 #[test]
42 fn display() {
43 assert_eq!(format!("{}", BitI8::<7>::MIN), "-64");
44 assert_eq!(format!("{}", BitI8::<7>::MAX), "63");
45 }
46
47 #[test]
48 fn octal() {
49 assert_eq!(format!("{:o}", BitI8::<7>::MIN), "300");
50 assert_eq!(format!("{:#o}", BitI8::<7>::MAX), "0o77");
51 }
52
53 #[test]
54 fn lower_hex() {
55 assert_eq!(format!("{:x}", BitI8::<7>::MIN), "c0");
56 assert_eq!(format!("{:#x}", BitI8::<7>::MAX), "0x3f");
57 }
58
59 #[test]
60 fn upper_hex() {
61 assert_eq!(format!("{:X}", BitI8::<7>::MIN), "C0");
62 assert_eq!(format!("{:#X}", BitI8::<7>::MAX), "0x3F");
63 }
64
65 #[test]
66 fn binary() {
67 assert_eq!(format!("{:b}", BitI8::<7>::MIN), "11000000");
68 assert_eq!(format!("{:#b}", BitI8::<7>::MAX), "0b111111");
69 }
70
71 #[test]
72 fn lower_exp() {
73 assert_eq!(format!("{:e}", BitI8::<7>::MIN), "-6.4e1");
74 assert_eq!(format!("{:e}", BitI8::<7>::MAX), "6.3e1");
75 }
76
77 #[test]
78 fn upper_exp() {
79 assert_eq!(format!("{:E}", BitI8::<7>::MIN), "-6.4E1");
80 assert_eq!(format!("{:E}", BitI8::<7>::MAX), "6.3E1");
81 }
82}