salsa/tests/incremental/memoized_inputs.rs

78 lines
1.7 KiB
Rust
Raw Normal View History

2018-09-30 14:50:46 +00:00
use crate::implementation::{TestContext, TestContextImpl};
use salsa::Database;
2018-09-30 14:50:46 +00:00
#[salsa::query_group]
pub(crate) trait MemoizedInputsContext: TestContext {
fn max(&self) -> usize;
#[salsa::input]
fn input1(&self) -> usize;
#[salsa::input]
fn input2(&self) -> usize;
2018-09-30 14:50:46 +00:00
}
2018-10-19 01:26:48 +00:00
fn max(db: &impl MemoizedInputsContext) -> usize {
2018-10-05 14:30:17 +00:00
db.log().add("Max invoked");
2018-10-19 01:26:48 +00:00
std::cmp::max(db.input1(), db.input2())
2018-09-30 14:50:46 +00:00
}
#[test]
fn revalidate() {
let db = &mut TestContextImpl::default();
2018-09-30 14:50:46 +00:00
db.query_mut(Input1Query).set((), 0);
db.query_mut(Input2Query).set((), 0);
2018-10-30 16:59:33 +00:00
2018-10-19 01:26:48 +00:00
let v = db.max();
2018-09-30 14:50:46 +00:00
assert_eq!(v, 0);
db.assert_log(&["Max invoked"]);
2018-09-30 14:50:46 +00:00
2018-10-19 01:26:48 +00:00
let v = db.max();
2018-09-30 14:50:46 +00:00
assert_eq!(v, 0);
db.assert_log(&[]);
2018-09-30 14:50:46 +00:00
db.query_mut(Input1Query).set((), 44);
db.assert_log(&[]);
2018-09-30 14:50:46 +00:00
2018-10-19 01:26:48 +00:00
let v = db.max();
2018-09-30 14:50:46 +00:00
assert_eq!(v, 44);
db.assert_log(&["Max invoked"]);
2018-09-30 14:50:46 +00:00
2018-10-19 01:26:48 +00:00
let v = db.max();
2018-09-30 14:50:46 +00:00
assert_eq!(v, 44);
db.assert_log(&[]);
2018-09-30 14:50:46 +00:00
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(&[]);
2018-09-30 14:50:46 +00:00
2018-10-19 01:26:48 +00:00
let v = db.max();
2018-09-30 14:50:46 +00:00
assert_eq!(v, 66);
db.assert_log(&["Max invoked"]);
2018-09-30 14:50:46 +00:00
2018-10-19 01:26:48 +00:00
let v = db.max();
2018-09-30 14:50:46 +00:00
assert_eq!(v, 66);
db.assert_log(&[]);
2018-09-30 14:50:46 +00:00
}
2018-10-30 16:59:33 +00:00
/// 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);
2018-10-30 16:59:33 +00:00
db.query_mut(Input1Query).set((), 44);
2018-10-19 01:26:48 +00:00
let v = db.max();
assert_eq!(v, 44);
db.assert_log(&["Max invoked"]);
db.query_mut(Input1Query).set((), 44);
2018-10-19 01:26:48 +00:00
let v = db.max();
assert_eq!(v, 44);
2018-10-30 16:59:33 +00:00
db.assert_log(&["Max invoked"]);
}