From 899480e8151832c66090fc4d95bdc126126bcac9 Mon Sep 17 00:00:00 2001 From: Andreja Tonev Date: Tue, 27 Feb 2024 19:35:51 +0100 Subject: [PATCH 1/3] Bugfix: Inverting bool value in rust --- rust/rsmgp-sys/src/value/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/rsmgp-sys/src/value/mod.rs b/rust/rsmgp-sys/src/value/mod.rs index e8262f1ab..f53975a86 100644 --- a/rust/rsmgp-sys/src/value/mod.rs +++ b/rust/rsmgp-sys/src/value/mod.rs @@ -506,7 +506,7 @@ pub(crate) unsafe fn mgp_raw_value_to_value( match invoke_mgp_func!(mgp_value_type, ffi::mgp_value_get_type, value).unwrap() { mgp_value_type::MGP_VALUE_TYPE_NULL => Ok(Value::Null), mgp_value_type::MGP_VALUE_TYPE_BOOL => Ok(Value::Bool( - invoke_mgp_func!(::std::os::raw::c_int, ffi::mgp_value_get_bool, value).unwrap() == 0, + invoke_mgp_func!(::std::os::raw::c_int, ffi::mgp_value_get_bool, value).unwrap() != 0, )), mgp_value_type::MGP_VALUE_TYPE_INT => Ok(Value::Int( invoke_mgp_func!(i64, ffi::mgp_value_get_int, value).unwrap(), From d5839147648b6b9d0543f0e35b698e5263e3ecac Mon Sep 17 00:00:00 2001 From: Andreja Tonev Date: Wed, 3 Apr 2024 14:35:54 +0200 Subject: [PATCH 2/3] Added tests --- rust/rsmgp-sys/src/value/tests.rs | 179 ++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) diff --git a/rust/rsmgp-sys/src/value/tests.rs b/rust/rsmgp-sys/src/value/tests.rs index bb36638aa..49a5fea0b 100644 --- a/rust/rsmgp-sys/src/value/tests.rs +++ b/rust/rsmgp-sys/src/value/tests.rs @@ -49,6 +49,40 @@ fn test_make_false_bool_mgp_value() { }); } +#[test] +#[serial] +fn test_convert_false_bool_mgp_value() { + mock_mgp_once!(mgp_value_get_bool_context, |_, out| { + unsafe { + *out = 0; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + mock_mgp_once!(mgp_value_get_type_context, |_, out| { + unsafe { + *out = mgp_value_type::MGP_VALUE_TYPE_BOOL; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + + with_dummy!(|memgraph: &Memgraph| { + unsafe { + let res = mgp_raw_value_to_value(null_mut(), &memgraph); + match res { + Ok(value) => match value { + Value::Bool(value) => assert!(value == false, "Wrong boolean value"), + _ => { + assert!(false, "Value is not a Bool") + } + }, + _ => { + assert!(false, "Failed to convert raw value") + } + } + } + }); +} + #[test] #[serial] fn test_make_true_bool_mgp_value() { @@ -63,6 +97,40 @@ fn test_make_true_bool_mgp_value() { }); } +#[test] +#[serial] +fn test_convert_true_bool_mgp_value() { + mock_mgp_once!(mgp_value_get_bool_context, |_, out| { + unsafe { + *out = 1; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + mock_mgp_once!(mgp_value_get_type_context, |_, out| { + unsafe { + *out = mgp_value_type::MGP_VALUE_TYPE_BOOL; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + + with_dummy!(|memgraph: &Memgraph| { + unsafe { + let res = mgp_raw_value_to_value(null_mut(), &memgraph); + match res { + Ok(value) => match value { + Value::Bool(value) => assert!(value == true, "Wrong boolean value"), + _ => { + assert!(false, "Value is not a Bool") + } + }, + _ => { + assert!(false, "Failed to convert raw value") + } + } + } + }); +} + #[test] #[serial] fn test_make_int_mgp_value() { @@ -77,6 +145,40 @@ fn test_make_int_mgp_value() { }); } +#[test] +#[serial] +fn test_convert_int_mgp_value() { + mock_mgp_once!(mgp_value_get_int_context, |_, out| { + unsafe { + *out = 123; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + mock_mgp_once!(mgp_value_get_type_context, |_, out| { + unsafe { + *out = mgp_value_type::MGP_VALUE_TYPE_INT; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + + with_dummy!(|memgraph: &Memgraph| { + unsafe { + let res = mgp_raw_value_to_value(null_mut(), &memgraph); + match res { + Ok(value) => match value { + Value::Int(value) => assert!(value == 123, "Wrong integer value"), + _ => { + assert!(false, "Value is not an Int") + } + }, + _ => { + assert!(false, "Failed to convert raw value") + } + } + } + }); +} + #[test] #[serial] fn test_make_double_mgp_value() { @@ -91,6 +193,40 @@ fn test_make_double_mgp_value() { }); } +#[test] +#[serial] +fn test_convert_double_mgp_value() { + mock_mgp_once!(mgp_value_get_double_context, |_, out| { + unsafe { + *out = 1.23; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + mock_mgp_once!(mgp_value_get_type_context, |_, out| { + unsafe { + *out = mgp_value_type::MGP_VALUE_TYPE_DOUBLE; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + + with_dummy!(|memgraph: &Memgraph| { + unsafe { + let res = mgp_raw_value_to_value(null_mut(), &memgraph); + match res { + Ok(value) => match value { + Value::Float(value) => assert!(value == 1.23, "Wrong double value"), + _ => { + assert!(false, "Value is not a Double") + } + }, + _ => { + assert!(false, "Failed to convert raw value") + } + } + } + }); +} + #[test] #[serial] fn test_make_string_mgp_value() { @@ -105,6 +241,49 @@ fn test_make_string_mgp_value() { }); } +#[test] +#[serial] +fn test_convert_string_mgp_value() { + let cstr = CString::new("a string").expect("CString::new failed"); + mock_mgp_once!(mgp_value_get_string_context, move |_, out| { + unsafe { + *out = cstr.as_ptr() as *const i8; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + mock_mgp_once!(mgp_value_get_type_context, |_, out| { + unsafe { + *out = mgp_value_type::MGP_VALUE_TYPE_STRING; + } + mgp_error::MGP_ERROR_NO_ERROR + }); + + with_dummy!(|memgraph: &Memgraph| { + unsafe { + let res = mgp_raw_value_to_value(null_mut(), &memgraph); + match res { + Ok(value) => match value { + Value::String(value) => match value.to_str() { + Ok(vs) => match vs { + "a string" => {} + _ => assert!(false, "Wrong string value"), + }, + _ => { + assert!(false, "Value is not a String") + } + }, + _ => { + assert!(false, "Value is not a String") + } + }, + _ => { + assert!(false, "Failed to convert raw value") + } + } + } + }); +} + #[test] #[serial] fn test_make_list_mgp_value() { From abc17f3c7931f4b58174d72b6f15b73fed58c36d Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 15 Dec 2025 14:13:34 +0000 Subject: [PATCH 3/3] fix test on arm --- rust/rsmgp-sys/src/value/tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/rsmgp-sys/src/value/tests.rs b/rust/rsmgp-sys/src/value/tests.rs index 49a5fea0b..1c13ee29a 100644 --- a/rust/rsmgp-sys/src/value/tests.rs +++ b/rust/rsmgp-sys/src/value/tests.rs @@ -15,6 +15,7 @@ use c_str_macro::c_str; use serial_test::serial; use std::ffi::CStr; +use std::os::raw::c_char; use std::ptr::null_mut; use super::*; @@ -247,7 +248,7 @@ fn test_convert_string_mgp_value() { let cstr = CString::new("a string").expect("CString::new failed"); mock_mgp_once!(mgp_value_get_string_context, move |_, out| { unsafe { - *out = cstr.as_ptr() as *const i8; + *out = cstr.as_ptr() as *const c_char; } mgp_error::MGP_ERROR_NO_ERROR });