Skip to content

Commit

Permalink
return empty str error.
Browse files Browse the repository at this point in the history
  • Loading branch information
friendbear committed Jun 19, 2024
1 parent 3dac7a9 commit 6ddf94d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
112 changes: 112 additions & 0 deletions iriam-profile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//! ```
//!
//! For more information, see the documentation of the individual types and methods.
#![allow(dead_code)]
use std::fmt::Display;

type S = LiveStreamer;
Expand Down Expand Up @@ -172,6 +173,55 @@ impl Builder {
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_builder_with_name() {
let streamer = Builder::new().with_name("TestStreamer").build();
assert_eq!(streamer.name, Some("TestStreamer".to_string()));
}

#[test]
fn test_builder_with_mark() {
let streamer = Builder::new().with_mark("TestMark").build();
assert_eq!(streamer.mark, Some("TestMark".to_string()));
}

#[test]
fn test_builder_with_handle_names() {
let handle_names = vec!["@handle1", "@handle2"];
let streamer = Builder::new().with_handle_names(handle_names.clone()).build();
assert_eq!(streamer.handle_names, Some(handle_names.iter().map(|&s| s.to_string()).collect()));
}

#[test]
fn test_builder_full() {
let handle_names = vec!["@handle1", "@handle2"];
let streamer = Builder::new()
.with_name("TestStreamer")
.with_mark("TestMark")
.with_handle_names(handle_names.clone())
.build();
assert_eq!(streamer.name, Some("TestStreamer".to_string()));
assert_eq!(streamer.mark, Some("TestMark".to_string()));
assert_eq!(streamer.handle_names, Some(handle_names.iter().map(|&s| s.to_string()).collect()));
}

#[test]
fn test_display() {
let handle_names = vec!["@handle1", "@handle2"];
let streamer = Builder::new()
.with_name("TestStreamer")
.with_mark("TestMark")
.with_handle_names(handle_names.clone())
.build();
let display_output = format!("{}", streamer);
assert_eq!(display_output, "TestStreamerTestMark\n@handle1, @handle2");
}
}

#[allow(dead_code)]
fn str_sort() {
let mut s: Vec<char> =
Expand Down Expand Up @@ -292,3 +342,65 @@ fn test_reference_anoymous() {
println!("{}", container.get_reference());
println!("{}", container.get_reference_anonymous());
}

#[derive(Debug)]
struct A1<'a> {
a01: &'a str,
a02: i32,
}

impl<'a> A1<'a> {
fn new(a01: &'a str, a02: i32) -> Self {
A1 { a01, a02 }
}

fn get_a01(&self) -> &str {
self.a01
}

fn set_a01(&mut self, a01: &'a str) {
self.a01 = a01;
}
}

#[cfg(test)]
mod tests_lifetime {
use super::*;

// https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html
//fn return_short_lifetime(v: &[String]) -> &str {
// &v.get(3).unwrap_or_else(|| Box::new(&"".to_string())).as_str()
//}

fn return_short_lifetime_unwrap_or(v: &[String]) -> &str {
v.get(3).map(|s| s.as_str()).unwrap_or("")
}

fn return_short_lifetime_return_string(v: &[String]) -> String {
v.get(3).map(|s| s.clone()).unwrap_or_else(|| "".to_string())
}

use std::borrow::Cow;
fn return_short_lifetime_clone_on_write(v: &[String], index: usize) -> Cow<str> {

match v.get(index) {
Some(s) => Cow::Borrowed(s),
None => Cow::Borrowed(""),
}
}

#[test]
/// Lifetime in deep dive
fn test_lifetime_a1() {
let v1 = vec!["aaa".to_string(), "bbb".to_string(), "ccc".to_string()];
let mut a1 = A1::new(&v1[0], 4);
println!("{}", a1.get_a01());
assert_eq!(a1.get_a01(), "aaa");
a1.set_a01(&v1[1]);
assert_eq!(return_short_lifetime_unwrap_or(&v1), "ccc"); // unwrap_or
assert_eq!(return_short_lifetime_return_string(&v1), "ccc"); // return String
assert_eq!(return_short_lifetime_clone_on_write(&v1, 3), "ccc"); // return Cow<str>
let v2 = vec![String::from("aaa"), String::from("bbb"), String::from("ccc")];
let _ = return_short_lifetime_clone_on_write(&v2, 3).into_owned();
}
}
2 changes: 1 addition & 1 deletion write_hello_world_from_any_language_in_the_rust
Submodule write_hello_world_from_any_language_in_the_rust updated from 621950 to d2bdd2

0 comments on commit 6ddf94d

Please sign in to comment.