首页 > 其他分享 >[Rust Unit testing] test should_panic

[Rust Unit testing] test should_panic

时间:2024-02-23 18:56:08浏览次数:20  
标签:i32 testing should width Rectangle test Rust

struct Rectangle {
    width: i32,
    height: i32
}

impl Rectangle {
    // Only change the test functions themselves
    pub fn new(width: i32, height: i32) -> Self {
        if width <= 0 || height <= 0 {
            panic!("Rectangle width and height cannot be negative!")
        }
        Rectangle {width, height}
    }
}

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

    #[test]
    fn correct_width_and_height() {
        // This test should check if the rectangle is the size that we pass into its constructor
        let rect = Rectangle::new(10, 20);
        assert_eq!(rect.width, 10); // check width
        assert_eq!(rect.height, 20); // check height
    }

    #[test]
    #[should_panic]
    fn negative_width() {
        // This test should check if program panics when we try to create rectangle with negative width
        let _rect = Rectangle::new(-10, 10);

    }

    #[test]
    #[should_panic]
    fn negative_height() {
        // This test should check if program panics when we try to create rectangle with negative height
        let _rect = Rectangle::new(10, -10);
    }
}

 

标签:i32,testing,should,width,Rectangle,test,Rust
From: https://www.cnblogs.com/Answer1215/p/18030225

相关文章

  • Rust 智能指针
    本文在原文基础上有删减,原文参考Rust智能指针目录使用Box指向堆上的数据使用Box在堆上储存数据Box允许创建递归类型conslist的更多内容计算非递归类型的大小使用Box<T>给递归类型一个已知的大小通过Dereftrait将智能指针当作常规引用处理追踪指针的值像引用一样使用B......
  • [Rust] Implicitly returning values from functions
    Codehaserror:fnmain(){letanswer=square(3);println!("Thesquareof3is{}",answer);}fnsquare(num:i32)->i32{num*num;}Error:⚠️Compilingofexercises/functions/functions5.rsfailed!Pleasetryagain.Here&#......
  • [Rust] Shadowing
    Refto:https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing fnmain(){letnumber="T-H-R-E-E";//don'tchangethislineprintln!("SpellaNumber:{}",number);number=3;//don'......
  • [Rust] Handle errors in Rust using Pattern Matching
    Inthislessonwe'llexplorehowtounwrapa Result typeusingalanguagefeaturecalledPatternMatching. usestd::io;fnmain(){letmutfirst=String::new();io::stdin().read_line(&mutfirst).unwrap();letmuta:u32=......
  • [Rust] Exit a program using std::process in Rust
    Inthislessonwe'lllearnhowtoexitaprogramusingthe std::process moduleinRustandit's exit() method. usestd::io;usestd::process;fnmain(){letmutfirst=String::new();io::stdin().read_line(&mutfirst).unwrap()......
  • [Rust] Create a loop in Rust
    ThislessonshowshowtouseaRust loop torunaprograminfinitely. usestd::io;usestd::process;fnmain(){loop{println!("Pleaseenterafirstnumber:");leta=read_user_input();println!("Plea......
  • [Rust] Handle errors in Rust using expect()
    Thislessondiscusseshowtoimproveerrorhandlingbyconfiguringcustomerrormessagesusingthe expect() function. usestd::io;fnmain(){letmutfirst=String::new();io::stdin().read_line(&mutfirst);//Notrecommnedto......
  • [Rust] Char vs String
    usestd::path::PathBuf;useclap::Parser;#[derive(Parser,Debug)]#[clap()]pubstructOpts{pubargs:Vec<String>,#[clap(short='c',long="config")]pubconfig:Option<PathBuf>,#[clap(short='p'......
  • Rust 编译报 spurious network error (1 tries remaining): [7] Couldn't connect to
    现象:当执行 cargobuild时报类似错误:warning:spuriousnetworkerror(3triesremaining):[7]Couldn'tconnecttoserver(Failedtoconnectto127.0.0.1port8899after2040ms:Couldn'tconnecttoserver);class=Net(12)warning:spuriousnetworkerror......
  • Docker 运行图形界面版 aTrust
    1、Docker、Docker-Compose安装https://www.cnblogs.com/a120608yby/p/9883175.htmlhttps://www.cnblogs.com/a120608yby/p/14582853.html2、服务Docker-Compose配置#catdocker-compose.ymlversion:'3'services:atrust:image:hagb/docker-atrustc......