salsa/tests/incremental/memoized_inputs.rs

77 lines
1.8 KiB
Rust

use crate::implementation::{TestContext, TestContextImpl};
use salsa::Database;
#[salsa::query_group(MemoizedInputs)]
pub(crate) trait MemoizedInputsContext: TestContext {
fn max(&self) -> usize;
#[salsa::input]
fn input1(&self) -> usize;
#[salsa::input]
fn input2(&self) -> usize;
}
fn max(db: &impl MemoizedInputsContext) -> usize {
db.log().add("Max invoked");
std::cmp::max(db.input1(), db.input2())
}
#[test]
fn revalidate() {
let db = &mut TestContextImpl::default();
db.query_mut(Input1Query).set((), 0);
db.query_mut(Input2Query).set((), 0);
let v = db.max();
assert_eq!(v, 0);
db.assert_log(&["Max invoked"]);
let v = db.max();
assert_eq!(v, 0);
db.assert_log(&[]);
db.query_mut(Input1Query).set((), 44);
db.assert_log(&[]);
let v = db.max();
assert_eq!(v, 44);
db.assert_log(&["Max invoked"]);
let v = db.max();
assert_eq!(v, 44);
db.assert_log(&[]);
db.query_mut(Input1Query).set((), 44);
db.assert_log(&[]);
db.query_mut(Input2Query).set((), 66);
db.assert_log(&[]);
db.query_mut(Input1Query).set((), 64);
db.assert_log(&[]);
let v = db.max();
assert_eq!(v, 66);
db.assert_log(&["Max invoked"]);
let v = db.max();
assert_eq!(v, 66);
db.assert_log(&[]);
}
/// Test that invoking `set` on an input with the same value still
/// triggers a new revision.
#[test]
fn set_after_no_change() {
let db = &mut TestContextImpl::default();
db.query_mut(Input2Query).set((), 0);
db.query_mut(Input1Query).set((), 44);
let v = db.max();
assert_eq!(v, 44);
db.assert_log(&["Max invoked"]);
db.query_mut(Input1Query).set((), 44);
let v = db.max();
assert_eq!(v, 44);
db.assert_log(&["Max invoked"]);
}