@@ -28,6 +28,7 @@ pub struct PriorityCollector {
28
28
priorities : BTreeMap < SortitionRound , BinaryHeap < PriorityInfoSummary > > ,
29
29
}
30
30
31
+ #[ cfg_attr( test, derive( Debug ) ) ]
31
32
#[ derive( Eq , PartialEq ) ]
32
33
pub struct PriorityInfoSummary {
33
34
priority : Priority ,
@@ -75,3 +76,83 @@ impl PriorityCollector {
75
76
self . priorities = new_collector;
76
77
}
77
78
}
79
+
80
+ #[ cfg( test) ]
81
+ mod priority_collector_tests {
82
+ use super :: * ;
83
+ use crate :: consensus:: PriorityInfo ;
84
+ #[ test]
85
+ fn compare_priority_info_summary1 ( ) {
86
+ let greater_priority_info_summary = PriorityInfoSummary {
87
+ priority : 0xffu64 . into ( ) ,
88
+ signer_idx : 0 ,
89
+ } ;
90
+ let less_priority_info_summary = PriorityInfoSummary {
91
+ priority : 0x7fu64 . into ( ) ,
92
+ signer_idx : 1 ,
93
+ } ;
94
+ assert ! ( greater_priority_info_summary > less_priority_info_summary) ;
95
+ }
96
+
97
+ #[ test]
98
+ fn compare_priority_info_summary2 ( ) {
99
+ let greater_priority_info_summary = PriorityInfoSummary {
100
+ priority : 0x55555544u64 . into ( ) ,
101
+ signer_idx : 0 ,
102
+ } ;
103
+ let less_priority_info_summary = PriorityInfoSummary {
104
+ priority : 0x55555523u64 . into ( ) ,
105
+ signer_idx : 22 ,
106
+ } ;
107
+ assert ! ( greater_priority_info_summary > less_priority_info_summary) ;
108
+ }
109
+
110
+ fn add_fixed_priorities ( collector : & mut PriorityCollector , round : SortitionRound ) {
111
+ let priority_messages = [ 0x55u64 , 0xffu64 , 0x44u64 , 0xeeu64 ] . into_iter ( ) . map ( |priority| {
112
+ let info = PriorityInfo :: create_from_members ( ( * priority) . into ( ) , 1 , vec ! [ ] , vec ! [ ] ) ;
113
+ PriorityMessage {
114
+ seed : 0xffffu64 . into ( ) ,
115
+ info,
116
+ }
117
+ } ) ;
118
+ priority_messages
119
+ . zip ( [ 1 , 2 , 3 , 4 ] . iter ( ) )
120
+ . for_each ( |( message, signer_idx) | collector. insert ( & message, * signer_idx, round) ) ;
121
+ }
122
+
123
+ #[ test]
124
+ fn insert_and_get_highest ( ) {
125
+ let mut collector: PriorityCollector = Default :: default ( ) ;
126
+ let round = SortitionRound {
127
+ height : 1 ,
128
+ view : 0 ,
129
+ } ;
130
+ add_fixed_priorities ( & mut collector, round) ;
131
+ assert_eq ! ( collector. get_highest_priority( round) . unwrap( ) , PriorityInfoSummary {
132
+ priority: 0xffu64 . into( ) ,
133
+ signer_idx: 2 ,
134
+ } ) ;
135
+ }
136
+
137
+ #[ test]
138
+ fn throw_away_old ( ) {
139
+ let mut collector: PriorityCollector = Default :: default ( ) ;
140
+ let rounds = [ ( 1 , 0 ) , ( 3 , 1 ) , ( 5 , 2 ) , ( 100 , 7 ) , ( 0 , 8 ) ] . into_iter ( ) . map ( |( height, view) | SortitionRound {
141
+ height : * height,
142
+ view : * view,
143
+ } ) ;
144
+ rounds. clone ( ) . for_each ( |round| add_fixed_priorities ( & mut collector, round) ) ;
145
+ let target_round = SortitionRound {
146
+ height : 5 ,
147
+ view : 2 ,
148
+ } ;
149
+ collector. throw_away_old ( & target_round) ;
150
+ rounds
151
+ . clone ( )
152
+ . filter ( |round| round >= & target_round)
153
+ . for_each ( |round_gte| assert ! ( collector. get_highest_priority( round_gte) . is_some( ) ) ) ;
154
+ rounds
155
+ . filter ( |round| round < & target_round)
156
+ . for_each ( |round_lt| assert ! ( collector. get_highest_priority( round_lt) . is_none( ) ) )
157
+ }
158
+ }
0 commit comments