rust/tests/coverage/issue-84561.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
3.4 KiB
Rust
Raw Permalink Normal View History

2021-04-27 07:27:54 +08:00
// This demonstrated Issue #84561: function-like macros produce unintuitive coverage results.
//@ failure-status: 101
2021-04-27 07:27:54 +08:00
#[derive(PartialEq, Eq)]
struct Foo(u32);
#[rustfmt::skip]
2021-04-28 12:45:30 +08:00
fn test3() {
2021-04-27 07:27:54 +08:00
let is_true = std::env::args().len() == 1;
let bar = Foo(1);
assert_eq!(bar, Foo(1));
let baz = Foo(0);
assert_ne!(baz, Foo(1));
println!("{:?}", Foo(1));
println!("{:?}", bar);
println!("{:?}", baz);
assert_eq!(Foo(1), Foo(1));
assert_ne!(Foo(0), Foo(1));
assert_eq!(Foo(2), Foo(2));
2021-04-27 07:27:54 +08:00
let bar = Foo(0);
assert_ne!(bar, Foo(3));
assert_ne!(Foo(0), Foo(4));
2021-04-27 07:27:54 +08:00
assert_eq!(Foo(3), Foo(3), "with a message");
println!("{:?}", bar);
println!("{:?}", Foo(1));
2021-04-27 07:27:54 +08:00
assert_ne!(Foo(0), Foo(5), "{}", if is_true { "true message" } else { "false message" });
assert_ne!(
Foo(0)
,
Foo(5)
,
"{}"
,
if
is_true
{
"true message"
} else {
"false message"
}
);
let is_true = std::env::args().len() == 1;
assert_eq!(
Foo(1),
Foo(1)
);
assert_ne!(
Foo(0),
Foo(1)
);
assert_eq!(
Foo(2),
Foo(2)
);
2021-04-27 07:27:54 +08:00
let bar = Foo(1);
assert_ne!(
2021-04-27 07:27:54 +08:00
bar,
Foo(3)
);
if is_true {
assert_ne!(
Foo(0),
Foo(4)
);
} else {
assert_eq!(
Foo(3),
Foo(3)
);
}
2021-04-27 07:27:54 +08:00
if is_true {
assert_ne!(
Foo(0),
Foo(4),
"with a message"
);
} else {
assert_eq!(
Foo(3),
Foo(3),
"with a message"
);
}
assert_ne!(
if is_true {
Foo(0)
} else {
Foo(1)
},
Foo(5)
);
assert_ne!(
Foo(5),
if is_true {
Foo(0)
} else {
Foo(1)
}
);
assert_ne!(
if is_true {
assert_eq!(
Foo(3),
Foo(3)
);
Foo(0)
} else {
assert_ne!(
if is_true {
Foo(0)
} else {
Foo(1)
},
Foo(5)
);
Foo(1)
},
2021-04-27 07:27:54 +08:00
Foo(5),
"with a message"
);
assert_eq!(
Foo(1),
Foo(3),
"this assert should fail"
);
assert_eq!(
Foo(3),
Foo(3),
"this assert should not be reached"
);
}
impl std::fmt::Debug for Foo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "try and succeed")?;
Ok(())
}
}
static mut DEBUG_LEVEL_ENABLED: bool = false;
macro_rules! debug {
($($arg:tt)+) => (
if unsafe { DEBUG_LEVEL_ENABLED } {
println!($($arg)+);
}
);
}
2021-04-27 07:27:54 +08:00
fn test1() {
debug!("debug is enabled");
debug!("debug is enabled");
let _ = 0;
debug!("debug is enabled");
unsafe {
DEBUG_LEVEL_ENABLED = true;
}
debug!("debug is enabled");
}
2021-04-28 12:45:30 +08:00
macro_rules! call_debug {
($($arg:tt)+) => (
fn call_print(s: &str) {
print!("{}", s);
}
call_print("called from call_debug: ");
debug!($($arg)+);
);
}
fn test2() {
call_debug!("debug is enabled");
}
2021-04-27 07:27:54 +08:00
fn main() {
test1();
test2();
2021-04-28 12:45:30 +08:00
test3();
2021-04-27 07:27:54 +08:00
}