|
| 1 | +using System.Text; |
| 2 | +using MuConvert.generator; |
| 3 | +using MuConvert.maidata; |
| 4 | +using MuConvert.parser; |
| 5 | +using MuConvert.utils; |
| 6 | +using static MuConvert.Tests.TestUtils; |
| 7 | + |
| 8 | +namespace MuConvert.Tests; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// 官谱中 golden MA2 头为 <c>1.03.00</c> 的谱面:Simai(lv5)→ <see cref="MA2_103Generator"/> 与对应 <c>*03.ma2</c> 音符段一致。 |
| 12 | +/// </summary> |
| 13 | +public class MA2_103测试 |
| 14 | +{ |
| 15 | + public static IEnumerable<object[]> Official103Lv5() |
| 16 | + { |
| 17 | + const int levelId = 5; |
| 18 | + var repoRoot = FindRepoRoot(); |
| 19 | + var testsetRoot = Path.Combine(repoRoot.FullName, "tests", "testset", "官谱"); |
| 20 | + if (!Directory.Exists(testsetRoot)) |
| 21 | + throw new DirectoryNotFoundException($"Testset root not found: {testsetRoot}"); |
| 22 | + |
| 23 | + foreach (var maidataPath in Directory.EnumerateFiles(testsetRoot, "maidata.txt", SearchOption.AllDirectories) |
| 24 | + .OrderBy(p => p, StringComparer.Ordinal)) |
| 25 | + { |
| 26 | + var maidata = new Maidata(File.ReadAllText(maidataPath, Encoding.UTF8)); |
| 27 | + if (!maidata.Levels.ContainsKey(levelId)) |
| 28 | + continue; |
| 29 | + |
| 30 | + var input = new TestInput(maidataPath, levelId); |
| 31 | + var golden = File.ReadAllText(input.MA2, Encoding.UTF8); |
| 32 | + if (TryParseMa2HeaderVersion(golden) != 103) |
| 33 | + continue; |
| 34 | + |
| 35 | + yield return [input]; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + [Theory] |
| 40 | + [MemberData(nameof(Official103Lv5))] |
| 41 | + public void Simai转MA2_103(TestInput input) |
| 42 | + { |
| 43 | + var maidata = new Maidata(File.ReadAllText(input.Maidata, Encoding.UTF8)); |
| 44 | + var chartInfo = maidata.Levels[input.LevelId]; |
| 45 | + var expectedMa2 = File.ReadAllText(input.MA2, Encoding.UTF8); |
| 46 | + |
| 47 | + var (chart, parseAlerts) = new SimaiParser(bigTouch: false, clockCount: maidata.ClockCount).Parse(chartInfo.Inote); |
| 48 | + Assert.DoesNotContain(parseAlerts, a => a.Level >= Alert.LEVEL.Error); |
| 49 | + |
| 50 | + var (ma2, genAlerts) = new MA2_103Generator(isUtage: false).Generate(chart); |
| 51 | + Assert.DoesNotContain(genAlerts, a => a.Level >= Alert.LEVEL.Error); |
| 52 | + |
| 53 | + ma2 = KeepNotesOnly(ma2); |
| 54 | + expectedMa2 = KeepNotesOnly(expectedMa2); |
| 55 | + AssertMa2NotesEqual(expectedMa2, ma2, input.ToString()); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// 提取音符段至 <c>T_REC</c> 之前:跳过头部与 <c>BPM</c> 行;若存在 <c>MET\t</c> 小节行则跳过该行; |
| 60 | + /// 部分旧官谱 golden 无 <c>MET</c>,则在 <c>BPM</c> 块后的首条非头行开始收集。 |
| 61 | + /// </summary> |
| 62 | + private static string KeepNotesOnly(string text) |
| 63 | + { |
| 64 | + var result = new StringBuilder(); |
| 65 | + var inNotes = false; |
| 66 | + foreach (var l in text.EnumerateLines()) |
| 67 | + { |
| 68 | + var line = l.ToString().TrimEnd('\r'); |
| 69 | + if (string.IsNullOrWhiteSpace(line)) continue; |
| 70 | + if (line.StartsWith("T_REC", StringComparison.Ordinal)) break; |
| 71 | + |
| 72 | + if (!inNotes) |
| 73 | + { |
| 74 | + if (IsMa2HeaderOrBpmLine(line)) continue; |
| 75 | + if (line.StartsWith("MET\t", StringComparison.Ordinal)) continue; |
| 76 | + inNotes = true; |
| 77 | + } |
| 78 | + |
| 79 | + result.Append(line).Append('\n'); |
| 80 | + } |
| 81 | + |
| 82 | + return result.ToString(); |
| 83 | + } |
| 84 | + |
| 85 | + private static bool IsMa2HeaderOrBpmLine(string line) => |
| 86 | + line.StartsWith("VERSION\t", StringComparison.Ordinal) || |
| 87 | + line.StartsWith("FES_MODE\t", StringComparison.Ordinal) || |
| 88 | + line.StartsWith("BPM_DEF\t", StringComparison.Ordinal) || |
| 89 | + line.StartsWith("MET_DEF\t", StringComparison.Ordinal) || |
| 90 | + line.StartsWith("RESOLUTION\t", StringComparison.Ordinal) || |
| 91 | + line.StartsWith("CLK_DEF\t", StringComparison.Ordinal) || |
| 92 | + line.StartsWith("COMPATIBLE_CODE\t", StringComparison.Ordinal) || |
| 93 | + line.StartsWith("GENERATED_BY\t", StringComparison.Ordinal) || |
| 94 | + line.StartsWith("BPM\t", StringComparison.Ordinal); |
| 95 | + |
| 96 | + private static (int TimeTick, int Len, string Extra) GetSlideTime(string slide) |
| 97 | + { |
| 98 | + var values = slide.Split('\t'); |
| 99 | + return (int.Parse(values[1]) * 384 + int.Parse(values[2]), |
| 100 | + int.Parse(values[5]), |
| 101 | + string.Join("\t", values[0], values[3], values[4], values[6])); |
| 102 | + } |
| 103 | + |
| 104 | + private static bool CompareLine(string exp, string act) |
| 105 | + { |
| 106 | + var result = string.Equals(exp, act, StringComparison.Ordinal); |
| 107 | + if (!result && exp.Length >= 5 && act.Length >= 5 && exp[..5] == act[..5] && SlideTypeTool.IsSlide(exp[2..5])) |
| 108 | + { |
| 109 | + var (expTime, expLen, expExtra) = GetSlideTime(exp); |
| 110 | + var (actTime, actLen, actExtra) = GetSlideTime(act); |
| 111 | + if (expExtra != actExtra) return result; |
| 112 | + if (exp[..2] == "CN") |
| 113 | + { |
| 114 | + if (expTime + expLen == actTime + actLen || Math.Abs(expLen - actLen) <= 1) result = true; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + if (expTime == actTime && Math.Abs(expLen - actLen) <= 1) result = true; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + private static void AssertMa2NotesEqual(string expected, string actual, string context) |
| 126 | + { |
| 127 | + var expectedLines = expected.Split('\n'); |
| 128 | + var actualLines = actual.Split('\n'); |
| 129 | + var max = Math.Max(expectedLines.Length, actualLines.Length); |
| 130 | + |
| 131 | + for (var i = 0; i < max; i++) |
| 132 | + { |
| 133 | + var exp = i < expectedLines.Length ? expectedLines[i] : "<EOF>"; |
| 134 | + var act = i < actualLines.Length ? actualLines[i] : "<EOF>"; |
| 135 | + var result = CompareLine(exp, act); |
| 136 | + if (!result) |
| 137 | + { |
| 138 | + for (var j = 1; j < Math.Min(expectedLines.Length, i + 5); j++) |
| 139 | + { |
| 140 | + if (CompareLine(expectedLines[j], act)) |
| 141 | + { |
| 142 | + (expectedLines[j], expectedLines[i]) = (expectedLines[i], expectedLines[j]); |
| 143 | + result = true; |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (!result) |
| 150 | + { |
| 151 | + Assert.Fail( |
| 152 | + $"{context}: first difference at line {i + 1}:{Environment.NewLine}" + |
| 153 | + $"EXPECTED: {exp}{Environment.NewLine}" + |
| 154 | + $"ACTUAL : {act}"); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments