Skip to content

Commit d141a73

Browse files
committed
threads3: Improve the test
1 parent 631f443 commit d141a73

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

exercises/20_threads/threads3.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use std::{sync::mpsc, thread, time::Duration};
22

33
struct Queue {
4-
length: u32,
54
first_half: Vec<u32>,
65
second_half: Vec<u32>,
76
}
87

98
impl Queue {
109
fn new() -> Self {
1110
Self {
12-
length: 10,
1311
first_half: vec![1, 2, 3, 4, 5],
1412
second_half: vec![6, 7, 8, 9, 10],
1513
}
@@ -48,17 +46,15 @@ mod tests {
4846
fn threads3() {
4947
let (tx, rx) = mpsc::channel();
5048
let queue = Queue::new();
51-
let queue_length = queue.length;
5249

5350
send_tx(queue, tx);
5451

55-
let mut total_received: u32 = 0;
56-
for received in rx {
57-
println!("Got: {received}");
58-
total_received += 1;
52+
let mut received = Vec::with_capacity(10);
53+
for value in rx {
54+
received.push(value);
5955
}
6056

61-
println!("Number of received values: {total_received}");
62-
assert_eq!(total_received, queue_length);
57+
received.sort();
58+
assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
6359
}
6460
}

solutions/20_threads/threads3.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use std::{sync::mpsc, thread, time::Duration};
22

33
struct Queue {
4-
length: u32,
54
first_half: Vec<u32>,
65
second_half: Vec<u32>,
76
}
87

98
impl Queue {
109
fn new() -> Self {
1110
Self {
12-
length: 10,
1311
first_half: vec![1, 2, 3, 4, 5],
1412
second_half: vec![6, 7, 8, 9, 10],
1513
}
@@ -50,17 +48,15 @@ mod tests {
5048
fn threads3() {
5149
let (tx, rx) = mpsc::channel();
5250
let queue = Queue::new();
53-
let queue_length = queue.length;
5451

5552
send_tx(queue, tx);
5653

57-
let mut total_received: u32 = 0;
58-
for received in rx {
59-
println!("Got: {received}");
60-
total_received += 1;
54+
let mut received = Vec::with_capacity(10);
55+
for value in rx {
56+
received.push(value);
6157
}
6258

63-
println!("Number of received values: {total_received}");
64-
assert_eq!(total_received, queue_length);
59+
received.sort();
60+
assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
6561
}
6662
}

0 commit comments

Comments
 (0)