forked from iotaledger/iota.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parents.rs
145 lines (115 loc) · 3.93 KB
/
parents.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
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use core::ops::Deref;
use iota_types::block::{
parent::Parents,
rand::block::{rand_block_id, rand_block_ids},
BlockId, Error,
};
use packable::{bounded::TryIntoBoundedU8Error, error::UnpackError, prefix::VecPrefix, PackableExt};
#[test]
fn new_valid_iter() {
let inner = rand_block_ids(8);
let parents = Parents::new(inner.clone()).unwrap();
let parents_vec = parents.iter().copied().collect::<Vec<BlockId>>();
assert_eq!(inner, parents_vec[0..].to_vec());
}
#[test]
fn new_valid_deref() {
let inner = rand_block_ids(8);
let parents = Parents::new(inner.clone()).unwrap();
assert_eq!(parents.deref(), &inner.into_boxed_slice());
}
#[test]
fn new_invalid_more_than_max() {
let mut inner = vec![rand_block_id()];
for _ in 0..8 {
Parents::new(inner.clone()).unwrap();
inner.push(rand_block_id());
inner.sort();
}
assert!(matches!(
Parents::new(inner),
Err(Error::InvalidParentCount(TryIntoBoundedU8Error::Invalid(9)))
));
}
#[test]
fn new_not_sorted() {
let mut inner_1 = rand_block_ids(8);
let inner_2 = inner_1.clone();
inner_1.reverse();
let parents = Parents::new(inner_1).unwrap();
assert_eq!(*parents.to_vec(), inner_2);
}
#[test]
fn new_not_unique() {
let mut inner_1 = rand_block_ids(7);
let inner_2 = inner_1.clone();
inner_1.push(*inner_1.last().unwrap());
let parents = Parents::new(inner_1).unwrap();
assert_eq!(*parents.to_vec(), inner_2);
}
#[test]
fn packed_len() {
let parents = Parents::new(rand_block_ids(5)).unwrap();
assert_eq!(parents.packed_len(), 1 + 5 * 32);
assert_eq!(parents.pack_to_vec().len(), 1 + 5 * 32);
}
#[test]
fn pack_unpack_valid() {
let parents_1 = Parents::new(rand_block_ids(8)).unwrap();
let parents_2 = Parents::unpack_verified(parents_1.pack_to_vec().as_slice(), &()).unwrap();
assert_eq!(parents_1, parents_2);
}
#[test]
fn pack_unpack_invalid_less_than_min() {
let bytes = vec![
0, 227, 127, 245, 158, 220, 152, 191, 107, 27, 218, 187, 247, 227, 25, 215, 141, 92, 95, 138, 21, 98, 20, 83,
206, 92, 26, 62, 9, 221, 81, 191, 4, 96, 54, 232, 50, 83, 49, 236, 80, 189, 251, 191, 192, 122, 206, 202, 209,
145, 50, 168, 233, 176, 12, 164, 138, 207, 22, 96, 82, 189, 64, 188, 130,
];
assert!(matches!(
Parents::unpack_verified(bytes.as_slice(), &()),
Err(UnpackError::Packable(Error::InvalidParentCount(
TryIntoBoundedU8Error::Invalid(0)
)))
));
}
#[test]
fn pack_unpack_invalid_more_than_max() {
let bytes = vec![
9, 227, 127, 245, 158, 220, 152, 191, 107, 27, 218, 187, 247, 227, 25, 215, 141, 92, 95, 138, 21, 98, 20, 83,
206, 92, 26, 62, 9, 221, 81, 191, 4, 96, 54, 232, 50, 83, 49, 236, 80, 189, 251, 191, 192, 122, 206, 202, 209,
145, 50, 168, 233, 176, 12, 164, 138, 207, 22, 96, 82, 189, 64, 188, 130,
];
assert!(matches!(
Parents::unpack_verified(bytes.as_slice(), &()),
Err(UnpackError::Packable(Error::InvalidParentCount(
TryIntoBoundedU8Error::Invalid(9)
)))
));
}
#[test]
fn unpack_invalid_not_sorted() {
let mut inner = rand_block_ids(8);
inner.reverse();
let inner = VecPrefix::<_, u8>::try_from(inner).unwrap();
let packed = inner.pack_to_vec();
let parents = Parents::unpack_verified(packed.as_slice(), &());
assert!(matches!(
parents,
Err(UnpackError::Packable(Error::ParentsNotUniqueSorted))
),);
}
#[test]
fn unpack_invalid_not_unique() {
let mut inner = rand_block_ids(7);
inner.push(*inner.last().unwrap());
let inner = VecPrefix::<_, u8>::try_from(inner).unwrap();
let packed = inner.pack_to_vec();
let parents = Parents::unpack_verified(packed.as_slice(), &());
assert!(matches!(
parents,
Err(UnpackError::Packable(Error::ParentsNotUniqueSorted))
),);
}