首页 > 其他分享 >[Rust Unit testing] assert & assert_eq

[Rust Unit testing] assert & assert_eq

时间:2024-02-23 18:58:06浏览次数:15  
标签:even tests testing module assert test eq


pub fn is_even(num: i32) -> bool {
    num % 2 == 0
}

/*
This attribute indicates that the following module is a conditional compilation module that should only be compiled and run when tests are being executed. In other words, this code will not be included in the final build unless it's being compiled for testing.
*/
#[cfg(test)]
// Defines a test module named tests. Rust's testing framework recognizes this as a special module for holding tests.
mod tests { 
    // This line imports all items from the parent module into the scope of the test module. This is necessary because the tests module is technically a separate module, and it needs explicit access to the functions (like is_even) defined in the parent module that are being tested.
    use super::*;

    // This attribute marks a function as a test case. When you run cargo test, Rust's test runner will execute all functions annotated with #[test].
    #[test]
    fn is_true_when_even() {
        assert!(is_even(4));
    }

    #[test]
    fn is_false_when_odd() {
        assert!(!is_even(5));
    }
}

assert!(is_even(4)); uses the assert! macro to ensure that is_even(4) returns true. If is_even(4) returns false, the test will fail, indicating a problem with the is_even function's ability to correctly identify even numbers.

 

#[cfg(test)]
mod tests {
    #[test]
    fn you_can_assert_eq() {
        assert_eq!(5, 5);
    }
}

 

标签:even,tests,testing,module,assert,test,eq
From: https://www.cnblogs.com/Answer1215/p/18030214

相关文章

  • [Rust Unit testing] test should_panic
    structRectangle{width:i32,height:i32}implRectangle{//Onlychangethetestfunctionsthemselvespubfnnew(width:i32,height:i32)->Self{ifwidth<=0||height<=0{panic!("Rectanglewi......
  • 异步线程处理request,导致参数获取为null
    原文链接:千万不要把Request传递到异步线程里面!有坑!一、post接口接口参数: 后端代码:@PostMapping("/postTest")publicStringpostTest(HttpServletRequestrequest,HttpServletResponseresponse){//AsyncContextasyncContext=request.startAsync(request,re......
  • 在sequence 中 通过后门方式调用task
     可以使用void‘($cast(slaver_drv_use,uvm_top_find("xxxx")));在sequence中调用svt_axi_slave_agent(component) 的task。代码示意 svt_axi_slave_agent   slaver_drv_use;声明句柄void‘($cast(slaver_drv_use,uvm_top_find("uvm_test_top.te_env_inst.amba_s......
  • 读论文-基于序列模式的电子商务推荐系统综述(A Survey of Sequential Pattern Based E
    前言今天读的论文为一篇于2023年10月3日发表在《算法》(Algorithms)的论文,这篇文章综述了基于序列模式的电子商务推荐系统,强调了通过整合用户购买和点击行为的序列模式来提高推荐准确性、减少数据稀疏性、增加推荐新颖性,并改善推荐系统的可扩展性。文章详细分析了现有推荐系统的......
  • SciTech-Mathmatics-FourierSeries: Time Domain and Frequency Domain
    TimeDomainandFrequencyDomainFrequencydomain:measuredbySpectrumAnalysiszerTellsushowproperties(amplitudes)changeoverfrequencies:TimeDomain:measuredbyOscilloscopeTellsushowproperties(suchasAmplitude(Power),Phase,andsoon)......
  • C# 采用HttpWebRequest 、WebClient和HttpClient下载https的文件异常问题
    今天有个客户反应,程序下载文件失败,无法正常使用。远程客户电脑后,查看错误日志,都是提示https:****************************.dll等一系列文件的下载错误提示提示基础连接已经关闭:发送时发生错误。在网上找了很多方案都没有能解决,大多都是https链接时增加指定协议,很遗憾未能解......
  • stl源码解析,deque的insert_aux
    直接上结论:deque的insert_aux中插入开始会pushback或front一个和最末尾或最前面值相同的值是为了看是否需要扩充deque内存,选这个值应该是顺手。stl中deque的实现是通过一个存储指向各个存储区域指针的map(注意就是个指针地图,不是stl的map数据结构),里面再指向对应区域去存储实际......
  • 一般处理程序中的IRequiresSessionState
    一般处理程序中,还未跑入主程序,就直接断掉了,按F12提示错误:异常详细信息:System.Web.HttpException:请求在此上下文中不可用第一次遇见这种情况,百思不得其解,通过与以前的代码对比,发现少了一个接口IRequiresSessionState,加上后可以了!!! 接口的简介:在一般处理程序中,如果要使用Se......
  • requests基础
    笔记requests模块-urllib模块-requests模块request模块:python中原生的一款基于网络请求的模块,功能非常强大,简单便捷,效率极高。作用:模拟浏览器发请求。如何使用:(requests模块的编码流程)-指定url-发起请求-获取响应数据-持久化存储环境......
  • Go - concurrent processing is not always faster than sequential processing
      ......