Skip to content
0.2.0-dev Download ZIP
219 lines ·7.0 KB Raw
1using System.Security.Cryptography;
2using EvilEngine.Formats;
3using EvilEngine.Mounts;
4
5namespace EvilEngine.Conversion.Tests;
6
7public sealed class SharedParseTests
8{
9 private const int Kept = 8;
10 private static readonly VirtualPath Box = VirtualPath.Parse("maps/box.bin");
11 private static readonly VirtualPath Lit = VirtualPath.Parse("maps/box.lit");
12
13 private static (ConversionPipeline Pipeline, MemorySource Files, ParseConverter Converter) Alpha()
14 {
15 var files = new MemorySource("alpha", CaseRule.Insensitive).Add(Box, Bytes(1));
16 var pipeline = new ConversionPipeline(new MemoryConversionCache());
17 var converter = new ParseConverter();
18 pipeline.AddNamespace(new ContentNamespace("alpha", files));
19 pipeline.Bind("alpha", converter);
20 return (pipeline, files, converter);
21 }
22
23 private static byte[] Bytes(byte seed, int length = 256)
24 {
25 return Enumerable.Range(0, length).Select(index => (byte)(index * seed)).ToArray();
26 }
27
28 private static string Hash(byte[] bytes)
29 {
30 return Convert.ToHexStringLower(SHA256.HashData(bytes));
31 }
32
33 private static ConversionResult Item(ConversionPipeline pipeline, int index, string key = "box")
34 {
35 return pipeline.Get(ContentKind.Record, ContentId.Parse($"alpha:{key}/{index}"));
36 }
37
38 [Fact]
39 public void ParsesAFileOnceForEveryItemOverIt()
40 {
41 var (pipeline, _, converter) = Alpha();
42
43 var results = Enumerable.Range(0, 16).Select(index => Item(pipeline, index)).ToArray();
44
45 Assert.Equal(1, converter.Builds);
46 Assert.All(results, result =>
47 {
48 Assert.Equal(ConversionStatus.Converted, result.Status);
49 Assert.Equal([(Box, Hash(Bytes(1))), (Lit, null)],
50 result.Content!.Inputs.Select(input => (input.Path, input.Hash)));
51 Assert.Equal([new ContentReference(ContentKind.Texture, ContentId.Parse("alpha:box/palette"))],
52 result.Content.References);
53 Assert.Equal(["box parsed"], result.Diagnostics);
54 });
55 Assert.Equal(Bytes(1)[4..8], results[1].Content!.Data.ToArray());
56 }
57
58 [Fact]
59 public void ParsesAFileOnceWhenItsItemsConvertTogether()
60 {
61 var (pipeline, _, converter) = Alpha();
62
63 var results = new ConversionResult[64];
64 Parallel.For(0, results.Length, index => results[index] = Item(pipeline, index % 32));
65
66 Assert.Equal(1, converter.Builds);
67 Assert.All(results, result => Assert.True(result.Succeeded));
68 }
69
70 [Fact]
71 public void InvalidatingAFileTheParseReadParsesItAgain()
72 {
73 var (pipeline, files, converter) = Alpha();
74 Item(pipeline, 0);
75 files.Add(Box, Bytes(3));
76
77 pipeline.Invalidate("alpha", [VirtualPath.Parse("MAPS/Box.bin")]);
78 var item = Item(pipeline, 1);
79
80 Assert.Equal(2, converter.Builds);
81 Assert.Equal(Bytes(3)[4..8], item.Content!.Data.ToArray());
82 Assert.Equal(Hash(Bytes(3)), item.Content.Inputs[0].Hash);
83 }
84
85 [Fact]
86 public void InvalidatingAFileTheParseMissedParsesItAgain()
87 {
88 var (pipeline, files, converter) = Alpha();
89 Item(pipeline, 0);
90 files.Add(Lit.Value, "lit");
91
92 pipeline.Invalidate("alpha", [Lit]);
93 var item = Item(pipeline, 1);
94
95 Assert.Equal(2, converter.Builds);
96 Assert.Equal(["box parsed", "box parsed with its lit"], item.Diagnostics);
97 Assert.NotNull(item.Content!.Inputs[1].Hash);
98 }
99
100 [Fact]
101 public void InvalidatingAnotherFileKeepsTheParse()
102 {
103 var (pipeline, files, converter) = Alpha();
104 Item(pipeline, 0);
105 files.Add("maps/other.bin", "other");
106
107 pipeline.Invalidate("alpha", [VirtualPath.Parse("maps/other.bin")]);
108 Item(pipeline, 1);
109
110 Assert.Equal(1, converter.Builds);
111 }
112
113 [Fact]
114 public void AFileOfAnotherLengthIsParsedAgain()
115 {
116 var (pipeline, files, converter) = Alpha();
117 Item(pipeline, 0);
118 files.Add(Box, Bytes(3, 512));
119
120 var item = Item(pipeline, 1);
121
122 Assert.Equal(2, converter.Builds);
123 Assert.Equal(Bytes(3, 512)[4..8], item.Content!.Data.ToArray());
124 }
125
126 [Fact]
127 public void ANamespaceAddedAgainParsesAgain()
128 {
129 var (pipeline, _, converter) = Alpha();
130 Item(pipeline, 0);
131
132 Assert.True(pipeline.RemoveNamespace("alpha"));
133 pipeline.AddNamespace(new ContentNamespace("alpha",
134 new MemorySource("alpha", CaseRule.Insensitive).Add(Box, Bytes(3))));
135 var item = Item(pipeline, 1);
136
137 Assert.Equal(2, converter.Builds);
138 Assert.Equal(Bytes(3)[4..8], item.Content!.Data.ToArray());
139 }
140
141 [Fact]
142 public void AFailedParseIsTriedAgainByTheNextItem()
143 {
144 var (pipeline, _, converter) = Alpha();
145 converter.Fail = true;
146
147 var failed = Item(pipeline, 0);
148 var next = Item(pipeline, 1);
149
150 Assert.Equal(ConversionStatus.Failed, failed.Status);
151 Assert.Equal(["box parsed", "test.parse failed on alpha:box/0 while reading 'maps/box.bin': box is broken."],
152 failed.Diagnostics);
153 Assert.Equal(ConversionStatus.Converted, next.Status);
154 Assert.Equal(2, converter.Builds);
155 }
156
157 [Fact]
158 public void KeepsOnlyTheMostRecentlyUsedParses()
159 {
160 var (pipeline, _, converter) = Alpha();
161
162 for (var key = 0; key <= Kept; key++)
163 Item(pipeline, 0, $"box{key}");
164 Item(pipeline, 1, $"box{Kept}");
165 Item(pipeline, 1, "box1");
166 Item(pipeline, 1, "box0");
167
168 Assert.Equal(Kept + 2, converter.Builds);
169 }
170
171 private sealed record Parsed(byte[] Bytes);
172
173 private sealed class ParseConverter : IConverter
174 {
175 private int _builds;
176
177 public bool Fail { get; set; }
178
179 public int Builds => Volatile.Read(ref _builds);
180
181 public string Id => "test.parse";
182
183 public int Version => 1;
184
185 public ContentKind Produces => ContentKind.Record;
186
187 public bool Accepts(ConversionContext context)
188 {
189 return context.Id.Name.StartsWith("box", StringComparison.Ordinal);
190 }
191
192 public void Convert(ConversionContext context, Stream output)
193 {
194 var key = context.Id.Name[..context.Id.Name.IndexOf('/')];
195 var index = int.Parse(context.Id.Name[(key.Length + 1)..]);
196 var parsed = context.Shared($"test.parse:{key}", shared =>
197 {
198 Interlocked.Increment(ref _builds);
199 var bytes = shared.Read(Box).ToArray();
200 shared.Warn("box parsed");
201 if (Fail)
202 {
203 Fail = false;
204 throw new InvalidDataException("box is broken.");
205 }
206
207 if (shared.Exists(Lit))
208 {
209 shared.Read(Lit);
210 shared.Warn("box parsed with its lit");
211 }
212
213 shared.Reference(ContentKind.Texture, "box/palette");
214 return new Parsed(bytes);
215 });
216 output.Write(parsed.Bytes.AsSpan(index * 4, 4));
217 }
218 }
219}