use dig_block::{Bytes32, CheckpointStatus};
fn byte_tag(b: u8) -> Bytes32 {
Bytes32::new([b; 32])
}
#[test]
fn ckp003_create_pending_matches() {
let s = CheckpointStatus::Pending;
assert!(matches!(s, CheckpointStatus::Pending));
}
#[test]
fn ckp003_create_collecting_matches() {
let s = CheckpointStatus::Collecting;
assert!(matches!(s, CheckpointStatus::Collecting));
}
#[test]
fn ckp003_create_failed_matches() {
let s = CheckpointStatus::Failed;
assert!(matches!(s, CheckpointStatus::Failed));
}
#[test]
fn ckp003_winner_selected_construct_and_extract_fields() {
let hash = byte_tag(0x5a);
let score = 0xdead_beef_cafe_babe;
let s = CheckpointStatus::WinnerSelected {
winner_hash: hash,
winner_score: score,
};
match s {
CheckpointStatus::WinnerSelected {
winner_hash,
winner_score,
} => {
assert_eq!(winner_hash, hash);
assert_eq!(winner_score, score);
}
_ => panic!("expected WinnerSelected"),
}
}
#[test]
fn ckp003_finalized_construct_and_extract_fields() {
let hash = byte_tag(0x71);
let height: u32 = 9_001;
let s = CheckpointStatus::Finalized {
winner_hash: hash,
l1_height: height,
};
match s {
CheckpointStatus::Finalized {
winner_hash,
l1_height,
} => {
assert_eq!(winner_hash, hash);
assert_eq!(l1_height, height);
}
_ => panic!("expected Finalized"),
}
}
#[test]
fn ckp003_exhaustive_match_all_variants() {
let cases: [CheckpointStatus; 5] = [
CheckpointStatus::Pending,
CheckpointStatus::Collecting,
CheckpointStatus::WinnerSelected {
winner_hash: byte_tag(1),
winner_score: 1,
},
CheckpointStatus::Finalized {
winner_hash: byte_tag(2),
l1_height: 2,
},
CheckpointStatus::Failed,
];
for s in cases {
let label = match s {
CheckpointStatus::Pending => "pending",
CheckpointStatus::Collecting => "collecting",
CheckpointStatus::WinnerSelected { .. } => "winner_selected",
CheckpointStatus::Finalized { .. } => "finalized",
CheckpointStatus::Failed => "failed",
};
assert!(!label.is_empty());
}
}
#[test]
fn ckp003_copy_and_eq_roundtrip() {
let a = CheckpointStatus::WinnerSelected {
winner_hash: byte_tag(0x0c),
winner_score: 42,
};
let b = a;
assert_eq!(a, b);
assert_eq!(
a,
CheckpointStatus::WinnerSelected {
winner_hash: byte_tag(0x0c),
winner_score: 42,
}
);
}