From 7ba136bf1fb903cfc8ef5a13b5e021516001b51a Mon Sep 17 00:00:00 2001 From: Bastian Bloessl Date: Wed, 13 Nov 2024 23:34:12 +0100 Subject: [PATCH] mocker: add functions to get and take outbuffer --- src/runtime/mocker.rs | 39 ++++++++++++++++++--------------------- tests/tag.rs | 17 +++++++++++++++-- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/runtime/mocker.rs b/src/runtime/mocker.rs index f1a8627c..e47bb2b6 100644 --- a/src/runtime/mocker.rs +++ b/src/runtime/mocker.rs @@ -60,7 +60,7 @@ impl Mocker { /// Initialize output buffer with given size pub fn init_output(&mut self, id: usize, size: usize) where - T: Debug + Send + 'static, + T: Clone + Debug + Send + 'static, { self.block .sio @@ -69,32 +69,26 @@ impl Mocker { } /// Get data from output buffer - pub fn output(&mut self, id: usize) -> Vec + pub fn output(&mut self, id: usize) -> (Vec, Vec) where - T: Debug + Send + 'static, + T: Clone + Debug + Send + 'static, { let w = self.block.sio.output(id).writer_mut(); if let BufferWriter::Host(w) = w { - w.as_any().downcast_mut::>().unwrap().get() + w.as_any().downcast_ref::>().unwrap().get() } else { panic!("mocker: wrong output buffer (expected CPU, got Custom)"); } } - /// Get the list of tags from the output buffer at `id`. - /// - /// Type parameter `T` should be the same as the type of the output buffer. - pub fn output_tags(&mut self, id: usize) -> Vec + /// Taking data from output buffer, freeing up the buffer + pub fn take_output(&mut self, id: usize) -> (Vec, Vec) where - T: Debug + Send + 'static, + T: Clone + Debug + Send + 'static, { let w = self.block.sio.output(id).writer_mut(); if let BufferWriter::Host(w) = w { - w.as_any() - .downcast_mut::>() - .unwrap() - .tags() - .to_vec() + w.as_any().downcast_mut::>().unwrap().take() } else { panic!("mocker: wrong output buffer (expected CPU, got Custom)"); } @@ -207,12 +201,12 @@ impl BufferReaderHost for MockReader { } #[derive(Debug)] -struct MockWriter { +struct MockWriter { data: Vec, tags: Vec, } -impl MockWriter { +impl MockWriter { pub fn new(size: usize) -> Self { MockWriter:: { data: Vec::with_capacity(size), @@ -220,17 +214,20 @@ impl MockWriter { } } - pub fn get(&mut self) -> Vec { - std::mem::take(&mut self.data) + pub fn get(&self) -> (Vec, Vec) { + (self.data.clone(), self.tags.clone()) } - pub fn tags(&self) -> &[ItemTag] { - &self.tags + pub fn take(&mut self) -> (Vec, Vec) { + let (data, tags) = self.get(); + self.data.clear(); + self.tags = Vec::new(); + (data, tags) } } #[async_trait] -impl BufferWriterHost for MockWriter { +impl BufferWriterHost for MockWriter { fn add_reader( &mut self, _reader_inbox: Sender, diff --git a/tests/tag.rs b/tests/tag.rs index da5de6b3..293c0a00 100644 --- a/tests/tag.rs +++ b/tests/tag.rs @@ -30,12 +30,17 @@ fn tags_through_mock() -> Result<()> { mock.init_output::(0, input.len() * 2); mock.input(0, input.clone()); mock.run(); + + let (out_buffer, out_tags) = mock.output::(0); + assert_eq!(out_buffer.len(), 1024); + assert_eq!(out_tags.len(), 0); + mock.input_with_tags(0, input, tags.clone()); mock.run(); mock.deinit(); - let out_tags = mock.output_tags::(0); - + let (out_buffer, out_tags) = mock.output::(0); + assert_eq!(out_buffer.len(), 2048); assert_eq!(out_tags.len(), 3); for (i, tag) in tags.iter().enumerate() { @@ -46,5 +51,13 @@ fn tags_through_mock() -> Result<()> { assert!(matches!(out_tags[i].tag, Tag::Id(t) if t == tag_id)); } + let (out_buffer, out_tags) = mock.take_output::(0); + assert_eq!(out_buffer.len(), 2048); + assert_eq!(out_tags.len(), 3); + + let (out_buffer, out_tags) = mock.output::(0); + assert_eq!(out_buffer.len(), 0); + assert_eq!(out_tags.len(), 0); + Ok(()) }