Skip to content

Commit

Permalink
[eclipse-iceoryx#210] Move tests to integrationtests
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Jul 14, 2024
1 parent f0c0530 commit c3151a9
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 205 deletions.
1 change: 1 addition & 0 deletions iceoryx2-ffi/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ iceoryx2-ffi-macros = { workspace = true }

[dev-dependencies]
iceoryx2-bb-testing = { workspace = true }
generic-tests = { workspace = true }
142 changes: 0 additions & 142 deletions iceoryx2-ffi/ffi/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,145 +337,3 @@ pub unsafe extern "C" fn iox2_node_drop(node_handle: iox2_node_h) {
}

// END C API

#[cfg(test)]
mod test {
use crate::*;
use iceoryx2_bb_testing::assert_that;

fn create_sut_node() -> iox2_node_h {
unsafe {
let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut());

let mut node_name_handle = std::ptr::null_mut();
let node_name = "hypnotoad";
let ret_val = iox2_node_name_new(
std::ptr::null_mut(),
node_name.as_ptr() as *const _,
node_name.len() as _,
&mut node_name_handle,
);
assert_that!(ret_val, eq(IOX2_OK));
iox2_node_builder_set_name(
iox2_cast_node_builder_ref_h(node_builder_handle),
node_name_handle,
);

let mut node_handle: iox2_node_h = std::ptr::null_mut();
let ret_val = iox2_node_builder_create(
node_builder_handle,
std::ptr::null_mut(),
iox2_service_type_e::IPC,
&mut node_handle as *mut iox2_node_h,
);

assert_that!(ret_val, eq(IOX2_OK));

node_handle
}
}

#[test]
fn basic_node_api_test() {
unsafe {
let node_handle = create_sut_node();

assert_that!(node_handle, ne(std::ptr::null_mut()));

iox2_node_drop(node_handle);
}
}

#[test]
fn basic_node_config_test() {
unsafe {
let node_handle = create_sut_node();
let expected_config = (*iox2_node_t::cast(node_handle))
.value
.as_ref()
.ipc
.config();

let config = iox2_node_config(node_handle);

assert_that!(*(config as *const Config), eq(*expected_config));

iox2_node_drop(node_handle);
}
}

#[test]
fn basic_node_name_test() {
unsafe {
let node_handle = create_sut_node();
let expected_node_name = (*iox2_node_t::cast(node_handle)).value.as_ref().ipc.name();
assert_that!(expected_node_name.as_str(), eq("hypnotoad"));

let node_name = iox2_node_name(node_handle);

assert_that!(*(node_name as *const NodeName), eq(*expected_node_name));

iox2_node_drop(node_handle);
}
}

#[derive(Default)]
struct NodeListCtx {
alive: u64,
dead: u64,
inaccessible: u64,
undefined: u64,
}

extern "C" fn node_list_callback(
node_state: iox2_node_state_e,
_node_id_ptr: iox2_node_id_ptr,
_node_name_ptr: iox2_node_name_ptr,
_config_ptr: iox2_config_ptr,
ctx: iox2_node_list_callback_context,
) -> iox2_callback_progression_e {
let ctx = unsafe { &mut *(ctx as *mut NodeListCtx) };

match node_state {
iox2_node_state_e::ALIVE => {
ctx.alive += 1;
}
iox2_node_state_e::DEAD => {
ctx.dead += 1;
}
iox2_node_state_e::INACCESSIBLE => {
ctx.inaccessible += 1;
}
iox2_node_state_e::UNDEFINED => {
ctx.undefined += 1;
}
}

iox2_callback_progression_e::CONTINUE
}

#[test]
fn basic_node_list_test() {
unsafe {
let mut ctx = NodeListCtx::default();
let node_handle = create_sut_node();
let config = iox2_node_config(node_handle);

let ret_val = iox2_node_list(
iox2_service_type_e::IPC,
config,
node_list_callback,
&mut ctx as *mut _ as *mut _,
);

iox2_node_drop(node_handle);

assert_that!(ret_val, eq(IOX2_OK));

assert_that!(ctx.alive, eq(1));
assert_that!(ctx.dead, eq(0));
assert_that!(ctx.inaccessible, eq(0));
assert_that!(ctx.undefined, eq(0));
}
}
}
25 changes: 0 additions & 25 deletions iceoryx2-ffi/ffi/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,3 @@ pub unsafe extern "C" fn iox2_node_builder_create(
}

// END C API

#[cfg(test)]
mod test {
use crate::*;
use iceoryx2_bb_testing::assert_that;

#[test]
fn basic_node_builder_api_test() {
unsafe {
let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut());
let mut node_handle: iox2_node_h = std::ptr::null_mut();
let ret_val = iox2_node_builder_create(
node_builder_handle,
std::ptr::null_mut(),
iox2_service_type_e::LOCAL,
&mut node_handle as *mut iox2_node_h,
);

assert_that!(ret_val, eq(IOX2_OK));
assert_that!(node_handle, ne(std::ptr::null_mut()));

iox2_node_drop(node_handle);
}
}
}
38 changes: 0 additions & 38 deletions iceoryx2-ffi/ffi/src/node_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,41 +203,3 @@ pub unsafe extern "C" fn iox2_node_name_drop(node_name_handle: iox2_node_name_h)
}

