Rust中的测试用例的写法
2024-09-17 08:14:21
有点类似
#[derive(Debug)] pub struct Rectangle { length: u32, width: u32, } impl Rectangle { pub fn can_hold(&self, other: &Rectangle) -> bool { self.length > other.length && self.width > other.width } } pub fn add_two(a: i32) -> i32 { a + } pub fn greeting(name: &str) -> String { format!("Hello {}!", name) } pub struct Guess { value: i32, } impl Guess { pub fn new(value: i32) -> Guess { { panic!("Guess value must be great than or equal 1, got {}.", value); } { panic!("Guess value must less than or equal to 100, got {}.", value); } Guess { value } } } #[cfg(test)] mod tests { use super::*; #[test] fn larger_can_hold_smaller() { let larger = Rectangle { length: , width: }; let smaller = Rectangle { length: , width: }; assert!(larger.can_hold(&smaller)); } #[test] fn smaller_cannot_hold_larger() { let larger = Rectangle { length: , width: }; let smaller = Rectangle { length: , width: }; assert!(!smaller.can_hold(&larger)); } #[test] fn it_add_two() { assert_eq!(, add_two()); } #[test] fn greeting_contains_name() { let result = greeting("Carol"); assert!(result.contains("Carol")); } #[test] #[should_panic(expected = "Guess value must be less than or equal to 100")] fn greater_than_100() { Guess::); } /* #[test] fn it_works() -> Result<(), String> { if 2 + 2 == 4 { Ok(()) } else { Err(String::from("two plus two does not equal four")) } } */ }
最新文章
- CSS 后代选择器
- 解析for循环
- 解决在使用client object model的时候报“object does not belong to a list”错误
- UVA 10294 等价类计数
- 保存恢复临时信-Android 中使用onSaveInstanceState和onRestoreInstanceState
- hibernate注解@JoinTable说明
- 经典sql总结(2)
- AS3 IOC框架Spring Actionscript 的使用总结
- insert into select 堵塞update
- sql server数据库区分大小写设置
- iOS实践03
- Bootstrap3 栅格系统-实例:手机、平板、桌面
- QThread的一些使用心得
- jquery.validate.js的简单示例
- ELK部署与使用总结
- [APM] 2个实例+5个维度解读APM技术
- <;Yarn>; <;Capacity Scheduler>; <;Source Code>;
- 14. Longest Common Prefix (截取字符串)
- 【嵌入式】——arm裸机开发 step by step 之 按键控制 LED 和 蜂鸣器
- Python高手之路【七】python基础之模块