File: test_unsigned.rs

package info (click to toggle)
rust-num-format 0.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 628 kB
  • sloc: makefile: 2
file content (249 lines) | stat: -rw-r--r-- 7,734 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
mod common;

use num_format::{Buffer, CustomFormat};
#[cfg(feature = "std")]
use num_format::{ToFormattedString, WriteFormatted};

use crate::common::POLICIES;

#[test]
fn test_u8() {
    let test_cases: &[(&str, u8, &CustomFormat)] = &[
        ("0", 0, &POLICIES[0]),
        ("0", 0, &POLICIES[1]),
        ("0", 0, &POLICIES[2]),
        ("0", 0, &POLICIES[3]),
        ("0", 0, &POLICIES[4]),
        ("255", std::u8::MAX, &POLICIES[0]),
        ("255", std::u8::MAX, &POLICIES[1]),
        ("255", std::u8::MAX, &POLICIES[2]),
        ("255", std::u8::MAX, &POLICIES[3]),
        ("255", std::u8::MAX, &POLICIES[4]),
    ];

    for (expected, input, format) in test_cases {
        // Buffer
        let mut buf = Buffer::default();
        buf.write_formatted(input, *format);
        assert_eq!(*expected, buf.as_str());

        #[cfg(feature = "std")]
        {
            // ToFormattedString
            assert_eq!(expected.to_string(), input.to_formatted_string(*format));

            // WriteFormatted
            let mut s = String::new();
            s.write_formatted(input, *format).unwrap();
            assert_eq!(expected.to_string(), s);
        }
    }
}

#[test]
fn test_u16() {
    let test_cases: &[(&str, u16, &CustomFormat)] = &[
        ("0", 0, &POLICIES[0]),
        ("0", 0, &POLICIES[1]),
        ("0", 0, &POLICIES[2]),
        ("0", 0, &POLICIES[3]),
        ("0", 0, &POLICIES[4]),
        ("65,535", std::u16::MAX, &POLICIES[0]),
        ("65𠜱535", std::u16::MAX, &POLICIES[1]),
        ("65𠜱535", std::u16::MAX, &POLICIES[2]),
        ("65535", std::u16::MAX, &POLICIES[3]),
        ("65535", std::u16::MAX, &POLICIES[4]),
    ];

    for (expected, input, format) in test_cases {
        // Buffer
        let mut buf = Buffer::default();
        buf.write_formatted(input, *format);
        assert_eq!(*expected, buf.as_str());

        #[cfg(feature = "std")]
        {
            // ToFormattedString
            assert_eq!(expected.to_string(), input.to_formatted_string(*format));

            // WriteFormatted
            let mut s = String::new();
            s.write_formatted(input, *format).unwrap();
            assert_eq!(expected.to_string(), s);
        }
    }
}

#[test]
fn test_u32() {
    let test_cases: &[(&str, u32, &CustomFormat)] = &[
        ("0", 0, &POLICIES[0]),
        ("0", 0, &POLICIES[1]),
        ("0", 0, &POLICIES[2]),
        ("0", 0, &POLICIES[3]),
        ("0", 0, &POLICIES[4]),
        ("4,294,967,295", std::u32::MAX, &POLICIES[0]),
        ("4𠜱294𠜱967𠜱295", std::u32::MAX, &POLICIES[1]),
        ("4𠜱29𠜱49𠜱67𠜱295", std::u32::MAX, &POLICIES[2]),
        ("4294967295", std::u32::MAX, &POLICIES[3]),
        ("4294967295", std::u32::MAX, &POLICIES[4]),
    ];

    for (expected, input, format) in test_cases {
        // Buffer
        let mut buf = Buffer::default();
        buf.write_formatted(input, *format);
        assert_eq!(*expected, buf.as_str());

        #[cfg(feature = "std")]
        {
            // ToFormattedString
            assert_eq!(expected.to_string(), input.to_formatted_string(*format));

            // WriteFormatted
            let mut s = String::new();
            s.write_formatted(input, *format).unwrap();
            assert_eq!(expected.to_string(), s);
        }
    }
}

