mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-13 00:40:22 +00:00
restructure incremental test to permit more tests
This commit is contained in:
parent
e2da42d36a
commit
f8e7071da2
4 changed files with 73 additions and 78 deletions
|
@ -1,25 +1,55 @@
|
||||||
use crate::counter::Counter;
|
use crate::counter::Counter;
|
||||||
use crate::log::Log;
|
use crate::log::Log;
|
||||||
use crate::queries;
|
use crate::memoized_volatile;
|
||||||
|
|
||||||
|
crate trait TestContext: salsa::QueryContext {
|
||||||
|
fn clock(&self) -> &Counter;
|
||||||
|
fn log(&self) -> &Log;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct QueryContextImpl {
|
crate struct TestContextImpl {
|
||||||
runtime: salsa::runtime::Runtime<QueryContextImpl>,
|
runtime: salsa::runtime::Runtime<TestContextImpl>,
|
||||||
clock: Counter,
|
clock: Counter,
|
||||||
log: Log,
|
log: Log,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TestContextImpl {
|
||||||
|
crate fn assert_log(&self, expected_log: &[&str]) {
|
||||||
|
use difference::{Changeset, Difference};
|
||||||
|
|
||||||
|
let expected_text = &format!("{:#?}", expected_log);
|
||||||
|
let actual_text = &format!("{:#?}", self.log().take());
|
||||||
|
|
||||||
|
if expected_text == actual_text {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Changeset { diffs, .. } = Changeset::new(expected_text, actual_text, "\n");
|
||||||
|
|
||||||
|
for i in 0..diffs.len() {
|
||||||
|
match &diffs[i] {
|
||||||
|
Difference::Same(x) => println!(" {}", x),
|
||||||
|
Difference::Add(x) => println!("+{}", x),
|
||||||
|
Difference::Rem(x) => println!("-{}", x),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic!("incorrect log results");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
salsa::query_context_storage! {
|
salsa::query_context_storage! {
|
||||||
pub struct QueryContextImplStorage for QueryContextImpl {
|
crate struct TestContextImplStorage for TestContextImpl {
|
||||||
impl queries::QueryContext {
|
impl memoized_volatile::MemoizedVolatileContext {
|
||||||
fn memoized2() for queries::Memoized2;
|
fn memoized2() for memoized_volatile::Memoized2;
|
||||||
fn memoized1() for queries::Memoized1;
|
fn memoized1() for memoized_volatile::Memoized1;
|
||||||
fn volatile() for queries::Volatile;
|
fn volatile() for memoized_volatile::Volatile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl queries::CounterContext for QueryContextImpl {
|
impl TestContext for TestContextImpl {
|
||||||
fn clock(&self) -> &Counter {
|
fn clock(&self) -> &Counter {
|
||||||
&self.clock
|
&self.clock
|
||||||
}
|
}
|
||||||
|
@ -29,8 +59,8 @@ impl queries::CounterContext for QueryContextImpl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl salsa::QueryContext for QueryContextImpl {
|
impl salsa::QueryContext for TestContextImpl {
|
||||||
fn salsa_runtime(&self) -> &salsa::runtime::Runtime<QueryContextImpl> {
|
fn salsa_runtime(&self) -> &salsa::runtime::Runtime<TestContextImpl> {
|
||||||
&self.runtime
|
&self.runtime
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
mod counter;
|
mod counter;
|
||||||
mod implementation;
|
mod implementation;
|
||||||
mod log;
|
mod log;
|
||||||
mod queries;
|
mod memoized_volatile;
|
||||||
mod tests;
|
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -1,38 +1,42 @@
|
||||||
#![cfg(test)]
|
use crate::implementation::{TestContext, TestContextImpl};
|
||||||
|
use salsa::QueryContext;
|
||||||
|
|
||||||
use crate::implementation::QueryContextImpl;
|
crate trait MemoizedVolatileContext: TestContext {
|
||||||
use crate::queries::CounterContext;
|
salsa::query_prototype! {
|
||||||
use crate::queries::QueryContext as _;
|
// Queries for testing a "volatile" value wrapped by
|
||||||
use salsa::QueryContext as _;
|
// memoization.
|
||||||
|
fn memoized2() for Memoized2;
|
||||||
impl QueryContextImpl {
|
fn memoized1() for Memoized1;
|
||||||
fn assert_log(&self, expected_log: &[&str]) {
|
fn volatile() for Volatile;
|
||||||
use difference::{Changeset, Difference};
|
|
||||||
|
|
||||||
let expected_text = &format!("{:#?}", expected_log);
|
|
||||||
let actual_text = &format!("{:#?}", self.log().take());
|
|
||||||
|
|
||||||
if expected_text == actual_text {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let Changeset { diffs, .. } = Changeset::new(expected_text, actual_text, "\n");
|
salsa::query_definition! {
|
||||||
|
crate Memoized2(query: &impl MemoizedVolatileContext, (): ()) -> usize {
|
||||||
for i in 0..diffs.len() {
|
query.log().add("Memoized2 invoked");
|
||||||
match &diffs[i] {
|
query.memoized1().of(())
|
||||||
Difference::Same(x) => println!(" {}", x),
|
|
||||||
Difference::Add(x) => println!("+{}", x),
|
|
||||||
Difference::Rem(x) => println!("-{}", x),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
panic!("incorrect log results");
|
salsa::query_definition! {
|
||||||
|
crate Memoized1(query: &impl MemoizedVolatileContext, (): ()) -> usize {
|
||||||
|
query.log().add("Memoized1 invoked");
|
||||||
|
let v = query.volatile().of(());
|
||||||
|
v / 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
salsa::query_definition! {
|
||||||
|
#[storage(volatile)]
|
||||||
|
crate Volatile(query: &impl MemoizedVolatileContext, (): ()) -> usize {
|
||||||
|
query.log().add("Volatile invoked");
|
||||||
|
query.clock().increment()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn volatile_x2() {
|
fn volatile_x2() {
|
||||||
let query = QueryContextImpl::default();
|
let query = TestContextImpl::default();
|
||||||
|
|
||||||
// Invoking volatile twice will simply execute twice.
|
// Invoking volatile twice will simply execute twice.
|
||||||
query.volatile().of(());
|
query.volatile().of(());
|
||||||
|
@ -52,7 +56,7 @@ fn volatile_x2() {
|
||||||
fn revalidate() {
|
fn revalidate() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let query = QueryContextImpl::default();
|
let query = TestContextImpl::default();
|
||||||
|
|
||||||
query.memoized2().of(());
|
query.memoized2().of(());
|
||||||
query.assert_log(&["Memoized2 invoked", "Memoized1 invoked", "Volatile invoked"]);
|
query.assert_log(&["Memoized2 invoked", "Memoized1 invoked", "Volatile invoked"]);
|
|
@ -1,38 +0,0 @@
|
||||||
use crate::counter::Counter;
|
|
||||||
use crate::log::Log;
|
|
||||||
|
|
||||||
crate trait CounterContext: salsa::QueryContext {
|
|
||||||
fn clock(&self) -> &Counter;
|
|
||||||
fn log(&self) -> &Log;
|
|
||||||
}
|
|
||||||
|
|
||||||
crate trait QueryContext: CounterContext {
|
|
||||||
salsa::query_prototype! {
|
|
||||||
fn memoized2() for Memoized2;
|
|
||||||
fn memoized1() for Memoized1;
|
|
||||||
fn volatile() for Volatile;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
salsa::query_definition! {
|
|
||||||
crate Memoized2(query: &impl QueryContext, (): ()) -> usize {
|
|
||||||
query.log().add("Memoized2 invoked");
|
|
||||||
query.memoized1().of(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
salsa::query_definition! {
|
|
||||||
crate Memoized1(query: &impl QueryContext, (): ()) -> usize {
|
|
||||||
query.log().add("Memoized1 invoked");
|
|
||||||
let v = query.volatile().of(());
|
|
||||||
v / 2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
salsa::query_definition! {
|
|
||||||
#[storage(volatile)]
|
|
||||||
crate Volatile(query: &impl QueryContext, (): ()) -> usize {
|
|
||||||
query.log().add("Volatile invoked");
|
|
||||||
query.clock().increment()
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue