use super::*;
#[test]
fn test_reader_overlapping_spans_and_hash_collisions() {
let temp_dir = std::env::temp_dir().join(format!("ridx_rdr_overlap_{}", std::process::id()));
let _ = std::fs::create_dir_all(&temp_dir);
let idx_path = temp_dir.join("overlap.ridx");
let mut writer = crate::writer::IndexWriter::new("overlap_crate", "0.1.0");
writer.add_definition(crate::writer::RawDefinition {
def_id: 1,
kind: crate::format::DefKind::Mod,
visibility: 0,
is_macro_expansion: false,
name: "outer_mod".to_string(),
path: "overlap_crate::outer_mod".to_string(),
signature: None,
file: "src/lib.rs".to_string(),
start_line: 1,
start_col: 1,
end_line: 50,
end_col: 1,
});
writer.add_definition(crate::writer::RawDefinition {
def_id: 2,
kind: crate::format::DefKind::Fn,
visibility: 0,
is_macro_expansion: false,
name: "inner_fn".to_string(),
path: "overlap_crate::outer_mod::inner_fn".to_string(),
signature: Some("fn() -> i32".to_string()),
file: "src/lib.rs".to_string(),
start_line: 10,
start_col: 5,
end_line: 15,
end_col: 6,
});
writer.add_reference(crate::writer::RawReference {
file: "src/lib.rs".to_string(),
start_line: 20,
start_col: 1,
end_line: 20,
end_col: 30,
target_def_id: Some(1),
target_path: "overlap_crate::outer_mod".to_string(),
target_unit: None,
target_file: Some("src/lib.rs".to_string()),
target_line: 1,
target_col: 1,
kind: crate::format::DefKind::Mod,
is_write: false,
is_macro_expansion: false,
});
writer.add_reference(crate::writer::RawReference {
file: "src/lib.rs".to_string(),
start_line: 20,
start_col: 10,
end_line: 20,
end_col: 20,
target_def_id: Some(2),
target_path: "overlap_crate::outer_mod::inner_fn".to_string(),
target_unit: None,
target_file: Some("src/lib.rs".to_string()),
target_line: 10,
target_col: 5,
kind: crate::format::DefKind::Fn,
is_write: false,
is_macro_expansion: false,
});
writer.add_reference(crate::writer::RawReference {
file: "src/lib.rs".to_string(),
start_line: 30,
start_col: 5,
end_line: 30,
end_col: 15,
target_def_id: None,
target_path: "ext_crate::ext_fn".to_string(),
target_unit: Some("ext_crate:1.0.0".to_string()),
target_file: Some("ext.rs".to_string()),
target_line: 5,
target_col: 1,
kind: crate::format::DefKind::Fn,
is_write: false,
is_macro_expansion: false,
});
writer.add_type(crate::writer::RawType { file: "src/lib.rs".to_string(), start_line: 25, start_col: 1, end_line: 25, end_col: 30, type_name: "OuterType".to_string() });
writer.add_type(crate::writer::RawType { file: "src/lib.rs".to_string(), start_line: 25, start_col: 5, end_line: 25, end_col: 15, type_name: "InnerType".to_string() });
writer.add_reference(crate::writer::RawReference {
file: "src/lib.rs".to_string(),
start_line: 40,
start_col: 1,
end_line: 40,
end_col: 10,
target_def_id: Some(9999), target_path: "unresolved::missing".to_string(),
target_unit: None,
target_file: Some("src/missing.rs".to_string()),
target_line: 1,
target_col: 1,
kind: crate::format::DefKind::Fn,
is_write: false,
is_macro_expansion: false,
});
writer.add_definition(crate::writer::RawDefinition {
def_id: 3,
kind: crate::format::DefKind::Fn,
visibility: 0,
is_macro_expansion: false,
name: "inner_def".to_string(),
path: "overlap_crate::inner_def".to_string(),
signature: None,
file: "src/other.rs".to_string(),
start_line: 5,
start_col: 5,
end_line: 5,
end_col: 10,
});
writer.add_definition(crate::writer::RawDefinition {
def_id: 4,
kind: crate::format::DefKind::Mod,
visibility: 0,
is_macro_expansion: false,
name: "outer_mod2".to_string(),
path: "overlap_crate::outer_mod2".to_string(),
signature: None,
file: "src/other.rs".to_string(),
start_line: 1,
start_col: 1,
end_line: 10,
end_col: 1,
});
writer.add_reference(crate::writer::RawReference {
file: "src/other.rs".to_string(),
start_line: 15,
start_col: 5,
end_line: 15,
end_col: 10,
target_def_id: Some(3),
target_path: "overlap_crate::inner_def".to_string(),
target_unit: None,
target_file: Some("src/other.rs".to_string()),
target_line: 5,
target_col: 5,
kind: crate::format::DefKind::Fn,
is_write: false,
is_macro_expansion: false,
});
writer.add_reference(crate::writer::RawReference {
file: "src/other.rs".to_string(),
start_line: 15,
start_col: 1,
end_line: 15,
end_col: 20,
target_def_id: Some(4),
target_path: "overlap_crate::outer_mod2".to_string(),
target_unit: None,
target_file: Some("src/other.rs".to_string()),
target_line: 1,
target_col: 1,
kind: crate::format::DefKind::Mod,
is_write: false,
is_macro_expansion: false,
});
writer.add_type(crate::writer::RawType { file: "src/other.rs".to_string(), start_line: 20, start_col: 5, end_line: 20, end_col: 10, type_name: "InnerOther".to_string() });
writer.add_type(crate::writer::RawType { file: "src/other.rs".to_string(), start_line: 20, start_col: 1, end_line: 20, end_col: 20, type_name: "OuterOther".to_string() });
let mut f = std::fs::File::create(&idx_path).unwrap();
writer.write_to(&mut f).unwrap();
drop(f);
let reader = IndexReader::open(&idx_path).unwrap();
let def = reader.find_definition_at("src/lib.rs", 12, 6).unwrap();
assert_eq!(def.name, "inner_fn");
assert_eq!(def.signature, Some("fn() -> i32"));
let def_outer = reader.find_definition_at("src/lib.rs", 2, 1).unwrap();
assert_eq!(def_outer.name, "outer_mod");
assert_eq!(def_outer.signature, None);
let ref_inner = reader.find_definition_at("src/lib.rs", 20, 15).unwrap();
assert_eq!(ref_inner.name, "inner_fn");
let ref_ext = reader.find_definition_at("src/lib.rs", 30, 8).unwrap();
assert_eq!(ref_ext.path, "ext_crate::ext_fn");
assert_eq!(ref_ext.file, "ext.rs");
let ref_unresolved = reader.find_definition_at("src/lib.rs", 40, 5).unwrap();
assert_eq!(ref_unresolved.path, "unresolved::missing");
assert_eq!(reader.type_at("src/lib.rs", 25, 10), Some("InnerType"));
assert_eq!(reader.type_at("src/lib.rs", 25, 2), Some("OuterType"));
assert_eq!(reader.type_at("src/lib.rs", 99, 99), None);
assert_eq!(reader.type_at("missing.rs", 1, 1), None);
assert_eq!(reader.find_definition_at("src/other.rs", 5, 6).unwrap().name, "inner_def");
assert_eq!(reader.find_definition_at("src/other.rs", 15, 6).unwrap().name, "inner_def");
assert_eq!(reader.type_at("src/other.rs", 20, 6), Some("InnerOther"));
let refs = reader.find_references("overlap_crate::outer_mod::inner_fn");
assert_eq!(refs.len(), 1);
let missing_refs = reader.find_references("missing::path");
assert!(missing_refs.is_empty());
let _ = std::fs::remove_dir_all(&temp_dir);
}
#[test]
fn test_validate_sections_comprehensive() {
let mut hdr = Header {
magic: crate::format::MAGIC,
version: crate::format::CURRENT_FORMAT_VERSION,
string_pool_offset: 7, string_pool_len: 16,
files_offset: 16,
defs_offset: 16,
defs_by_hash_offset: 16,
refs_offset: 16,
refs_by_target_offset: 16,
types_offset: 16,
defs_by_owner_offset: 16,
derefs_offset: 16,
notes_offset: 16,
ty_defs_offset: 16,
ty_entries_offset: 16,
ty_bounds_offset: 16,
ty_args_offset: 16,
fn_sigs_offset: 16,
impls_offset: 16,
assoc_tys_offset: 16,
fields_offset: 16,
def_tys_offset: 16,
generic_params_offset: 16,
where_preds_offset: 16,
lang_items_offset: 16,
uses_offset: 16,
cfgs_offset: 16,
macro_defs_offset: 16,
..Header::default()
};
assert!(validate_sections(1000, &hdr).is_err());
hdr.string_pool_offset = 16;
hdr.string_pool_len = u64::MAX;
assert!(validate_sections(1000, &hdr).is_err());
hdr.string_pool_len = 2000;
assert!(validate_sections(1000, &hdr).is_err());
hdr.string_pool_len = 100;
hdr.crate_name_offset = 200;
assert!(validate_sections(1000, &hdr).is_err());
hdr.crate_name_offset = 0;
hdr.crate_version_offset = 200;
assert!(validate_sections(1000, &hdr).is_err());
hdr.crate_version_offset = 0;
hdr.defs_offset = 3; assert!(validate_sections(1000, &hdr).is_err());
hdr.defs_offset = 16;
hdr.defs_by_hash_offset = 3;
assert!(validate_sections(1000, &hdr).is_err());
hdr.defs_by_hash_offset = 16;
hdr.refs_offset = 3;
assert!(validate_sections(1000, &hdr).is_err());
hdr.refs_offset = 16;
hdr.refs_by_target_offset = 3;
assert!(validate_sections(1000, &hdr).is_err());
hdr.refs_by_target_offset = 16;
hdr.types_offset = 3;
assert!(validate_sections(1000, &hdr).is_err());
assert!(IndexReader::open("/nonexistent/ridx/file.ridx").is_err());
}