| 1 | | import Foundation |
| 2 | | |
| 3 | | /// How many branch outcomes or MC/DC conditions a file, module or run has and how many |
| 4 | | /// were covered (DESIGN.md D4). |
| 5 | | public struct CoverageCount: Sendable, Equatable { |
| 6 | | public let total: Int |
| 7 | | public let covered: Int |
| 8 | | |
| 9 | 169 | public init(total: Int, covered: Int) { |
| 10 | 169 | self.total = total |
| 11 | 169 | self.covered = covered |
| 12 | 169 | } |
| 13 | | |
| 14 | | /// Sums of several counts, such as a module's files. |
| 15 | 115 | init(adding counts: some Sequence<CoverageCount>) { |
| 16 | 115 | var total = 0 |
| 17 | 115 | var covered = 0 |
| 18 | 128 | for count in counts { |
| 19 | 128 | total += count.total |
| 20 | 128 | covered += count.covered |
| 21 | 128 | } |
| 22 | 115 | self.init(total: total, covered: covered) |
| 23 | 115 | } |
| 24 | | |
| 25 | | /// The covered share, such as `62.5%`; a count of nothing shows 100%, matching its |
| 26 | | /// tier. |
| 27 | 85 | var percentText: String { |
| 28 | 85 | String(format: "%.1f%%", total == 0 ? 100 : Double(covered) * 100 / Double(total)) |
| 29 | 85 | } |
| 30 | | |
| 31 | 51 | var tier: CoverageTier { CoverageTier(covered: covered, total: total) } |
| 32 | | } |
| 33 | | |
| 34 | | /// One branch from `branches` in the export: a decision with two outcomes. |
| 35 | | public struct BranchRegion: Sendable, Equatable, Decodable { |
| 36 | | /// Times the true outcome ran. |
| 37 | | public let trueCount: UInt64 |
| 38 | | public let falseCount: UInt64 |
| 39 | | |
| 40 | 26 | public init(trueCount: UInt64, falseCount: UInt64) { |
| 41 | 26 | self.trueCount = trueCount |
| 42 | 26 | self.falseCount = falseCount |
| 43 | 26 | } |
| 44 | | |
| 45 | | /// llvm-cov encodes a branch as `[lineStart, columnStart, lineEnd, columnEnd, |
| 46 | | /// executionCount, falseExecutionCount, fileId, expandedFileId, kind]`. |
| 47 | 24 | public init(from decoder: any Decoder) throws { |
| 48 | 24 | var fields = try decoder.unkeyedContainer() |
| 49 | 96 | for _ in 0..<Self.regionPositionFieldCount { _ = try fields.decode(Int.self) } |
| 50 | 24 | self.init( |
| 51 | 24 | trueCount: try fields.decode(UInt64.self), falseCount: try fields.decode(UInt64.self)) |
| 52 | 24 | } |
| 53 | | |
| 54 | | /// The four line and column fields that open the array. |
| 55 | | private static let regionPositionFieldCount = 4 |
| 56 | | } |
| 57 | | |
| 58 | | /// One MC/DC decision from `mcdc_records` in the export, with one flag per condition. |
| 59 | | public struct ConditionRecord: Sendable, Equatable, Decodable { |
| 60 | | /// True for each condition that has shown it independently changes the decision. |
| 61 | | public let conditionsCovered: [Bool] |
| 62 | | |
| 63 | 14 | public init(conditionsCovered: [Bool]) { |
| 64 | 14 | self.conditionsCovered = conditionsCovered |
| 65 | 14 | } |
| 66 | | |
| 67 | | /// llvm-cov encodes a record as nine numbers (the decision's line and column range, |
| 68 | | /// its execution counts, file ids and kind) followed by the array of condition flags. |
| 69 | 12 | public init(from decoder: any Decoder) throws { |
| 70 | 12 | var fields = try decoder.unkeyedContainer() |
| 71 | 108 | for _ in 0..<Self.leadingFieldCount { _ = try fields.decode(UInt64.self) } |
| 72 | 12 | self.init(conditionsCovered: try fields.decode([Bool].self)) |
| 73 | 12 | } |
| 74 | | |
| 75 | | private static let leadingFieldCount = 9 |
| 76 | | } |
| 77 | | |
| 78 | | extension CoverageCount { |
| 79 | | /// The sum of the counts that exist; nil when none do, so a module of files without |
| 80 | | /// data has none either. |
| 81 | 2178 | init?(addingPresent counts: some Sequence<CoverageCount?>) { |
| 82 | 2735 | let present = counts.compactMap { $0 } |
| 83 | 2178 | guard !present.isEmpty else { return nil } |
| 84 | 114 | self.init(adding: present) |
| 85 | 114 | } |
| 86 | | } |
| 87 | | |
| 88 | | extension FileCoverage { |
| 89 | | /// Covered of all branch outcomes (each branch has a true and a false one); nil when |
| 90 | | /// the export holds no branches, as for Swift. |
| 91 | 43 | public var branchCount: CoverageCount? { |
| 92 | 43 | guard !branches.isEmpty else { return nil } |
| 93 | 12 | let covered = branches.reduce(0) { sum, branch in |
| 94 | 12 | sum + (branch.trueCount > 0 ? 1 : 0) + (branch.falseCount > 0 ? 1 : 0) |
| 95 | 12 | } |
| 96 | 3 | return CoverageCount(total: branches.count * Self.outcomesPerBranch, covered: covered) |
| 97 | 43 | } |
| 98 | | |
| 99 | | /// Covered of all MC/DC conditions; nil when the export holds no MC/DC records. |
| 100 | 43 | public var conditionCount: CoverageCount? { |
| 101 | 43 | guard !conditionRecords.isEmpty else { return nil } |
| 102 | 6 | let flags = conditionRecords.flatMap(\.conditionsCovered) |
| 103 | 12 | return CoverageCount(total: flags.count, covered: flags.count { $0 }) |
| 104 | 43 | } |
| 105 | | |
| 106 | | private static let outcomesPerBranch = 2 |
| 107 | | } |