Skip to content

Commit

Permalink
Add block test for a block with 12 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Feb 2, 2024
1 parent c30cf0b commit 8a723c1
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 44 deletions.
27 changes: 27 additions & 0 deletions crates/tests/extern/block_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef struct {

typedef int32_t (^IntBlock)();
typedef int32_t (^AddBlock)(int32_t);
typedef int32_t (^Add12)(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t);
typedef LargeStruct (^LargeStructBlock)(LargeStruct);


Expand Down Expand Up @@ -37,6 +38,32 @@ int32_t invoke_add_block(AddBlock block, int32_t a) {
return block(a);
}


Add12 get_add_12() {
return ^(
int32_t a1, int32_t a2, int32_t a3, int32_t a4,
int32_t a5, int32_t a6, int32_t a7, int32_t a8,
int32_t a9, int32_t a10, int32_t a11, int32_t a12
) { return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12; };
}

Add12 get_add_12_with(int32_t x) {
return Block_copy(^(
int32_t a1, int32_t a2, int32_t a3, int32_t a4,
int32_t a5, int32_t a6, int32_t a7, int32_t a8,
int32_t a9, int32_t a10, int32_t a11, int32_t a12
) { return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + x; });
}

int32_t invoke_add_12(
Add12 block,
int32_t a1, int32_t a2, int32_t a3, int32_t a4,
int32_t a5, int32_t a6, int32_t a7, int32_t a8,
int32_t a9, int32_t a10, int32_t a11, int32_t a12
) {
return block(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
}

LargeStructBlock get_large_struct_block() {
return ^(LargeStruct s) {
s.x -= 1.0;
Expand Down
136 changes: 92 additions & 44 deletions crates/tests/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ unsafe impl Encode for LargeStruct {
Encoding::Struct("LargeStruct", &[f32::ENCODING, <[u8; 100]>::ENCODING]);
}

type Add12 = Block<dyn Fn(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32) -> i32>;

extern "C" {
/// Returns a pointer to a global block that returns 7.
pub fn get_int_block() -> *mut Block<dyn Fn() -> i32>;
Expand All @@ -48,6 +50,24 @@ extern "C" {
/// Invokes a block with `a` and returns the result.
pub fn invoke_add_block(block: &Block<dyn Fn(i32) -> i32>, a: i32) -> i32;

pub fn get_add_12() -> *mut Add12;
pub fn get_add_12_with(x: i32) -> *mut Add12;
pub fn invoke_add_12(
block: &Add12,
a1: i32,
a2: i32,
a3: i32,
a4: i32,
a5: i32,
a6: i32,
a7: i32,
a8: i32,
a9: i32,
a10: i32,
a11: i32,
a12: i32,
) -> i32;

pub fn get_large_struct_block() -> *mut Block<dyn Fn(LargeStruct) -> LargeStruct>;
pub fn get_large_struct_block_with(
i: LargeStruct,
Expand All @@ -69,18 +89,84 @@ fn test_block_debugging() {

#[test]
fn test_int_block() {
unsafe {
assert_eq!(invoke_int_block(&*get_int_block()), 7);
assert_eq!(invoke_int_block(&*get_int_block_with(13)), 13);
#[track_caller]
fn invoke_assert(block: &Block<dyn Fn() -> i32>, expected: i32) {
assert_eq!(block.call(()), expected);
assert_eq!(unsafe { invoke_int_block(block) }, expected);
}

global_block! {
static GLOBAL_BLOCK = || -> i32 {
42
};
}

invoke_assert(unsafe { &*get_int_block() }, 7);
invoke_assert(
&unsafe { RcBlock::from_raw(get_int_block_with(3)) }.unwrap(),
3,
);
invoke_assert(&StackBlock::new(|| 10), 10);
invoke_assert(&RcBlock::new(|| 6), 6);
invoke_assert(&GLOBAL_BLOCK, 42);
}

#[test]
fn test_add_block() {
unsafe {
assert_eq!(invoke_add_block(&*get_add_block(), 5), 12);
assert_eq!(invoke_add_block(&*get_add_block_with(3), 5), 8);
#[track_caller]
fn invoke_assert(block: &Block<dyn Fn(i32) -> i32>, expected: i32) {
assert_eq!(block.call((5,)), expected);
assert_eq!(unsafe { invoke_add_block(block, 5) }, expected);
}

global_block! {
static GLOBAL_BLOCK = |x: i32| -> i32 {
x + 42
};
}

invoke_assert(unsafe { &*get_add_block() }, 12);
invoke_assert(
&unsafe { RcBlock::from_raw(get_add_block_with(3)) }.unwrap(),
8,
);
invoke_assert(&StackBlock::new(|a: i32| a + 6), 11);
invoke_assert(&RcBlock::new(|a: i32| a + 6), 11);
invoke_assert(&GLOBAL_BLOCK, 47);
}

#[test]
fn test_add_12() {
#[track_caller]
fn invoke_assert(block: &Add12, expected: i32) {
assert_eq!(
block.call((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)),
expected
);
assert_eq!(
unsafe { invoke_add_12(block, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) },
expected
);
}

global_block! {
static GLOBAL_BLOCK = |
a1: i32, a2: i32, a3: i32, a4: i32,
a5: i32, a6: i32, a7: i32, a8: i32,
a9: i32, a10: i32, a11: i32, a12: i32,
| -> i32 {
a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + 42
};
}

invoke_assert(unsafe { &*get_add_12() }, 78);
invoke_assert(unsafe { &*get_add_12_with(13) }, 91);
let closure = |a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12| {
a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12
};
invoke_assert(&StackBlock::new(closure), 78);
invoke_assert(&RcBlock::new(closure), 78);
invoke_assert(&GLOBAL_BLOCK, 120);
}

#[test]
Expand Down Expand Up @@ -122,44 +208,6 @@ fn test_large_struct_block() {
assert_eq!(unsafe { invoke_large_struct_block(&block, data) }, new_data);
}

global_block! {
/// Test `global_block` in an external crate
static MY_BLOCK = || -> i32 {
42
};
}

#[test]
fn test_global_block() {
assert_eq!(unsafe { invoke_int_block(&MY_BLOCK) }, 42);
}

#[test]
fn test_call_block() {
let block = unsafe { RcBlock::from_raw(get_int_block_with(13)) }.unwrap();
assert_eq!(block.call(()), 13);
}

#[test]
fn test_call_block_args() {
let block = unsafe { RcBlock::from_raw(get_add_block_with(13)) }.unwrap();
assert_eq!(block.call((2,)), 15);
}

#[test]
fn test_create_block() {
let block = StackBlock::new(|| 13);
let result = unsafe { invoke_int_block(&block) };
assert_eq!(result, 13);
}

#[test]
fn test_create_block_args() {
let block = StackBlock::new(|a: i32| a + 5);
let result = unsafe { invoke_add_block(&block, 6) };
assert_eq!(result, 11);
}

#[test]
fn test_block_copy() {
let s = "Hello!".to_string();
Expand Down

0 comments on commit 8a723c1

Please sign in to comment.