Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: capi protobuf cstring convension #1145

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ fn test_c_api_list_options() {
);
}

#[test]
fn test_c_api_parse_file() {
test_c_api_without_wrapper::<ParseFileArgs, ParseFileResult>(
"KclvmService.ParseFile",
"parse-file.json",
"parse-file.response.json",
);
}

#[test]
fn test_c_api_testing() {
test_c_api::<TestArgs, TestResult, _>(
Expand Down Expand Up @@ -259,10 +268,18 @@ where
CString::from_vec_unchecked(serde_json::from_str::<A>(&input).unwrap().encode_to_vec())
};
let call = CString::new(svc_name).unwrap();
let result_ptr = kclvm_service_call(serv, call.as_ptr(), args.as_ptr());
let result = unsafe { CStr::from_ptr(result_ptr) };
let mut result_len: usize = 0;
let src_ptr =
kclvm_service_call_with_length(serv, call.as_ptr(), args.as_ptr(), &mut result_len);

let mut dest_data: Vec<u8> = Vec::with_capacity(result_len);
unsafe {
let dest_ptr: *mut u8 = dest_data.as_mut_ptr();
std::ptr::copy_nonoverlapping(src_ptr as *const u8, dest_ptr, result_len);
dest_data.set_len(result_len);
}

let mut result = R::decode(result.to_bytes()).unwrap();
let mut result = R::decode(dest_data.as_slice()).unwrap();
let result_json = serde_json::to_string(&result).unwrap();

let except_result_path = Path::new(TEST_DATA_PATH).join(output);
Expand All @@ -278,7 +295,7 @@ where
assert_eq!(result, except_result, "\nresult json is {result_json}");
unsafe {
kclvm_service_delete(serv);
kclvm_service_free_string(result_ptr as *mut c_char);
kclvm_service_free_string(src_ptr as *mut c_char);
}
}

Expand Down
Loading
Loading