Skip to content

Commit

Permalink
Expose tree constants for internal testing.
Browse files Browse the repository at this point in the history
Fix for issue #92.
  • Loading branch information
cessen committed Oct 15, 2023
1 parent dfb0a78 commit c185ec5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ pub use crate::rope::Rope;
pub use crate::rope_builder::RopeBuilder;
pub use crate::slice::RopeSlice;

/// NOT PART OF THE PUBLIC API (hidden from docs for a reason!)
/// These are only exposed for tests that live in the `tests` directory.
#[doc(hidden)]
pub use crate::tree::{MAX_BYTES, MAX_CHILDREN, MIN_BYTES, MIN_CHILDREN};

//==============================================================
// Error reporting types.

Expand Down
28 changes: 18 additions & 10 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ mod constants {
};

// Node maximums.
pub(crate) const MAX_CHILDREN: usize = {
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MAX_CHILDREN: usize = {
let node_list_align = align_of::<Arc<u8>>();
let info_list_align = align_of::<TextInfo>();
let field_gap = if node_list_align >= info_list_align {
Expand All @@ -66,7 +67,8 @@ mod constants {

target_size / (size_of::<Arc<u8>>() + size_of::<TextInfo>())
};
pub(crate) const MAX_BYTES: usize = {
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MAX_BYTES: usize = {
let smallvec_overhead = size_of::<SmallVec<[u8; 16]>>() - 16;
TARGET_TOTAL_SIZE - START_OFFSET - smallvec_overhead
};
Expand All @@ -75,8 +77,10 @@ mod constants {
// Note: MIN_BYTES is intentionally a little smaller than half
// MAX_BYTES, to give a little wiggle room when on the edge of
// merging/splitting.
pub(crate) const MIN_CHILDREN: usize = MAX_CHILDREN / 2;
pub(crate) const MIN_BYTES: usize = (MAX_BYTES / 2) - (MAX_BYTES / 32);
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MIN_CHILDREN: usize = MAX_CHILDREN / 2;
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MIN_BYTES: usize = (MAX_BYTES / 2) - (MAX_BYTES / 32);

// Compile-time assertion.
const _: () = {
Expand All @@ -92,16 +96,20 @@ mod constants {
// the tests.
#[cfg(any(test, feature = "small_chunks"))]
mod test_constants {
pub(crate) const MAX_CHILDREN: usize = 5;
pub(crate) const MIN_CHILDREN: usize = MAX_CHILDREN / 2;
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MAX_CHILDREN: usize = 5;
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MIN_CHILDREN: usize = MAX_CHILDREN / 2;

// MAX_BYTES must be >= 4 to allow for 4-byte utf8 characters.
pub(crate) const MAX_BYTES: usize = 9; // Note: can't be 8, because 3-byte characters.
pub(crate) const MIN_BYTES: usize = (MAX_BYTES / 2) - (MAX_BYTES / 32);
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MAX_BYTES: usize = 9; // Note: can't be 8, because 3-byte characters.
#[doc(hidden)] // NOT PART OF THE PUBLIC API!
pub const MIN_BYTES: usize = (MAX_BYTES / 2) - (MAX_BYTES / 32);
}

#[cfg(not(any(test, feature = "small_chunks")))]
pub(crate) use self::constants::{MAX_BYTES, MAX_CHILDREN, MIN_BYTES, MIN_CHILDREN};
pub use self::constants::{MAX_BYTES, MAX_CHILDREN, MIN_BYTES, MIN_CHILDREN};

#[cfg(any(test, feature = "small_chunks"))]
pub(crate) use self::test_constants::{MAX_BYTES, MAX_CHILDREN, MIN_BYTES, MIN_CHILDREN};
pub use self::test_constants::{MAX_BYTES, MAX_CHILDREN, MIN_BYTES, MIN_CHILDREN};
8 changes: 3 additions & 5 deletions tests/proptest_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use proptest::collection::vec;
use proptest::test_runner::Config;
use ropey::{
str_utils::{byte_to_char_idx, byte_to_line_idx, char_to_byte_idx, char_to_line_idx},
Rope,
Rope, MAX_BYTES,
};

fn string_insert(text: &mut String, char_idx: usize, text_ins: &str) {
Expand Down Expand Up @@ -130,8 +130,7 @@ proptest! {
rope.assert_invariants();
assert_eq!(rope, rope_clone);

let max_leaf_bytes = 1024 - 33;
assert!((rope.capacity() - rope.len_bytes()) <= max_leaf_bytes);
assert!((rope.capacity() - rope.len_bytes()) <= MAX_BYTES);
assert!(rope.capacity() <= capacity_before);
}

Expand All @@ -153,8 +152,7 @@ proptest! {
rope.assert_invariants();
assert_eq!(rope, rope_clone);

let max_leaf_bytes = 1024 - 33;
let max_diff = max_leaf_bytes + ((rope.len_bytes() / max_leaf_bytes) * ins_text.len());
let max_diff = MAX_BYTES + ((rope.len_bytes() / MAX_BYTES) * ins_text.len());

assert!((rope.capacity() - rope.len_bytes()) <= max_diff);
}
Expand Down

0 comments on commit c185ec5

Please sign in to comment.