From f8e7071da2ba8b1a84733e9ce7ad85348dfcbb68 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 30 Sep 2018 10:34:52 -0400 Subject: [PATCH] restructure incremental test to permit more tests --- tests/incremental/implementation.rs | 52 +++++++++++++---- tests/incremental/main.rs | 3 +- .../{tests.rs => memoized_volatile.rs} | 58 ++++++++++--------- tests/incremental/queries.rs | 38 ------------ 4 files changed, 73 insertions(+), 78 deletions(-) rename tests/incremental/{tests.rs => memoized_volatile.rs} (59%) delete mode 100644 tests/incremental/queries.rs diff --git a/tests/incremental/implementation.rs b/tests/incremental/implementation.rs index c89d060b..41ea5be0 100644 --- a/tests/incremental/implementation.rs +++ b/tests/incremental/implementation.rs @@ -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, +crate struct TestContextImpl { + runtime: salsa::runtime::Runtime, 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 { +impl salsa::QueryContext for TestContextImpl { + fn salsa_runtime(&self) -> &salsa::runtime::Runtime { &self.runtime } } diff --git a/tests/incremental/main.rs b/tests/incremental/main.rs index 38095b91..27092b9f 100644 --- a/tests/incremental/main.rs +++ b/tests/incremental/main.rs @@ -4,7 +4,6 @@ mod counter; mod implementation; mod log; -mod queries; -mod tests; +mod memoized_volatile; fn main() {} diff --git a/tests/incremental/tests.rs b/tests/incremental/memoized_volatile.rs similarity index 59% rename from tests/incremental/tests.rs rename to tests/incremental/memoized_volatile.rs index f4cbacb6..cddd4a17 100644 --- a/tests/incremental/tests.rs +++ b/tests/incremental/memoized_volatile.rs @@ -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"]); diff --git a/tests/incremental/queries.rs b/tests/incremental/queries.rs deleted file mode 100644 index 328adfbd..00000000 --- a/tests/incremental/queries.rs +++ /dev/null @@ -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() - } -}