Skip to content

Commit d8fb0f0

Browse files
committed
Move all string.h/cstring related tests in a single file
1 parent a54bbfd commit d8fb0f0

8 files changed

Lines changed: 462 additions & 66 deletions

File tree

tests/unit/cstring.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// no-compile: refcount
2+
#include <cassert>
3+
#include <cstring>
4+
5+
static void test_memcpy() {
6+
const char src[] = "hello";
7+
char dst[6] = {0};
8+
void *r = std::memcpy(dst, src, 6);
9+
assert(r == dst);
10+
assert(dst[0] == 'h' && dst[1] == 'e' && dst[2] == 'l');
11+
assert(dst[3] == 'l' && dst[4] == 'o' && dst[5] == '\0');
12+
}
13+
14+
static void test_memset() {
15+
char buf[4];
16+
void *r = std::memset(buf, 'x', 4);
17+
assert(r == buf);
18+
assert(buf[0] == 'x' && buf[1] == 'x' && buf[2] == 'x' && buf[3] == 'x');
19+
}
20+
21+
static void test_memcmp() {
22+
const char a[] = {1, 2, 3, 4};
23+
const char b[] = {1, 2, 3, 4};
24+
const char c[] = {1, 2, 9, 4};
25+
assert(std::memcmp(a, b, 4) == 0);
26+
assert(std::memcmp(a, c, 4) < 0);
27+
assert(std::memcmp(c, a, 4) > 0);
28+
}
29+
30+
static void test_memmove() {
31+
char buf[6] = {'a', 'b', 'c', 'd', 'e', '\0'};
32+
void *r = std::memmove(buf + 1, buf, 4);
33+
assert(r == buf + 1);
34+
assert(buf[0] == 'a' && buf[1] == 'a' && buf[2] == 'b');
35+
assert(buf[3] == 'c' && buf[4] == 'd' && buf[5] == '\0');
36+
}
37+
38+
static void test_strchr() {
39+
const char *s = "hello world";
40+
const char *r = std::strchr(s, 'w');
41+
assert(r != nullptr);
42+
assert(*r == 'w');
43+
assert(std::strchr(s, 'z') == nullptr);
44+
}
45+
46+
int main() {
47+
test_memcpy();
48+
test_memset();
49+
test_memcmp();
50+
test_memmove();
51+
test_strchr();
52+
return 0;
53+
}

