ethnum/int/
fmt.rs

1//! Module implementing formatting for `I256` type.
2
3use crate::int::I256;
4
5impl_fmt! {
6    impl Fmt for I256;
7}
8
9#[cfg(test)]
10mod tests {
11    use super::*;
12    use alloc::format;
13
14    #[test]
15    fn from_str() {
16        assert_eq!("42".parse::<I256>().unwrap(), 42);
17    }
18
19    #[test]
20    fn debug() {
21        assert_eq!(
22            format!("{:?}", I256::MAX),
23            "57896044618658097711785492504343953926634992332820282019728792003956564819967",
24        );
25        assert_eq!(
26            format!("{:x?}", I256::MIN),
27            "8000000000000000000000000000000000000000000000000000000000000000",
28        );
29        assert_eq!(
30            format!("{:#X?}", I256::MAX),
31            "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
32        );
33    }
34
35    #[test]
36    fn display() {
37        assert_eq!(
38            format!("{}", I256::MIN),
39            "-57896044618658097711785492504343953926634992332820282019728792003956564819968",
40        );
41        assert_eq!(
42            format!(
43                "{}",
44                I256::from_words(0, -1329227995784915854529085396220968961)
45            ),
46            "338953138925153547608845522035547242495",
47        );
48    }
49
50    #[test]
51    fn radix() {
52        assert_eq!(format!("{:b}", I256::new(42)), "101010");
53        assert_eq!(format!("{:o}", I256::new(42)), "52");
54        assert_eq!(format!("{:x}", I256::new(42)), "2a");
55
56        // Note that there is no '-' sign for binary, octal or hex formatting!
57        // This is the same behaviour for the standard iN types.
58        assert_eq!(format!("{:b}", I256::MINUS_ONE), "1".repeat(256));
59        assert_eq!(format!("{:o}", I256::MINUS_ONE), format!("{:7<86}", "1"));
60        assert_eq!(format!("{:x}", I256::MINUS_ONE), "f".repeat(64));
61    }
62
63    #[test]
64    fn exp() {
65        assert_eq!(format!("{:e}", I256::new(42)), "4.2e1");
66        assert_eq!(format!("{:e}", I256::new(10).pow(76)), "1e76");
67        assert_eq!(format!("{:E}", -I256::new(10).pow(39) * 1337), "-1.337E42");
68    }
69}