From 177333f93acecc2830a52238711ad96b1fb17c6e Mon Sep 17 00:00:00 2001 From: Dikshant Date: Sun, 21 Jul 2024 18:22:02 +0530 Subject: [PATCH] added hpx::equal Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 8 +++++ hpx-sys/src/lib.rs | 76 ++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index fb43239..38f15a9 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -102,3 +102,11 @@ inline bool hpx_ends_with(rust::Slice src, dest.begin(), dest.end(), std::equal_to()); } + +inline bool hpx_equal(rust::Slice src, rust::Slice dest) { + return hpx::equal( + hpx::execution::par, + src.begin(), src.end(), + dest.begin(), dest.end() + ); +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index c421969..b88d769 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -24,6 +24,7 @@ pub mod ffi { fn hpx_count(vec: &Vec, value: i32) -> i64; fn hpx_count_if(vec: &Vec, pred: fn(i32) -> bool) -> i64; fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool; + fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; } } @@ -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 = 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 = 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() @@ -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); + } + } }