summary refs log tree commit diff
path: root/library/alloctests/tests/num.rs
blob: a169bbec8e0c9cadbf7df56bc9ca9fe87416a904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt::{Debug, Display};
use std::str::FromStr;

fn assert_nb<Int: ToString + FromStr + Debug + Display + Eq>(value: Int) {
    let s = value.to_string();
    let s2 = format!("s: {}.", value);

    assert_eq!(format!("s: {s}."), s2);
    let Ok(ret) = Int::from_str(&s) else {
        panic!("failed to convert into to string");
    };
    assert_eq!(ret, value);
}

macro_rules! uint_to_s {
    ($($fn_name:ident, $int:ident,)+) => {
        $(
            #[test]
            fn $fn_name() {
                assert_nb::<$int>($int::MIN);
                assert_nb::<$int>($int::MAX);
                assert_nb::<$int>(1);
                assert_nb::<$int>($int::MIN / 2);
                assert_nb::<$int>($int::MAX / 2);
            }
        )+
    }
}
macro_rules! int_to_s {
    ($($fn_name:ident, $int:ident,)+) => {
        $(
            #[test]
            fn $fn_name() {
                assert_nb::<$int>($int::MIN);
                assert_nb::<$int>($int::MAX);
                assert_nb::<$int>(1);
                assert_nb::<$int>(0);
                assert_nb::<$int>(-1);
                assert_nb::<$int>($int::MIN / 2);
                assert_nb::<$int>($int::MAX / 2);
            }
        )+
    }
}

int_to_s!(
    test_i8_to_string,
    i8,
    test_i16_to_string,
    i16,
    test_i32_to_string,
    i32,
    test_i64_to_string,
    i64,
    test_isize_to_string,
    isize,
    test_i128_to_string,
    i128,
);
uint_to_s!(
    test_u8_to_string,
    u8,
    test_u16_to_string,
    u16,
    test_u32_to_string,
    u32,
    test_u64_to_string,
    u64,
    test_usize_to_string,
    usize,
    test_u128_to_string,
    u128,
);