feat: add tests

This commit is contained in:
2024-01-07 23:51:29 +08:00
parent 7ec4c4a526
commit 97baed2e2a
11 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
fn main() {
let arr = [1, 2, 3, 4, 5];
println!("{:?}", arr);
println!("First value: {}", arr[0]);
println!("Last value: {}", arr[arr.len() - 1]);
let sum = arr.into_iter().reduce(|a, b| a + b);
println!("Sum: {:?}", sum);
}

View File

@@ -0,0 +1,13 @@
#[cfg(target_os = "macos")]
fn get_os() -> &'static str {
"macos"
}
#[cfg(not(target_os = "macos"))]
fn get_os() -> &'static str {
"unknown"
}
fn main() {
println!("OS: {}", get_os());
}

View File

@@ -0,0 +1,17 @@
#[derive(Debug)]
struct Object {}
impl Drop for Object {
fn drop(&mut self) {
println!("Object#drop");
}
}
fn main() {
{
let object = Object {};
println!("Dump: {:?}", object);
println!("Block end.");
}
println!("Main end.");
}

View File

@@ -0,0 +1,10 @@
fn print_name(name: String) {
println!("Name: {}", name);
}
fn main() {
let name = String::from("Hatter");
print_name(name);
let name_ref = &name;
println!("Name: {}", name_ref);
}

View File

@@ -0,0 +1,4 @@
fn main() {
let s = "中国";
println!("{}", &s[0..2]);
}

View File

@@ -0,0 +1,10 @@
// https://utf8everywhere.org/
fn main() {
let s1 = "hello";
let s2 = "中国";
let s3 = "🎉🎉";
println!("{}: {} {}", s1, s1.len(), s1.chars().collect::<Vec<_>>().len());
println!("{}: {} {}", s2, s2.len(), s2.chars().collect::<Vec<_>>().len());
println!("{}: {} {}", s3, s3.len(), s3.chars().collect::<Vec<_>>().len());
}

View File

@@ -0,0 +1,11 @@
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[test]
fn test_01() {
assert_eq!(2, add(1, 1));
assert_eq!(3, add(1, 2));
}
fn main() {}

View File

@@ -0,0 +1,8 @@
fn get_person_info() -> (String, i32) {
("Hatter".into(), 40)
}
fn main() {
let (name, age) = get_person_info();
println!("{} is {} year(s) old", name, age);
}

View File

@@ -0,0 +1,9 @@
fn main() {
let mut arr = vec![1, 2, 3, 4, 5];
arr.push(6);
println!("{:?}", arr);
println!("First value: {}", arr[0]);
println!("Last value: {}", arr[arr.len() - 1]);
let sum = arr.into_iter().reduce(|a, b| a + b);
println!("Sum: {:?}", sum);
}

View File

@@ -0,0 +1,7 @@
fn main() {
let mut i = 0;
while i <= 99 {
println!("#i: {}", i);
i += 1;
}
}

View File

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