-
Notifications
You must be signed in to change notification settings - Fork 8
/
demo.rs
153 lines (129 loc) · 4.17 KB
/
demo.rs
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
#[macro_use]
extern crate galvanic_test;
extern crate human_format;
test_suite! {
name demo_examples;
use human_format::*;
test should_allow_use_of_si_scale_implicitly() {
assert_eq!(Formatter::new()
.format(1000 as f64),
"1.00 K");
}
test should_allow_explicit_decimals() {
assert_eq!(Formatter::new()
.with_decimals(1)
.format(1000 as f64),
"1.0 K");
}
test should_allow_explicit_separator() {
assert_eq!(Formatter::new()
.with_separator(" - ")
.format(1000 as f64),
"1.00 - K");
}
test should_allow_use_of_si_scale_explicitly() {
assert_eq!(Formatter::new()
.with_scales(Scales::SI())
.format(1000 as f64),
"1.00 K");
}
test should_allow_use_of_binary_scale_explicitly() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.format(1024 as f64),
"1.00 Ki");
}
test should_allow_use_of_binary_units_explicitly() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.with_units("B")
.format(102400 as f64),
"100.00 KiB");
}
test should_output_10_24_mib() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.with_units("B")
.format(1024.0 * 1024.0 as f64),
"1.00 MiB");
}
test should_output_75_11_pib() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.with_units("B")
.format(84_567_942_345_572_238.0),
"75.11 PiB");
}
test should_output_1_00_gbps() {
assert_eq!(Formatter::new()
.with_units("B/s")
.format(1e9),
"1.00 GB/s");
}
test should_allow_explicit_suffix_and_unit() {
assert_eq!(Formatter::new()
.with_suffix("k")
.with_units("m")
.format(1024 as f64),
"1.02 Km");
}
test should_allow_use_of_explicit_scale() {
let mut scales = Scales::new();
scales
.with_base(1024)
.with_suffixes(vec!["","Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"]);
assert_eq!(Formatter::new()
.with_scales(scales)
.with_units("B")
.format(1024 as f64),
"1.00 KiB");
}
test should_allow_parsing_to_f64() {
assert_eq!(Formatter::new()
.parse("1.00 K"), 1000.0);
}
test should_allow_try_parsing_to_f64() {
assert_eq!(Formatter::new()
.try_parse("1.00 M"), Ok(1000000.0));
}
test should_allow_parsing_binary_values_to_f64() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.parse("1.00 Ki"), 1024.0);
}
test should_allow_parsing_binary_values_with_units_to_f64() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.with_units("B")
.parse("1.00 KiB"), 1024.0);
}
test should_allow_try_parsing_binary_values_with_units_to_f64() {
assert_eq!(Formatter::new()
.with_scales(Scales::Binary())
.with_units("B")
.try_parse("1.00 KiB"), Ok(1024.0));
}
test should_surface_errors() {
let result = Formatter::new()
.with_scales(Scales::Binary())
.with_units("B")
.try_parse("1.00 DN");
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Unknown suffix: DN, valid suffixes are: Ki, Mi, Gi, Ti, Pi, Ei, Zi, Yi");
}
test try_parse_explicit_suffix_and_unit() {
assert_eq!(Formatter::new()
.with_units("m")
.try_parse("1.024Mm"), Ok(1024000.0));
}
test try_parse_explicit_suffix_and_unitless() {
assert_eq!(Formatter::new()
.with_units("m")
.try_parse("1.024M"), Ok(1024000.0));
}
test try_parse_very_large_value() {
assert_eq!(Formatter::new()
.with_units("B")
.try_parse("2PB"), Ok(2_000_000_000_000_000.0));
}
}