Skip to content

Commit fe71a12

Browse files
committed
Add tests for mem* functions
Signed-off-by: Joe Richey <[email protected]>
1 parent de4ed28 commit fe71a12

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

testcrate/tests/mem.rs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
extern crate compiler_builtins;
2+
use compiler_builtins::mem::{memcmp, memcpy, memmove, memset};
3+
4+
#[test]
5+
fn memcpy_3() {
6+
let mut arr: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
7+
unsafe {
8+
let src = arr.as_ptr().offset(9);
9+
let dst = arr.as_mut_ptr().offset(1);
10+
assert_eq!(memcpy(dst, src, 3), dst);
11+
assert_eq!(arr, [0, 9, 10, 11, 4, 5, 6, 7, 8, 9, 10, 11]);
12+
}
13+
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
14+
unsafe {
15+
let src = arr.as_ptr().offset(1);
16+
let dst = arr.as_mut_ptr().offset(9);
17+
assert_eq!(memcpy(dst, src, 3), dst);
18+
assert_eq!(arr, [0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3]);
19+
}
20+
}
21+
22+
#[test]
23+
fn memcpy_10() {
24+
let arr: [u8; 18] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
25+
let mut dst: [u8; 12] = [0; 12];
26+
unsafe {
27+
let src = arr.as_ptr().offset(1);
28+
assert_eq!(memcpy(dst.as_mut_ptr(), src, 10), dst.as_mut_ptr());
29+
assert_eq!(dst, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0]);
30+
}
31+
unsafe {
32+
let src = arr.as_ptr().offset(8);
33+
assert_eq!(memcpy(dst.as_mut_ptr(), src, 10), dst.as_mut_ptr());
34+
assert_eq!(dst, [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 0]);
35+
}
36+
}
37+
38+
#[test]
39+
fn memcpy_big() {
40+
// Make the arrays cross 3 pages
41+
const SIZE: usize = 8193;
42+
let src: [u8; SIZE] = [22; SIZE];
43+
struct Dst {
44+
start: usize,
45+
buf: [u8; SIZE],
46+
end: usize,
47+
}
48+
49+
let mut dst = Dst {
50+
start: 0,
51+
buf: [0; SIZE],
52+
end: 0,
53+
};
54+
unsafe {
55+
assert_eq!(
56+
memcpy(dst.buf.as_mut_ptr(), src.as_ptr(), SIZE),
57+
dst.buf.as_mut_ptr()
58+
);
59+
assert_eq!(dst.start, 0);
60+
assert_eq!(dst.buf, [22; SIZE]);
61+
assert_eq!(dst.end, 0);
62+
}
63+
}
64+
65+
#[test]
66+
fn memmove_forward() {
67+
let mut arr: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
68+
unsafe {
69+
let src = arr.as_ptr().offset(6);
70+
let dst = arr.as_mut_ptr().offset(3);
71+
assert_eq!(memmove(dst, src, 5), dst);
72+
assert_eq!(arr, [0, 1, 2, 6, 7, 8, 9, 10, 8, 9, 10, 11]);
73+
}
74+
}
75+
76+
#[test]
77+
fn memmove_backward() {
78+
let mut arr: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
79+
unsafe {
80+
let src = arr.as_ptr().offset(3);
81+
let dst = arr.as_mut_ptr().offset(6);
82+
assert_eq!(memmove(dst, src, 5), dst);
83+
assert_eq!(arr, [0, 1, 2, 3, 4, 5, 3, 4, 5, 6, 7, 11]);
84+
}
85+
}
86+
87+
#[test]
88+
fn memset_zero() {
89+
let mut arr: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
90+
unsafe {
91+
let ptr = arr.as_mut_ptr().offset(5);
92+
assert_eq!(memset(ptr, 0, 2), ptr);
93+
assert_eq!(arr, [0, 1, 2, 3, 4, 0, 0, 7]);
94+
95+
// Only the LSB matters for a memset
96+
assert_eq!(memset(arr.as_mut_ptr(), 0x2000, 8), arr.as_mut_ptr());
97+
assert_eq!(arr, [0, 0, 0, 0, 0, 0, 0, 0]);
98+
}
99+
}
100+
101+
#[test]
102+
fn memset_nonzero() {
103+
let mut arr: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
104+
unsafe {
105+
let ptr = arr.as_mut_ptr().offset(2);
106+
assert_eq!(memset(ptr, 22, 3), ptr);
107+
assert_eq!(arr, [0, 1, 22, 22, 22, 5, 6, 7]);
108+
109+
// Only the LSB matters for a memset
110+
assert_eq!(memset(arr.as_mut_ptr(), 0x2009, 8), arr.as_mut_ptr());
111+
assert_eq!(arr, [9, 9, 9, 9, 9, 9, 9, 9]);
112+
}
113+
}
114+
115+
#[test]
116+
fn memcmp_eq() {
117+
let arr1: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
118+
let arr2: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
119+
unsafe {
120+
assert_eq!(memcmp(arr1.as_ptr(), arr2.as_ptr(), 8), 0);
121+
assert_eq!(memcmp(arr1.as_ptr(), arr2.as_ptr(), 3), 0);
122+
}
123+
}
124+
125+
#[test]
126+
fn memcmp_ne() {
127+
let arr1: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
128+
let arr2: [u8; 8] = [0, 1, 2, 3, 4, 5, 7, 7];
129+
unsafe {
130+
assert!(memcmp(arr1.as_ptr(), arr2.as_ptr(), 8) < 0);
131+
assert!(memcmp(arr2.as_ptr(), arr1.as_ptr(), 8) > 0);
132+
}
133+
}

0 commit comments

Comments
 (0)