From d141a73493ede10cb5d97cdd16b08560fa44de25 Mon Sep 17 00:00:00 2001 From: mo8it Date: Tue, 20 Aug 2024 13:35:07 +0200 Subject: [PATCH] threads3: Improve the test --- exercises/20_threads/threads3.rs | 14 +++++--------- solutions/20_threads/threads3.rs | 14 +++++--------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/exercises/20_threads/threads3.rs b/exercises/20_threads/threads3.rs index 8aa7291..6d16bd9 100644 --- a/exercises/20_threads/threads3.rs +++ b/exercises/20_threads/threads3.rs @@ -1,7 +1,6 @@ use std::{sync::mpsc, thread, time::Duration}; struct Queue { - length: u32, first_half: Vec, second_half: Vec, } @@ -9,7 +8,6 @@ struct Queue { impl Queue { fn new() -> Self { Self { - length: 10, first_half: vec![1, 2, 3, 4, 5], second_half: vec![6, 7, 8, 9, 10], } @@ -48,17 +46,15 @@ mod tests { fn threads3() { let (tx, rx) = mpsc::channel(); let queue = Queue::new(); - let queue_length = queue.length; send_tx(queue, tx); - let mut total_received: u32 = 0; - for received in rx { - println!("Got: {received}"); - total_received += 1; + let mut received = Vec::with_capacity(10); + for value in rx { + received.push(value); } - println!("Number of received values: {total_received}"); - assert_eq!(total_received, queue_length); + received.sort(); + assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); } } diff --git a/solutions/20_threads/threads3.rs b/solutions/20_threads/threads3.rs index cd2dfbe..7ceefea 100644 --- a/solutions/20_threads/threads3.rs +++ b/solutions/20_threads/threads3.rs @@ -1,7 +1,6 @@ use std::{sync::mpsc, thread, time::Duration}; struct Queue { - length: u32, first_half: Vec, second_half: Vec, } @@ -9,7 +8,6 @@ struct Queue { impl Queue { fn new() -> Self { Self { - length: 10, first_half: vec![1, 2, 3, 4, 5], second_half: vec![6, 7, 8, 9, 10], } @@ -50,17 +48,15 @@ mod tests { fn threads3() { let (tx, rx) = mpsc::channel(); let queue = Queue::new(); - let queue_length = queue.length; send_tx(queue, tx); - let mut total_received: u32 = 0; - for received in rx { - println!("Got: {received}"); - total_received += 1; + let mut received = Vec::with_capacity(10); + for value in rx { + received.push(value); } - println!("Number of received values: {total_received}"); - assert_eq!(total_received, queue_length); + received.sort(); + assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); } }