salsa/tests/incremental/memoized_inputs.rs
Fabian Schuiki 93c30a953d make query_group macro procedural
Switch to a procedural implementation of the `query_group!` macro,
residing in the `components/salsa_macros` subcrate.

Allow the user to override the invoked function via `salsa::invoke(...)`
and the name of the generated query type via `salsa::query_type(...)`.

In all tests, replace the `salsa::query_group! { ... }` invocations with
the new attribute-style `#[salsa::query_group]` macro, and change them
to the new naming scheme for query types (`...Query`).

Update README, examples, and documentation.
2019-01-17 07:24:18 +01:00

77 lines
1.7 KiB
Rust

use crate::implementation::{TestContext, TestContextImpl};
use salsa::Database;
#[salsa::query_group]
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"]);
}