Skip to content

Commit 75c8826

Browse files
committed
More tests
1 parent fa58ba5 commit 75c8826

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

zk_ee/src/utils/aligned_vector.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ impl<A: Allocator> UsizeAlignedByteBox<A> {
143143
alloc::boxed::Box::new_uninit_slice_in(buffer_size, allocator);
144144
let written_words = init_fn(&mut inner);
145145
assert!(written_words <= buffer_size); // we do not want to truncate or realloc, but we will expose only written part below
146-
// Safety: init_fn only guarantees that it initialized `written_words` elements.
147-
// Initialize the remainder to keep the full allocation initialized.
146+
// Safety: init_fn only guarantees that it initialized `written_words` elements.
147+
// Initialize the remainder to keep the full allocation initialized.
148148
for dst in inner.iter_mut().skip(written_words) {
149149
dst.write(0);
150150
}
@@ -318,6 +318,19 @@ mod tests {
318318
assert_eq!(buffer.as_slice(), &[]);
319319
}
320320

321+
#[test]
322+
fn writer_drop_without_writes_keeps_existing_initialized_prefix() {
323+
let input = [1u8, 2, 3, 4, 5];
324+
let mut buffer = UsizeAlignedByteBox::from_slice_in(&input, Global);
325+
326+
{
327+
let _writer = buffer.as_writable();
328+
// Intentionally perform no writes.
329+
}
330+
331+
assert_eq!(buffer.as_slice(), &input);
332+
}
333+
321334
#[test]
322335
fn from_usize_iterator_in_serializes_words() {
323336
let words = [1usize, 2usize, usize::MAX];
@@ -428,4 +441,21 @@ mod tests {
428441
assert_eq!(buffer.as_slice().len(), byte_len);
429442
assert_eq!(buffer.as_slice(), expected.as_slice());
430443
}
444+
445+
#[test]
446+
fn preallocated_partial_write_panics_on_read() {
447+
let mut buffer = UsizeAlignedByteBox::preallocated_in(2 * USIZE_SIZE, Global);
448+
449+
{
450+
let mut writer = buffer.as_writable();
451+
writer.try_write(123).unwrap();
452+
// One word initialized, but `byte_capacity` requires two words.
453+
}
454+
455+
let panicked = std::panic::catch_unwind(AssertUnwindSafe(|| {
456+
let _ = buffer.as_slice();
457+
}))
458+
.is_err();
459+
assert!(panicked);
460+
}
431461
}

0 commit comments

Comments
 (0)