Skip to content

Commit 57cc8ce

Browse files
committed
Auto merge of #970 - RalfJung:align_offset, r=RalfJung
more align_offset tests Cc @christianpoveda
2 parents ea3eee0 + a9c207d commit 57cc8ce

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

tests/run-pass/align_offset.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
fn test_align_offset() {
2+
let d = Box::new([0u32; 4]);
3+
// Get u8 pointer to base
4+
let raw = d.as_ptr() as *const u8;
5+
6+
assert_eq!(raw.align_offset(2), 0);
7+
assert_eq!(raw.align_offset(4), 0);
8+
assert_eq!(raw.align_offset(8), usize::max_value()); // requested alignment higher than allocation alignment
9+
10+
assert_eq!(raw.wrapping_offset(1).align_offset(2), 1);
11+
assert_eq!(raw.wrapping_offset(1).align_offset(4), 3);
12+
assert_eq!(raw.wrapping_offset(1).align_offset(8), usize::max_value()); // requested alignment higher than allocation alignment
13+
14+
assert_eq!(raw.wrapping_offset(2).align_offset(2), 0);
15+
assert_eq!(raw.wrapping_offset(2).align_offset(4), 2);
16+
assert_eq!(raw.wrapping_offset(2).align_offset(8), usize::max_value()); // requested alignment higher than allocation alignment
17+
}
18+
19+
fn test_align_to() {
20+
const N: usize = 4;
21+
let d = Box::new([0u32; N]);
22+
// Get u8 slice covering the entire thing
23+
let s = unsafe { std::slice::from_raw_parts(d.as_ptr() as *const u8, 4 * N) };
24+
let raw = s.as_ptr();
25+
26+
{
27+
let (l, m, r) = unsafe { s.align_to::<u32>() };
28+
assert_eq!(l.len(), 0);
29+
assert_eq!(r.len(), 0);
30+
assert_eq!(m.len(), N);
31+
assert_eq!(raw, m.as_ptr() as *const u8);
32+
}
33+
34+
{
35+
let (l, m, r) = unsafe { s[1..].align_to::<u32>() };
36+
assert_eq!(l.len(), 3);
37+
assert_eq!(m.len(), N-1);
38+
assert_eq!(r.len(), 0);
39+
assert_eq!(raw.wrapping_offset(4), m.as_ptr() as *const u8);
40+
}
41+
42+
{
43+
let (l, m, r) = unsafe { s[..4*N - 1].align_to::<u32>() };
44+
assert_eq!(l.len(), 0);
45+
assert_eq!(m.len(), N-1);
46+
assert_eq!(r.len(), 3);
47+
assert_eq!(raw, m.as_ptr() as *const u8);
48+
}
49+
50+
{
51+
let (l, m, r) = unsafe { s[1..4*N - 1].align_to::<u32>() };
52+
assert_eq!(l.len(), 3);
53+
assert_eq!(m.len(), N-2);
54+
assert_eq!(r.len(), 3);
55+
assert_eq!(raw.wrapping_offset(4), m.as_ptr() as *const u8);
56+
}
57+
58+
{
59+
#[repr(align(8))] struct Align8(u64);
60+
let (l, m, r) = unsafe { s.align_to::<Align8>() }; // requested alignment higher than allocation alignment
61+
assert_eq!(l.len(), 4*N);
62+
assert_eq!(r.len(), 0);
63+
assert_eq!(m.len(), 0);
64+
}
65+
}
66+
67+
fn test_from_utf8() {
68+
const N: usize = 10;
69+
let vec = vec![0x4141414141414141u64; N];
70+
let content = unsafe { std::slice::from_raw_parts(vec.as_ptr() as *const u8, 8 * N) };
71+
println!("{:?}", std::str::from_utf8(content).unwrap());
72+
}
73+
74+
fn main() {
75+
test_align_offset();
76+
test_align_to();
77+
test_from_utf8();
78+
}

tests/run-pass/aligned_utf8_check.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)