2018-10-24 09:02:02 +00:00
|
|
|
use crate::db;
|
|
|
|
use crate::group::*;
|
|
|
|
use salsa::debug::DebugQueryTable;
|
2018-10-25 09:47:25 +00:00
|
|
|
use salsa::{Database, SweepStrategy};
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compute_one() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = db::DatabaseImpl::default();
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
// Will compute fibonacci(5)
|
2019-01-25 10:16:57 +00:00
|
|
|
db.set_use_triangular(5, false);
|
2018-10-24 09:02:02 +00:00
|
|
|
db.compute(5);
|
|
|
|
|
|
|
|
db.salsa_runtime().next_revision();
|
|
|
|
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (),
|
|
|
|
FibonacciQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
ComputeQuery => (5),
|
|
|
|
UseTriangularQuery => (5),
|
|
|
|
MinQuery => (),
|
|
|
|
MaxQuery => (),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Memoized, but will compute fibonacci(5) again
|
|
|
|
db.compute(5);
|
2018-12-18 23:40:02 +00:00
|
|
|
db.sweep_all(SweepStrategy::discard_old());
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (),
|
|
|
|
FibonacciQuery => (5),
|
|
|
|
ComputeQuery => (5),
|
|
|
|
UseTriangularQuery => (5),
|
|
|
|
MinQuery => (),
|
|
|
|
MaxQuery => (),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compute_switch() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = db::DatabaseImpl::default();
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
// Will compute fibonacci(5)
|
2019-01-25 10:16:57 +00:00
|
|
|
db.set_use_triangular(5, false);
|
2018-10-24 09:02:02 +00:00
|
|
|
assert_eq!(db.compute(5), 5);
|
|
|
|
|
|
|
|
// Change to triangular mode
|
2019-01-25 10:16:57 +00:00
|
|
|
db.set_use_triangular(5, true);
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
// Now computes triangular(5)
|
|
|
|
assert_eq!(db.compute(5), 15);
|
|
|
|
|
|
|
|
// We still have entries for Fibonacci, even though they
|
|
|
|
// are not relevant to the most recent value of `Compute`
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
FibonacciQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
ComputeQuery => (5),
|
|
|
|
UseTriangularQuery => (5),
|
|
|
|
MinQuery => (),
|
|
|
|
MaxQuery => (),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 23:40:02 +00:00
|
|
|
db.sweep_all(SweepStrategy::discard_old());
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
// Now we just have `Triangular` and not `Fibonacci`
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
FibonacciQuery => (),
|
|
|
|
ComputeQuery => (5),
|
|
|
|
UseTriangularQuery => (5),
|
|
|
|
MinQuery => (),
|
|
|
|
MaxQuery => (),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now run `compute` *again* in next revision.
|
|
|
|
db.salsa_runtime().next_revision();
|
|
|
|
assert_eq!(db.compute(5), 15);
|
2018-12-18 23:40:02 +00:00
|
|
|
db.sweep_all(SweepStrategy::discard_old());
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
// We keep triangular, but just the outermost one.
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (5),
|
|
|
|
FibonacciQuery => (),
|
|
|
|
ComputeQuery => (5),
|
|
|
|
UseTriangularQuery => (5),
|
|
|
|
MinQuery => (),
|
|
|
|
MaxQuery => (),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test a query with multiple layers of keys.
|
|
|
|
#[test]
|
|
|
|
fn compute_all() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = db::DatabaseImpl::default();
|
2018-10-24 09:02:02 +00:00
|
|
|
|
2018-10-30 16:59:33 +00:00
|
|
|
for i in 0..6 {
|
2019-01-25 10:16:57 +00:00
|
|
|
db.set_use_triangular(i, (i % 2) != 0);
|
2018-10-30 16:59:33 +00:00
|
|
|
}
|
2018-10-24 09:02:02 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(MinQuery).set((), 0);
|
|
|
|
db.query_mut(MaxQuery).set((), 6);
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
db.compute_all();
|
|
|
|
db.salsa_runtime().next_revision();
|
|
|
|
db.compute_all();
|
2018-12-18 23:40:02 +00:00
|
|
|
db.sweep_all(SweepStrategy::discard_old());
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (1, 3, 5),
|
|
|
|
FibonacciQuery => (0, 2, 4),
|
|
|
|
ComputeQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
ComputeAllQuery => (()),
|
|
|
|
UseTriangularQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
MinQuery => (()),
|
|
|
|
MaxQuery => (()),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce the range to exclude index 5.
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(MaxQuery).set((), 5);
|
2018-10-24 09:02:02 +00:00
|
|
|
db.compute_all();
|
|
|
|
|
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (1, 3, 5),
|
|
|
|
FibonacciQuery => (0, 2, 4),
|
|
|
|
ComputeQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
ComputeAllQuery => (()),
|
|
|
|
UseTriangularQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
MinQuery => (()),
|
|
|
|
MaxQuery => (()),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 23:40:02 +00:00
|
|
|
db.sweep_all(SweepStrategy::discard_old());
|
2018-10-24 09:02:02 +00:00
|
|
|
|
|
|
|
// We no longer used `Compute(5)` and `Triangular(5)`; note that
|
2019-01-12 10:11:59 +00:00
|
|
|
// `UseTriangularQuery(5)` is not collected, as it is an input.
|
2018-10-24 09:02:02 +00:00
|
|
|
assert_keys! {
|
|
|
|
db,
|
2019-01-12 10:11:59 +00:00
|
|
|
TriangularQuery => (1, 3),
|
|
|
|
FibonacciQuery => (0, 2, 4),
|
|
|
|
ComputeQuery => (0, 1, 2, 3, 4),
|
|
|
|
ComputeAllQuery => (()),
|
|
|
|
UseTriangularQuery => (0, 1, 2, 3, 4, 5),
|
|
|
|
MinQuery => (()),
|
|
|
|
MaxQuery => (()),
|
2018-10-24 09:02:02 +00:00
|
|
|
}
|
|
|
|
}
|