restructure incremental test to permit more tests

This commit is contained in:
Niko Matsakis 2018-09-30 10:34:52 -04:00
parent e2da42d36a
commit f8e7071da2
4 changed files with 73 additions and 78 deletions

View file

@ -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
} }
} }

View file

@ -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() {}

View file

@ -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;
fn memoized1() for Memoized1;
fn volatile() for Volatile;
}
}
impl QueryContextImpl { salsa::query_definition! {
fn assert_log(&self, expected_log: &[&str]) { crate Memoized2(query: &impl MemoizedVolatileContext, (): ()) -> usize {
use difference::{Changeset, Difference}; query.log().add("Memoized2 invoked");
query.memoized1().of(())
}
}
let expected_text = &format!("{:#?}", expected_log); salsa::query_definition! {
let actual_text = &format!("{:#?}", self.log().take()); crate Memoized1(query: &impl MemoizedVolatileContext, (): ()) -> usize {
query.log().add("Memoized1 invoked");
let v = query.volatile().of(());
v / 2
}
}
if expected_text == actual_text { salsa::query_definition! {
return; #[storage(volatile)]
} crate Volatile(query: &impl MemoizedVolatileContext, (): ()) -> usize {
query.log().add("Volatile invoked");
let Changeset { diffs, .. } = Changeset::new(expected_text, actual_text, "\n"); query.clock().increment()
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");
} }
} }
#[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"]);

View file

@ -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()
}
}