]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/coverage/issue-84561.rs
Rollup merge of #102854 - semarie:openbsd-immutablestack, r=m-ou-se
[rust.git] / src / test / run-make / coverage / issue-84561.rs
1 // This demonstrated Issue #84561: function-like macros produce unintuitive coverage results.
2
3 // expect-exit-status-101
4 #[derive(PartialEq, Eq)]
5 struct Foo(u32);
6 fn test3() {
7     let is_true = std::env::args().len() == 1;
8     let bar = Foo(1);
9     assert_eq!(bar, Foo(1));
10     let baz = Foo(0);
11     assert_ne!(baz, Foo(1));
12     println!("{:?}", Foo(1));
13     println!("{:?}", bar);
14     println!("{:?}", baz);
15
16     assert_eq!(Foo(1), Foo(1));
17     assert_ne!(Foo(0), Foo(1));
18     assert_eq!(Foo(2), Foo(2));
19     let bar = Foo(0);
20     assert_ne!(bar, Foo(3));
21     assert_ne!(Foo(0), Foo(4));
22     assert_eq!(Foo(3), Foo(3), "with a message");
23     println!("{:?}", bar);
24     println!("{:?}", Foo(1));
25
26     assert_ne!(Foo(0), Foo(5), "{}", if is_true { "true message" } else { "false message" });
27     assert_ne!(
28         Foo(0)
29         ,
30         Foo(5)
31         ,
32         "{}"
33         ,
34         if
35         is_true
36         {
37             "true message"
38         } else {
39             "false message"
40         }
41     );
42
43     let is_true = std::env::args().len() == 1;
44
45     assert_eq!(
46         Foo(1),
47         Foo(1)
48     );
49     assert_ne!(
50         Foo(0),
51         Foo(1)
52     );
53     assert_eq!(
54         Foo(2),
55         Foo(2)
56     );
57     let bar = Foo(1);
58     assert_ne!(
59         bar,
60         Foo(3)
61     );
62     if is_true {
63         assert_ne!(
64             Foo(0),
65             Foo(4)
66         );
67     } else {
68         assert_eq!(
69             Foo(3),
70             Foo(3)
71         );
72     }
73     if is_true {
74         assert_ne!(
75             Foo(0),
76             Foo(4),
77             "with a message"
78         );
79     } else {
80         assert_eq!(
81             Foo(3),
82             Foo(3),
83             "with a message"
84         );
85     }
86     assert_ne!(
87         if is_true {
88             Foo(0)
89         } else {
90             Foo(1)
91         },
92         Foo(5)
93     );
94     assert_ne!(
95         Foo(5),
96         if is_true {
97             Foo(0)
98         } else {
99             Foo(1)
100         }
101     );
102     assert_ne!(
103         if is_true {
104             assert_eq!(
105                 Foo(3),
106                 Foo(3)
107             );
108             Foo(0)
109         } else {
110             assert_ne!(
111                 if is_true {
112                     Foo(0)
113                 } else {
114                     Foo(1)
115                 },
116                 Foo(5)
117             );
118             Foo(1)
119         },
120         Foo(5),
121         "with a message"
122     );
123     assert_eq!(
124         Foo(1),
125         Foo(3),
126         "this assert should fail"
127     );
128     assert_eq!(
129         Foo(3),
130         Foo(3),
131         "this assert should not be reached"
132     );
133 }
134
135 impl std::fmt::Debug for Foo {
136     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
137         write!(f, "try and succeed")?;
138         Ok(())
139     }
140 }
141
142 static mut DEBUG_LEVEL_ENABLED: bool = false;
143
144 macro_rules! debug {
145     ($($arg:tt)+) => (
146         if unsafe { DEBUG_LEVEL_ENABLED } {
147             println!($($arg)+);
148         }
149     );
150 }
151
152 fn test1() {
153     debug!("debug is enabled");
154     debug!("debug is enabled");
155     let _ = 0;
156     debug!("debug is enabled");
157     unsafe {
158         DEBUG_LEVEL_ENABLED = true;
159     }
160     debug!("debug is enabled");
161 }
162
163 macro_rules! call_debug {
164     ($($arg:tt)+) => (
165         fn call_print(s: &str) {
166             print!("{}", s);
167         }
168
169         call_print("called from call_debug: ");
170         debug!($($arg)+);
171     );
172 }
173
174 fn test2() {
175     call_debug!("debug is enabled");
176 }
177
178 fn main() {
179     test1();
180     test2();
181     test3();
182 }