Skip to content

Commit

Permalink
Avoid Vec use in static_new_in_place() tests
Browse files Browse the repository at this point in the history
The tests added for `Scratchpad::static_new_in_place()` were failing to
compile when disabling the `std` and `unstable` crate features. The
`Scratchpad` used doesn't need a large amount of stack space, so we can
create it on the stack instead, avoiding the use of `Vec` entirely.
  • Loading branch information
okready committed Jun 16, 2018
1 parent c144808 commit d4e566c
Showing 1 changed file with 40 additions and 52 deletions.
92 changes: 40 additions & 52 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,49 +361,43 @@ fn validate_back_operations() {
/// using a `Scratchpad` created with `static_new_in_place()`.
#[test]
fn validate_front_operations_in_place() {
let mut buffer = Vec::with_capacity(1);
unsafe {
buffer.set_len(1);
SimpleScratchpad::static_new_in_place(&mut buffer[0]);
let mut scratchpad = uninitialized();
SimpleScratchpad::static_new_in_place(&mut scratchpad);
validate_basic_operations(
&scratchpad,
|scratchpad| scratchpad.mark_front(),
|stack| (stack.iter().map(|x| *x).collect(), ArrayVec::new()),
);
}

let scratchpad = &buffer[0];
validate_basic_operations(
scratchpad,
|scratchpad| scratchpad.mark_front(),
|stack| (stack.iter().map(|x| *x).collect(), ArrayVec::new()),
);
}

/// General test for validating that the internal state of a `Scratchpad` and
/// `Marker` match the expected state across various "back" operations when
/// using a `Scratchpad` created with `static_new_in_place()`.
#[test]
fn validate_back_operations_in_place() {
let mut buffer = Vec::with_capacity(1);
unsafe {
buffer.set_len(1);
SimpleScratchpad::static_new_in_place(&mut buffer[0]);
let mut scratchpad = uninitialized();
SimpleScratchpad::static_new_in_place(&mut scratchpad);
validate_basic_operations(
&scratchpad,
|scratchpad| scratchpad.mark_back(),
|stack| {
let flipped_stack = stack
.iter()
.map(|x| {
if *x == core::usize::MAX {
*x
} else {
BUFFER_SIZE - *x
}
})
.collect();
(ArrayVec::new(), flipped_stack)
},
);
}

let scratchpad = &buffer[0];
validate_basic_operations(
scratchpad,
|scratchpad| scratchpad.mark_back(),
|stack| {
let flipped_stack = stack
.iter()
.map(|x| {
if *x == core::usize::MAX {
*x
} else {
BUFFER_SIZE - *x
}
})
.collect();
(ArrayVec::new(), flipped_stack)
},
);
}

/// Shared implementation for `validate_front_memory_limits()` and
Expand Down Expand Up @@ -466,37 +460,31 @@ fn validate_back_memory_limits() {
/// using a `Scratchpad` created with `static_new_in_place()`.
#[test]
fn validate_front_memory_limits_in_place() {
let mut buffer = Vec::with_capacity(1);
unsafe {
buffer.set_len(1);
SimpleScratchpad::static_new_in_place(&mut buffer[0]);
let mut scratchpad = uninitialized();
SimpleScratchpad::static_new_in_place(&mut scratchpad);
validate_memory_limits(
&scratchpad,
|scratchpad| scratchpad.mark_front(),
|scratchpad| scratchpad.mark_back(),
);
}

let scratchpad = &buffer[0];
validate_memory_limits(
scratchpad,
|scratchpad| scratchpad.mark_front(),
|scratchpad| scratchpad.mark_back(),
);
}

/// Verifies that the available space for allocations at the back of a
/// scratchpad is correctly adjusted as allocations at the front increase when
/// using a `Scratchpad` created with `static_new_in_place()`.
#[test]
fn validate_back_memory_limits_in_place() {
let mut buffer = Vec::with_capacity(1);
unsafe {
buffer.set_len(1);
SimpleScratchpad::static_new_in_place(&mut buffer[0]);
let mut scratchpad = uninitialized();
SimpleScratchpad::static_new_in_place(&mut scratchpad);
validate_memory_limits(
&scratchpad,
|scratchpad| scratchpad.mark_back(),
|scratchpad| scratchpad.mark_front(),
);
}

let scratchpad = &buffer[0];
validate_memory_limits(
scratchpad,
|scratchpad| scratchpad.mark_back(),
|scratchpad| scratchpad.mark_front(),
);
}

/// Shared implementation for `validate_front_marker_limits()` and
Expand Down

0 comments on commit d4e566c

Please sign in to comment.