feat: add tests
This commit is contained in:
7
__std/misc/Cargo.lock
generated
Normal file
7
__std/misc/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "misc"
|
||||||
|
version = "0.1.0"
|
||||||
8
__std/misc/Cargo.toml
Normal file
8
__std/misc/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "misc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
31
__std/misc/src/bin/channel_test.rs
Normal file
31
__std/misc/src/bin/channel_test.rs
Normal 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();
|
||||||
|
}
|
||||||
20
__std/misc/src/bin/dynamic_bind_test.rs
Normal file
20
__std/misc/src/bin/dynamic_bind_test.rs
Normal 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));
|
||||||
|
}
|
||||||
19
__std/misc/src/bin/enum_test.rs
Normal file
19
__std/misc/src/bin/enum_test.rs
Normal 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");
|
||||||
|
// }
|
||||||
|
}
|
||||||
8
__std/misc/src/bin/fn_test.rs
Normal file
8
__std/misc/src/bin/fn_test.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fn is_odd(num: i32) -> bool {
|
||||||
|
num % 2 == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("{} is odd: {}", 9, is_odd(9));
|
||||||
|
}
|
||||||
|
|
||||||
10
__std/misc/src/bin/for_test.rs
Normal file
10
__std/misc/src/bin/for_test.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
__std/misc/src/bin/iff_test.rs
Normal file
10
__std/misc/src/bin/iff_test.rs
Normal 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);
|
||||||
|
}
|
||||||
10
__std/misc/src/bin/loop_test.rs
Normal file
10
__std/misc/src/bin/loop_test.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut i = 0;
|
||||||
|
loop {
|
||||||
|
println!("#i: {}", i);
|
||||||
|
i += 1;
|
||||||
|
if i > 99 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
__std/misc/src/bin/mutex_test.rs
Normal file
26
__std/misc/src/bin/mutex_test.rs
Normal 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();
|
||||||
|
}
|
||||||
12
__std/misc/src/bin/option_test.rs
Normal file
12
__std/misc/src/bin/option_test.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
24
__std/misc/src/bin/result_test.rs
Normal file
24
__std/misc/src/bin/result_test.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
__std/misc/src/bin/static_bind_test.rs
Normal file
24
__std/misc/src/bin/static_bind_test.rs
Normal 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);
|
||||||
|
}
|
||||||
13
__std/misc/src/bin/thread_test.rs
Normal file
13
__std/misc/src/bin/thread_test.rs
Normal 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();
|
||||||
|
}
|
||||||
26
__std/misc/src/bin/trait_struct_test.rs
Normal file
26
__std/misc/src/bin/trait_struct_test.rs
Normal 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());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user