Skip to content

Commit 9650b78

Browse files
committed
more align_offset tests
1 parent b625812 commit 9650b78

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

tests/run-pass/align_offset.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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());
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());
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());
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+
fn test_from_utf8() {
60+
const N: usize = 10;
61+
let vec = vec![0x4141414141414141u64; N];
62+
let content = unsafe { std::slice::from_raw_parts(vec.as_ptr() as *const u8, 8 * N) };
63+
println!("{:?}", std::str::from_utf8(content).unwrap());
64+
}
65+
66+
fn main() {
67+
test_align_offset();
68+
test_align_to();
69+
test_from_utf8();
70+
}

tests/run-pass/aligned_utf8_check.rs

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

0 commit comments

Comments
 (0)