use super::encoding::{VERSION, crc32};
use super::{DispatchEdge, DispatchError, Dispatcher, Index, PointKind, Wait};
use crate::{Ticks, TimePoint};
const N: usize = 2;
const EDGES: usize = 1;
const E: usize = 2 * EDGES + 1;
const FRAMED_LEN: usize = 24 + 21;
#[cfg(kani)]
const EMPTY_LEN: usize = 24;
#[cfg(kani)]
const POINT_EDGE_LEN: usize = 24 + 21 + 16;
#[derive(Debug, Clone, Copy)]
pub(super) struct Scenario {
pub(super) points: [(bool, u8, i64, i64); N],
pub(super) point_count: u8,
pub(super) edges: [(u8, u8, i64); EDGES],
pub(super) edge_count: u8,
pub(super) wait: Option<(u8, u8, i64)>,
}
pub(super) type Call = (u8, u8, i64);
fn handle(raw: u8) -> TimePoint {
TimePoint(usize::from(raw) % (N + 1))
}
pub(super) fn run_scenario<I: Index>(s: &Scenario, calls: &[Call]) {
let points = s.points.map(|(exec, a, lo, hi)| {
if exec {
PointKind::Executable
} else {
PointKind::Contingent {
activation: handle(a),
lo: Ticks(lo),
hi: Ticks(hi),
}
}
});
let edges = s.edges.map(|(f, t, w)| DispatchEdge {
from: handle(f),
to: handle(t),
weight: Ticks(w),
});
let waits = s.wait.map(|(x, c, d)| Wait {
waiter: handle(x),
contingent: handle(c),
delay: Ticks(d),
});
let n = usize::from(s.point_count) % (N + 1);
let m = usize::from(s.edge_count) % (EDGES + 1);
let mut d = Dispatcher::<N, E, I>::EMPTY;
if d.load(&points[..n], &edges[..m], waits.as_slice()).is_err() {
return;
}
for &(selector, p, now) in calls {
call(&mut d, selector, handle(p), Ticks(now));
}
}
fn call<I: Index>(d: &mut Dispatcher<N, E, I>, selector: u8, tp: TimePoint, now: Ticks) {
match selector % 5 {
0 => drop(d.observe(tp, now)),
1 => execute(d, tp, now),
2 => drop(d.can_execute(tp, now)),
3 => drop(d.step(now, &[tp], &mut [TimePoint(0); N])),
_ => drop(d.check_deadlines(now)),
}
let _ = (d.next_wakeup(), d.is_complete(), d.window(tp), d.time(tp));
}
fn execute<I: Index>(d: &mut Dispatcher<N, E, I>, tp: TimePoint, now: Ticks) {
let window = d.window(tp);
let result = d.execute(tp, now);
if result == Err(DispatchError::NotExecutable(tp)) {
return;
}
let (lb, ub) = window.expect("only known points execute");
assert!(lb.is_none_or(|l| l <= now) && ub.is_none_or(|u| now <= u));
assert_eq!(d.time(tp), Some(now));
}
pub(super) fn decode_bytes(bytes: &[u8]) {
let mut d = Dispatcher::<2, 4, u16>::EMPTY;
if d.load_encoded(bytes).is_ok() {
run_ticks(&mut d, 3);
}
}
fn run_ticks(d: &mut Dispatcher<2, 4, u16>, ticks: i64) {
for t in 0..ticks {
let _ = d.step(Ticks(t), &[], &mut [TimePoint(0); 2]);
}
}
#[cfg(kani)]
fn load_records(bytes: &[u8], points: usize, edges: usize) {
let encoded = super::encoding::Encoded::assume_valid_framing(bytes, points, edges);
let _ = Dispatcher::<1, 2, u16>::EMPTY.load_from(&encoded);
}
#[cfg(kani)]
fn any_canonical_point() -> [u8; 21] {
let mut record: [u8; 21] = kani::any();
if record[0] != 1 {
record = [0; 21];
}
record
}
#[cfg(kani)]
fn header<const L: usize>(points: u32, edges: u32) -> [u8; L] {
let mut bytes = [0u8; L];
bytes[..4].copy_from_slice(b"TCDN");
bytes[4..6].copy_from_slice(&VERSION.to_le_bytes());
bytes[8..12].copy_from_slice(&points.to_le_bytes());
bytes[12..16].copy_from_slice(&edges.to_le_bytes());
bytes
}
pub(super) fn framed(point: [u8; 21]) -> [u8; FRAMED_LEN] {
let mut bytes = [0u8; FRAMED_LEN];
bytes[..4].copy_from_slice(b"TCDN");
bytes[4..6].copy_from_slice(&VERSION.to_le_bytes());
bytes[8..12].copy_from_slice(&1u32.to_le_bytes());
bytes[20..41].copy_from_slice(&point);
let crc = crc32(&bytes[..41]);
bytes[41..].copy_from_slice(&crc.to_le_bytes());
bytes
}
#[cfg(kani)]
mod harnesses {
use super::{
Call, EMPTY_LEN, FRAMED_LEN, POINT_EDGE_LEN, Scenario, any_canonical_point, header,
load_records, run_scenario,
};
fn any_scenario() -> Scenario {
Scenario {
points: kani::any(),
point_count: kani::any(),
edges: kani::any(),
edge_count: kani::any(),
wait: kani::any(),
}
}
fn any_call(kind: u8) -> Call {
(kind, kani::any(), kani::any())
}
#[kani::proof]
#[kani::unwind(4)]
fn dispatcher_observe_u16() {
run_scenario::<u16>(&any_scenario(), &[any_call(0)]);
}
#[kani::proof]
#[kani::unwind(4)]
fn dispatcher_execute_u16() {
run_scenario::<u16>(&any_scenario(), &[any_call(1)]);
}
#[kani::proof]
#[kani::unwind(4)]
fn dispatcher_can_execute_u16() {
run_scenario::<u16>(&any_scenario(), &[any_call(2)]);
}
#[kani::proof]
#[kani::unwind(4)]
fn dispatcher_step_u16() {
run_scenario::<u16>(&any_scenario(), &[any_call(3)]);
}
#[kani::proof]
#[kani::unwind(4)]
fn dispatcher_check_deadlines_u16() {
run_scenario::<u16>(&any_scenario(), &[any_call(4)]);
}
#[kani::proof]
#[kani::unwind(4)]
fn dispatcher_execute_usize() {
run_scenario::<usize>(&any_scenario(), &[any_call(1)]);
}
#[kani::proof]
#[kani::unwind(4)]
fn decoder_short_input() {
let bytes: [u8; EMPTY_LEN - 1] = kani::any();
let len: usize = kani::any();
kani::assume(len < EMPTY_LEN);
assert!(super::super::encoding::Encoded::parse(&bytes[..len]).is_err());
}
#[kani::proof]
#[kani::unwind(22)]
fn decoder_framing_and_checksum() {
let mut bytes: [u8; EMPTY_LEN] = [0; EMPTY_LEN];
let head: [u8; 8] = kani::any();
let crc: [u8; 4] = kani::any();
bytes[..8].copy_from_slice(&head);
bytes[20..].copy_from_slice(&crc);
let _ = super::super::encoding::Encoded::parse(&bytes);
}
#[kani::proof]
#[kani::unwind(22)]
fn decoder_canonical_point_record() {
let mut bytes: [u8; FRAMED_LEN] = header(1, 0);
let point: [u8; 21] = kani::any();
bytes[20..41].copy_from_slice(&point);
let _ = super::super::encoding::Encoded::parse_without_checksum(&bytes);
}
#[kani::proof]
#[kani::unwind(4)]
fn decoder_load_point_record() {
let mut bytes: [u8; FRAMED_LEN] = header(1, 0);
bytes[20..41].copy_from_slice(&any_canonical_point());
load_records(&bytes, 1, 0);
}
#[kani::proof]
#[kani::unwind(4)]
fn decoder_load_point_and_edge_records() {
let mut bytes: [u8; POINT_EDGE_LEN] = header(1, 1);
let edge: [u8; 16] = kani::any();
bytes[20..41].copy_from_slice(&any_canonical_point());
bytes[41..57].copy_from_slice(&edge);
load_records(&bytes, 1, 1);
}
}
#[cfg(test)]
mod tests {
use super::{Call, EDGES, N, Scenario, decode_bytes, framed, run_scenario};
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
fn byte(&mut self) -> u8 {
self.next().to_le_bytes()[0]
}
fn sparse_byte(&mut self) -> u8 {
if self.next() % 8 == 0 {
self.byte() % 4
} else {
0
}
}
fn tick(&mut self) -> i64 {
match self.next() % 8 {
0 => i64::MIN,
1 => i64::MAX,
2 => i64::from_le_bytes(self.next().to_le_bytes()),
_ => i64::from(self.byte() % 16) - 6,
}
}
fn scenario(&mut self) -> Scenario {
Scenario {
points: [(); N]
.map(|()| (self.next() % 2 == 0, self.byte(), self.tick(), self.tick())),
point_count: self.byte(),
edges: [(); EDGES].map(|()| (self.byte(), self.byte(), self.tick())),
edge_count: self.byte(),
wait: (self.next() % 2 == 0).then(|| (self.byte(), self.byte(), self.tick())),
}
}
fn calls(&mut self) -> [Call; 3] {
[(); 3].map(|()| (self.byte(), self.byte(), self.tick()))
}
}
#[test]
fn dispatcher_scenarios() {
let mut rng = Rng(0x9E37_79B9_7F4A_7C15);
for _ in 0..20_000 {
let s = rng.scenario();
let calls = rng.calls();
run_scenario::<u16>(&s, &calls);
run_scenario::<usize>(&s, &calls);
}
}
#[test]
fn decoder_inputs() {
let mut rng = Rng(0xD1B5_4A32_D192_ED03);
for _ in 0..20_000 {
let point = [(); 21].map(|()| rng.sparse_byte());
let bytes = framed(point);
decode_bytes(&bytes);
decode_bytes(&bytes[..usize::from(rng.byte()) % bytes.len()]);
}
}
}