Skip to content

Commit 0b7b69a

Browse files
committed
Add more goto tests and their expected output
1 parent 77f486e commit 0b7b69a

27 files changed

Lines changed: 1342 additions & 0 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
4+
static int agg(int n) {
5+
char buf[40];
6+
int total = 0;
7+
if (n < 0) {
8+
goto out;
9+
}
10+
memset(buf, 1, sizeof(buf));
11+
for (int i = 0; i < 40; i++) {
12+
total += buf[i];
13+
}
14+
out:
15+
return total;
16+
}
17+
18+
int main(void) {
19+
assert(agg(-1) == 0);
20+
assert(agg(2) == 40);
21+
return 0;
22+
}

tests/unit/goto_backward.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <assert.h>
2+
3+
static int retry(int n) {
4+
int count = 0;
5+
int acc = 0;
6+
again:
7+
count += 1;
8+
acc += n;
9+
if (count < 3) {
10+
goto again;
11+
}
12+
return acc;
13+
}
14+
15+
int main(void) {
16+
assert(retry(4) == 12);
17+
return 0;
18+
}

tests/unit/goto_cleanup.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <assert.h>
2+
3+
static int early(int n) {
4+
int ret = 0;
5+
if (n < 0) {
6+
ret = -1;
7+
goto out;
8+
}
9+
ret = 100;
10+
out:
11+
return ret;
12+
}
13+
14+
static int from_loop(int n) {
15+
int ret = 0;
16+
for (int i = 0; i < n; i++) {
17+
if (i == 3) {
18+
ret = 7;
19+
goto out;
20+
}
21+
ret += i;
22+
}
23+
ret = 999;
24+
out:
25+
return ret;
26+
}
27+
28+
static int from_switch(int n) {
29+
int ret = 0;
30+
switch (n) {
31+
case 1:
32+
ret = 10;
33+
goto out;
34+
default:
35+
ret = 20;
36+
break;
37+
}
38+
ret = 999;
39+
out:
40+
return ret;
41+
}
42+
43+
int main(void) {
44+
assert(early(-1) == -1);
45+
assert(early(5) == 100);
46+
assert(from_loop(2) == 999);
47+
assert(from_loop(10) == 7);
48+
assert(from_switch(1) == 10);
49+
assert(from_switch(2) == 999);
50+
return 0;
51+
}

tests/unit/goto_forward_skip.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <assert.h>
2+
3+
static int skip(int n) {
4+
int x = 0;
5+
if (n > 0) {
6+
goto mid;
7+
}
8+
x += 10;
9+
mid:
10+
x += 1;
11+
return x;
12+
}
13+
14+
int main(void) {
15+
assert(skip(1) == 1);
16+
assert(skip(-1) == 11);
17+
return 0;
18+
}

tests/unit/goto_loop_control.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <assert.h>
2+
3+
static int loopctl(int n) {
4+
int total = 0;
5+
for (int i = 0; i < n; i++) {
6+
if (i == 2) {
7+
continue;
8+
}
9+
if (i == 5) {
10+
break;
11+
}
12+
if (i % 2 == 0) {
13+
goto even;
14+
}
15+
total += 1;
16+
even:
17+
total += 10;
18+
}
19+
return total;
20+
}
21+
22+
int main(void) {
23+
assert(loopctl(10) == 42);
24+
return 0;
25+
}

tests/unit/goto_multi_label.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <assert.h>
2+
3+
static int classify(int n) {
4+
int ret = 0;
5+
if (n < 0) {
6+
goto error;
7+
}
8+
if (n == 0) {
9+
goto out;
10+
}
11+
ret = n;
12+
goto out;
13+
error:
14+
ret = -1;
15+
out:
16+
return ret;
17+
}
18+
19+
int main(void) {
20+
assert(classify(5) == 5);
21+
assert(classify(0) == 0);
22+
assert(classify(-2) == -1);
23+
return 0;
24+
}

tests/unit/goto_nested_label.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <assert.h>
2+
3+
static int scan(int n) {
4+
int total = 0;
5+
for (int i = 0; i < n; i++) {
6+
int j = 0;
7+
while (j < 10) {
8+
if (j == 5) {
9+
goto next;
10+
}
11+
total += 1;
12+
j++;
13+
}
14+
total += 100;
15+
next:
16+
total += 1000;
17+
}
18+
return total;
19+
}
20+
21+
int main(void) {
22+
assert(scan(2) == 2010);
23+
return 0;
24+
}

tests/unit/goto_switch_exit.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <assert.h>
2+
3+
static int classify(int n) {
4+
int ret = 0;
5+
switch (n) {
6+
case 1:
7+
ret = 10;
8+
goto out;
9+
case 2:
10+
ret = 20;
11+
break;
12+
default:
13+
ret = 30;
14+
break;
15+
}
16+
ret += 1;
17+
out:
18+
return ret;
19+
}
20+
21+
int main(void) {
22+
assert(classify(1) == 10);
23+
assert(classify(2) == 21);
24+
assert(classify(9) == 31);
25+
return 0;
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <assert.h>
2+
3+
static int sm(int n) {
4+
int ret = 0;
5+
switch (n) {
6+
case 0:
7+
ret += 1;
8+
/* fallthrough */
9+
case 1:
10+
ret += 10;
11+
goto out;
12+
default:
13+
ret += 100;
14+
break;
15+
}
16+
ret += 1000;
17+
out:
18+
return ret;
19+
}
20+
21+
int main(void) {
22+
assert(sm(0) == 11);
23+
assert(sm(1) == 10);
24+
assert(sm(9) == 1100);
25+
return 0;
26+
}
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 agg_0(n: i32) -> i32 {
10+
let n: Value<i32> = Rc::new(RefCell::new(n));
11+
let mut buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
12+
(0..40).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
13+
));
14+
let mut total: Value<i32> = <Value<i32>>::default();
15+
goto_block!({
16+
'__entry: {
17+
total = Rc::new(RefCell::new(0));
18+
if ((((*n.borrow()) < 0) as i32) != 0) {
19+
goto!('out);
20+
}
21+
{
22+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>)
23+
.to_any()
24+
.memset((1) as u8, ::std::mem::size_of::<[u8; 40]>() as u64 as usize);
25+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
26+
};
27+
let i: Value<i32> = Rc::new(RefCell::new(0));
28+
'loop_: while ((((*i.borrow()) < 40) as i32) != 0) {
29+
(*total.borrow_mut()) += ((*buf.borrow())[(*i.borrow()) as usize] as i32);
30+
(*i.borrow_mut()).postfix_inc();
31+
}
32+
}
33+
'out: {
34+
return (*total.borrow());
35+
}
36+
});
37+
panic!("ub: non-void function does not return a value")
38+
}
39+
pub fn main() {
40+
std::process::exit(main_0());
41+
}
42+
fn main_0() -> i32 {
43+
assert!(
44+
(((({
45+
let _n: i32 = -1_i32;
46+
agg_0(_n)
47+
}) == 0) as i32)
48+
!= 0)
49+
);
50+
assert!(
51+
(((({
52+
let _n: i32 = 2;
53+
agg_0(_n)
54+
}) == 40) as i32)
55+
!= 0)
56+
);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)