mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-15 17:49:46 +00:00
1e3c2f22aa
Co-authored-by: Tim Robinson <tim.g.robinson@gmail.com>
86 lines
1.8 KiB
Rust
86 lines
1.8 KiB
Rust
use crate::implementation::{TestContext, TestContextImpl};
|
|
|
|
#[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: &dyn MemoizedInputsContext) -> usize {
|
|
db.log().add("Max invoked");
|
|
std::cmp::max(db.input1(), db.input2())
|
|
}
|
|
|
|
#[test]
|
|
fn revalidate() {
|
|
let db = &mut TestContextImpl::default();
|
|
|
|
db.set_input1(0);
|
|
db.set_input2(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.set_input1(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.set_input1(44);
|
|
db.assert_log(&[]);
|
|
db.set_input2(66);
|
|
db.assert_log(&[]);
|
|
db.set_input1(64);
|
|
db.assert_log(&[]);
|
|
|
|
let value = db.remove_input1() + 1;
|
|
db.set_input1(value);
|
|
db.assert_log(&[]);
|
|
let value = db.remove_input2() + 1;
|
|
db.set_input2(value);
|
|
db.assert_log(&[]);
|
|
let value = db.remove_input1() + 1;
|
|
db.set_input1(value);
|
|
db.assert_log(&[]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 67);
|
|
db.assert_log(&["Max invoked"]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 67);
|
|
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.set_input2(0);
|
|
|
|
db.set_input1(44);
|
|
let v = db.max();
|
|
assert_eq!(v, 44);
|
|
db.assert_log(&["Max invoked"]);
|
|
|
|
db.set_input1(44);
|
|
let v = db.max();
|
|
assert_eq!(v, 44);
|
|
db.assert_log(&["Max invoked"]);
|
|
}
|