• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MeltyPlayer / MeltyTool / 17723030649

15 Sep 2025 05:26AM UTC coverage: 39.997% (+0.02%) from 39.981%
17723030649

push

github

MeltyPlayer
Hopefully, hopefully, hopefully fixed the float flakiness issue.

5777 of 16365 branches covered (35.3%)

Branch coverage included in aggregate %.

11 of 11 new or added lines in 2 files covered. (100.0%)

9 existing lines in 2 files now uncovered.

24412 of 59114 relevant lines covered (41.3%)

75218.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.44
/FinModelUtility/Fin/Fin.Testing/src/GoldenAssert.cs
1
using System.IO.Hashing;
2
using System.Reflection;
3

4
using CommunityToolkit.HighPerformance;
5

6
using fin.image;
7
using fin.io;
8
using fin.util.asserts;
9
using fin.util.hex;
10
using fin.util.streams;
11
using fin.util.strings;
12

13
using Microsoft.VisualStudio.TestTools.UnitTesting;
14

15
using SixLabors.ImageSharp.PixelFormats;
16

17

18
namespace fin.testing;
19

20
public static class GoldenAssert {
21
  private const string TMP_NAME = "tmp";
22

23
  public static ISystemDirectory GetRootGoldensDirectory(
24
      Assembly executingAssembly) {
24✔
25
    var assemblyName =
24✔
26
        executingAssembly.ManifestModule.Name.SubstringUpTo(".dll");
24✔
27

28
    var executingAssemblyDll = new FinFile(executingAssembly.Location);
24✔
29
    var executingAssemblyDir = executingAssemblyDll.AssertGetParent();
24✔
30

31
    var currentDir = executingAssemblyDir;
24✔
32
    while (!currentDir.Name.Equals(assemblyName,
120✔
33
                                   StringComparison.OrdinalIgnoreCase)) {
216✔
34
      currentDir = currentDir.AssertGetParent();
96✔
35
    }
96✔
36

37
    Assert.IsNotNull(currentDir);
24✔
38

39
    var gloTestsDir = currentDir;
24✔
40
    var goldensDirectory = gloTestsDir.AssertGetExistingSubdir("goldens");
24✔
41

42
    return goldensDirectory;
24✔
43
  }
24✔
44

45
  public static IEnumerable<IFileHierarchyDirectory> GetGoldenDirectories(
46
      ISystemDirectory rootGoldenDirectory) {
21✔
47
    var hierarchy = FileHierarchy.From(rootGoldenDirectory);
21✔
48
    return hierarchy.Root.GetExistingSubdirs()
21✔
49
                    .Where(subdir => !subdir.Name.SequenceEqual(TMP_NAME));
88✔
50
  }
21✔
51

52
  public static void AssertGoldenFiles(
53
      IFileHierarchyDirectory goldenSubdir,
54
      Action<IFileHierarchyDirectory, ISystemDirectory> handler) {
77✔
55
    var inputDirectory = goldenSubdir.AssertGetExistingSubdir("input");
77✔
56
    var outputDirectory = goldenSubdir.AssertGetExistingSubdir("output");
77✔
57
    var hasGoldenExport = !outputDirectory.IsEmpty;
77✔
58

59
    GoldenAssert.RunInTestDirectory(
77✔
60
        goldenSubdir,
77✔
61
        tmpDirectory => {
77✔
62
          var targetDirectory =
77!
63
              hasGoldenExport ? tmpDirectory : outputDirectory.Impl;
77✔
64

77✔
65
          handler(inputDirectory, targetDirectory);
77✔
66

77✔
67
          if (hasGoldenExport) {
154✔
68
            GoldenAssert.AssertFilesInDirectoriesAreIdentical(
77✔
69
                tmpDirectory,
77✔
70
                outputDirectory.Impl);
77✔
71
          }
77✔
72
        });
154✔
73
  }
77✔
74

75
  public static void RunInTestDirectory(
76
      IFileHierarchyDirectory goldenSubdir,
77
      Action<ISystemDirectory> handler) {
78✔
78
    var tmpDirectory = goldenSubdir.Impl.GetOrCreateSubdir(TMP_NAME);
78✔
79
    tmpDirectory.DeleteContents();
78✔
80

81
    try {
78✔
82
      handler(tmpDirectory);
78✔
83
    } finally {
156✔
84
      tmpDirectory.DeleteContents();
78✔
85
      tmpDirectory.Delete();
78✔
86
    }
78✔
87
  }
78✔
88

89
  public static void AssertFilesInDirectoriesAreIdentical(
90
      IReadOnlyTreeDirectory lhs,
91
      IReadOnlyTreeDirectory rhs) {
78✔
92
    var lhsFiles = lhs.GetExistingFiles()
78✔
93
                      .ToDictionary(file => file.Name.ToString());
4,783✔
94
    var rhsFiles = rhs.GetExistingFiles()
78✔
95
                      .ToDictionary(file => file.Name.ToString());
4,783✔
96

97
    CollectionAssert.AreEquivalent(lhsFiles.Keys, rhsFiles.Keys);
78✔
98

99
    foreach (var (name, lhsFile) in lhsFiles) {
14,349✔
100
      var rhsFile = rhsFiles[name];
4,705✔
101
      try {
4,705✔
102
        try {
4,705✔
103
          AssertFilesAreIdentical_(lhsFile, rhsFile);
4,705✔
104
        } catch {
4,763✔
105
          if (lhsFile.FileType.ToLower() is ".bmp"
58!
106
                                            or ".jpg"
58✔
107
                                            or ".jpeg"
58✔
108
                                            or ".gif"
58✔
109
                                            or ".png") {
116✔
110
            AssertImageFilesAreIdentical_(lhsFile, rhsFile);
58✔
111
          } else {
58✔
UNCOV
112
            throw;
×
113
          }
114
        }
58✔
115
      } catch (Exception ex) {
4,705✔
UNCOV
116
        throw new Exception($"Found a change in file {name}: ", ex);
×
117
      }
118
    }
4,705✔
119
  }
78✔
120

121
  private static void AssertFilesAreIdentical_(
122
      IReadOnlyTreeFile lhs,
123
      IReadOnlyTreeFile rhs) {
4,705✔
124
    using var lhsStream = lhs.OpenRead();
4,705✔
125
    using var rhsStream = rhs.OpenRead();
4,705✔
126

127
    Assert.AreEqual(lhsStream.Length, rhsStream.Length);
4,705✔
128

129
    var bytesToRead = sizeof(long);
4,651✔
130
    int iterations =
4,651✔
131
        (int) Math.Ceiling((double) lhsStream.Length / bytesToRead);
4,651✔
132

133
    long lhsLong = 0;
4,651✔
134
    long rhsLong = 0;
4,651✔
135

136
    var lhsSpan = new Span<long>(ref lhsLong).AsBytes();
4,651✔
137
    var rhsSpan = new Span<long>(ref rhsLong).AsBytes();
4,651✔
138

139
    for (int i = 0; i < iterations; i++) {
127,688,562✔
140
      lhsStream.Read(lhsSpan);
42,559,756✔
141
      rhsStream.Read(rhsSpan);
42,559,756✔
142

143
      if (lhsLong != rhsLong) {
42,559,760✔
144
        lhsStream.Position = 0;
4✔
145
        var lhsChecksum = Crc32.HashToUInt32(lhsStream.ReadAllBytes());
4✔
146

147
        rhsStream.Position = 0;
4✔
148
        var rhsChecksum = Crc32.HashToUInt32(rhsStream.ReadAllBytes());
4✔
149

150
        Asserts.Fail(
4✔
151
            $"Files with name \"{lhs.Name}\" are different around byte #: {i * bytesToRead}.\nCrc32 was 0x{lhsChecksum.ToHex()}, now is 0x{rhsChecksum.ToHex()}");
4✔
UNCOV
152
      }
×
153
    }
42,559,752✔
154
  }
9,294✔
155

156
  private static void AssertImageFilesAreIdentical_(
157
      IReadOnlyTreeFile lhs,
158
      IReadOnlyTreeFile rhs,
159
      float allowableError = 2) {
58✔
160
    using var lhsImage = FinImage.FromFile(lhs);
58✔
161
    using var rhsImage = FinImage.FromFile(rhs);
58✔
162

163
    Assert.AreEqual(lhsImage.Width, rhsImage.Width);
58✔
164
    Assert.AreEqual(lhsImage.Height, rhsImage.Height);
58✔
165

166
    lhsImage.Access(lhsGet => {
116✔
167
      rhsImage.Access(rhsGet => {
116✔
168
        for (var y = 0; y < lhsImage.Height; ++y) {
31,934✔
169
          for (var x = 0; x < lhsImage.Width; ++x) {
19,210,544✔
170
            lhsGet(x,
6,396,444✔
171
                   y,
6,396,444✔
172
                   out var lhsR,
6,396,444✔
173
                   out var lhsG,
6,396,444✔
174
                   out var lhsB,
6,396,444✔
175
                   out var lhsA);
6,396,444✔
176

58✔
177
            rhsGet(x,
6,396,444✔
178
                   y,
6,396,444✔
179
                   out var rhsR,
6,396,444✔
180
                   out var rhsG,
6,396,444✔
181
                   out var rhsB,
6,396,444✔
182
                   out var rhsA);
6,396,444✔
183

58✔
184
            var lPixel = new Rgba32(lhsR, lhsG, lhsB, lhsA);
6,396,444✔
185
            var rPixel = new Rgba32(rhsR, rhsG, rhsB, rhsA);
6,396,444✔
186

58✔
187
            if (Math.Abs(lhsR - rhsR) > allowableError ||
6,396,444!
188
                Math.Abs(lhsG - rhsG) > allowableError ||
6,396,444✔
189
                Math.Abs(lhsB - rhsB) > allowableError ||
6,396,444✔
190
                Math.Abs(lhsA - rhsA) > allowableError) {
6,396,444✔
UNCOV
191
              Asserts.Fail(
×
192
                  $"Files with name \"{lhs.Name}\" are different at pixel ({x},{y}): {lPixel} / {rPixel}");
×
193
            }
×
194
          }
6,396,444✔
195
        }
10,606✔
196
      });
116✔
197
    });
116✔
198
  }
116✔
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc