Add testing suite for chunk bitmaps
This commit is contained in:
parent
81bb5c2d27
commit
aa39f979b9
7 changed files with 473 additions and 1 deletions
|
@ -3764,3 +3764,80 @@ fn init_settings(cx: &mut App, f: fn(&mut AllLanguageSettingsContent)) {
|
|||
settings.update_user_settings::<AllLanguageSettings>(cx, f);
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test(iterations = 100)]
|
||||
fn test_random_chunk_bitmaps(cx: &mut App, mut rng: StdRng) {
|
||||
use util::RandomCharIter;
|
||||
|
||||
// Generate random text
|
||||
let len = rng.gen_range(0..10000);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
|
||||
let buffer = cx.new(|cx| Buffer::local(text, cx));
|
||||
let snapshot = buffer.read(cx).snapshot();
|
||||
|
||||
// Get all chunks and verify their bitmaps
|
||||
let chunks = snapshot.chunks(0..snapshot.len(), false);
|
||||
|
||||
for chunk in chunks {
|
||||
let chunk_text = chunk.text;
|
||||
let chars_bitmap = chunk.chars;
|
||||
let tabs_bitmap = chunk.tabs;
|
||||
|
||||
// Check empty chunks have empty bitmaps
|
||||
if chunk_text.is_empty() {
|
||||
assert_eq!(
|
||||
chars_bitmap, 0,
|
||||
"Empty chunk should have empty chars bitmap"
|
||||
);
|
||||
assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Verify that chunk text doesn't exceed 128 bytes
|
||||
assert!(
|
||||
chunk_text.len() <= 128,
|
||||
"Chunk text length {} exceeds 128 bytes",
|
||||
chunk_text.len()
|
||||
);
|
||||
|
||||
// Verify chars bitmap
|
||||
let char_indices = chunk_text
|
||||
.char_indices()
|
||||
.map(|(i, _)| i)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for byte_idx in 0..chunk_text.len() {
|
||||
let should_have_bit = char_indices.contains(&byte_idx);
|
||||
let has_bit = chars_bitmap & (1 << byte_idx) != 0;
|
||||
|
||||
if has_bit != should_have_bit {
|
||||
eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
|
||||
eprintln!("Char indices: {:?}", char_indices);
|
||||
eprintln!("Chars bitmap: {:#b}", chars_bitmap);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
has_bit, should_have_bit,
|
||||
"Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
|
||||
byte_idx, chunk_text, should_have_bit, has_bit
|
||||
);
|
||||
}
|
||||
|
||||
// Verify tabs bitmap
|
||||
for (byte_idx, byte) in chunk_text.bytes().enumerate() {
|
||||
let is_tab = byte == b'\t';
|
||||
let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
|
||||
|
||||
if has_bit != is_tab {
|
||||
eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
|
||||
eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
|
||||
assert_eq!(
|
||||
has_bit, is_tab,
|
||||
"Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
|
||||
byte_idx, chunk_text, byte as char, is_tab, has_bit
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue