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
|
use std::convert::TryFrom;
use gix_config_value::{integer::Suffix, Integer};
use crate::b;
#[test]
fn from_str_no_suffix() {
assert_eq!(Integer::try_from(b("1")).unwrap(), Integer { value: 1, suffix: None });
assert_eq!(
Integer::try_from(b("-1")).unwrap(),
Integer {
value: -1,
suffix: None
}
);
}
#[test]
fn from_str_with_suffix() {
assert_eq!(
Integer::try_from(b("1k")).unwrap(),
Integer {
value: 1,
suffix: Some(Suffix::Kibi),
}
);
assert_eq!(
Integer::try_from(b("1m")).unwrap(),
Integer {
value: 1,
suffix: Some(Suffix::Mebi),
}
);
assert_eq!(
Integer::try_from(b("1g")).unwrap(),
Integer {
value: 1,
suffix: Some(Suffix::Gibi),
}
);
}
#[test]
fn invalid_from_str() {
assert!(Integer::try_from(b("")).is_err());
assert!(Integer::try_from(b("-")).is_err());
assert!(Integer::try_from(b("k")).is_err());
assert!(Integer::try_from(b("m")).is_err());
assert!(Integer::try_from(b("g")).is_err());
assert!(Integer::try_from(b("123123123123123123123123")).is_err());
assert!(Integer::try_from(b("gg")).is_err());
}
#[test]
fn as_decimal() {
fn decimal(input: &str) -> Option<i64> {
Integer::try_from(b(input)).unwrap().to_decimal()
}
assert_eq!(decimal("12"), Some(12), "works without suffix");
assert_eq!(decimal("13k"), Some(13 * 1024), "works with kilobyte suffix");
assert_eq!(decimal("13K"), Some(13 * 1024), "works with Kilobyte suffix");
assert_eq!(decimal("14m"), Some(14 * 1_048_576), "works with megabyte suffix");
assert_eq!(decimal("14M"), Some(14 * 1_048_576), "works with Megabyte suffix");
assert_eq!(decimal("15g"), Some(15 * 1_073_741_824), "works with gigabyte suffix");
assert_eq!(decimal("15G"), Some(15 * 1_073_741_824), "works with Gigabyte suffix");
assert_eq!(decimal(&format!("{}g", i64::MAX)), None, "overflow results in None");
assert_eq!(decimal(&format!("{}g", i64::MIN)), None, "underflow results in None");
}
|