From 35768ea149edbc9acaf8cabceba52802fe43ad18 Mon Sep 17 00:00:00 2001 From: Matthew Gregan Date: Tue, 11 Feb 2025 09:51:48 +1300 Subject: [PATCH] test_capi: Create a test ffi::cubeb_stream rather than using a nullptr. --- cubeb-backend/tests/test_capi.rs | 67 +++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/cubeb-backend/tests/test_capi.rs b/cubeb-backend/tests/test_capi.rs index f6b55ad..4cedc78 100644 --- a/cubeb-backend/tests/test_capi.rs +++ b/cubeb-backend/tests/test_capi.rs @@ -232,7 +232,7 @@ fn test_ops_context_device_collection_destroy() { #[test] fn test_ops_stream_latency() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); + let s: *mut ffi::cubeb_stream = get_stream(); let mut latency = u32::max_value(); assert_eq!( unsafe { OPS.stream_get_latency.unwrap()(s, &mut latency) }, @@ -243,7 +243,7 @@ fn test_ops_stream_latency() { #[test] fn test_ops_stream_set_volume() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); + let s: *mut ffi::cubeb_stream = get_stream(); unsafe { OPS.stream_set_volume.unwrap()(s, 0.5); } @@ -251,7 +251,7 @@ fn test_ops_stream_set_volume() { #[test] fn test_ops_stream_set_name() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); + let s: *mut ffi::cubeb_stream = get_stream(); unsafe { OPS.stream_set_name.unwrap()(s, CStr::from_bytes_with_nul(b"test\0").unwrap().as_ptr()); } @@ -259,7 +259,7 @@ fn test_ops_stream_set_name() { #[test] fn test_ops_stream_current_device() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); + let s: *mut ffi::cubeb_stream = get_stream(); let mut device: *mut ffi::cubeb_device = ptr::null_mut(); assert_eq!( unsafe { OPS.stream_get_current_device.unwrap()(s, &mut device) }, @@ -270,7 +270,7 @@ fn test_ops_stream_current_device() { #[test] fn test_ops_stream_set_input_mute() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); + let s: *mut ffi::cubeb_stream = get_stream(); assert_eq!( unsafe { OPS.stream_set_input_mute.unwrap()(s, 1) }, ffi::CUBEB_OK @@ -279,7 +279,7 @@ fn test_ops_stream_set_input_mute() { #[test] fn test_ops_stream_set_input_processing_params() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); + let s: *mut ffi::cubeb_stream = get_stream(); assert_eq!( unsafe { OPS.stream_set_input_processing_params.unwrap()( @@ -291,14 +291,6 @@ fn test_ops_stream_set_input_processing_params() { ); } -#[test] -fn test_ops_stream_device_destroy() { - let s: *mut ffi::cubeb_stream = ptr::null_mut(); - unsafe { - OPS.stream_device_destroy.unwrap()(s, 0xDEAD_BEEF as *mut _); - } -} - fn get_ctx() -> *mut ffi::cubeb { CONTEXT.get_or_init(TestContextPtr::new).ptr } @@ -330,3 +322,50 @@ impl Drop for TestContextPtr { unsafe { OPS.destroy.unwrap()(self.ptr) } } } + +fn get_stream() -> *mut ffi::cubeb_stream { + STREAM.get_or_init(TestStreamPtr::new).ptr +} + +static STREAM: OnceLock = OnceLock::new(); + +struct TestStreamPtr { + ptr: *mut ffi::cubeb_stream, +} + +// Safety: ffi::cubeb_stream implementations are expected to be thread-safe. +unsafe impl Send for TestStreamPtr {} +unsafe impl Sync for TestStreamPtr {} + +impl TestStreamPtr { + fn new() -> Self { + let c: *mut ffi::cubeb = get_ctx(); + let mut s: *mut ffi::cubeb_stream = ptr::null_mut(); + assert_eq!( + unsafe { + OPS.stream_init.unwrap()( + c, + &mut s, + ptr::null(), + ptr::null(), + ptr::null_mut(), + ptr::null(), + ptr::null_mut(), + 0, + None, + None, + ptr::null_mut(), + ) + }, + ffi::CUBEB_OK + ); + assert!(!s.is_null()); + TestStreamPtr { ptr: s } + } +} + +impl Drop for TestStreamPtr { + fn drop(&mut self) { + unsafe { OPS.stream_destroy.unwrap()(self.ptr) } + } +}