#[test]
fn test_usize() {
    #[cfg(target_pointer_width = "64")]let expected = ["18,446,744,073,709,551,615","18𠜱446𠜱744𠜱073𠜱709𠜱551𠜱615","1𠜱84𠜱46𠜱74𠜱40𠜱73𠜱70𠜱95𠜱51𠜱615","18446744073709551615","18446744073709551615"];
    #[cfg(target_pointer_width = "32")]let expected = ["4,294,967,295","4𠜱294𠜱967𠜱295","4𠜱29𠜱49𠜱67𠜱295","4294967295","4294967295"];
    let test_cases: &[(&str, usize, &CustomFormat)] = &[
        ("0", 0, &POLICIES[0]),
        ("0", 0, &POLICIES[1]),
        ("0", 0, &POLICIES[2]),
        ("0", 0, &POLICIES[3]),
        ("0", 0, &POLICIES[4]),
        (expected[0], std::usize::MAX, &POLICIES[0]),
        (
            expected[1],
            std::usize::MAX,
            &POLICIES[1],
        ),
        (
            expected[2],
            std::usize::MAX,
            &POLICIES[2],
        ),
        (expected[3], std::usize::MAX, &POLICIES[3]),
        (expected[4], std::usize::MAX, &POLICIES[4]),
    ];

    for (expected, input, format) in test_cases {
        // Buffer
        let mut buf = Buffer::default();
        buf.write_formatted(input, *format);
        assert_eq!(*expected, buf.as_str());

        #[cfg(feature = "std")]
        {
            // ToFormattedString
            assert_eq!(expected.to_string(), input.to_formatted_string(*format));

            // WriteFormatted
            let mut s = String::new();
            s.write_formatted(input, *format).unwrap();
            assert_eq!(expected.to_string(), s);
        }
    }
}

#[test]
fn test_u64() {
    let test_cases: &[(&str, u64, &CustomFormat)] = &[
        ("0", 0, &POLICIES[0]),
        ("0", 0, &POLICIES[1]),
        ("0", 0, &POLICIES[2]),
        ("0", 0, &POLICIES[3]),
        ("0", 0, &POLICIES[4]),
        ("18,446,744,073,709,551,615", std::u64::MAX, &POLICIES[0]),
        (
            "18𠜱446𠜱744𠜱073𠜱709𠜱551𠜱615",
            std::u64::MAX,
            &POLICIES[1],
        ),
        (
            "1𠜱84𠜱46𠜱74𠜱40𠜱73𠜱70𠜱95𠜱51𠜱615",
            std::u64::MAX,
            &POLICIES[2],
        ),
        ("18446744073709551615", std::u64::MAX, &POLICIES[3]),
        ("18446744073709551615", std::u64::MAX, &POLICIES[4]),
    ];

    for (expected, input, format) in test_cases {
        // Buffer
        let mut buf = Buffer::default();
        buf.write_formatted(input, *format);
        assert_eq!(*expected, buf.as_str());

        #[cfg(feature = "std")]
        {
            // ToFormattedString
            assert_eq!(expected.to_string(), input.to_formatted_string(*format));

            // WriteFormatted
            let mut s = String::new();
            s.write_formatted(input, *format).unwrap();
            assert_eq!(expected.to_string(), s);
        }
    }
}

#[test]
fn test_u128() {
    let test_cases: &[(&str, u128, &CustomFormat)] = &[
        ("0", 0, &POLICIES[0]),
        ("0", 0, &POLICIES[1]),
        ("0", 0, &POLICIES[2]),
        ("0", 0, &POLICIES[3]),
        ("0", 0, &POLICIES[4]),
        (
            "340,282,366,920,938,463,463,374,607,431,768,211,455",
            std::u128::MAX,
            &POLICIES[0],
        ),
        (
            "340𠜱282𠜱366𠜱920𠜱938𠜱463𠜱463𠜱374𠜱607𠜱431𠜱768𠜱211𠜱455",
            std::u128::MAX,
            &POLICIES[1],
        ),
        (
            "34𠜱02𠜱82𠜱36𠜱69𠜱20𠜱93𠜱84𠜱63𠜱46𠜱33𠜱74𠜱60𠜱74𠜱31𠜱76𠜱82𠜱11𠜱455",
            std::u128::MAX,
            &POLICIES[2],
        ),
        (
            "340282366920938463463374607431768211455",
            std::u128::MAX,
            &POLICIES[3],
        ),
        (
            "340282366920938463463374607431768211455",
            std::u128::MAX,
            &POLICIES[4],
        ),
    ];

    for (expected, input, format) in test_cases {
        // Buffer
        let mut buf = Buffer::default();
        buf.write_formatted(input, *format);
        assert_eq!(*expected, buf.as_str());

        #[cfg(feature = "std")]
        {
            // ToFormattedString
            assert_eq!(expected.to_string(), input.to_formatted_string(*format));

            // WriteFormatted
            let mut s = String::new();
            s.write_formatted(input, *format).unwrap();
            assert_eq!(expected.to_string(), s);
        }
    }
}