Skip to content

Commit 2340d75

Browse files
authored
Add switch tests (#28)
This adds switch/case tests. Not all tests compile, but that's fine, they will be fixed in future PR's. Some tests are marked with the new ```cpp // translation-fail ``` marker that's aded to tests which break the translator
1 parent 711a935 commit 2340d75

97 files changed

Lines changed: 4278 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self):
2626
self.regex_xfail = re.compile(r"//\s*XFAIL:\s*(.*)")
2727
self.regex_panic = re.compile(r"//\s*panic\s*(?::\s*(.*))?$", re.MULTILINE)
2828
self.regex_nocompile = re.compile(r"//\s*no-compile\s*(?::\s*(.*))?$", re.MULTILINE)
29+
self.regex_translation_fail = re.compile(r"//\s*translation-fail\s*(?::\s*(.*))?$", re.MULTILINE)
2930
self.regex_nondet_result = re.compile(r"//\s*nondet-result\s*(?::\s*(.*))?$", re.MULTILINE)
3031
self.rust_version = read_rust_version()
3132
os.environ['RUSTFLAGS'] = '-Awarnings -A dangerous-implicit-autorefs'
@@ -83,6 +84,7 @@ def matches_model(match, model):
8384

8485
should_panic = matches_model(self.regex_panic.search(text), model)
8586
should_not_compile = matches_model(self.regex_nocompile.search(text), model)
87+
should_not_translate = matches_model(self.regex_translation_fail.search(text), model)
8688
is_nondet_result = matches_model(self.regex_nondet_result.search(text), model)
8789

8890
tmp_dir = "tmp/" + fname + "-" + model + "_" + format(random.getrandbits(64), "x")
@@ -94,10 +96,6 @@ def fail(str, code = fail_code):
9496
shutil.rmtree(tmp_dir, True)
9597
return code, str
9698

97-
expected_file = self.getExpectedFile(filepath, model, fname)
98-
if not os.path.exists(expected_file) and not replace_expected:
99-
return fail('no expected file')
100-
10199
cmd = ['./cpp2rust/cpp2rust', '-file', cc_input, '-model', model,
102100
'-o', rs_file]
103101

@@ -113,8 +111,14 @@ def fail(str, code = fail_code):
113111
generated = f.read()
114112

115113
if returncode != 0:
114+
if should_not_translate:
115+
return lit.Test.XFAIL, ''
116116
return fail('cpp2rust failed\n' + err)
117117

