1/// The colour band a coverage percentage falls in (docs/DESIGN.md, D4).
2public enum CoverageTier: String, Sendable {
3 /// 90% and up.
4 case high
5 /// 75% and up.
6 case medium
7 case low
8
9 /// The tier of `covered` out of `total`; a file with no code counts as high,
10 /// since nothing in it went untested.
11714 public init(covered: Int, total: Int) {
12714 // Whole numbers, so a file exactly on a boundary never lands below it.
13714 if covered * 100 >= total * Self.highPercent {
14517 self = .high
15517 } else if covered * 100 >= total * Self.mediumPercent {
1668 self = .medium
17129 } else {
18129 self = .low
19129 }
20714 }
21
22 /// The tier of a percentage computed in floating point, such as a mean of shares.
2375 init(percent: Double) {
2475 if percent >= Double(Self.highPercent) - Self.roundingError {
258 self = .high
2667 } else if percent >= Double(Self.mediumPercent) - Self.roundingError {
2761 self = .medium
2861 } else {
296 self = .low
306 }
3175 }
32
33 private static let highPercent = 90
34 private static let mediumPercent = 75
35 /// Far above what averaging shares can lose, far below any real shortfall.
36 private static let roundingError = 1e-9
37}