215 lines
5.5 KiB
Rust
215 lines
5.5 KiB
Rust
use tst::TST;
|
|
use std::env::temp_dir;
|
|
use std::fs;
|
|
use std::time::SystemTime;
|
|
|
|
fn get_test_db_path() -> String {
|
|
let timestamp = SystemTime::now()
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_secs();
|
|
|
|
let path = temp_dir().join(format!("tst_prefix_test_{}", timestamp));
|
|
fs::create_dir_all(&path).unwrap();
|
|
|
|
path.to_string_lossy().to_string()
|
|
}
|
|
|
|
fn cleanup_test_db(path: &str) {
|
|
let _ = fs::remove_dir_all(path);
|
|
}
|
|
|
|
#[test]
|
|
fn test_prefix_with_common_prefixes() {
|
|
let path = get_test_db_path();
|
|
|
|
let mut tree = TST::new(&path, true).unwrap();
|
|
|
|
// Insert keys with common prefixes
|
|
let test_data = [
|
|
("test", b"value1".to_vec()),
|
|
("testing", b"value2".to_vec()),
|
|
("tested", b"value3".to_vec()),
|
|
("tests", b"value4".to_vec()),
|
|
("tester", b"value5".to_vec()),
|
|
];
|
|
|
|
for (key, value) in &test_data {
|
|
tree.set(key, value.clone()).unwrap();
|
|
}
|
|
|
|
// Test prefix "test"
|
|
let keys = tree.list("test").unwrap();
|
|
assert_eq!(keys.len(), 5);
|
|
|
|
for (key, _) in &test_data {
|
|
assert!(keys.contains(&key.to_string()));
|
|
}
|
|
|
|
// Test prefix "teste"
|
|
let keys = tree.list("teste").unwrap();
|
|
assert_eq!(keys.len(), 2);
|
|
assert!(keys.contains(&"tested".to_string()));
|
|
assert!(keys.contains(&"tester".to_string()));
|
|
|
|
cleanup_test_db(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn test_prefix_with_different_prefixes() {
|
|
let path = get_test_db_path();
|
|
|
|
let mut tree = TST::new(&path, true).unwrap();
|
|
|
|
// Insert keys with different prefixes
|
|
let test_data = [
|
|
("apple", b"fruit1".to_vec()),
|
|
("banana", b"fruit2".to_vec()),
|
|
("cherry", b"fruit3".to_vec()),
|
|
("date", b"fruit4".to_vec()),
|
|
("elderberry", b"fruit5".to_vec()),
|
|
];
|
|
|
|
for (key, value) in &test_data {
|
|
tree.set(key, value.clone()).unwrap();
|
|
}
|
|
|
|
// Test each prefix
|
|
for (key, _) in &test_data {
|
|
let prefix = &key[0..1]; // First character
|
|
let keys = tree.list(prefix).unwrap();
|
|
assert!(keys.contains(&key.to_string()));
|
|
}
|
|
|
|
// Test non-existent prefix
|
|
let keys = tree.list("z").unwrap();
|
|
assert_eq!(keys.len(), 0);
|
|
|
|
cleanup_test_db(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn test_prefix_with_empty_string() {
|
|
let path = get_test_db_path();
|
|
|
|
let mut tree = TST::new(&path, true).unwrap();
|
|
|
|
// Insert some keys
|
|
let test_data = [
|
|
("apple", b"fruit1".to_vec()),
|
|
("banana", b"fruit2".to_vec()),
|
|
("cherry", b"fruit3".to_vec()),
|
|
];
|
|
|
|
for (key, value) in &test_data {
|
|
tree.set(key, value.clone()).unwrap();
|
|
}
|
|
|
|
// Test empty prefix (should return all keys)
|
|
let keys = tree.list("").unwrap();
|
|
assert_eq!(keys.len(), test_data.len());
|
|
|
|
for (key, _) in &test_data {
|
|
assert!(keys.contains(&key.to_string()));
|
|
}
|
|
|
|
cleanup_test_db(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn test_getall_with_prefix() {
|
|
let path = get_test_db_path();
|
|
|
|
let mut tree = TST::new(&path, true).unwrap();
|
|
|
|
// Insert keys with common prefixes
|
|
let test_data = [
|
|
("test", b"value1".to_vec()),
|
|
("testing", b"value2".to_vec()),
|
|
("tested", b"value3".to_vec()),
|
|
("tests", b"value4".to_vec()),
|
|
("tester", b"value5".to_vec()),
|
|
];
|
|
|
|
for (key, value) in &test_data {
|
|
tree.set(key, value.clone()).unwrap();
|
|
}
|
|
|
|
// Test getall with prefix "test"
|
|
let values = tree.getall("test").unwrap();
|
|
assert_eq!(values.len(), 5);
|
|
|
|
for (_, value) in &test_data {
|
|
assert!(values.contains(value));
|
|
}
|
|
|
|
cleanup_test_db(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn test_prefix_with_unicode_characters() {
|
|
let path = get_test_db_path();
|
|
|
|
let mut tree = TST::new(&path, true).unwrap();
|
|
|
|
// Insert keys with Unicode characters
|
|
let test_data = [
|
|
("café", b"coffee".to_vec()),
|
|
("cafétéria", b"cafeteria".to_vec()),
|
|
("caffè", b"italian coffee".to_vec()),
|
|
("café au lait", b"coffee with milk".to_vec()),
|
|
];
|
|
|
|
for (key, value) in &test_data {
|
|
tree.set(key, value.clone()).unwrap();
|
|
}
|
|
|
|
// Test prefix "café"
|
|
let keys = tree.list("café").unwrap();
|
|
assert_eq!(keys.len(), 2);
|
|
assert!(keys.contains(&"café".to_string()));
|
|
assert!(keys.contains(&"café au lait".to_string()));
|
|
|
|
// Test prefix "caf"
|
|
let keys = tree.list("caf").unwrap();
|
|
assert_eq!(keys.len(), 4);
|
|
|
|
for (key, _) in &test_data {
|
|
assert!(keys.contains(&key.to_string()));
|
|
}
|
|
|
|
cleanup_test_db(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn test_prefix_with_long_keys() {
|
|
let path = get_test_db_path();
|
|
|
|
let mut tree = TST::new(&path, true).unwrap();
|
|
|
|
// Insert long keys
|
|
let test_data = [
|
|
("this_is_a_very_long_key_for_testing_purposes_1", b"value1".to_vec()),
|
|
("this_is_a_very_long_key_for_testing_purposes_2", b"value2".to_vec()),
|
|
("this_is_a_very_long_key_for_testing_purposes_3", b"value3".to_vec()),
|
|
("this_is_another_long_key_for_testing", b"value4".to_vec()),
|
|
];
|
|
|
|
for (key, value) in &test_data {
|
|
tree.set(key, value.clone()).unwrap();
|
|
}
|
|
|
|
// Test prefix "this_is_a_very"
|
|
let keys = tree.list("this_is_a_very").unwrap();
|
|
assert_eq!(keys.len(), 3);
|
|
|
|
// Test prefix "this_is"
|
|
let keys = tree.list("this_is").unwrap();
|
|
assert_eq!(keys.len(), 4);
|
|
|
|
for (key, _) in &test_data {
|
|
assert!(keys.contains(&key.to_string()));
|
|
}
|
|
|
|
cleanup_test_db(&path);
|
|
} |