118+
expected_file = self.getExpectedFile(filepath, model, fname)
119+
if not os.path.exists(expected_file) and not replace_expected:
120+
return fail('no expected file')
121+
118122
if replace_expected:
119123
self.updateExpected(generated, expected_file)
120124

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn basic_0(x: i32) -> i32 {
10+
let x: Value<i32> = Rc::new(RefCell::new(x));
11+
let r: Value<i32> = Rc::new(RefCell::new(0));
12+
'switch: {
13+
let __match_cond = (*x.borrow());
14+
match __match_cond {
15+
v if v == 0 => {
16+
(*r.borrow_mut()) = 10;
17+
break 'switch;
18+
}
19+
v if v == 1 => {
20+
(*r.borrow_mut()) = 20;
21+
break 'switch;
22+
}
23+
v if v == 2 => {
24+
(*r.borrow_mut()) = 30;
25+
break 'switch;
26+
}
27+
_ => {
28+
(*r.borrow_mut()) = 40;
29+
break 'switch;
30+
}
31+
}
32+
};
33+
return (*r.borrow());
34+
}
35+
pub fn main() {
36+
std::process::exit(main_0());
37+
}
38+
fn main_0() -> i32 {
39+
assert!(
40+
(({
41+
let _x: i32 = 0;
42+
basic_0(_x)
43+
}) == 10)
44+
);
45+
assert!(
46+
(({
47+
let _x: i32 = 2;
48+
basic_0(_x)
49+
}) == 30)
50+
);
51+
assert!(
52+
(({
53+
let _x: i32 = 99;
54+
basic_0(_x)
55+
}) == 40)
56+
);
57+
return 0;
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn borrow_in_condition_and_in_body_0(x: i32) -> i32 {
10+
let x: Value<i32> = Rc::new(RefCell::new(x));
11+
'switch: {
12+
let __match_cond = (*x.borrow());
13+
match __match_cond {
14+
v if v == 0 => {}
15+
_ => {
16+
return ((*x.borrow()) + 1);
17+
}
18+
}
19+
};
20+
panic!("ub: non-void function does not return a value")
21+
}
22+
pub fn main() {
23+
std::process::exit(main_0());
24+
}
25+
fn main_0() -> i32 {
26+
assert!(
27+
(({
28+
let _x: i32 = 0;
29+
borrow_in_condition_and_in_body_0(_x)
30+
}) == 1)
31+
);
32+
assert!(
33+
(({
34+
let _x: i32 = 1;
35+
borrow_in_condition_and_in_body_0(_x)
36+
}) == 2)
37+
);
38+
return 0;
39+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn switch_char_0(c: u8) -> i32 {
10+
let c: Value<u8> = Rc::new(RefCell::new(c));
11+
'switch: {
12+
let __match_cond = ((*c.borrow()) as i32);
13+
match __match_cond {
14+
v if v == (('a' as u8) as i32) => {
15+
return 1;
16+
}
17+
v if v == (('b' as u8) as i32) => {
18+
return 2;
19+
}
20+
v if v == (('\n' as u8) as i32) => {
21+
return 3;
22+
}
23+
v if v == (('\0' as u8) as i32) => {
24+
return 4;
25+
}
26+
_ => {
27+
return 0;
28+
}
29+
}
30+
};
31+
panic!("ub: non-void function does not return a value")
32+
}
33+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
34+
enum Color {
35+
#[default]
36+
kRed = 0,
37+
kGreen = 1,
38+
kBlue = 2,
39+
}
40+
pub fn main() {
41+
std::process::exit(main_0());
42+
}
43+
fn main_0() -> i32 {
44+
assert!(
45+
(({
46+
let _c: u8 = ('a' as u8);
47+
switch_char_0(_c)
48+
}) == 1)
49+
);
50+
assert!(
51+
(({
52+
let _c: u8 = ('b' as u8);
53+
switch_char_0(_c)
54+
}) == 2)
55+
);
56+
assert!(
57+
(({
58+
let _c: u8 = ('\n' as u8);
59+
switch_char_0(_c)
60+
}) == 3)
61+
);
62+
assert!(
63+
(({
64+
let _c: u8 = ('\0' as u8);
65+
switch_char_0(_c)
66+
}) == 4)
67+
);
68+
assert!(
69+
(({
70+
let _c: u8 = ('z' as u8);
71+
switch_char_0(_c)
72+
}) == 0)
73+
);
74+
return 0;
75+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn switch_complex_cond_0(p: Ptr<i32>, bias: i32) -> i32 {
10+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p));
11+
let bias: Value<i32> = Rc::new(RefCell::new(bias));
12+
'switch: {
13+
let __match_cond = {
14+
let _lhs = ((*p.borrow()).read());
15+
_lhs + (*bias.borrow())
16+
};
17+
match __match_cond {
18+
v if v == 0 => {
19+
return 1;
20+
}
21+
v if v == 5 => {
22+
return 2;
23+
}
24+
v if v == 10 => {
25+
return 3;
26+
}
27+
_ => {
28+
return 0;
29+
}
30+
}
31+
};
32+
panic!("ub: non-void function does not return a value")
33+
}
34+
pub fn main() {
35+
std::process::exit(main_0());
36+
}
37+
fn main_0() -> i32 {
38+
let p_val: Value<i32> = Rc::new(RefCell::new(5));
39+
assert!(
40+
(({
41+
let _p: Ptr<i32> = (p_val.as_pointer());
42+
let _bias: i32 = 0;
43+
switch_complex_cond_0(_p, _bias)
44+
}) == 2)
45+
);
46+
assert!(
47+
(({
48+
let _p: Ptr<i32> = (p_val.as_pointer());
49+
let _bias: i32 = 5;
50+
switch_complex_cond_0(_p, _bias)
51+
}) == 3)
52+
);
53+
assert!(
54+
(({
55+
let _p: Ptr<i32> = (p_val.as_pointer());
56+
let _bias: i32 = -5_i32;
57+
switch_complex_cond_0(_p, _bias)
58+
}) == 1)
59+
);
60+
assert!(
61+
(({
62+
let _p: Ptr<i32> = (p_val.as_pointer());
63+
let _bias: i32 = 99;
64+
switch_complex_cond_0(_p, _bias)
65+
}) == 0)
66+
);
67+
return 0;
68+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn compound_case_body_0(x: i32) -> i32 {
10+
let x: Value<i32> = Rc::new(RefCell::new(x));
11+
let r: Value<i32> = Rc::new(RefCell::new(0));
12+
'switch: {
13+
let __match_cond = (*x.borrow());
14+
match __match_cond {
15+
v if v == 1 => {
16+
let y: Value<i32> = Rc::new(RefCell::new(10));
17+
let z: Value<i32> = Rc::new(RefCell::new(20));
18+
(*r.borrow_mut()) = ((*y.borrow()) + (*z.borrow()));
19+
break 'switch;
20+
}
21+
v if v == 2 => {
22+
let y: Value<i32> = Rc::new(RefCell::new(100));
23+
(*r.borrow_mut()) = ((*y.borrow()) - 1);
24+
break 'switch;
25+
}
26+
_ => {
27+
(*r.borrow_mut()) = -1_i32;
28+
break 'switch;
29+
}
30+
}
31+
};
32+
return (*r.borrow());
33+
}
34+
pub fn main() {
35+
std::process::exit(main_0());
36+
}
37+
fn main_0() -> i32 {
38+
assert!(
39+
(({
40+
let _x: i32 = 1;
41+
compound_case_body_0(_x)
42+
}) == 30)
43+
);
44+
assert!(
45+
(({
46+
let _x: i32 = 2;
47+
compound_case_body_0(_x)
48+
}) == 99)
49+
);
50+
assert!(
51+
(({
52+
let _x: i32 = 9;
53+
compound_case_body_0(_x)
54+
}) == -1_i32)
55+
);
56+
return 0;
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn continue_inside_switch_0(n: i32) -> i32 {
10+
let n: Value<i32> = Rc::new(RefCell::new(n));
11+
let r: Value<i32> = Rc::new(RefCell::new(0));
12+
let i: Value<i32> = Rc::new(RefCell::new(0));
13+
'loop_: while ((*i.borrow()) < (*n.borrow())) {
14+
'switch: {
15+
let __match_cond = (*i.borrow());
16+
match __match_cond {
17+
v if v == 0 || v == 2 || v == 4 => {
18+
(*i.borrow_mut()).prefix_inc();
19+
continue 'loop_;
20+
}
21+
_ => {
22+
(*r.borrow_mut()) += (*i.borrow());
23+
break 'switch;
24+
}
25+
}
26+
};
27+
(*r.borrow_mut()) += 1000;
28+
(*i.borrow_mut()).prefix_inc();
29+
}
30+
return (*r.borrow());
31+
}
32+
pub fn main() {
33+
std::process::exit(main_0());
34+
}
35+
fn main_0() -> i32 {
36+
assert!(
37+
(({
38+
let _n: i32 = 6;
39+
continue_inside_switch_0(_n)
40+
}) == ((((1 + 3) + 5) as i32) + (3 * 1000)))
41+
);
42+
return 0;
43+
}

0 commit comments

Comments
 (0)