2024-05-16 09:18:56 +00:00
|
|
|
//! Test that a `tracked` fn on a `salsa::input`
|
|
|
|
//! compiles and executes successfully.
|
|
|
|
|
|
|
|
use expect_test::expect;
|
2024-10-10 00:45:33 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-05-16 09:18:56 +00:00
|
|
|
use test_log::test;
|
|
|
|
|
|
|
|
#[salsa::interned]
|
|
|
|
struct InternedString<'db> {
|
|
|
|
data: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::interned]
|
|
|
|
struct InternedPair<'db> {
|
|
|
|
data: (InternedString<'db>, InternedString<'db>),
|
|
|
|
}
|
|
|
|
|
2024-10-04 04:41:20 +00:00
|
|
|
#[salsa::interned]
|
|
|
|
struct InternedTwoFields<'db> {
|
|
|
|
data1: String,
|
|
|
|
data2: String,
|
|
|
|
}
|
|
|
|
|
2024-10-04 11:37:40 +00:00
|
|
|
#[salsa::interned]
|
|
|
|
struct InternedVec<'db> {
|
|
|
|
data1: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::interned]
|
|
|
|
struct InternedPathBuf<'db> {
|
|
|
|
data1: PathBuf,
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:18:56 +00:00
|
|
|
#[salsa::tracked]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn intern_stuff(db: &dyn salsa::Database) -> String {
|
2024-05-30 12:23:42 +00:00
|
|
|
let s1 = InternedString::new(db, "Hello, ".to_string());
|
2024-10-04 04:41:20 +00:00
|
|
|
let s2 = InternedString::new(db, "World, ");
|
2024-05-16 09:18:56 +00:00
|
|
|
let s3 = InternedPair::new(db, (s1, s2));
|
2024-10-04 04:41:20 +00:00
|
|
|
|
2024-07-17 09:47:56 +00:00
|
|
|
format!("{s3:?}")
|
2024-05-16 09:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute() {
|
2024-07-27 12:29:41 +00:00
|
|
|
let db = salsa::DatabaseImpl::new();
|
2024-05-16 09:18:56 +00:00
|
|
|
expect![[r#"
|
2024-07-17 09:47:56 +00:00
|
|
|
"InternedPair { data: (InternedString { data: \"Hello, \" }, InternedString { data: \"World, \" }) }"
|
2024-05-16 09:18:56 +00:00
|
|
|
"#]].assert_debug_eq(&intern_stuff(&db));
|
|
|
|
}
|
2024-10-04 04:41:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn interning_returns_equal_keys_for_equal_data() {
|
|
|
|
let db = salsa::DatabaseImpl::new();
|
|
|
|
let s1 = InternedString::new(&db, "Hello, ".to_string());
|
|
|
|
let s2 = InternedString::new(&db, "World, ".to_string());
|
|
|
|
let s1_2 = InternedString::new(&db, "Hello, ");
|
|
|
|
let s2_2 = InternedString::new(&db, "World, ");
|
|
|
|
assert_eq!(s1, s1_2);
|
|
|
|
assert_eq!(s2, s2_2);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn interning_returns_equal_keys_for_equal_data_multi_field() {
|
|
|
|
let db = salsa::DatabaseImpl::new();
|
|
|
|
let s1 = InternedTwoFields::new(&db, "Hello, ".to_string(), "World");
|
|
|
|
let s2 = InternedTwoFields::new(&db, "World, ", "Hello".to_string());
|
|
|
|
let s1_2 = InternedTwoFields::new(&db, "Hello, ", "World");
|
|
|
|
let s2_2 = InternedTwoFields::new(&db, "World, ", "Hello");
|
2024-10-04 11:37:40 +00:00
|
|
|
let new = InternedTwoFields::new(&db, "Hello, World", "");
|
|
|
|
|
2024-10-04 04:41:20 +00:00
|
|
|
assert_eq!(s1, s1_2);
|
|
|
|
assert_eq!(s2, s2_2);
|
2024-10-04 11:37:40 +00:00
|
|
|
assert_ne!(s1, s2_2);
|
|
|
|
assert_ne!(s1, new);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn interning_vec() {
|
|
|
|
let db = salsa::DatabaseImpl::new();
|
|
|
|
let s1 = InternedVec::new(&db, ["Hello, ".to_string(), "World".to_string()].as_slice());
|
|
|
|
let s2 = InternedVec::new(&db, ["Hello, ", "World"].as_slice());
|
|
|
|
let s3 = InternedVec::new(&db, vec!["Hello, ".to_string(), "World".to_string()]);
|
|
|
|
let s4 = InternedVec::new(&db, ["Hello, ", "World"].as_slice());
|
|
|
|
let s5 = InternedVec::new(&db, ["Hello, ", "World", "Test"].as_slice());
|
|
|
|
let s6 = InternedVec::new(&db, ["Hello, ", "World", ""].as_slice());
|
|
|
|
let s7 = InternedVec::new(&db, ["Hello, "].as_slice());
|
|
|
|
assert_eq!(s1, s2);
|
|
|
|
assert_eq!(s1, s3);
|
|
|
|
assert_eq!(s1, s4);
|
|
|
|
assert_ne!(s1, s5);
|
|
|
|
assert_ne!(s1, s6);
|
|
|
|
assert_ne!(s5, s6);
|
|
|
|
assert_ne!(s6, s7);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn interning_path_buf() {
|
|
|
|
let db = salsa::DatabaseImpl::new();
|
|
|
|
let s1 = InternedPathBuf::new(&db, PathBuf::from("test_path".to_string()));
|
|
|
|
let s2 = InternedPathBuf::new(&db, Path::new("test_path"));
|
|
|
|
let s3 = InternedPathBuf::new(&db, Path::new("test_path/"));
|
|
|
|
let s4 = InternedPathBuf::new(&db, Path::new("test_path/a"));
|
|
|
|
assert_eq!(s1, s2);
|
|
|
|
assert_eq!(s1, s3);
|
|
|
|
assert_ne!(s1, s4);
|
2024-10-04 04:41:20 +00:00
|
|
|
}
|