Skip to content

Commit

Permalink
add test for bytewise ptr::swap of a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 30, 2024
1 parent 9bfd462 commit c28877c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![feature(const_eval_select)]
#![feature(const_heap)]
#![feature(const_nonnull_new)]
#![feature(const_swap)]
#![feature(const_trait_impl)]
#![feature(core_intrinsics)]
#![feature(core_io_borrowed_buf)]
Expand Down
19 changes: 19 additions & 0 deletions library/core/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,25 @@ fn test_const_copy() {
};
}

#[test]
fn test_const_swap() {
const {
let mut ptr1 = &1;
let mut ptr2 = &666;

// Swap ptr1 and ptr2, bytewise. `swap` does not take a count
// so the best we can do is use an array.
type T = [u8; mem::size_of::<&i32>()];
unsafe {
ptr::swap(ptr::from_mut(&mut ptr1).cast::<T>(), ptr::from_mut(&mut ptr2).cast::<T>());
}

// Make sure they still work.
assert!(*ptr1 == 666);
assert!(*ptr2 == 1);
};
}

#[test]
fn test_null_array_as_slice() {
let arr: *mut [u8; 4] = null_mut();
Expand Down

0 comments on commit c28877c

Please sign in to comment.