Skip to content

Commit 3a1a848

Browse files
committed
Add basic va_args tests
1 parent c6441cb commit 3a1a848

9 files changed

Lines changed: 269 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn sum_ints_0(first: i32, args: &[VaArg]) -> i32 {
11+
let first: Value<i32> = Rc::new(RefCell::new(first));
12+
let ap: Value<VaList> = Rc::new(RefCell::new(<VaList>::default()));
13+
let total: Value<i32> = Rc::new(RefCell::new((*first.borrow())));
14+
(*ap.borrow_mut()) = VaList::new(args);
15+
let val: Value<i32> = <Value<i32>>::default();
16+
'loop_: while ((({
17+
(*val.borrow_mut()) = ((*ap.borrow_mut()).arg::<i32>()).clone();
18+
(*val.borrow())
19+
}) as i32)
20+
!= 0)
21+
{
22+
(*total.borrow_mut()) += (*val.borrow());
23+
}
24+
return (*total.borrow());
25+
}
26+
pub fn main() {
27+
std::process::exit(main_0());
28+
}
29+
fn main_0() -> i32 {
30+
return (({
31+
let _first: i32 = 1;
32+
sum_ints_0(_first, &[2.into(), 3.into(), 4.into(), 0.into()])
33+
}) - 10);
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn inner_0(count: i32, ap: VaList) -> i32 {
11+
let count: Value<i32> = Rc::new(RefCell::new(count));
12+
let ap: Value<VaList> = Rc::new(RefCell::new(ap));
13+
let total: Value<i32> = Rc::new(RefCell::new(0));
14+
let i: Value<i32> = Rc::new(RefCell::new(0));
15+
'loop_: while ((*i.borrow()) < (*count.borrow())) {
16+
(*total.borrow_mut()) += ((*ap.borrow_mut()).arg::<i32>()).clone();
17+
(*i.borrow_mut()).postfix_inc();
18+
}
19+
return (*total.borrow());
20+
}
21+
pub fn outer_1(count: i32, args: &[VaArg]) -> i32 {
22+
let count: Value<i32> = Rc::new(RefCell::new(count));
23+
let ap: Value<VaList> = Rc::new(RefCell::new(<VaList>::default()));
24+
(*ap.borrow_mut()) = VaList::new(args);
25+
let result: Value<i32> = Rc::new(RefCell::new(
26+
({
27+
let _count: i32 = (*count.borrow());
28+
let _ap: VaList = (*ap.borrow()).clone();
29+
inner_0(_count, _ap)
30+
}),
31+
));
32+
return (*result.borrow());
33+
}
34+
pub fn main() {
35+
std::process::exit(main_0());
36+
}
37+
fn main_0() -> i32 {
38+
return (({
39+
let _count: i32 = 3;
40+
outer_1(_count, &[10.into(), 20.into(), 30.into()])
41+
}) - 60);
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
pub fn sum_mixed_0(count: i32, args: &[VaArg]) -> f64 {
11+
let count: Value<i32> = Rc::new(RefCell::new(count));
12+
let ap: Value<VaList> = Rc::new(RefCell::new(<VaList>::default()));
13+
(*ap.borrow_mut()) = VaList::new(args);
14+
let total: Value<f64> = Rc::new(RefCell::new(0_f64));
15+
let i: Value<i32> = Rc::new(RefCell::new(0));
16+
'loop_: while ((*i.borrow()) < (*count.borrow())) {
17+
(*total.borrow_mut()) += ((*ap.borrow_mut()).arg::<f64>()).clone();
18+
(*i.borrow_mut()).postfix_inc();
19+
}
20+
return (*total.borrow());
21+
}
22+
pub fn main() {
23+
std::process::exit(main_0());
24+
}
25+
fn main_0() -> i32 {
26+
return ((({
27+
let _count: i32 = 3;
28+
sum_mixed_0(_count, &[1.5E+0.into(), 2.5E+0.into(), 3.0E+0.into()])
29+
}) as i32)
30+
- 7);
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
9+
use std::rc::Rc;
10+
pub unsafe fn sum_ints_0(mut first: i32, args: &[VaArg]) -> i32 {
11+
let mut ap: VaList = <VaList>::default();
12+
let mut total: i32 = first;
13+
ap = VaList::new(args);
14+
let mut val: i32 = <i32>::default();
15+
'loop_: while ((({
16+
val = ap.arg::<i32>();
17+
val
18+
}) as i32)
19+
!= (0))
20+
{
21+
total += val;
22+
}
23+
return total;
24+
}
25+
pub fn main() {
26+
unsafe {
27+
std::process::exit(main_0() as i32);
28+
}
29+
}
30+
unsafe fn main_0() -> i32 {
31+
return ((unsafe {
32+
let _first: i32 = 1;
33+
sum_ints_0(_first, &[2.into(), 3.into(), 4.into(), 0.into()])
34+
}) - (10));
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
9+
use std::rc::Rc;
10+
pub unsafe fn inner_0(mut count: i32, mut ap: VaList) -> i32 {
11+
let mut total: i32 = 0;
12+
let mut i: i32 = 0;
13+
'loop_: while ((i) < (count)) {
14+
total += ap.arg::<i32>();
15+
i.postfix_inc();
16+
}
17+
return total;
18+
}
19+
pub unsafe fn outer_1(mut count: i32, args: &[VaArg]) -> i32 {
20+
let mut ap: VaList = <VaList>::default();
21+
ap = VaList::new(args);
22+
let mut result: i32 = (unsafe {
23+
let _count: i32 = count;
24+
let _ap: VaList = ap;
25+
inner_0(_count, _ap)
26+
});
27+
return result;
28+
}
29+
pub fn main() {
30+
unsafe {
31+
std::process::exit(main_0() as i32);
32+
}
33+
}
34+
unsafe fn main_0() -> i32 {
35+
return ((unsafe {
36+
let _count: i32 = 3;
37+
outer_1(_count, &[10.into(), 20.into(), 30.into()])
38+
}) - (60));
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
9+
use std::rc::Rc;
10+
pub unsafe fn sum_mixed_0(mut count: i32, args: &[VaArg]) -> f64 {
11+
let mut ap: VaList = <VaList>::default();
12+
ap = VaList::new(args);
13+
let mut total: f64 = 0_f64;
14+
let mut i: i32 = 0;
15+
'loop_: while ((i) < (count)) {
16+
total += ap.arg::<f64>();
17+
i.postfix_inc();
18+
}
19+
return total;
20+
}
21+
pub fn main() {
22+
unsafe {
23+
std::process::exit(main_0() as i32);
24+
}
25+
}
26+
unsafe fn main_0() -> i32 {
27+
return (((unsafe {
28+
let _count: i32 = 3;
29+
sum_mixed_0(_count, &[1.5E+0.into(), 2.5E+0.into(), 3.0E+0.into()])
30+
}) as i32)
31+
- (7));
32+
}

