@@ -165,6 +165,7 @@ mod test {
165165 use crate :: BlockId ;
166166
167167 use super :: * ;
168+ use std:: cmp:: Ordering ;
168169
169170 #[ test]
170171 fn chain_position_ord ( ) {
@@ -250,4 +251,88 @@ mod test {
250251 ]
251252 ) ;
252253 }
254+
255+ #[ test]
256+ fn test_sort_unconfirmed_chain_position ( ) {
257+ let mut v = vec ! [
258+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
259+ first_seen: Some ( 5 ) ,
260+ last_seen: Some ( 20 ) ,
261+ } ,
262+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
263+ first_seen: None ,
264+ last_seen: Some ( 30 ) ,
265+ } ,
266+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
267+ first_seen: Some ( 1 ) ,
268+ last_seen: Some ( 10 ) ,
269+ } ,
270+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
271+ first_seen: Some ( 3 ) ,
272+ last_seen: None ,
273+ } ,
274+ ] ;
275+
276+ // Sort by first_seen: lowest first_seen first, None at the bottom
277+ v. sort_by ( |a, b| match ( a, b) {
278+ (
279+ ChainPosition :: < ConfirmationBlockTime > :: Unconfirmed { first_seen : a, .. } ,
280+ ChainPosition :: < ConfirmationBlockTime > :: Unconfirmed { first_seen : b, .. } ,
281+ ) => a. unwrap_or ( u64:: MAX ) . cmp ( & b. unwrap_or ( u64:: MAX ) ) ,
282+ _ => Ordering :: Equal ,
283+ } ) ;
284+
285+ assert_eq ! (
286+ v,
287+ vec![
288+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
289+ first_seen: Some ( 1 ) ,
290+ last_seen: Some ( 10 )
291+ } ,
292+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
293+ first_seen: Some ( 3 ) ,
294+ last_seen: None
295+ } ,
296+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
297+ first_seen: Some ( 5 ) ,
298+ last_seen: Some ( 20 )
299+ } ,
300+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
301+ first_seen: None ,
302+ last_seen: Some ( 30 )
303+ } ,
304+ ]
305+ ) ;
306+
307+ // Sort by last_seen: highest last_seen first, None at the bottom
308+ v. sort_by ( |a, b| match ( a, b) {
309+ (
310+ ChainPosition :: < ConfirmationBlockTime > :: Unconfirmed { last_seen : a, .. } ,
311+ ChainPosition :: < ConfirmationBlockTime > :: Unconfirmed { last_seen : b, .. } ,
312+ ) => b. unwrap_or ( 0 ) . cmp ( & a. unwrap_or ( 0 ) ) ,
313+ _ => Ordering :: Equal ,
314+ } ) ;
315+
316+ assert_eq ! (
317+ v,
318+ vec![
319+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
320+ first_seen: None ,
321+ last_seen: Some ( 30 )
322+ } ,
323+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
324+ first_seen: Some ( 5 ) ,
325+ last_seen: Some ( 20 )
326+ } ,
327+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
328+ first_seen: Some ( 1 ) ,
329+ last_seen: Some ( 10 )
330+ } ,
331+ ChainPosition :: <ConfirmationBlockTime >:: Unconfirmed {
332+ first_seen: Some ( 3 ) ,
333+ last_seen: None
334+ } ,
335+ ]
336+ ) ;
337+ }
253338}
0 commit comments