feat: add tests

This commit is contained in:
2024-01-07 18:26:09 +08:00
parent 526dd4172b
commit 7ec4c4a526
15 changed files with 248 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
use std::sync::mpsc::channel;
use std::thread;
fn main() {
let (sender, receiver) = channel::<Option<i32>>();
let h = thread::spawn(move || {
loop {
match receiver.recv() {
Ok(o) => match o {
None => {
println!("The end!");
return;
}
Some(i) => println!("Received: {}", i),
}
Err(e) => { // when sender closed
println!("Error: {}", e);
return;
}
}
}
});
sender.send(Some(1)).unwrap();
sender.send(Some(2)).unwrap();
sender.send(Some(3)).unwrap();
sender.send(None).unwrap();
h.join().unwrap();
}

View File

@@ -0,0 +1,20 @@
trait Say {
fn say(&self);
}
struct Dog {}
impl Say for Dog {
fn say(&self) {
println!("Wangwang!");
}
}
fn do_say(s: Box<dyn Say>) {
s.say();
}
fn main() {
let dog = Dog {};
do_say(Box::new(dog));
}

View File

@@ -0,0 +1,19 @@
enum Os {
Windows,
Linux,
Other(String),
}
fn main() {
let os = Os::Other("Unix".to_string());
match &os {
Os::Windows => println!("OS is Windows."),
Os::Other(name) if name == "Unix" => println!("OS is Unix"),
Os::Other(name) => println!("OS is other: {}", name),
_ => {} // IGNORE
}
// if let Os::Windows = os {
// println!("OS is Windows");
// }
}

View File

@@ -0,0 +1,8 @@
fn is_odd(num: i32) -> bool {
num % 2 == 1
}
fn main() {
println!("{} is odd: {}", 9, is_odd(9));
}

View File

@@ -0,0 +1,10 @@
fn main() {
for i in 0..100 {
println!("#i: {}", i);
}
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for i in arr {
println!("#i: {}", i);
}
}

View File

@@ -0,0 +1,10 @@
use std::env::args;
#[macro_export] macro_rules! iff {
($c:expr, $t:expr, $f:expr) => ( if $c { $t } else { $f } )
}
fn main() {
let has_arg = iff!(args().skip(1).len() > 0, true, false);
println!("Has args: {}", has_arg);
}

View File

@@ -0,0 +1,10 @@
fn main() {
let mut i = 0;
loop {
println!("#i: {}", i);
i += 1;
if i > 99 {
break;
}
}
}

View File

@@ -0,0 +1,26 @@
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let count = Arc::new(Mutex::new(0_usize));
let h1 = {
let count = count.clone();
thread::spawn(move || {
let mut c = count.lock().unwrap();
*c += 1;
println!("Thread #1: {}", c);
})
};
let h2 = {
let count = count.clone();
thread::spawn(move || {
let mut c = count.lock().unwrap();
*c += 1;
println!("Thread #2: {}", c);
})
};
h1.join().unwrap();
h2.join().unwrap();
}

View File

@@ -0,0 +1,12 @@
use std::env;
fn get_name() -> Option<String> {
env::var("NAME").ok()
}
fn main() {
match get_name() {
None => println!("No NAME"),
Some(name) => println!("Found NAME: {}", name),
}
}

View File

@@ -0,0 +1,24 @@
enum ParseToIntError {
Empty,
Syntax(String),
}
fn parse_to_int(n: &str) -> Result<i32, ParseToIntError> {
if n.is_empty() {
return Err(ParseToIntError::Empty);
}
match n.parse::<i32>() {
Ok(i) => Ok(i),
Err(e) => Err(ParseToIntError::Syntax(format!("Error:{}", e))),
}
}
fn main() {
match parse_to_int("123") {
Ok(i) => println!("Parsed: {}", i),
Err(e) => match e {
ParseToIntError::Empty => println!("Empty input."),
ParseToIntError::Syntax(m) => println!("Error syntax: {}", m),
}
}
}

View File

@@ -0,0 +1,24 @@
trait Say {
fn say(&self);
}
struct Dog {}
impl Say for Dog {
fn say(&self) {
println!("Wangwang!");
}
}
fn do_say<T>(s: T) where T: Say {
s.say();
}
// fn do_say2<T: Say>(s: T) {
// s.say();
// }
fn main() {
let dog = Dog {};
do_say(dog);
}

View File

@@ -0,0 +1,13 @@
use std::thread;
fn main() {
let h1 = thread::spawn(|| {
println!("Thread #1");
});
let h2 = thread::spawn(|| {
println!("Thread #2");
});
h1.join().unwrap();
h2.join().unwrap();
}

View File

@@ -0,0 +1,26 @@
use std::ops::Deref;
trait WithName {
fn get_name(&self) -> &str;
}
struct Human {
name: String,
}
impl Human {
fn new(name: String) -> Self {
Self { name }
}
}
impl WithName for Human {
fn get_name(&self) -> &str {
self.name.deref()
}
}
fn main() {
let with_name = Human::new("Hatter".into());
println!("With name: {}", with_name.get_name());
}