Skip to content

Commit 210e58d

Browse files
committed
add snapshot test for unary expression
1 parent c589c0a commit 210e58d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
int puts(const char *str);
2+
3+
static int side_effect(){
4+
puts("the return of side effect");
5+
return 10;
6+
}
7+
8+
void unary_without_side_effect(){
9+
int i = 5;
10+
-i;
11+
+i;
12+
~i;
13+
!i;
14+
&i;
15+
*&i;
16+
i++;
17+
i--;
18+
--i;
19+
++i;
20+
}
21+
22+
void unary_with_side_effect(){
23+
char* arr[1] = {0};
24+
25+
-side_effect();
26+
+side_effect();
27+
~side_effect();
28+
!side_effect();
29+
&""[side_effect()];
30+
*arr[side_effect()];
31+
++arr[side_effect()];
32+
--arr[side_effect()];
33+
arr[side_effect()]++;
34+
arr[side_effect()]--;
35+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
expression: cat tests/snapshots/exprs.rs
4+
input_file: c2rust-transpile/tests/snapshots/exprs.c
5+
---
6+
#![allow(
7+
dead_code,
8+
mutable_transmutes,
9+
non_camel_case_types,
10+
non_snake_case,
11+
non_upper_case_globals,
12+
unused_assignments,
13+
unused_mut
14+
)]
15+
extern "C" {
16+
fn puts(str: *const std::ffi::c_char) -> std::ffi::c_int;
17+
}
18+
unsafe extern "C" fn side_effect() -> std::ffi::c_int {
19+
puts(b"the return of side effect\0" as *const u8 as *const std::ffi::c_char);
20+
return 10 as std::ffi::c_int;
21+
}
22+
#[no_mangle]
23+
pub unsafe extern "C" fn unary_without_side_effect() {
24+
let mut i: std::ffi::c_int = 5 as std::ffi::c_int;
25+
-i;
26+
i;
27+
!i;
28+
(i == 0) as std::ffi::c_int;
29+
&mut i;
30+
i;
31+
i += 1;
32+
i;
33+
i -= 1;
34+
i;
35+
i -= 1;
36+
i;
37+
i += 1;
38+
i;
39+
}
40+
#[no_mangle]
41+
pub unsafe extern "C" fn unary_with_side_effect() {
42+
let mut arr: [*mut std::ffi::c_char; 1] = [0 as *mut std::ffi::c_char];
43+
-side_effect();
44+
side_effect();
45+
!side_effect();
46+
(side_effect() == 0) as std::ffi::c_int;
47+
&*(b"\0" as *const u8 as *const std::ffi::c_char).offset(::core::mem::transmute::<
48+
unsafe extern "C" fn() -> std::ffi::c_int,
49+
unsafe extern "C" fn() -> std::ffi::c_int,
50+
>(side_effect)() as isize) as *const std::ffi::c_char;
51+
*arr[side_effect() as usize];
52+
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(1);
53+
arr[side_effect() as usize];
54+
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(-1);
55+
arr[side_effect() as usize];
56+
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(1);
57+
arr[side_effect() as usize];
58+
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(-1);
59+
arr[side_effect() as usize];
60+
}

0 commit comments

Comments
 (0)