From f8e3f7a4ca64a1b216ec0df465e13e90e9462c1a Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 23 Jan 2022 14:28:08 +0800 Subject: [PATCH] add some basic list tests --- nac3standalone/demo/demo.rs | 36 +++++++++++++++++++++++++++ nac3standalone/demo/interpret_demo.py | 4 +-- nac3standalone/demo/src/lists.py | 20 +++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 nac3standalone/demo/src/lists.py diff --git a/nac3standalone/demo/demo.rs b/nac3standalone/demo/demo.rs index a2794c9c..ab209494 100644 --- a/nac3standalone/demo/demo.rs +++ b/nac3standalone/demo/demo.rs @@ -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) { + 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; } diff --git a/nac3standalone/demo/interpret_demo.py b/nac3standalone/demo/interpret_demo.py index ba714123..3125410a 100755 --- a/nac3standalone/demo/interpret_demo.py +++ b/nac3standalone/demo/interpret_demo.py @@ -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 diff --git a/nac3standalone/demo/src/lists.py b/nac3standalone/demo/src/lists.py new file mode 100644 index 00000000..5bde25e0 --- /dev/null +++ b/nac3standalone/demo/src/lists.py @@ -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