Skip to content

Commit

Permalink
Merge pull request #13 from flyingmutant/fuzz
Browse files Browse the repository at this point in the history
fuzz: assert String equivalence
  • Loading branch information
noib3 authored Dec 7, 2023
2 parents 96bb8de + af84483 commit 37a0a78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 14 additions & 4 deletions fuzz/fuzz_targets/editing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ enum StartingText<'a> {
fuzz_target!(|data: (StartingText, Vec<EditOp>)| {
let (starting, ops) = data;

let mut rope = Rope::from(match starting {
let start = match starting {
StartingText::Custom(s) => s,
StartingText::NonAscii => NON_ASCII,
});
};
let mut rope = Rope::from(start);
let mut string = String::from(start);

for op in ops {
match op {
Expand All @@ -38,6 +40,7 @@ fuzz_target!(|data: (StartingText, Vec<EditOp>)| {
byte_offset += 1;
}
rope.insert(byte_offset, text);
string.insert_str(byte_offset, text);
},

EditOp::Delete { mut byte_range }
Expand All @@ -50,7 +53,8 @@ fuzz_target!(|data: (StartingText, Vec<EditOp>)| {
while !rope.is_char_boundary(byte_range.end) {
byte_range.end += 1;
}
rope.delete(byte_range);
rope.delete(byte_range.clone());
string.replace_range(byte_range, "");
},

EditOp::Replace { mut byte_range, text }
Expand All @@ -63,12 +67,18 @@ fuzz_target!(|data: (StartingText, Vec<EditOp>)| {
while !rope.is_char_boundary(byte_range.end) {
byte_range.end += 1;
}
rope.replace(byte_range, text);
rope.replace(byte_range.clone(), text);
string.replace_range(byte_range, text);
},

_ => continue,
}
}

rope.assert_invariants();
assert_eq!(rope, string);
assert_eq!(
rope.lines().collect::<Vec<_>>(),
string.lines().collect::<Vec<_>>(),
);
});
10 changes: 7 additions & 3 deletions src/rope/rope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ use super::RopeSlice;
use crate::range_bounds_to_start_end;
use crate::tree::Tree;

#[cfg(any(test, feature = "arity_4"))]
#[cfg(any(test, fuzzing, feature = "arity_4"))]
const ARITY: usize = 4;

#[cfg(not(any(test, feature = "arity_4")))]
#[cfg(not(any(test, fuzzing, feature = "arity_4")))]
const ARITY: usize = 16;

#[cfg(any(test, feature = "small_chunks"))]
const CHUNK_MAX_BYTES: usize = 4;

#[cfg(not(any(test, feature = "small_chunks")))]
// With 4-byte chunks, fuzzing is unbearably slow.
#[cfg(fuzzing)]
const CHUNK_MAX_BYTES: usize = 16;

#[cfg(not(any(test, fuzzing, feature = "small_chunks")))]
const CHUNK_MAX_BYTES: usize = 2048;

pub(super) type RopeChunk = GapBuffer<CHUNK_MAX_BYTES>;
Expand Down

0 comments on commit 37a0a78

Please sign in to comment.