-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdecode.rs
76 lines (66 loc) · 1.78 KB
/
decode.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
mod cases;
#[cfg(feature = "check")]
use assert_matches::assert_matches;
#[test]
fn test_decode() {
for &(val, s) in cases::TEST_CASES.iter() {
assert_eq!(val.to_vec(), bs58::decode(s).into_vec().unwrap());
assert_eq!(val.to_vec(), bs58::decode(s).into_vec_unsafe().unwrap());
}
}
#[test]
fn test_decode_small_buffer_err() {
let mut output = [0; 2];
assert_eq!(
bs58::decode("a3gV").into(&mut output),
Err(bs58::decode::Error::BufferTooSmall)
);
}
#[test]
fn test_decode_invalid_char() {
let sample = "123456789abcd!efghij";
assert_eq!(
bs58::decode(sample).into_vec().unwrap_err(),
bs58::decode::Error::InvalidCharacter {
character: '!',
index: 13
}
);
}
#[test]
#[cfg(feature = "check")]
fn test_decode_check() {
for &(val, s) in cases::CHECK_TEST_CASES.iter() {
assert_eq!(
val.to_vec(),
bs58::decode(s).with_check(None).into_vec().unwrap()
);
}
for &(val, s) in cases::CHECK_TEST_CASES[1..].iter() {
assert_eq!(
val.to_vec(),
bs58::decode(s).with_check(Some(val[0])).into_vec().unwrap()
);
}
}
#[test]
#[cfg(feature = "check")]
fn test_check_ver_failed() {
let d = bs58::decode("K5zqBMZZTzUbAZQgrt4")
.with_check(Some(0x01))
.into_vec();
assert!(d.is_err());
assert_matches!(d.unwrap_err(), bs58::decode::Error::InvalidVersion { .. });
}
#[test]
fn append() {
let mut buf = b"hello world".to_vec();
bs58::decode("a").into(&mut buf).unwrap();
assert_eq!(b"hello world!", buf.as_slice());
}
#[test]
fn no_append() {
let mut buf = b"hello world".to_owned();
bs58::decode("a").into(buf.as_mut()).unwrap();
assert_eq!(b"!ello world", buf.as_ref());
}