image.domain\self_evolve\adaptive.mbt

88.9% · 32 / 36 points · 156 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 自适应 self_evolve/adaptive
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 自适应测试策略 — 根据覆盖率自动调整测试优先级
  7. 7// Adaptive test strategy: auto-adjust test priority based on coverage
  8. 8//
  9. 9// Phase 7 v292: 将测试生成从"均匀"推向"自适应" — 低覆盖算子优先。
  10. 10
  11. 11///|
  12. 12/// 测试优先级。
  13. 13/// 描述 Test priority level.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum TestPriority {
  18. 18 HighPriority
  19. 19 MediumPriority
  20. 20 LowPriority
  21. 21}
  22. 22
  23. 23///|
  24. 24pub extend TestPriority with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27pub extend TestPriority with Eq::{not_equal, equal}
  28. 28
  29. 29///|
  30. 30/// 单算子自适应策略。
  31. 31/// 自适应 Adaptive strategy for a single op.
  32. 32#derive(Eq, @debug.Debug)
  33. 33pub(all) struct AdaptiveStrategy {
  34. 34 op_tag : String
  35. 35 current_coverage : Int32
  36. 36 priority : TestPriority
  37. 37 suggested_tests : Int32
  38. 38}
  39. 39
  40. 40///|
  41. 41pub extend AdaptiveStrategy with @debug.Debug::{to_repr}
  42. 42
  43. 43///|
  44. 44pub extend AdaptiveStrategy with Eq::{not_equal, equal}
  45. 45
  46. 46///|
  47. 47/// 自适应报告。
  48. 48/// 自适应 Adaptive report.
  49. 49#derive(Eq, @debug.Debug)
  50. 50pub(all) struct AdaptiveReport {
  51. 51 strategies : Array[AdaptiveStrategy]
  52. 52 total_prioritized : Int32
  53. 53 high_priority_count : Int32
  54. 54 summary : String
  55. 55}
  56. 56
  57. 57///|
  58. 58pub extend AdaptiveReport with @debug.Debug::{to_repr}
  59. 59
  60. 60///|
  61. 61pub extend AdaptiveReport with Eq::{not_equal, equal}
  62. 62
  63. 63///|
  64. 64/// 根据覆盖度建议测试数量。
  65. 65/// 描述 Suggest test count based on coverage (0-100).
  66. 66pub fn suggest_test_count(coverage : Int32) -> Int32 {
  67. 67● if coverage >= 80 {
  68. 68● 2
  69. 69● } else if coverage >= 50 {
  70. 70● 5
  71. 71● } else if coverage >= 25 {
  72. 72● 8
  73. 73 } else {
  74. 74● 12
  75. 75 }
  76. 76}
  77. 77
  78. 78///|
  79. 79/// 根据覆盖度判定优先级。
  80. 80/// 描述 Determine priority based on coverage.
  81. 81pub fn priority_from_coverage(coverage : Int32) -> TestPriority {
  82. 82● if coverage >= 50 {
  83. 83● LowPriority
  84. 84● } else if coverage >= 25 {
  85. 85● MediumPriority
  86. 86 } else {
  87. 87● HighPriority
  88. 88 }
  89. 89}
  90. 90
  91. 91///|
  92. 92/// 分析单算子自适应策略。
  93. 93/// 描述 Analyze adaptive strategy for a single op.
  94. 94pub fn adaptive_strategy(op : @imgir.IROp) -> AdaptiveStrategy {
  95. 95● let degs = detect_degradation(op)
  96. 96● let coverage = if degs.length() == 0 {
  97. 97● 100
  98. 98 } else {
  99. 99● 100 / (degs.length() + 1)
  100. 100 }
  101. 101● let priority = priority_from_coverage(coverage)
  102. 102● let suggested = suggest_test_count(coverage)
  103. 103 {
  104. 104● op_tag: op.tag(),
  105. 105 current_coverage: coverage,
  106. 106 priority,
  107. 107 suggested_tests: suggested,
  108. 108 }
  109. 109}
  110. 110
  111. 111///|
  112. 112/// 对全部 19 种算子分析自适应策略。
  113. 113/// 描述 Analyze adaptive strategy for all 19 ops.
  114. 114pub fn adaptive_strategy_all() -> AdaptiveReport {
  115. 115● let strategies : Array[AdaptiveStrategy] = []
  116. 116 let mut high_count = 0
  117. 117● for op in all_ops() {
  118. 118● let s = adaptive_strategy(op)
  119. 119 if s.priority is HighPriority {
  120. 120○ high_count = high_count + 1
  121. 121 }
  122. 122● strategies.push(s)
  123. 123 }
  124. 124● let total = strategies.length()
  125. 125 let summary = "自适应策略: " +
  126. 126● total.to_string() +
  127. 127 " 个算子, " +
  128. 128● high_count.to_string() +
  129. 129 " 个高优先级"
  130. 130 {
  131. 131 strategies,
  132. 132 total_prioritized: total,
  133. 133 high_priority_count: high_count,
  134. 134 summary,
  135. 135 }
  136. 136}
  137. 137
  138. 138///|
  139. 139/// 获取高优先级算子标签。
  140. 140/// 获取 Get high-priority op tags.
  141. 141pub fn high_priority_ops() -> Array[String] {
  142. 142● let result : Array[String] = []
  143. 143● for s in adaptive_strategy_all().strategies {
  144. 144● if s.priority is HighPriority {
  145. 145○ result.push(s.op_tag)
  146. 146 }
  147. 147 }
  148. 148 result
  149. 149}
  150. 150
  151. 151///|
  152. 152/// 自适应报告是否全部高覆盖。
  153. 153/// 描述 Whether adaptive report is all high coverage.
  154. 154pub fn AdaptiveReport::is_well_covered(self : AdaptiveReport) -> Bool {
  155. 155○ self.high_priority_count == 0
  156. 156}