Skip to content

Commit

Permalink
moved wrappers from tests to main
Browse files Browse the repository at this point in the history
Signed-off-by: Dikshant <[email protected]>
  • Loading branch information
pingu-73 committed Aug 10, 2024
1 parent 1ea64f2 commit fa1e347
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hpx-rs"
version = "0.1.0"
authors = ["Shreyas Atre <[email protected]>", "Dikshant <[email protected]"]
authors = ["Shreyas Atre <[email protected]>", "Dikshant <[email protected]>"]
edition = "2021"
readme = "README.md"
repository = "https://github.com/STEllAR-GROUP/hpx-rs"
Expand Down
94 changes: 51 additions & 43 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,58 +36,66 @@ pub mod ffi {
}

// ================================================================================================
// Tests (to be shifted to systests crate within hpx-rs workspace)
// Wrapper for the above Bindings.
// reffer to tests to understand how to use them. [NOTE: Not all bindings have wrapper.]
// ================================================================================================
#[cfg(test)]
mod tests {
use super::ffi;
use serial_test::serial;
use std::ffi::CString;
use std::os::raw::c_char;
use std::thread;
use std::time::Duration;
use std::ffi::CString;
use std::os::raw::c_char;

fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) {
let c_args: Vec<CString> = args.iter().map(|s| CString::new(*s).unwrap()).collect();
let ptrs: Vec<*mut c_char> = c_args.iter().map(|s| s.as_ptr() as *mut c_char).collect();
(ptrs.len() as i32, ptrs)
}
pub fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) {
let c_args: Vec<CString> = args.iter().map(|s| CString::new(*s).unwrap()).collect();
let ptrs: Vec<*mut c_char> = c_args.iter().map(|s| s.as_ptr() as *mut c_char).collect();
(ptrs.len() as i32, ptrs)
}

fn copy_vector(src: &Vec<i32>) -> Vec<i32> {
let mut dest = vec![0; src.len()];
ffi::hpx_copy(src, &mut dest);
dest
}
pub fn copy_vector(src: &Vec<i32>) -> Vec<i32> {
let mut dest = vec![0; src.len()];
ffi::hpx_copy(src, &mut dest);
dest
}

fn copy_vector_range(src: &Vec<i32>, start: usize, end: usize) -> Vec<i32> {
let slice = &src[start..end];
let mut dest = vec![0; slice.len()];
ffi::hpx_copy(&slice.to_vec(), &mut dest);
dest
}
pub fn copy_vector_range(src: &Vec<i32>, start: usize, end: usize) -> Vec<i32> {
let slice = &src[start..end];
let mut dest = vec![0; slice.len()];
ffi::hpx_copy(&slice.to_vec(), &mut dest);
dest
}

fn copy_n(src: &[i32], count: usize) -> Vec<i32> {
let mut dest = Vec::with_capacity(count);
ffi::hpx_copy_n(&src.to_vec(), count, &mut dest);
dest
}
pub fn copy_n(src: &[i32], count: usize) -> Vec<i32> {
let mut dest = Vec::with_capacity(count);
ffi::hpx_copy_n(&src.to_vec(), count, &mut dest);
dest
}

fn copy_if_positive(src: &Vec<i32>) -> Vec<i32> {
let mut dest = Vec::new();
ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0);
dest
}
pub fn copy_if_positive(src: &Vec<i32>) -> Vec<i32> {
let mut dest = Vec::new();
ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0);
dest
}

fn count(vec: &Vec<i32>, value: i32) -> i64 {
ffi::hpx_count(vec, value)
}
pub fn count(vec: &Vec<i32>, value: i32) -> i64 {
ffi::hpx_count(vec, value)
}

fn find(vec: &Vec<i32>, value: i32) -> Option<usize> {
match ffi::hpx_find(vec, value) {
-1 => None,
index => Some(index as usize),
}
pub fn find(vec: &Vec<i32>, value: i32) -> Option<usize> {
match ffi::hpx_find(vec, value) {
-1 => None,
index => Some(index as usize),
}
}

// ================================================================================================
// Tests (to be shifted to systests crate within hpx-rs workspace)
// ================================================================================================
#[cfg(test)]
mod tests {
use super::ffi;
use crate::{
copy_if_positive, copy_n, copy_vector, copy_vector_range, count, create_c_args, find,
};
use serial_test::serial;
use std::ffi::CString;
use std::os::raw::c_char;

#[test]
#[serial]
Expand Down

0 comments on commit fa1e347

Please sign in to comment.