2018-10-10 00:35:57 +00:00
|
|
|
use crate::implementation::{TestContext, TestContextImpl};
|
2018-10-11 08:53:24 +00:00
|
|
|
use salsa::debug::DebugQueryTable;
|
2018-10-10 00:35:57 +00:00
|
|
|
use salsa::Database;
|
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
#[salsa::query_group]
|
|
|
|
pub(crate) trait ConstantsDatabase: TestContext {
|
|
|
|
#[salsa::input]
|
|
|
|
fn constants_input(&self, key: char) -> usize;
|
|
|
|
|
|
|
|
fn constants_add(&self, keys: (char, char)) -> usize;
|
2018-10-10 00:35:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 08:53:24 +00:00
|
|
|
fn constants_add(db: &impl ConstantsDatabase, (key1, key2): (char, char)) -> usize {
|
2018-10-11 08:56:34 +00:00
|
|
|
db.log().add(format!("add({}, {})", key1, key2));
|
2018-10-11 08:53:24 +00:00
|
|
|
db.constants_input(key1) + db.constants_input(key2)
|
2018-10-10 00:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn invalidate_constant() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 44);
|
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 66);
|
2018-10-10 00:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn invalidate_constant_1() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2018-10-10 00:35:57 +00:00
|
|
|
|
|
|
|
// Not constant:
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set('a', 44);
|
2018-10-10 00:35:57 +00:00
|
|
|
|
|
|
|
// Becomes constant:
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 44);
|
2018-10-10 00:35:57 +00:00
|
|
|
|
|
|
|
// Invalidates:
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 66);
|
2018-10-11 08:53:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:59:33 +00:00
|
|
|
/// Test that invoking `set` on a constant is an error, even if you
|
|
|
|
/// don't change the value.
|
2018-10-11 08:59:07 +00:00
|
|
|
#[test]
|
2018-10-30 16:59:33 +00:00
|
|
|
#[should_panic]
|
2018-10-11 08:59:07 +00:00
|
|
|
fn set_after_constant_same_value() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 44);
|
|
|
|
db.query_mut(ConstantsInputQuery).set('a', 44);
|
2018-10-11 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 08:53:24 +00:00
|
|
|
#[test]
|
|
|
|
fn not_constant() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2018-10-11 08:53:24 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set('a', 22);
|
|
|
|
db.query_mut(ConstantsInputQuery).set('b', 44);
|
2018-10-11 08:53:24 +00:00
|
|
|
assert_eq!(db.constants_add(('a', 'b')), 66);
|
2019-01-12 10:11:59 +00:00
|
|
|
assert!(!db.query(ConstantsAddQuery).is_constant(('a', 'b')));
|
2018-10-11 08:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn is_constant() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2018-10-11 08:53:24 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 22);
|
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('b', 44);
|
2018-10-11 08:53:24 +00:00
|
|
|
assert_eq!(db.constants_add(('a', 'b')), 66);
|
2019-01-12 10:11:59 +00:00
|
|
|
assert!(db.query(ConstantsAddQuery).is_constant(('a', 'b')));
|
2018-10-11 08:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mixed_constant() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2018-10-11 08:53:24 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 22);
|
|
|
|
db.query_mut(ConstantsInputQuery).set('b', 44);
|
2018-10-11 08:53:24 +00:00
|
|
|
assert_eq!(db.constants_add(('a', 'b')), 66);
|
2019-01-12 10:11:59 +00:00
|
|
|
assert!(!db.query(ConstantsAddQuery).is_constant(('a', 'b')));
|
2018-10-11 08:53:24 +00:00
|
|
|
}
|
2018-10-11 08:53:33 +00:00
|
|
|
|
|
|
|
#[test]
|
2018-10-22 13:12:28 +00:00
|
|
|
fn becomes_constant_with_change() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let db = &mut TestContextImpl::default();
|
2018-10-11 08:53:33 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set('a', 22);
|
|
|
|
db.query_mut(ConstantsInputQuery).set('b', 44);
|
2018-10-11 08:53:33 +00:00
|
|
|
assert_eq!(db.constants_add(('a', 'b')), 66);
|
2019-01-12 10:11:59 +00:00
|
|
|
assert!(!db.query(ConstantsAddQuery).is_constant(('a', 'b')));
|
2018-10-11 08:53:33 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('a', 23);
|
2018-10-11 08:53:33 +00:00
|
|
|
assert_eq!(db.constants_add(('a', 'b')), 67);
|
2019-01-12 10:11:59 +00:00
|
|
|
assert!(!db.query(ConstantsAddQuery).is_constant(('a', 'b')));
|
2018-10-11 08:53:33 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(ConstantsInputQuery).set_constant('b', 45);
|
2018-10-11 08:53:33 +00:00
|
|
|
assert_eq!(db.constants_add(('a', 'b')), 68);
|
2019-01-12 10:11:59 +00:00
|
|
|
assert!(db.query(ConstantsAddQuery).is_constant(('a', 'b')));
|
2018-10-10 00:35:57 +00:00
|
|
|
}
|