|
| 1 | +using System.Text; |
| 2 | +using MuConvert.generator; |
| 3 | +using MuConvert.parser; |
| 4 | +using Xunit.Abstractions; |
| 5 | +using static MuConvert.Tests.TestUtils; |
| 6 | + |
| 7 | +namespace MuConvert.Tests; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// 官谱 MA2 → Chart(<see cref="MA2Parser"/>)→ MA2(<see cref="MA2Generator"/>)轮回合后, |
| 11 | +/// 与原版 MA2 末尾统计段(<c>T_REC_*</c> 起至文件结束)逐项一致。 |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// 下列键与官机/导出工具的规则尚未在 <see cref="MA2Generator"/> 内完全复现,测试中跳过比对: |
| 15 | +/// <list type="bullet"> |
| 16 | +/// <item><description><c>TTM_EACHPAIRS</c>:官机 EACH 对计数与谱面对象模型对应关系仍待考证。</description></item> |
| 17 | +/// <item><description><c>T_JUDGE_HLD</c> / <c>T_JUDGE_ALL</c>:Hold 在判定统计中的折算与官机不一致(生成器侧 HLD 仍为 TODO)。</description></item> |
| 18 | +/// </list> |
| 19 | +/// </remarks> |
| 20 | +public class Statistics测试 |
| 21 | +{ |
| 22 | + private readonly ITestOutputHelper _output; |
| 23 | + |
| 24 | + public Statistics测试(ITestOutputHelper output) => _output = output; |
| 25 | + |
| 26 | + /// <summary>8 张官谱各取 maidata 等级 5(对应 <c>*03.ma2</c>)。</summary> |
| 27 | + public static IEnumerable<object[]> OfficialLevel5() |
| 28 | + { |
| 29 | + const int levelId = 5; |
| 30 | + var repoRoot = FindRepoRoot(); |
| 31 | + var testsetRoot = Path.Combine(repoRoot.FullName, "tests", "testset", "官谱"); |
| 32 | + if (!Directory.Exists(testsetRoot)) |
| 33 | + throw new DirectoryNotFoundException($"Testset root not found: {testsetRoot}"); |
| 34 | + |
| 35 | + foreach (var maidataPath in Directory.EnumerateFiles(testsetRoot, "maidata.txt", SearchOption.AllDirectories) |
| 36 | + .OrderBy(p => p, StringComparer.Ordinal)) |
| 37 | + yield return [new TestInput(maidataPath, levelId)]; |
| 38 | + } |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [MemberData(nameof(OfficialLevel5))] |
| 42 | + public void 统计段与原版一致(TestInput input) |
| 43 | + { |
| 44 | + var ma2Original = File.ReadAllText(input.MA2, Encoding.UTF8); |
| 45 | + var (chart, parseAlerts) = new MA2Parser().Parse(ma2Original); |
| 46 | + Assert.Empty(parseAlerts); |
| 47 | + |
| 48 | + var (ma2RoundTrip, genAlerts) = new MA2Generator().Generate(chart); |
| 49 | + Assert.Empty(genAlerts); |
| 50 | + |
| 51 | + var expected = Ma2StatisticsSection.Parse(ma2Original); |
| 52 | + Assert.NotEmpty(expected); |
| 53 | + |
| 54 | + var actual = Ma2StatisticsSection.Parse(ma2RoundTrip); |
| 55 | + Ma2StatisticsSection.AssertEqual(expected, actual, input.ToString(), _output); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +internal static class Ma2StatisticsSection |
| 60 | +{ |
| 61 | + /// <summary>与 <see cref="MA2Generator"/> 当前实现尚未对齐的统计键,不参与等价断言。</summary> |
| 62 | + public static readonly HashSet<string> SkippedKeys = |
| 63 | + [ |
| 64 | + "TTM_EACHPAIRS", |
| 65 | + "T_JUDGE_HLD", |
| 66 | + "T_JUDGE_ALL", |
| 67 | + ]; |
| 68 | + |
| 69 | + /// <summary>从首个 <c>T_REC_</c> 行起解析到文件末尾,得到 <c>键 → 值</c>(不含键前缀)。</summary> |
| 70 | + public static Dictionary<string, string> Parse(string ma2Text) |
| 71 | + { |
| 72 | + var dict = new Dictionary<string, string>(StringComparer.Ordinal); |
| 73 | + var started = false; |
| 74 | + foreach (var raw in ma2Text.EnumerateLines()) |
| 75 | + { |
| 76 | + var line = raw.ToString().TrimEnd('\r'); |
| 77 | + if (!started) |
| 78 | + { |
| 79 | + if (line.StartsWith("T_REC_", StringComparison.Ordinal)) |
| 80 | + started = true; |
| 81 | + else |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + if (string.IsNullOrWhiteSpace(line)) |
| 86 | + continue; |
| 87 | + |
| 88 | + var tab = line.IndexOf('\t'); |
| 89 | + if (tab <= 0) |
| 90 | + continue; |
| 91 | + |
| 92 | + dict[line[..tab]] = line[(tab + 1)..]; |
| 93 | + } |
| 94 | + |
| 95 | + return dict; |
| 96 | + } |
| 97 | + |
| 98 | + public static void AssertEqual( |
| 99 | + IReadOnlyDictionary<string, string> expected, |
| 100 | + IReadOnlyDictionary<string, string> actual, |
| 101 | + string context, |
| 102 | + ITestOutputHelper? output) |
| 103 | + { |
| 104 | + foreach (var kv in expected.OrderBy(k => k.Key, StringComparer.Ordinal)) |
| 105 | + { |
| 106 | + if (SkippedKeys.Contains(kv.Key)) |
| 107 | + continue; |
| 108 | + |
| 109 | + if (!actual.TryGetValue(kv.Key, out var actVal)) |
| 110 | + { |
| 111 | + Assert.Fail($"{context}: 缺少统计键 {kv.Key}(expected {kv.Value})"); |
| 112 | + } |
| 113 | + |
| 114 | + if (!ValuesEqual(kv.Key, kv.Value, actVal)) |
| 115 | + { |
| 116 | + Assert.Fail($"{context}: 统计键 {kv.Key} 不一致:expected {kv.Value},actual {actVal}"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + foreach (var kv in actual) |
| 121 | + { |
| 122 | + if (SkippedKeys.Contains(kv.Key)) |
| 123 | + continue; |
| 124 | + if (expected.ContainsKey(kv.Key)) |
| 125 | + continue; |
| 126 | + if (IsZeroStatValue(kv.Value)) |
| 127 | + continue; |
| 128 | + Assert.Fail($"{context}: 多出原版没有的统计键 {kv.Key}={kv.Value}"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static bool IsZeroStatValue(string v) => |
| 133 | + v == "0" || v == "0.0" || v == "0.00" || v == "0.000"; |
| 134 | + |
| 135 | + private static bool ValuesEqual(string key, string expected, string actual) |
| 136 | + { |
| 137 | + return string.Equals(expected, actual, StringComparison.Ordinal); |
| 138 | + } |
| 139 | +} |
0 commit comments