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::log::Log;
use crate::queries;
use crate::memoized_volatile;
crate trait TestContext: salsa::QueryContext {
fn clock(&self) -> &Counter;
fn log(&self) -> &Log;
}
#[derive(Default)]
pub struct QueryContextImpl {
runtime: salsa::runtime::Runtime<QueryContextImpl>,
crate struct TestContextImpl {
runtime: salsa::runtime::Runtime<TestContextImpl>,
clock: Counter,
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! {
pub struct QueryContextImplStorage for QueryContextImpl {
impl queries::QueryContext {
fn memoized2() for queries::Memoized2;
fn memoized1() for queries::Memoized1;
fn volatile() for queries::Volatile;
crate struct TestContextImplStorage for TestContextImpl {
impl memoized_volatile::MemoizedVolatileContext {
fn memoized2() for memoized_volatile::Memoized2;
fn memoized1() for memoized_volatile::Memoized1;
fn volatile() for memoized_volatile::Volatile;
}
}
}
impl queries::CounterContext for QueryContextImpl {
impl TestContext for TestContextImpl {
fn clock(&self) -> &Counter {
&self.clock
}
@ -29,8 +59,8 @@ impl queries::CounterContext for QueryContextImpl {
}
}
impl salsa::QueryContext for QueryContextImpl {
fn salsa_runtime(&self) -> &salsa::runtime::Runtime<QueryContextImpl> {
impl salsa::QueryContext for TestContextImpl {
fn salsa_runtime(&self) -> &salsa::runtime::Runtime<TestContextImpl> {
&self.runtime
}
}

View file

@ -4,7 +4,6 @@
mod counter;
mod implementation;
mod log;
mod queries;
mod tests;
mod memoized_volatile;
fn main() {}

View file

@ -1,38 +1,42 @@
#![cfg(test)]
use crate::implementation::{TestContext, TestContextImpl};
use salsa::QueryContext;
use crate::implementation::QueryContextImpl;
use crate::queries::CounterContext;
use crate::queries::QueryContext as _;
use salsa::QueryContext as _;
crate trait MemoizedVolatileContext: TestContext {
salsa::query_prototype! {
// Queries for testing a "volatile" value wrapped by
// memoization.
fn memoized2() for Memoized2;
fn memoized1() for Memoized1;
fn volatile() for Volatile;
}
}
impl QueryContextImpl {
fn assert_log(&self, expected_log: &[&str]) {
use difference::{Changeset, Difference};
salsa::query_definition! {
crate Memoized2(query: &impl MemoizedVolatileContext, (): ()) -> usize {
query.log().add("Memoized2 invoked");
query.memoized1().of(())
}
}
let expected_text = &format!("{:#?}", expected_log);
let actual_text = &format!("{:#?}", self.log().take());
salsa::query_definition! {
crate Memoized1(query: &impl MemoizedVolatileContext, (): ()) -> usize {
query.log().add("Memoized1 invoked");
let v = query.volatile().of(());
v / 2
}
}
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_definition! {
#[storage(volatile)]
crate Volatile(query: &impl MemoizedVolatileContext, (): ()) -> usize {
query.log().add("Volatile invoked");
query.clock().increment()
}
}
#[test]
fn volatile_x2() {
let query = QueryContextImpl::default();
let query = TestContextImpl::default();
// Invoking volatile twice will simply execute twice.
query.volatile().of(());
@ -52,7 +56,7 @@ fn volatile_x2() {
fn revalidate() {
env_logger::init();
let query = QueryContextImpl::default();
let query = TestContextImpl::default();
query.memoized2().of(());
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()
}
}