Skip to content

Commit 7763f76

Browse files
committed
misc: add more tests
1 parent 496a245 commit 7763f76

File tree

8 files changed

+211
-5
lines changed

8 files changed

+211
-5
lines changed

tests/channel.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
mod tests {
2+
use std::ops::Add;
3+
use std::sync::mpsc;
4+
use std::thread;
5+
6+
#[test]
7+
fn test_channel() {
8+
let (tx1, rx) = mpsc::channel();
9+
let tx2 = mpsc::Sender::clone(&tx1);
10+
11+
thread::spawn(move || {
12+
for i in 0..4 {
13+
tx1.send(String::from("hi => ").add(i.to_string().as_str()))
14+
.unwrap();
15+
}
16+
});
17+
18+
thread::spawn(move || {
19+
tx2.send(String::from("other")).unwrap();
20+
});
21+
22+
loop {
23+
match rx.recv() {
24+
Ok(str) => println!("{}", str),
25+
Err(_) => {
26+
// println!("none: {}", rec_err.to_string());
27+
break;
28+
}
29+
}
30+
}
31+
}
32+
}

tests/closure.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn test_add() {
5+
let s = |x| x;
6+
println!("{}", s(1234));
7+
}
8+
}

tests/generator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ mod tests {
1010
#[test]
1111
fn test_generator() {
1212
let mut generator = || {
13-
println!("2");
14-
yield;
15-
println!("4");
13+
println!("generator_2");
14+
yield "ss";
15+
println!("generator_4");
1616
};
1717

1818
println!("1");

tests/lifetime.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::fmt::Display;
2+
3+
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
4+
if x.len() > y.len() {
5+
x
6+
} else {
7+
y
8+
}
9+
}
10+
11+
fn longest_2<'a>(x: &'a str, y: &str) -> &'a str {
12+
println!("{}", y);
13+
x
14+
}
15+
16+
fn smaller_size(x: &str, y: &str) -> usize {
17+
if x.len() > y.len() {
18+
x.len()
19+
} else {
20+
y.len()
21+
}
22+
}
23+
24+
fn longest_with_an_announcement<'a, T: Display>(x: &'a str, y: &'a str, ann: T) -> &'a str {
25+
println!("Announcement! {}", ann);
26+
if x.len() > y.len() {
27+
x
28+
} else {
29+
y
30+
}
31+
}
32+
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn test_lifetime() {
38+
let longest: &str = longest("1", "123");
39+
println!("{}", longest);
40+
println!("{}", longest_2("1", "123"));
41+
println!("{}", smaller_size("1", "123"));
42+
println!("{}", longest_with_an_announcement("a", "abc", 1));
43+
}
44+
}

tests/mutex.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::sync::Mutex;
2+
3+
fn mutex() {
4+
let m = Mutex::new(5);
5+
let mut num = m.lock().unwrap();
6+
*num = 1;
7+
println!("{}", num);
8+
}
9+
10+
mod tests {
11+
use super::*;
12+
13+
#[test]
14+
fn test_mutex() {
15+
mutex();
16+
}
17+
}

tests/thread.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::thread;
2+
use std::time::Duration;
3+
4+
fn test() {
5+
println!("in main thread");
6+
7+
let _ = thread::spawn(|| {
8+
thread::sleep(Duration::from_millis(100));
9+
println!("in a thread");
10+
})
11+
.join();
12+
}
13+
14+
fn test_move() {
15+
let v = vec![1, 2, 3];
16+
17+
let handle = thread::spawn(move || {
18+
println!("Here's a vector: {:?}", v);
19+
});
20+
21+
// println!("In main: {:?}", v); value borrowed here after move
22+
23+
handle.join().unwrap();
24+
}
25+
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_thread() {
31+
test();
32+
test_move();
33+
}
34+
}

tests/trait.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::{Display, Error, Formatter};
2+
13
struct Duck;
24

35
struct Pig;
@@ -12,16 +14,45 @@ impl Fly for Duck {
1214
}
1315
}
1416

17+
impl Display for Duck {
18+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
19+
f.write_str(self.fly().to_string().as_str())
20+
}
21+
}
22+
1523
impl Fly for Pig {
1624
fn fly(&self) -> bool {
1725
false
1826
}
1927
}
2028

21-
fn fly_static<T: Fly>(s: T) -> bool {
29+
impl Display for Pig {
30+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
31+
f.write_str(self.fly().to_string().as_str())
32+
}
33+
}
34+
35+
fn fly_static<T: Fly + Display>(s: T) -> bool {
36+
s.fly()
37+
}
38+
39+
// syntax sugar for fly_static
40+
fn fly_static_1(s: impl Fly + Display) -> bool {
41+
s.fly()
42+
}
43+
44+
fn fly_static_2(s: &(impl Fly + Display)) -> bool {
2245
s.fly()
2346
}
2447

48+
fn fly_static_3<T, U>(t: &T, u: &U) -> bool
49+
where
50+
T: Fly + Display,
51+
U: Fly + Display,
52+
{
53+
t.fly() && u.fly()
54+
}
55+
2556
fn fly_dyn(s: &dyn Fly) -> bool {
2657
s.fly()
2758
}
@@ -34,8 +65,11 @@ mod tests {
3465
fn test_trait() {
3566
let pig = Pig;
3667
assert_eq!(fly_static::<Pig>(pig), false);
68+
assert_eq!(fly_static_1(Pig {}), false);
69+
assert_eq!(fly_static_2(&Pig {}), false);
70+
assert_eq!(fly_static_3(&Pig {}, &Pig {}), false);
3771

3872
let duck = Duck;
3973
assert_eq!(fly_dyn(&duck), true);
4074
}
41-
}
75+
}

tests/traits_2.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::fmt::Display;
2+
3+
struct Pair<T> {
4+
x: T,
5+
y: T,
6+
}
7+
8+
impl<T> Pair<T> {
9+
fn new(x: T, y: T) -> Self {
10+
Self { x, y }
11+
}
12+
}
13+
14+
impl<T: Display + PartialOrd> Pair<T> {
15+
fn cmp_display(&self) {
16+
if self.x >= self.y {
17+
println!("The larger member is x = {}", self.x);
18+
} else {
19+
println!("The larger member is y = {}", self.y);
20+
}
21+
}
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn test_trait_2() {
30+
let p = Pair::new(1, 2);
31+
println!("Pair x: {}", p.x);
32+
p.cmp_display();
33+
34+
let pp = Pair::new(&p, &p);
35+
// pp.cmp_display(); trait bounds were not satisfied
36+
}
37+
}

0 commit comments

Comments
 (0)