// END C API

#[cfg(test)]
mod test {
use super::*;

use iceoryx2_bb_testing::assert_that;

#[test]
fn basic_node_name_test() -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let expected_node_name = NodeName::new("hypnotaod")?;

let mut node_name_handle: iox2_node_name_h = std::ptr::null_mut();
let ret_val = iox2_node_name_new(
std::ptr::null_mut(),
expected_node_name.as_str().as_ptr() as *const _,
expected_node_name.len() as _,
&mut node_name_handle,
);
assert_that!(ret_val, eq(IOX2_OK));

let mut node_name_len = 0;
let node_name_c_str = iox2_node_name_as_c_str(
iox2_cast_node_name_ptr(node_name_handle),
&mut node_name_len,
);

let slice = slice::from_raw_parts(node_name_c_str as *const _, node_name_len as _);
let node_name = str::from_utf8(slice)?;

assert_that!(node_name, eq(expected_node_name.as_str()));

iox2_node_name_drop(node_name_handle);

Ok(())
}
}
}
65 changes: 65 additions & 0 deletions iceoryx2-ffi/ffi/tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![allow(dead_code)]

use iceoryx2::prelude::*;
use iceoryx2_bb_testing::assert_that;
use iceoryx2_ffi::*;

pub(crate) trait ServiceTypeMapping {
fn service_type() -> iox2_service_type_e;
}

impl ServiceTypeMapping for iceoryx2::service::zero_copy::Service {
fn service_type() -> iox2_service_type_e {
iox2_service_type_e::IPC
}
}

impl ServiceTypeMapping for iceoryx2::service::process_local::Service {
fn service_type() -> iox2_service_type_e {
iox2_service_type_e::LOCAL
}
}

#[cfg(test)]
pub(crate) fn create_node<S: Service + ServiceTypeMapping>(node_name: &str) -> iox2_node_h {
unsafe {
let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut());

let mut node_name_handle = std::ptr::null_mut();
let ret_val = iox2_node_name_new(
std::ptr::null_mut(),
node_name.as_ptr() as *const _,
node_name.len() as _,
&mut node_name_handle,
);
assert_that!(ret_val, eq(IOX2_OK));
iox2_node_builder_set_name(
iox2_cast_node_builder_ref_h(node_builder_handle),
node_name_handle,
);

let mut node_handle: iox2_node_h = std::ptr::null_mut();
let ret_val = iox2_node_builder_create(
node_builder_handle,
std::ptr::null_mut(),
S::service_type(),
&mut node_handle as *mut iox2_node_h,
);

assert_that!(ret_val, eq(IOX2_OK));

node_handle
}
}
47 changes: 47 additions & 0 deletions iceoryx2-ffi/ffi/tests/node_builder_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

mod common;

#[generic_tests::define]
mod node_builder {

use crate::common::*;
use iceoryx2::prelude::*;
use iceoryx2_bb_testing::assert_that;
use iceoryx2_ffi::*;

#[test]
fn basic_node_builder_api_test<S: Service + ServiceTypeMapping>() {
unsafe {
let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut());
let mut node_handle: iox2_node_h = std::ptr::null_mut();
let ret_val = iox2_node_builder_create(
node_builder_handle,
std::ptr::null_mut(),
S::service_type(),
&mut node_handle as *mut iox2_node_h,
);

assert_that!(ret_val, eq(IOX2_OK));
assert_that!(node_handle, ne(std::ptr::null_mut()));

iox2_node_drop(node_handle);
}
}

#[instantiate_tests(<iceoryx2::service::zero_copy::Service>)]
mod ipc {}

#[instantiate_tests(<iceoryx2::service::process_local::Service>)]
mod local {}
}
48 changes: 48 additions & 0 deletions iceoryx2-ffi/ffi/tests/node_name_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use iceoryx2::prelude::*;
use iceoryx2_bb_testing::assert_that;
use iceoryx2_ffi::*;

use core::{slice, str};

#[test]
fn basic_node_name_test() -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let expected_node_name = NodeName::new("hypnotaod")?;

let mut node_name_handle: iox2_node_name_h = std::ptr::null_mut();
let ret_val = iox2_node_name_new(
std::ptr::null_mut(),
expected_node_name.as_str().as_ptr() as *const _,
expected_node_name.len() as _,
&mut node_name_handle,
);
assert_that!(ret_val, eq(IOX2_OK));

let mut node_name_len = 0;
let node_name_c_str = iox2_node_name_as_c_str(
iox2_cast_node_name_ptr(node_name_handle),
&mut node_name_len,
);

let slice = slice::from_raw_parts(node_name_c_str as *const _, node_name_len as _);
let node_name = str::from_utf8(slice)?;

assert_that!(node_name, eq(expected_node_name.as_str()));

iox2_node_name_drop(node_name_handle);

Ok(())
}
}
Loading

0 comments on commit c3151a9

Please sign in to comment.