mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-14 22:18:46 +00:00
Add test case for assignment 12
This commit is contained in:
committed by
Seungmin Jeon
parent
3f6b16863e
commit
69330fbd0f
@@ -14,3 +14,4 @@ lazy_static = "1.4.0"
|
|||||||
pest = "2.5.1"
|
pest = "2.5.1"
|
||||||
pest_derive = "2.5.1"
|
pest_derive = "2.5.1"
|
||||||
rayon = "1.6.0"
|
rayon = "1.6.0"
|
||||||
|
ntest = "0.9.0"
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ pub fn pong(rx1: &mut Receiver<u32>, tx2: &mut Sender<u32>) -> bool {
|
|||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes the given functions (f1, f2) and returns the results.
|
/// Executes the given functions (f1, f2) in concurrent and returns the results.
|
||||||
pub fn use_scoped_thread<'scope, 'env, T1, T2, F1, F2>(
|
pub fn use_scoped_thread<'scope, 'env, T1, T2, F1, F2>(
|
||||||
s: &'scope thread::Scope<'scope, 'env>,
|
s: &'scope thread::Scope<'scope, 'env>,
|
||||||
f1: F1,
|
f1: F1,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use ntest::timeout;
|
||||||
use super::super::assignment12::*;
|
use super::super::assignment12::*;
|
||||||
|
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
@@ -41,4 +42,43 @@ mod test {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[timeout(5000)]
|
||||||
|
fn test_scoped_thread_concurrent() {
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
let m = Mutex::new(0);
|
||||||
|
let (r1, r2) = thread::scope(|s| {
|
||||||
|
use_scoped_thread(
|
||||||
|
s,
|
||||||
|
|| {
|
||||||
|
for i in 0..100 {
|
||||||
|
loop {
|
||||||
|
let mut a = m.lock().unwrap();
|
||||||
|
if *a == 2 * i {
|
||||||
|
*a += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thread::current().id()
|
||||||
|
},
|
||||||
|
|| {
|
||||||
|
for i in 0..100 {
|
||||||
|
loop {
|
||||||
|
let mut a = m.lock().unwrap();
|
||||||
|
if *a == 2 * i + 1 {
|
||||||
|
*a += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thread::current().id()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
assert!(r1 != r2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user