2024-07-20 06:47:51 +00:00
|
|
|
mod common;
|
|
|
|
|
2024-08-04 05:33:58 +00:00
|
|
|
use common::LogDatabase;
|
2024-07-20 06:47:51 +00:00
|
|
|
use expect_test::expect;
|
2024-08-04 05:33:58 +00:00
|
|
|
use salsa::Setter as _;
|
2024-07-20 06:47:51 +00:00
|
|
|
|
|
|
|
#[salsa::input]
|
|
|
|
struct Input {
|
|
|
|
number: i16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(no_eq)]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn abs_float(db: &dyn LogDatabase, input: Input) -> f32 {
|
2024-07-20 06:47:51 +00:00
|
|
|
let number = input.number(db);
|
|
|
|
|
|
|
|
db.push_log(format!("abs_float({number})"));
|
|
|
|
number.abs() as f32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn derived(db: &dyn LogDatabase, input: Input) -> u32 {
|
2024-07-20 06:47:51 +00:00
|
|
|
let x = abs_float(db, input);
|
|
|
|
db.push_log("derived".to_string());
|
|
|
|
|
|
|
|
x as u32
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn invoke() {
|
2024-08-04 05:33:58 +00:00
|
|
|
let mut db = common::LoggerDatabase::default();
|
2024-07-20 06:47:51 +00:00
|
|
|
|
|
|
|
let input = Input::new(&db, 5);
|
|
|
|
let x = derived(&db, input);
|
|
|
|
|
|
|
|
assert_eq!(x, 5);
|
|
|
|
|
|
|
|
input.set_number(&mut db).to(-5);
|
|
|
|
|
|
|
|
// Derived should re-execute even the result of `abs_float` is the same.
|
|
|
|
let x = derived(&db, input);
|
|
|
|
assert_eq!(x, 5);
|
|
|
|
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
|
|
|
"abs_float(5)",
|
|
|
|
"derived",
|
|
|
|
"abs_float(-5)",
|
|
|
|
"derived",
|
|
|
|
]"#]]);
|
|
|
|
}
|