use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use dfir_rs::scheduled::ticks::TickInstant;
use multiplatform_test::multiplatform_test;
macro_rules! assert_contains_each_by_tick {
($results:expr, $tick:expr, &[]) => {{
assert_eq!($results.borrow().get(&$tick), None);
}};
($results:expr, $tick:expr, $input:expr) => {{
for v in $input {
assert!(
$results.borrow()[&$tick].contains(v),
"did not contain: {:?} in {:?}",
v,
$results.borrow()[&$tick]
);
}
}};
}
#[multiplatform_test]
pub fn tick_tick() {
let results = Rc::new(RefCell::new(HashMap::<TickInstant, Vec<_>>::new()));
let results_inner = Rc::clone(&results);
let mut df = dfir_rs::dfir_syntax! {
source_iter([1, 2])
-> [0]my_cross_join_multiset;
source_iter([0]) -> unioner;
source_iter([1]) -> defer_tick() -> unioner;
source_iter([2]) -> defer_tick() -> defer_tick() -> unioner;
unioner = union()
-> [1]my_cross_join_multiset;
my_cross_join_multiset = cross_join_multiset::<'tick, 'tick>()
-> for_each(|x| results_inner.borrow_mut().entry(context.current_tick()).or_default().push(x));
};
df.run_available_sync();
assert_contains_each_by_tick!(results, TickInstant::new(0), &[(1, 0), (2, 0)]);
assert_contains_each_by_tick!(results, TickInstant::new(1), &[]);
}
#[multiplatform_test]
pub fn tick_static() {
let results = Rc::new(RefCell::new(HashMap::<TickInstant, Vec<_>>::new()));
let results_inner = Rc::clone(&results);
let mut df = dfir_rs::dfir_syntax! {
source_iter([1, 2])
-> [0]my_cross_join_multiset;
source_iter([0]) -> unioner;
source_iter([1]) -> defer_tick() -> unioner;
source_iter([2]) -> defer_tick() -> defer_tick() -> unioner;
unioner = union()
-> [1]my_cross_join_multiset;
my_cross_join_multiset = cross_join_multiset::<'tick, 'static>()
-> for_each(|x| results_inner.borrow_mut().entry(context.current_tick()).or_default().push(x));
};
df.run_available_sync();
assert_contains_each_by_tick!(results, TickInstant::new(0), &[(1, 0), (2, 0)]);
assert_contains_each_by_tick!(results, TickInstant::new(1), &[]);
}
#[multiplatform_test]
pub fn static_tick() {
let results = Rc::new(RefCell::new(HashMap::<TickInstant, Vec<_>>::new()));
let results_inner = Rc::clone(&results);
let mut df = dfir_rs::dfir_syntax! {
source_iter([1, 2])
-> [0]my_cross_join_multiset;
source_iter([0]) -> unioner;
source_iter([1]) -> defer_tick() -> unioner;
source_iter([2]) -> defer_tick() -> defer_tick() -> unioner;
unioner = union()
-> [1]my_cross_join_multiset;
my_cross_join_multiset = cross_join_multiset::<'static, 'tick>()
-> for_each(|x| results_inner.borrow_mut().entry(context.current_tick()).or_default().push(x));
};
df.run_available_sync();
assert_contains_each_by_tick!(results, TickInstant::new(0), &[(1, 0), (2, 0)]);
assert_contains_each_by_tick!(results, TickInstant::new(1), &[(1, 1), (2, 1)]);
assert_contains_each_by_tick!(results, TickInstant::new(2), &[(1, 2), (2, 2)]);
assert_contains_each_by_tick!(results, TickInstant::new(3), &[]);
}
#[multiplatform_test]
pub fn static_static() {
let results = Rc::new(RefCell::new(HashMap::<TickInstant, Vec<_>>::new()));
let results_inner = Rc::clone(&results);
let mut df = dfir_rs::dfir_syntax! {
source_iter([1, 2])
-> [0]my_cross_join_multiset;
source_iter([0]) -> unioner;
source_iter([1]) -> defer_tick() -> unioner;
source_iter([2]) -> defer_tick() -> defer_tick() -> unioner;
unioner = union()
-> [1]my_cross_join_multiset;
my_cross_join_multiset = cross_join_multiset::<'static, 'static>()
-> for_each(|x| results_inner.borrow_mut().entry(context.current_tick()).or_default().push(x));
};
df.run_available_sync();
#[expect(
clippy::disallowed_methods,
reason = "Sorts all vecs regardless of nondeterministic iteration order."
)]
for result_vec in results.borrow_mut().values_mut() {
result_vec.sort_unstable();
}
#[rustfmt::skip]
{
assert_contains_each_by_tick!(results, TickInstant::new(0), &[(1, 0), (2, 0)]);
assert_contains_each_by_tick!(results, TickInstant::new(1), &[(1, 0), (2, 0), (1, 1), (2, 1)]);
assert_contains_each_by_tick!(results, TickInstant::new(2), &[(1, 0), (2, 0), (1, 1), (2, 1), (1, 2), (2, 2)]);
assert_contains_each_by_tick!(results, TickInstant::new(3), &[]);
};
}
#[multiplatform_test]
pub fn replay_static() {
let results = Rc::new(RefCell::new(HashMap::<TickInstant, Vec<_>>::new()));
let results_inner = Rc::clone(&results);
let mut df = dfir_rs::dfir_syntax! {
source_iter([1, 2]) -> [0]my_cross_join_multiset;
source_iter([3, 4]) -> [1]my_cross_join_multiset;
my_cross_join_multiset = cross_join_multiset::<'static, 'static>()
-> for_each(|x| results_inner.borrow_mut().entry(context.current_tick()).or_default().push(x));
};
df.run_tick_sync();
df.run_tick_sync();
df.run_tick_sync();
#[expect(
clippy::disallowed_methods,
reason = "Sorts all vecs regardless of nondeterministic iteration order."
)]
for result_vec in results.borrow_mut().values_mut() {
result_vec.sort_unstable();
}
#[rustfmt::skip]
{
assert_contains_each_by_tick!(results, TickInstant::new(0), &[(1, 3), (1, 4), (2, 3), (2, 4)]);
assert_contains_each_by_tick!(results, TickInstant::new(1), &[(1, 3), (1, 4), (2, 3), (2, 4)]);
assert_contains_each_by_tick!(results, TickInstant::new(2), &[(1, 3), (1, 4), (2, 3), (2, 4)]);
};
}