tests/unit/va_arg_concat.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdarg.h>
2+
3+
int sum_ints(int first, ...) {
4+
va_list ap;
5+
int total = first;
6+
7+
va_start(ap, first);
8+
int val;
9+
while ((val = va_arg(ap, int)) != 0) {
10+
total += val;
11+
}
12+
va_end(ap);
13+
14+
return total;
15+
}
16+
17+
int main() {
18+
return sum_ints(1, 2, 3, 4, 0) - 10;
19+
}

tests/unit/va_arg_forward.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdarg.h>
2+
3+
int inner(int count, va_list ap) {
4+
int total = 0;
5+
for (int i = 0; i < count; i++) {
6+
total += va_arg(ap, int);
7+
}
8+
return total;
9+
}
10+
11+
int outer(int count, ...) {
12+
va_list ap;
13+
va_start(ap, count);
14+
int result = inner(count, ap);
15+
va_end(ap);
16+
return result;
17+
}
18+
19+
int main() {
20+
return outer(3, 10, 20, 30) - 60;
21+
}

tests/unit/va_arg_mixed_types.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdarg.h>
2+
3+
double sum_mixed(int count, ...) {
4+
va_list ap;
5+
va_start(ap, count);
6+
double total = 0;
7+
for (int i = 0; i < count; i++) {
8+
total += va_arg(ap, double);
9+
}
10+
va_end(ap);
11+
return total;
12+
}
13+
14+
int main() {
15+
return (int)sum_mixed(3, 1.5, 2.5, 3.0) - 7;
16+
}

0 commit comments

Comments
 (0)