]> git.lizzy.rs Git - rust.git/blob - tests/run-make/coverage/assert.rs
Auto merge of #106520 - ehuss:update-mdbook, r=Mark-Simulacrum
[rust.git] / tests / run-make / coverage / assert.rs
1 #![allow(unused_assignments)]
2 // expect-exit-status-101
3
4 fn might_fail_assert(one_plus_one: u32) {
5     println!("does 1 + 1 = {}?", one_plus_one);
6     assert_eq!(1 + 1, one_plus_one, "the argument was wrong");
7 }
8
9 fn main() -> Result<(),u8> {
10     let mut countdown = 10;
11     while countdown > 0 {
12         if countdown == 1 {
13             might_fail_assert(3);
14         } else if countdown < 5 {
15             might_fail_assert(2);
16         }
17         countdown -= 1;
18     }
19     Ok(())
20 }
21
22 // Notes:
23 //   1. Compare this program and its coverage results to those of the very similar test
24 //      `panic_unwind.rs`, and similar tests `abort.rs` and `try_error_result.rs`.
25 //   2. This test confirms the coverage generated when a program passes or fails an `assert!()` or
26 //      related `assert_*!()` macro.
27 //   3. Notably, the `assert` macros *do not* generate `TerminatorKind::Assert`. The macros produce
28 //      conditional expressions, `TerminatorKind::SwitchInt` branches, and a possible call to
29 //      `begin_panic_fmt()` (that begins a panic unwind, if the assertion test fails).
30 //   4. `TerminatoKind::Assert` is, however, also present in the MIR generated for this test
31 //      (and in many other coverage tests). The `Assert` terminator is typically generated by the
32 //      Rust compiler to check for runtime failures, such as numeric overflows.