Skip to content

Commit 2fe1fd2

Browse files
committed
transpile: Add enums.c snapshot test
1 parent 5a09f62 commit 2fe1fd2

4 files changed

Lines changed: 214 additions & 0 deletions

File tree

c2rust-transpile/tests/snapshots.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ fn test_empty_init() {
359359
transpile("empty_init.c").run();
360360
}
361361

362+
#[test]
363+
fn test_enums() {
364+
transpile("enums.c").run();
365+
}
366+
362367
#[test]
363368
fn test_exprs() {
364369
transpile("exprs.c").run();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
typedef enum { Foo0, Foo1, Foo2, Foo3 } Foo;
2+
enum Bar { BarN1 = -1, Bar0, Bar1, Bar2, Bar3 };
3+
4+
#define FOO1_MACRO Foo1
5+
#define BAR1_MACRO Bar1
6+
7+
void test_enums(void) {
8+
Foo foo = Foo0;
9+
enum Bar bar = Bar0;
10+
foo = Foo1;
11+
bar = BarN1;
12+
13+
// Assign enum constant of wrong type
14+
foo = Bar0;
15+
bar = Foo0;
16+
17+
// Assign integer that matches a constant
18+
foo = 1;
19+
bar = 1;
20+
21+
// Assign integer that doesn't match any constant
22+
foo = 3;
23+
bar = 3;
24+
25+
// Arithmetic
26+
foo -= 2;
27+
bar -= 2;
28+
29+
Foo e = Foo1;
30+
31+
// Compare to same type
32+
int enum_enum = e == foo;
33+
int enum_constant = e == Foo0;
34+
35+
// Compare to wrong type
36+
int wrong_enum_enum = e == bar;
37+
int wrong_enum_constant = e == Bar0;
38+
39+
switch (foo) {
40+
case Foo0:
41+
break;
42+
43+
case FOO1_MACRO:
44+
break;
45+
46+
// Integer with matching variant
47+
case 2:
48+
break;
49+
50+
// Wrong type
51+
case Bar3:
52+
break;
53+
54+
// No matching variant
55+
case 42:
56+
break;
57+
58+
// No matching variant, negative on unsigned enum
59+
case -42:
60+
break;
61+
}
62+
63+
switch (bar) {
64+
case Bar0:
65+
break;
66+
67+
case BAR1_MACRO:
68+
break;
69+
70+
// Integer with matching variant
71+
case 2:
72+
break;
73+
74+
// Wrong type
75+
case Foo3:
76+
break;
77+
78+
// Negative variant
79+
case BarN1:
80+
break;
81+
82+
// No matching variant
83+
case 42:
84+
break;
85+
86+
// No matching variant, negative on signed enum
87+
case -42:
88+
break;
89+
}
90+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
expression: cat tests/snapshots/enums.2021.clang15.rs
4+
---
5+
#![allow(
6+
clippy::missing_safety_doc,
7+
dead_code,
8+
non_camel_case_types,
9+
non_snake_case,
10+
non_upper_case_globals,
11+
unused_assignments,
12+
unused_mut
13+
)]
14+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
15+
#[repr(transparent)]
16+
pub struct Foo(pub ::core::ffi::c_uint);
17+
pub const Foo3: Foo = Foo(3);
18+
pub const Foo2: Foo = Foo(2);
19+
pub const Foo1: Foo = Foo(1);
20+
pub const Foo0: Foo = Foo(0);
21+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
22+
#[repr(transparent)]
23+
pub struct Bar(pub ::core::ffi::c_int);
24+
pub const Bar3: Bar = Bar(3);
25+
pub const Bar2: Bar = Bar(2);
26+
pub const Bar1: Bar = Bar(1);
27+
pub const Bar0: Bar = Bar(0);
28+
pub const BarN1: Bar = Bar(-1);
29+
pub const FOO1_MACRO: ::core::ffi::c_uint = 1 as ::core::ffi::c_uint;
30+
pub const BAR1_MACRO: ::core::ffi::c_int = 1;
31+
#[no_mangle]
32+
pub unsafe extern "C" fn test_enums() {
33+
let mut foo: Foo = Foo0;
34+
let mut bar: Bar = Bar0;
35+
foo = Foo1;
36+
bar = BarN1;
37+
foo = Foo(Bar0.0 as ::core::ffi::c_uint);
38+
bar = Bar(Foo0.0 as ::core::ffi::c_int);
39+
foo = Foo1;
40+
bar = Bar1;
41+
foo = Foo3;
42+
bar = Bar3;
43+
foo = Foo(foo.0.wrapping_sub(2 as ::core::ffi::c_uint));
44+
bar = Bar(bar.0 - 2 as ::core::ffi::c_int);
45+
let mut e: Foo = Foo1;
46+
let mut enum_enum: ::core::ffi::c_int = (e.0 == foo.0) as ::core::ffi::c_int;
47+
let mut enum_constant: ::core::ffi::c_int =
48+
(e.0 == Foo0.0 as ::core::ffi::c_int as ::core::ffi::c_uint) as ::core::ffi::c_int;
49+
let mut wrong_enum_enum: ::core::ffi::c_int =
50+
(e.0 == bar.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
51+
let mut wrong_enum_constant: ::core::ffi::c_int =
52+
(e.0 == Bar0.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
53+
match foo.0 {
54+
0 | 1 | 2 | 3 | 42 | 4294967254 | _ => {}
55+
}
56+
match bar.0 {
57+
0 | BAR1_MACRO | 2 | 3 | -1 | 42 | -42 | _ => {}
58+
};
59+
}
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/enums.2024.clang15.rs
4+
---
5+
#![allow(
6+
clippy::missing_safety_doc,
7+
dead_code,
8+
non_camel_case_types,
9+
non_snake_case,
10+
non_upper_case_globals,
11+
unsafe_op_in_unsafe_fn,
12+
unused_assignments,
13+
unused_mut
14+
)]
15+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
16+
#[repr(transparent)]
17+
pub struct Foo(pub ::core::ffi::c_uint);
18+
pub const Foo3: Foo = Foo(3);
19+
pub const Foo2: Foo = Foo(2);
20+
pub const Foo1: Foo = Foo(1);
21+
pub const Foo0: Foo = Foo(0);
22+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
23+
#[repr(transparent)]
24+
pub struct Bar(pub ::core::ffi::c_int);
25+
pub const Bar3: Bar = Bar(3);
26+
pub const Bar2: Bar = Bar(2);
27+
pub const Bar1: Bar = Bar(1);
28+
pub const Bar0: Bar = Bar(0);
29+
pub const BarN1: Bar = Bar(-1);
30+
pub const FOO1_MACRO: ::core::ffi::c_uint = 1 as ::core::ffi::c_uint;
31+
pub const BAR1_MACRO: ::core::ffi::c_int = 1;
32+
#[unsafe(no_mangle)]
33+
pub unsafe extern "C" fn test_enums() {
34+
let mut foo: Foo = Foo0;
35+
let mut bar: Bar = Bar0;
36+
foo = Foo1;
37+
bar = BarN1;
38+
foo = Foo(Bar0.0 as ::core::ffi::c_uint);
39+
bar = Bar(Foo0.0 as ::core::ffi::c_int);
40+
foo = Foo1;
41+
bar = Bar1;
42+
foo = Foo3;
43+
bar = Bar3;
44+
foo = Foo(foo.0.wrapping_sub(2 as ::core::ffi::c_uint));
45+
bar = Bar(bar.0 - 2 as ::core::ffi::c_int);
46+
let mut e: Foo = Foo1;
47+
let mut enum_enum: ::core::ffi::c_int = (e.0 == foo.0) as ::core::ffi::c_int;
48+
let mut enum_constant: ::core::ffi::c_int =
49+
(e.0 == Foo0.0 as ::core::ffi::c_int as ::core::ffi::c_uint) as ::core::ffi::c_int;
50+
let mut wrong_enum_enum: ::core::ffi::c_int =
51+
(e.0 == bar.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
52+
let mut wrong_enum_constant: ::core::ffi::c_int =
53+
(e.0 == Bar0.0 as ::core::ffi::c_uint) as ::core::ffi::c_int;
54+
match foo.0 {
55+
0 | 1 | 2 | 3 | 42 | 4294967254 | _ => {}
56+
}
57+
match bar.0 {
58+
0 | BAR1_MACRO | 2 | 3 | -1 | 42 | -42 | _ => {}
59+
};
60+
}

0 commit comments

Comments
 (0)