about summary refs log tree commit diff
path: root/tests/coverage/branch_if.rs
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2023-12-25 21:23:15 +1100
committerZalathar <Zalathar@users.noreply.github.com>2024-03-13 20:43:35 +1100
commit89fab06a772985cad3e117f52a7f5fdb12804fc1 (patch)
treeae0629335d9a72fd0a342561b7ce688c81cc193d /tests/coverage/branch_if.rs
parent762d3170f6d17b541b9306bae1502cef329168e1 (diff)
downloadrust-89fab06a772985cad3e117f52a7f5fdb12804fc1.tar.gz
rust-89fab06a772985cad3e117f52a7f5fdb12804fc1.zip
coverage: Add branch coverage tests (with branch coverage disabled)
Diffstat (limited to 'tests/coverage/branch_if.rs')
-rw-r--r--tests/coverage/branch_if.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/coverage/branch_if.rs b/tests/coverage/branch_if.rs
new file mode 100644
index 00000000000..55ef159ebdf
--- /dev/null
+++ b/tests/coverage/branch_if.rs
@@ -0,0 +1,81 @@
+#![feature(coverage_attribute)]
+//@ edition: 2021
+
+//@ llvm-cov-flags: --show-branches=count
+
+macro_rules! no_merge {
+    () => {
+        for _ in 0..1 {}
+    };
+}
+
+fn branch_not(a: bool) {
+    no_merge!();
+
+    if a {
+        say("a")
+    }
+    if !a {
+        say("not a");
+    }
+    if !!a {
+        say("not not a");
+    }
+    if !!!a {
+        say("not not not a");
+    }
+}
+
+fn branch_not_as(a: bool) {
+    no_merge!();
+
+    if !(a as bool) {
+        say("not (a as bool)");
+    }
+    if !!(a as bool) {
+        say("not not (a as bool)");
+    }
+    if !!!(a as bool) {
+        say("not not (a as bool)");
+    }
+}
+
+fn branch_and(a: bool, b: bool) {
+    no_merge!();
+
+    if a && b {
+        say("both");
+    } else {
+        say("not both");
+    }
+}
+
+fn branch_or(a: bool, b: bool) {
+    no_merge!();
+
+    if a || b {
+        say("either");
+    } else {
+        say("neither");
+    }
+}
+
+#[coverage(off)]
+fn say(message: &str) {
+    core::hint::black_box(message);
+}
+
+#[coverage(off)]
+fn main() {
+    for a in [false, true, true] {
+        branch_not(a);
+        branch_not_as(a);
+    }
+
+    for a in [false, true, true, true, true] {
+        for b in [false, true, true] {
+            branch_and(a, b);
+            branch_or(a, b);
+        }
+    }
+}