tests/unit/out/unsafe/cstring.rs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_memcpy_0() {
10+
let src: [u8; 6] = *b"hello\0";
11+
let mut dst: [u8; 6] = [0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8];
12+
let mut r: *mut ::libc::c_void = {
13+
if 6_u64 != 0 {
14+
::std::ptr::copy_nonoverlapping(
15+
(src.as_ptr() as *const u8 as *const ::libc::c_void),
16+
(dst.as_mut_ptr() as *mut u8 as *mut ::libc::c_void),
17+
6_u64 as usize,
18+
)
19+
}
20+
(dst.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)
21+
};
22+
assert!(((r) == (dst.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)));
23+
assert!(
24+
(((dst[(0) as usize] as i32) == (('h' as u8) as i32))
25+
&& ((dst[(1) as usize] as i32) == (('e' as u8) as i32)))
26+
&& ((dst[(2) as usize] as i32) == (('l' as u8) as i32))
27+
);
28+
assert!(
29+
(((dst[(3) as usize] as i32) == (('l' as u8) as i32))
30+
&& ((dst[(4) as usize] as i32) == (('o' as u8) as i32)))
31+
&& ((dst[(5) as usize] as i32) == (('\0' as u8) as i32))
32+
);
33+
}
34+
pub unsafe fn test_memset_1() {
35+
let mut buf: [u8; 4] = [0_u8; 4];
36+
let mut r: *mut ::libc::c_void = {
37+
let byte_0 = (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) as *mut u8;
38+
for offset in 0..4_u64 {
39+
*byte_0.offset(offset as isize) = (('x' as u8) as i32) as u8;
40+
}
41+
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)
42+
};
43+
assert!(((r) == (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)));
44+
assert!(
45+
((((buf[(0) as usize] as i32) == (('x' as u8) as i32))
46+
&& ((buf[(1) as usize] as i32) == (('x' as u8) as i32)))
47+
&& ((buf[(2) as usize] as i32) == (('x' as u8) as i32)))
48+
&& ((buf[(3) as usize] as i32) == (('x' as u8) as i32))
49+
);
50+
}
51+
pub unsafe fn test_memcmp_2() {
52+
let a: [u8; 4] = [1_u8, 2_u8, 3_u8, 4_u8];
53+
let b: [u8; 4] = [1_u8, 2_u8, 3_u8, 4_u8];
54+
let c: [u8; 4] = [1_u8, 2_u8, 9_u8, 4_u8];
55+
assert!(
56+
(({
57+
let sa = core::slice::from_raw_parts(
58+
(a.as_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
59+
4_u64 as usize,
60+
);
61+
let sb = core::slice::from_raw_parts(
62+
(b.as_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
63+
4_u64 as usize,
64+
);
65+
let mut diff = 0_i32;
66+
for (x, y) in sa.iter().zip(sb.iter()) {
67+
if x != y {
68+
diff = (*x as i32) - (*y as i32);
69+
break;
70+
}
71+
}
72+
diff
73+
}) == (0))
74+
);
75+
assert!(
76+
(({
77+
let sa = core::slice::from_raw_parts(
78+
(a.as_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
79+
4_u64 as usize,
80+
);
81+
let sb = core::slice::from_raw_parts(
82+
(c.as_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
83+
4_u64 as usize,
84+
);
85+
let mut diff = 0_i32;
86+
for (x, y) in sa.iter().zip(sb.iter()) {
87+
if x != y {
88+
diff = (*x as i32) - (*y as i32);
89+
break;
90+
}
91+
}
92+
diff
93+
}) < (0))
94+
);
95+
assert!(
96+
(({
97+
let sa = core::slice::from_raw_parts(
98+
(c.as_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
99+
4_u64 as usize,
100+
);
101+
let sb = core::slice::from_raw_parts(
102+
(a.as_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
103+
4_u64 as usize,
104+
);
105+
let mut diff = 0_i32;
106+
for (x, y) in sa.iter().zip(sb.iter()) {
107+
if x != y {
108+
diff = (*x as i32) - (*y as i32);
109+
break;
110+
}
111+
}
112+
diff
113+
}) > (0))
114+
);
115+
}
116+
pub unsafe fn test_memmove_3() {
117+
let mut buf: [u8; 6] = [
118+
('a' as u8),
119+
('b' as u8),
120+
('c' as u8),
121+
('d' as u8),
122+
('e' as u8),
123+
('\0' as u8),
124+
];
125+
let mut r: *mut ::libc::c_void = {
126+
if 4_u64 != 0 {
127+
::std::ptr::copy_nonoverlapping(
128+
(buf.as_mut_ptr() as *const u8 as *const ::libc::c_void),
129+
(buf.as_mut_ptr().offset((1) as isize) as *mut u8 as *mut ::libc::c_void),
130+
4_u64 as usize,
131+
)
132+
}
133+
(buf.as_mut_ptr().offset((1) as isize) as *mut u8 as *mut ::libc::c_void)
134+
};
135+
assert!(((r) == (buf.as_mut_ptr().offset((1) as isize) as *mut u8 as *mut ::libc::c_void)));
136+
assert!(
137+
(((buf[(0) as usize] as i32) == (('a' as u8) as i32))
138+
&& ((buf[(1) as usize] as i32) == (('a' as u8) as i32)))
139+
&& ((buf[(2) as usize] as i32) == (('b' as u8) as i32))
140+
);
141+
assert!(
142+
(((buf[(3) as usize] as i32) == (('c' as u8) as i32))
143+
&& ((buf[(4) as usize] as i32) == (('d' as u8) as i32)))
144+
&& ((buf[(5) as usize] as i32) == (('\0' as u8) as i32))
145+
);
146+
}
147+
pub unsafe fn test_strchr_4() {
148+
let mut s: *const u8 = b"hello world\0".as_ptr();
149+
let mut r: *const u8 = libc::strchr(s as *const i8, (('w' as u8) as i32)) as *const u8;
150+
assert!(!((r).is_null()));
151+
assert!((((*r) as i32) == (('w' as u8) as i32)));
152+
assert!((libc::strchr(s as *const i8, (('z' as u8) as i32)) as *const u8).is_null());
153+
}
154+
pub fn main() {
155+
unsafe {
156+
std::process::exit(main_0() as i32);
157+
}
158+
}
159+
unsafe fn main_0() -> i32 {
160+
(unsafe { test_memcpy_0() });
161+
(unsafe { test_memset_1() });
162+
(unsafe { test_memcmp_2() });
163+
(unsafe { test_memmove_3() });
164+
(unsafe { test_strchr_4() });
165+
return 0;
166+
}

tests/unit/out/unsafe/strchr_c.rs

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

tests/unit/out/unsafe/strchr_cpp.rs

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

0 commit comments

Comments
 (0)