From 4f6140258f438fdc180bad140d313af472207fac Mon Sep 17 00:00:00 2001 From: zhuyunxing Date: Thu, 4 Jul 2024 17:59:38 +0800 Subject: coverage. Group mcdc tests in one directory --- tests/coverage/mcdc/if.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/coverage/mcdc/if.rs (limited to 'tests/coverage/mcdc/if.rs') diff --git a/tests/coverage/mcdc/if.rs b/tests/coverage/mcdc/if.rs new file mode 100644 index 00000000000..a85843721c6 --- /dev/null +++ b/tests/coverage/mcdc/if.rs @@ -0,0 +1,103 @@ +#![feature(coverage_attribute)] +//@ edition: 2021 +//@ min-llvm-version: 18 +//@ compile-flags: -Zcoverage-options=mcdc +//@ llvm-cov-flags: --show-mcdc + +fn mcdc_check_neither(a: bool, b: bool) { + if a && b { + say("a and b"); + } else { + say("not both"); + } +} + +fn mcdc_check_a(a: bool, b: bool) { + if a && b { + say("a and b"); + } else { + say("not both"); + } +} + +fn mcdc_check_b(a: bool, b: bool) { + if a && b { + say("a and b"); + } else { + say("not both"); + } +} + +fn mcdc_check_both(a: bool, b: bool) { + if a && b { + say("a and b"); + } else { + say("not both"); + } +} + +fn mcdc_check_tree_decision(a: bool, b: bool, c: bool) { + // This expression is intentionally written in a way + // where 100% branch coverage indicates 100% mcdc coverage. + if a && (b || c) { + say("pass"); + } else { + say("reject"); + } +} + +fn mcdc_check_not_tree_decision(a: bool, b: bool, c: bool) { + // Contradict to `mcdc_check_tree_decision`, + // 100% branch coverage of this expression does not mean indicates 100% mcdc coverage. + if (a || b) && c { + say("pass"); + } else { + say("reject"); + } +} + +fn mcdc_nested_if(a: bool, b: bool, c: bool) { + if a || b { + say("a or b"); + if b && c { + say("b and c"); + } + } else { + say("neither a nor b"); + } +} + +#[coverage(off)] +fn main() { + mcdc_check_neither(false, false); + mcdc_check_neither(false, true); + + mcdc_check_a(true, true); + mcdc_check_a(false, true); + + mcdc_check_b(true, true); + mcdc_check_b(true, false); + + mcdc_check_both(false, true); + mcdc_check_both(true, true); + mcdc_check_both(true, false); + + mcdc_check_tree_decision(false, true, true); + mcdc_check_tree_decision(true, true, false); + mcdc_check_tree_decision(true, false, false); + mcdc_check_tree_decision(true, false, true); + + mcdc_check_not_tree_decision(false, true, true); + mcdc_check_not_tree_decision(true, true, false); + mcdc_check_not_tree_decision(true, false, false); + mcdc_check_not_tree_decision(true, false, true); + + mcdc_nested_if(true, false, true); + mcdc_nested_if(true, true, true); + mcdc_nested_if(true, true, false); +} + +#[coverage(off)] +fn say(message: &str) { + core::hint::black_box(message); +} -- cgit 1.4.1-3-g733a5 From c77788f011ee04cfc825926d89647af5ac3f6aa5 Mon Sep 17 00:00:00 2001 From: zhuyunxing Date: Tue, 2 Jul 2024 14:24:43 +0800 Subject: coverage. MCDC tests also report branches coverage --- tests/coverage/mcdc/if.coverage | 28 ++++++++++++++++++++++++++- tests/coverage/mcdc/if.rs | 2 +- tests/coverage/mcdc/nested_if.coverage | 25 +++++++++++++++++++++++- tests/coverage/mcdc/nested_if.rs | 2 +- tests/coverage/mcdc/non_control_flow.coverage | 25 +++++++++++++++++++++++- tests/coverage/mcdc/non_control_flow.rs | 2 +- 6 files changed, 78 insertions(+), 6 deletions(-) (limited to 'tests/coverage/mcdc/if.rs') diff --git a/tests/coverage/mcdc/if.coverage b/tests/coverage/mcdc/if.coverage index c2ed311a5bc..91fff073d0c 100644 --- a/tests/coverage/mcdc/if.coverage +++ b/tests/coverage/mcdc/if.coverage @@ -2,12 +2,15 @@ LL| |//@ edition: 2021 LL| |//@ min-llvm-version: 18 LL| |//@ compile-flags: -Zcoverage-options=mcdc - LL| |//@ llvm-cov-flags: --show-mcdc + LL| |//@ llvm-cov-flags: --show-branches=count --show-mcdc LL| | LL| 2|fn mcdc_check_neither(a: bool, b: bool) { LL| 2| if a && b { ^0 ------------------ + | Branch (LL:8): [True: 0, False: 2] + | Branch (LL:13): [True: 0, False: 0] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:14) | | Number of Conditions: 2 @@ -34,6 +37,9 @@ LL| 2| if a && b { ^1 ------------------ + | Branch (LL:8): [True: 1, False: 1] + | Branch (LL:13): [True: 1, False: 0] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:14) | | Number of Conditions: 2 @@ -60,6 +66,9 @@ LL| 2|fn mcdc_check_b(a: bool, b: bool) { LL| 2| if a && b { ------------------ + | Branch (LL:8): [True: 2, False: 0] + | Branch (LL:13): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:14) | | Number of Conditions: 2 @@ -87,6 +96,9 @@ LL| 3| if a && b { ^2 ------------------ + | Branch (LL:8): [True: 2, False: 1] + | Branch (LL:13): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:14) | | Number of Conditions: 2 @@ -117,6 +129,10 @@ LL| 4| if a && (b || c) { ^3 ^2 ------------------ + | Branch (LL:8): [True: 3, False: 1] + | Branch (LL:14): [True: 1, False: 2] + | Branch (LL:19): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:21) | | Number of Conditions: 3 @@ -150,6 +166,10 @@ LL| 4| if (a || b) && c { ^1 ------------------ + | Branch (LL:9): [True: 3, False: 1] + | Branch (LL:14): [True: 1, False: 0] + | Branch (LL:20): [True: 2, False: 2] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:21) | | Number of Conditions: 3 @@ -180,6 +200,9 @@ LL| 3| if a || b { ^0 ------------------ + | Branch (LL:8): [True: 3, False: 0] + | Branch (LL:13): [True: 0, False: 0] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:14) | | Number of Conditions: 2 @@ -200,6 +223,9 @@ LL| 3| if b && c { ^2 ------------------ + | Branch (LL:12): [True: 2, False: 1] + | Branch (LL:17): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:12) to (LL:18) | | Number of Conditions: 2 diff --git a/tests/coverage/mcdc/if.rs b/tests/coverage/mcdc/if.rs index a85843721c6..d8e6b61a9d5 100644 --- a/tests/coverage/mcdc/if.rs +++ b/tests/coverage/mcdc/if.rs @@ -2,7 +2,7 @@ //@ edition: 2021 //@ min-llvm-version: 18 //@ compile-flags: -Zcoverage-options=mcdc -//@ llvm-cov-flags: --show-mcdc +//@ llvm-cov-flags: --show-branches=count --show-mcdc fn mcdc_check_neither(a: bool, b: bool) { if a && b { diff --git a/tests/coverage/mcdc/nested_if.coverage b/tests/coverage/mcdc/nested_if.coverage index 19529cd6aa4..a273a713a8a 100644 --- a/tests/coverage/mcdc/nested_if.coverage +++ b/tests/coverage/mcdc/nested_if.coverage @@ -2,12 +2,17 @@ LL| |//@ edition: 2021 LL| |//@ min-llvm-version: 18 LL| |//@ compile-flags: -Zcoverage-options=mcdc - LL| |//@ llvm-cov-flags: --show-mcdc + LL| |//@ llvm-cov-flags: --show-branches=count --show-mcdc LL| | LL| 4|fn nested_if_in_condition(a: bool, b: bool, c: bool) { LL| 4| if a && if b || c { true } else { false } { ^3 ^2 ^2 ^1 ------------------ + | Branch (LL:8): [True: 3, False: 1] + | Branch (LL:13): [True: 2, False: 1] + | Branch (LL:16): [True: 1, False: 2] + | Branch (LL:21): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:46) | | Number of Conditions: 2 @@ -53,6 +58,13 @@ LL| 4| if a && if b || if c && d { true } else { false } { false } else { true } { ^3 ^2 ^1 ^1 ^1 ^2 ^1 ------------------ + | Branch (LL:8): [True: 3, False: 1] + | Branch (LL:13): [True: 1, False: 2] + | Branch (LL:16): [True: 1, False: 2] + | Branch (LL:21): [True: 1, False: 1] + | Branch (LL:24): [True: 1, False: 1] + | Branch (LL:29): [True: 1, False: 0] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:78) | | Number of Conditions: 2 @@ -117,6 +129,10 @@ LL| 3| if a && if b { false } else { true } { ^2 ^1 ^1 ------------------ + | Branch (LL:8): [True: 2, False: 1] + | Branch (LL:13): [True: 1, False: 1] + | Branch (LL:16): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:41) | | Number of Conditions: 2 @@ -145,6 +161,13 @@ LL| 7| if a && if b || c { if d && e { true } else { false } } else { false } { ^6 ^5 ^5 ^2 ^1 ^4 ^1 ------------------ + | Branch (LL:8): [True: 6, False: 1] + | Branch (LL:13): [True: 1, False: 5] + | Branch (LL:16): [True: 1, False: 5] + | Branch (LL:21): [True: 4, False: 1] + | Branch (LL:28): [True: 2, False: 3] + | Branch (LL:33): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:8) to (LL:75) | | Number of Conditions: 2 diff --git a/tests/coverage/mcdc/nested_if.rs b/tests/coverage/mcdc/nested_if.rs index 3d869771f75..f5068b5dcc2 100644 --- a/tests/coverage/mcdc/nested_if.rs +++ b/tests/coverage/mcdc/nested_if.rs @@ -2,7 +2,7 @@ //@ edition: 2021 //@ min-llvm-version: 18 //@ compile-flags: -Zcoverage-options=mcdc -//@ llvm-cov-flags: --show-mcdc +//@ llvm-cov-flags: --show-branches=count --show-mcdc fn nested_if_in_condition(a: bool, b: bool, c: bool) { if a && if b || c { true } else { false } { diff --git a/tests/coverage/mcdc/non_control_flow.coverage b/tests/coverage/mcdc/non_control_flow.coverage index cd733885a98..6ae796e8ed2 100644 --- a/tests/coverage/mcdc/non_control_flow.coverage +++ b/tests/coverage/mcdc/non_control_flow.coverage @@ -2,7 +2,7 @@ LL| |//@ edition: 2021 LL| |//@ min-llvm-version: 18 LL| |//@ compile-flags: -Zcoverage-options=mcdc - LL| |//@ llvm-cov-flags: --show-mcdc + LL| |//@ llvm-cov-flags: --show-branches=count --show-mcdc LL| | LL| |// This test ensures that boolean expressions that are not inside control flow LL| |// decisions are correctly instrumented. @@ -13,6 +13,9 @@ LL| 3| let x = a && b; ^2 ------------------ + | Branch (LL:13): [True: 2, False: 1] + | Branch (LL:18): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:13) to (LL:19) | | Number of Conditions: 2 @@ -38,6 +41,9 @@ LL| 3| let x = a || b; ^1 ------------------ + | Branch (LL:13): [True: 2, False: 1] + | Branch (LL:18): [True: 0, False: 1] + ------------------ |---> MC/DC Decision Region (LL:13) to (LL:19) | | Number of Conditions: 2 @@ -62,6 +68,10 @@ LL| 4| let x = a || b && c; ^2 ^1 ------------------ + | Branch (LL:13): [True: 2, False: 2] + | Branch (LL:18): [True: 1, False: 1] + | Branch (LL:23): [True: 1, False: 0] + ------------------ |---> MC/DC Decision Region (LL:13) to (LL:24) | | Number of Conditions: 3 @@ -89,6 +99,10 @@ LL| 4| let x = a && b || c; ^2 ^3 ------------------ + | Branch (LL:13): [True: 2, False: 2] + | Branch (LL:18): [True: 1, False: 1] + | Branch (LL:23): [True: 2, False: 1] + ------------------ |---> MC/DC Decision Region (LL:13) to (LL:24) | | Number of Conditions: 3 @@ -116,6 +130,12 @@ LL| 3| let x = a && (b && (c && (d && (e)))); ^2 ^1 ^1 ^1 ------------------ + | Branch (LL:13): [True: 2, False: 1] + | Branch (LL:19): [True: 1, False: 1] + | Branch (LL:25): [True: 1, False: 0] + | Branch (LL:31): [True: 1, False: 0] + | Branch (LL:36): [True: 1, False: 0] + ------------------ |---> MC/DC Decision Region (LL:13) to (LL:42) | | Number of Conditions: 5 @@ -151,6 +171,9 @@ LL| 3| foo(a && b); ^2 ------------------ + | Branch (LL:9): [True: 2, False: 1] + | Branch (LL:14): [True: 1, False: 1] + ------------------ |---> MC/DC Decision Region (LL:9) to (LL:15) | | Number of Conditions: 2 diff --git a/tests/coverage/mcdc/non_control_flow.rs b/tests/coverage/mcdc/non_control_flow.rs index 85c0a6c6ae5..77e64e6625b 100644 --- a/tests/coverage/mcdc/non_control_flow.rs +++ b/tests/coverage/mcdc/non_control_flow.rs @@ -2,7 +2,7 @@ //@ edition: 2021 //@ min-llvm-version: 18 //@ compile-flags: -Zcoverage-options=mcdc -//@ llvm-cov-flags: --show-mcdc +//@ llvm-cov-flags: --show-branches=count --show-mcdc // This test ensures that boolean expressions that are not inside control flow // decisions are correctly instrumented. -- cgit 1.4.1-3-g733a5