Add test case for assignment 12

This commit is contained in:
Minseong Jang
2022-12-06 18:42:13 +09:00
committed by Seungmin Jeon
parent 3f6b16863e
commit 69330fbd0f
3 changed files with 42 additions and 1 deletions

View File

@@ -14,3 +14,4 @@ lazy_static = "1.4.0"
pest = "2.5.1"
pest_derive = "2.5.1"
rayon = "1.6.0"
ntest = "0.9.0"

View File

@@ -17,7 +17,7 @@ pub fn pong(rx1: &mut Receiver<u32>, tx2: &mut Sender<u32>) -> bool {
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>(
s: &'scope thread::Scope<'scope, 'env>,
f1: F1,

View File

@@ -1,5 +1,6 @@
#[cfg(test)]
mod test {
use ntest::timeout;
use super::super::assignment12::*;
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);
}
}