add simple test for SweepStrategy::discard_values()

This commit is contained in:
Niko Matsakis 2018-10-25 08:41:26 -04:00
parent c21ea47cfc
commit a8e1e47a59
5 changed files with 111 additions and 1 deletions

View file

@ -1,8 +1,10 @@
use crate::group;
use crate::log::{HasLog, Log};
#[derive(Default)]
pub struct DatabaseImpl {
runtime: salsa::Runtime<DatabaseImpl>,
log: Log,
}
impl salsa::Database for DatabaseImpl {
@ -24,3 +26,34 @@ salsa::database_storage! {
}
}
}
impl DatabaseImpl {
pub(crate) fn clear_log(&self) {
self.log().take();
}
pub(crate) fn assert_log(&self, expected_log: &[&str]) {
let expected_text = &format!("{:#?}", expected_log);
let actual_text = &format!("{:#?}", self.log().take());
if expected_text == actual_text {
return;
}
for diff in diff::lines(expected_text, actual_text) {
match diff {
diff::Result::Left(l) => println!("-{}", l),
diff::Result::Both(l, _) => println!(" {}", l),
diff::Result::Right(r) => println!("+{}", r),
}
}
panic!("incorrect log results");
}
}
impl HasLog for DatabaseImpl {
fn log(&self) -> &Log {
&self.log
}
}

View file

@ -0,0 +1,49 @@
use crate::db;
use crate::group::{Fibonacci, GcDatabase};
use salsa::debug::DebugQueryTable;
use salsa::{Database, SweepStrategy};
#[test]
fn sweep_default() {
let db = db::DatabaseImpl::default();
db.fibonacci(5);
let k: Vec<_> = db.query(Fibonacci).keys();
assert_eq!(k.len(), 6);
db.salsa_runtime().next_revision();
db.fibonacci(5);
db.fibonacci(3);
// fibonacci is a constant, so it will not be invalidated,
// hence we keep 3 and 5 but remove the rest.
db.sweep_all(SweepStrategy::default());
let mut k: Vec<_> = db.query(Fibonacci).keys();
k.sort();
assert_eq!(k, vec![3, 5]);
// Even though we ran the sweep, 5 is still in cache
db.clear_log();
db.fibonacci(5);
db.assert_log(&[]);
// Same but we discard values this time.
db.sweep_all(SweepStrategy::default().discard_values());
let mut k: Vec<_> = db.query(Fibonacci).keys();
k.sort();
assert_eq!(k, vec![3, 5]);
// Now we have to recompute
db.clear_log();
db.fibonacci(5);
db.assert_log(&[
"fibonacci(5)",
"fibonacci(4)",
"fibonacci(3)",
"fibonacci(2)",
"fibonacci(1)",
"fibonacci(0)",
]);
}

View file

@ -1,5 +1,7 @@
use crate::log::HasLog;
salsa::query_group! {
pub(crate) trait GcDatabase: salsa::Database {
pub(crate) trait GcDatabase: salsa::Database + HasLog {
fn min() -> usize {
type Min;
storage input;
@ -34,6 +36,7 @@ salsa::query_group! {
}
fn fibonacci(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("fibonacci({:?})", key));
if key == 0 {
0
} else if key == 1 {
@ -44,6 +47,7 @@ fn fibonacci(db: &impl GcDatabase, key: usize) -> usize {
}
fn triangular(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("triangular({:?})", key));
if key == 0 {
0
} else {
@ -52,6 +56,7 @@ fn triangular(db: &impl GcDatabase, key: usize) -> usize {
}
fn compute(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("compute({:?})", key));
if db.use_triangular(key) {
db.triangular(key)
} else {
@ -60,5 +65,6 @@ fn compute(db: &impl GcDatabase, key: usize) -> usize {
}
fn compute_all(db: &impl GcDatabase) -> Vec<usize> {
db.log().add("compute_all()");
(db.min()..db.max()).map(|v| db.compute(v)).collect()
}

20
tests/gc/log.rs Normal file
View file

@ -0,0 +1,20 @@
use std::cell::RefCell;
pub(crate) trait HasLog {
fn log(&self) -> &Log;
}
#[derive(Default)]
pub(crate) struct Log {
data: RefCell<Vec<String>>,
}
impl Log {
pub(crate) fn add(&self, text: impl Into<String>) {
self.data.borrow_mut().push(text.into());
}
pub(crate) fn take(&self) -> Vec<String> {
std::mem::replace(&mut *self.data.borrow_mut(), vec![])
}
}

View file

@ -1,4 +1,6 @@
mod db;
mod derived_tests;
mod discard_values;
mod group;
mod log;
mod shallow_constant_tests;