add some basic list tests

This commit is contained in:
Sebastien Bourdeauducq 2022-01-23 14:28:08 +08:00
parent ba997ae094
commit f8e3f7a4ca
3 changed files with 57 additions and 3 deletions

View File

@ -1,3 +1,24 @@
mod cslice { // copied from https://github.com/dherman/cslice
use std::marker::PhantomData;
use std::slice;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct CSlice<'a, T> {
base: *const T,
len: usize,
marker: PhantomData<&'a ()>
}
impl<'a, T> AsRef<[T]> for CSlice<'a, T> {
fn as_ref(&self) -> &[T] {
unsafe {
slice::from_raw_parts(self.base, self.len)
}
}
}
}
#[no_mangle]
pub extern "C" fn output_int32(x: i32) {
println!("{}", x);
@ -18,6 +39,21 @@ pub extern "C" fn output_asciiart(x: i32) {
}
}
#[no_mangle]
pub extern "C" fn output_int32_list(x: &cslice::CSlice<i32>) {
print!("[");
let mut it = x.as_ref().iter().peekable();
while let Some(e) = it.next() {
if it.peek().is_none() {
print!("{}", e);
} else {
print!("{}, ", e);
}
}
println!("]");
}
extern "C" {
fn run() -> i32;
}

View File

@ -19,9 +19,7 @@ def patch(module):
name = fun.__name__
if name == "output_asciiart":
return output_asciiart
elif name == "output_int32":
return print
elif name == "output_int64":
elif name in {"output_int32", "output_int64", "output_int32_list"}:
return print
else:
raise NotImplementedError

View File

@ -0,0 +1,20 @@
@extern
def output_int32_list(x: list[int32]):
...
def run() -> int32:
data = [0, 1, 2, 3]
output_int32_list(data[2:3])
output_int32_list(data[:])
output_int32_list(data[1:])
output_int32_list(data[:-1])
m1 = -1
output_int32_list(data[::m1])
output_int32_list(data[:0:m1])
m2 = -2
output_int32_list(data[m2::m1])
return 0