use super::*;
const QUIET: Duration = Duration::from_millis(500);
fn debounce() -> Debounce<u64> {
Debounce::new(QUIET)
}
fn ms(millis: u64) -> Duration {
Duration::from_millis(millis)
}
#[test]
fn a_second_touch_resets_the_window() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(7, start);
debounce.touch(7, start + ms(10));
debounce.touch(7, start + ms(20));
assert_eq!(debounce.len(), 1, "three touches, one pending key");
assert_eq!(debounce.deadline(), Some(start + ms(520)));
assert!(
debounce.ripe(start + ms(500)).is_empty(),
"the FIRST touch plus the window is not the answer — the window restarted twice"
);
assert!(debounce.ripe(start + ms(519)).is_empty());
assert_eq!(debounce.ripe(start + ms(520)), vec![7]);
assert!(debounce.is_empty());
assert_eq!(debounce.deadline(), None);
}
#[test]
fn distinct_keys_ripen_independently() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(1, start);
debounce.touch(2, start + ms(100));
debounce.touch(2, start + ms(400));
assert_eq!(debounce.len(), 2);
assert_eq!(debounce.deadline(), Some(start + ms(500)));
assert_eq!(debounce.ripe(start + ms(500)), vec![1]);
assert_eq!(
debounce.deadline(),
Some(start + ms(900)),
"the head moves on to the key that is still pending"
);
assert!(debounce.ripe(start + ms(899)).is_empty());
assert_eq!(debounce.ripe(start + ms(900)), vec![2]);
}
#[test]
fn ripe_keys_come_back_in_ripening_order() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(30, start + ms(30));
debounce.touch(10, start + ms(10));
debounce.touch(20, start + ms(20));
debounce.touch(41, start + ms(40));
debounce.touch(42, start + ms(40));
assert_eq!(debounce.ripe(start + ms(1_000)), vec![10, 20, 30, 41, 42]);
}
#[test]
fn a_cancel_drops_the_pending_intake() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(1, start);
debounce.touch(2, start);
assert!(debounce.cancel(&1), "there was a pending intake for key 1");
assert!(!debounce.cancel(&1), "and now there is not");
assert!(
!debounce.cancel(&99),
"a key that was never touched cancels to false"
);
assert_eq!(debounce.len(), 1);
assert_eq!(
debounce.ripe(start + ms(1_000)),
vec![2],
"the cancelled key left no entry behind in the schedule"
);
}
#[test]
fn a_cancelled_key_can_be_touched_again() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(1, start);
assert!(debounce.cancel(&1));
assert_eq!(debounce.deadline(), None);
debounce.touch(1, start + ms(100));
assert_eq!(debounce.deadline(), Some(start + ms(600)));
assert_eq!(debounce.ripe(start + ms(600)), vec![1]);
}
#[test]
fn a_touch_after_a_ripe_opens_a_fresh_window() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(1, start);
assert_eq!(debounce.ripe(start + ms(500)), vec![1]);
debounce.touch(1, start + ms(600));
assert_eq!(debounce.deadline(), Some(start + ms(1_100)));
assert_eq!(debounce.ripe(start + ms(1_100)), vec![1]);
}
#[test]
fn a_burst_on_one_key_leaves_one_entry_and_ripens_once() {
let start = Instant::now();
let mut debounce = debounce();
for step in 0..1_000 {
debounce.touch(7, start + ms(step));
}
assert_eq!(debounce.len(), 1);
assert_eq!(
debounce.due.len(),
1,
"the schedule holds no superseded entries"
);
assert_eq!(debounce.deadline(), Some(start + ms(999 + 500)));
assert!(debounce.ripe(start + ms(1_498)).is_empty());
assert_eq!(debounce.ripe(start + ms(1_499)), vec![7]);
assert!(debounce.is_empty());
assert_eq!(debounce.due.len(), 0);
}
#[test]
fn a_re_touch_at_the_same_instant_leaves_one_entry() {
let start = Instant::now();
let mut debounce = debounce();
debounce.touch(1, start);
debounce.touch(1, start);
assert_eq!(debounce.len(), 1);
assert_eq!(debounce.due.len(), 1);
assert_eq!(debounce.deadline(), Some(start + QUIET));
assert_eq!(debounce.ripe(start + QUIET), vec![1]);
assert!(debounce.is_empty());
assert_eq!(debounce.due.len(), 0);
}
struct Lcg(u64);
impl Lcg {
const fn new(seed: u64) -> Self {
Self(seed)
}
fn next(&mut self) -> u64 {
self.0 = self
.0
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
self.0 >> 33
}
}
#[test]
fn no_key_ripens_before_its_last_touch_plus_the_window() {
let start = Instant::now();
let mut debounce = Debounce::<u8>::new(QUIET);
let mut last_touch: HashMap<u8, Instant> = HashMap::new();
let mut rng = Lcg::new(0x5eed_1234);
let mut now = start;
let mut harvested = 0_usize;
for step in 0..2_000 {
now += ms(rng.next() % 150);
if step < 1_500 && !rng.next().is_multiple_of(3) {
let key = (rng.next() % 8) as u8;
debounce.touch(key, now);
last_touch.insert(key, now);
}
for key in debounce.ripe(now) {
let touched = last_touch
.remove(&key)
.expect("a key came back that was never touched");
assert!(
now >= touched + QUIET,
"key {key} ripened at {now:?}, before its last touch {touched:?} plus {QUIET:?}"
);
harvested += 1;
}
}
assert!(
last_touch.is_empty(),
"every touched key ripened once the touching stopped"
);
assert!(
harvested > 8,
"the run exercised more than one round of ripening"
);
assert!(debounce.is_empty());
}
#[test]
fn parking_on_the_deadline_alone_takes_every_key_out() {
let start = Instant::now();
let mut debounce = debounce();
let mut rng = Lcg::new(0x0d15_ea5e);
let mut expected: Vec<(Instant, u64)> = Vec::new();
for key in 0..32 {
let touched = start + ms(rng.next() % 1_000);
debounce.touch(key, touched);
expected.push((touched + QUIET, key));
}
expected.sort_unstable();
let mut taken: Vec<(Instant, u64)> = Vec::new();
while let Some(deadline) = debounce.deadline() {
let before = taken.len();
for key in debounce.ripe(deadline) {
taken.push((deadline, key));
}
assert!(
taken.len() > before,
"a reported deadline that yields nothing at that instant would spin this loop forever"
);
}
assert_eq!(
taken, expected,
"every key, at exactly the instant it was due"
);
}