Skip to content

Commit

Permalink
added hpx::equal
Browse files Browse the repository at this point in the history
Signed-off-by: Dikshant <[email protected]>
  • Loading branch information
pingu-73 committed Jul 21, 2024
1 parent 8988ec2 commit 177333f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
8 changes: 8 additions & 0 deletions hpx-sys/include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ inline bool hpx_ends_with(rust::Slice<const int32_t> src,
dest.begin(), dest.end(),
std::equal_to<int32_t>());
}

inline bool hpx_equal(rust::Slice<const int32_t> src, rust::Slice<const int32_t> dest) {
return hpx::equal(
hpx::execution::par,
src.begin(), src.end(),
dest.begin(), dest.end()
);
}
76 changes: 56 additions & 20 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod ffi {
fn hpx_count(vec: &Vec<i32>, value: i32) -> i64;
fn hpx_count_if(vec: &Vec<i32>, pred: fn(i32) -> bool) -> i64;
fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool;
fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool;
}
}

Expand Down Expand Up @@ -220,26 +221,26 @@ mod tests {
let (argc, mut argv) = create_c_args(&["test_hpx_ends_with"]);

let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 {
let vec1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let vec2 = vec![8, 9, 10];
let vec3 = vec![7, 8, 9];
let vec4 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let vec5: Vec<i32> = vec![];

// checking with complete vectors
assert!(ffi::hpx_ends_with(&vec1, &vec2));
assert!(!ffi::hpx_ends_with(&vec1, &vec3));
assert!(ffi::hpx_ends_with(&vec1, &vec4));
assert!(ffi::hpx_ends_with(&vec1, &vec5));
assert!(ffi::hpx_ends_with(&vec5, &vec5));

// checking for vector slices
assert!(ffi::hpx_ends_with(&vec1[5..], &vec2));
assert!(ffi::hpx_ends_with(&vec1[..], &vec1[8..]));
assert!(!ffi::hpx_ends_with(&vec1[..5], &vec2));
assert!(ffi::hpx_ends_with(&vec1[..5], &vec1[3..5]));

assert!(ffi::hpx_ends_with(&vec1, &[]));
let v1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let v2 = vec![8, 9, 10];
let v3 = vec![7, 8, 9];
let v4 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let v5: Vec<i32> = vec![];

// passing vectors
assert!(ffi::hpx_ends_with(&v1, &v2));
assert!(!ffi::hpx_ends_with(&v1, &v3));
assert!(ffi::hpx_ends_with(&v1, &v4));
assert!(ffi::hpx_ends_with(&v1, &v5));
assert!(ffi::hpx_ends_with(&v5, &v5));

// passing slices
assert!(ffi::hpx_ends_with(&v1[5..], &v2));
assert!(ffi::hpx_ends_with(&v1[..], &v1[8..]));
assert!(!ffi::hpx_ends_with(&v1[..5], &v2));
assert!(ffi::hpx_ends_with(&v1[..5], &v1[3..5]));

assert!(ffi::hpx_ends_with(&v1, &[]));
assert!(ffi::hpx_ends_with(&[], &[]));

ffi::finalize()
Expand All @@ -250,4 +251,39 @@ mod tests {
assert_eq!(result, 0);
}
}

#[test]
#[serial]
fn test_hpx_equal() {
let (argc, mut argv) = create_c_args(&["test_hpx_equal"]);

let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 {
let v1 = vec![1, 2, 3, 4, 5];
let v2 = vec![1, 2, 3, 4, 5];
let v3 = vec![1, 2, 3, 4, 6];
let v4 = vec![1, 2, 3, 4];
let v5 = vec![0, 1, 2, 3, 4, 5, 6];

// passing vectors
assert!(ffi::hpx_equal(&v1, &v2));
assert!(!ffi::hpx_equal(&v1, &v3));
assert!(!ffi::hpx_equal(&v1, &v4));

// passing slices
assert!(ffi::hpx_equal(&v1[..], &v2[..]));
assert!(ffi::hpx_equal(&v1[1..4], &v2[1..4]));
assert!(ffi::hpx_equal(&v1[..3], &v4[..3]));
assert!(ffi::hpx_equal(&v1[..], &v5[1..6]));

assert!(ffi::hpx_equal(&v1, &v5[1..6]));
assert!(!ffi::hpx_equal(&v1[..4], &v3));

ffi::finalize()
};

unsafe {
let result = ffi::init(hpx_main, argc, argv.as_mut_ptr());
assert_eq!(result, 0);
}
}
}

0 comments on commit 177333f

Please sign in to comment.