forked from tModLoader/tModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingTests.cs
403 lines (367 loc) · 12.8 KB
/
SortingTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Terraria.ModLoader.Core;
using Terraria.ModLoader.Exceptions;
namespace Terraria.ModLoader
{
[TestClass]
public class SortingTests
{
private static LocalMod Make(string name,
ModSide side = ModSide.Both, string version = null,
IEnumerable<string> refs = null, IEnumerable<string> weakRefs = null,
IEnumerable<string> sortAfter = null, IEnumerable<string> sortBefore = null) {
return new LocalMod(new TmodFile(null, name), new BuildProperties {
side = side,
version = new Version(version ?? "1.0.0.0"),
modReferences = refs?.Select(BuildProperties.ModReference.Parse).ToArray() ?? new BuildProperties.ModReference[0],
weakReferences = weakRefs?.Select(BuildProperties.ModReference.Parse).ToArray() ?? new BuildProperties.ModReference[0],
sortAfter = sortAfter?.ToArray() ?? new string[0],
sortBefore = sortBefore?.ToArray() ?? new string[0]
});
}
private static void AssertSetsEqual<T>(ICollection<T> set1, ICollection<T> set2) {
var set = new HashSet<T>(set1);
Assert.AreEqual(set1.Count, set2.Count);
foreach (var e in set2)
Assert.IsTrue(set.Contains(e), "Missing Element: " + set2);
}
private static void AssertModException(Action func, string[] mods, string msg) {
try {
func();
Assert.Fail("Test method did not throw expected exception ModSortingException.");
}
catch (ModSortingException e) {
AssertSetsEqual(e.errored.Select(m => m.Name).ToList(), mods);
Assert.AreEqual(e.Message.Trim(), msg);
}
}
private static void Swap<T>(ref T a, ref T b) {
var tmp = a;
a = b;
b = tmp;
}
private static void Reverse<T>(T[] arr, int pos, int len) {
int end = pos + len - 1;
for (int i = 0; i < len / 2; i++)
Swap(ref arr[pos + i], ref arr[end - i]);
}
private static IEnumerable<int[]> Permutations(int n) {
var arr = Enumerable.Range(0, n).ToArray();
var b = Enumerable.Range(0, n).ToArray();
var c = Enumerable.Repeat(0, n).ToArray();
while (true) {
yield return arr;
int k = 1;
while (c[k] == k) {
c[k++] = 0;
if (k == n) yield break;
}
c[k]++;
Reverse(b, 1, k-1);
Swap(ref arr[0], ref arr[b[k]]);
}
}
private static List<LocalMod> AssertSortSatisfied(List<LocalMod> list) {
var sorted = ModOrganizer.Sort(list);
var indexMap = sorted.ToDictionary(m => m.Name, sorted.IndexOf);
foreach (var mod in list) {
int index = indexMap[mod.Name];
foreach (var dep in mod.properties.sortAfter) {
int i;
if (indexMap.TryGetValue(dep, out i) && i > index)
Assert.Fail(mod.Name + " sorted after " + dep);
}
foreach (var dep in mod.properties.sortBefore) {
int i;
if (indexMap.TryGetValue(dep, out i) && i < index)
Assert.Fail(mod.Name + " sorted before " + dep);
}
}
return sorted;
}
/*private static void AssertSortNameIndependent(List<LoadingMod> list) {
var base_perm = list.Select(m => m.Name).ToList();
var nameToIndex = base_perm.ToDictionary(m => m, base_perm.IndexOf);
foreach (var perm in Permutations(list.Count)) {
foreach (var m in list) {
m.modFile.name = base_perm[perm[nameToIndex]]
}
}
}*/
//test missing dependencies
[TestMethod]
public void TestDependenciesExist() {
//test A -> B
var list1 = new List<LocalMod> {
Make("A", refs: new[] {"B"}),
Make("B"),
};
ModOrganizer.EnsureDependenciesExist(list1, false);
//test A -> B (missing)
var list2 = new List<LocalMod> {
Make("A", refs: new[] {"B"})
};
AssertModException(
() => ModOrganizer.EnsureDependenciesExist(list2, false),
new[] {"A"},
"Missing mod: B required by A");
//test multi reference
var list3 = new List<LocalMod> {
Make("A", refs: new[] {"B"}),
Make("B"),
Make("C", refs: new[] {"A"})
};
ModOrganizer.EnsureDependenciesExist(list3, false);
//test one missing reference
var list4 = new List<LocalMod> {
Make("A", refs: new[] {"B"}),
Make("B", refs: new[] {"C"})
};
AssertModException(
() => ModOrganizer.EnsureDependenciesExist(list4, false),
new[] {"B"},
"Missing mod: C required by B");
//test weak reference (missing)
var list5 = new List<LocalMod> {
Make("A", weakRefs: new[] {"B"})
};
ModOrganizer.EnsureDependenciesExist(list5, false);
AssertModException(
() => ModOrganizer.EnsureDependenciesExist(list5, true),
new[] {"A"},
"Missing mod: B required by A");
//test weak reference (found)
var list6 = new List<LocalMod> {
Make("A", weakRefs: new[] {"B"}),
Make("B")
};
ModOrganizer.EnsureDependenciesExist(list6, true);
//test strong (found) and weak (missing)
var list7 = new List<LocalMod> {
Make("A", refs: new[] {"B"}),
Make("B", weakRefs: new[] {"C"})
};
ModOrganizer.EnsureDependenciesExist(list7, false);
AssertModException(
() => ModOrganizer.EnsureDependenciesExist(list7, true),
new[] {"B"},
"Missing mod: C required by B");
//multi test case (missing)
var list8 = new List<LocalMod> {
Make("A", refs: new[] {"X"}),
Make("B", refs: new[] {"Y"}),
Make("C", refs: new[] {"D"}),
Make("D", weakRefs: new[] {"E"}),
Make("E", weakRefs: new[] {"Z"}),
Make("F", weakRefs: new[] {"Z"})
};
AssertModException(
() => ModOrganizer.EnsureDependenciesExist(list8, false),
new[] {"A", "B"},
"Missing mod: X required by A\r\n" +
"Missing mod: Y required by B");
AssertModException(
() => ModOrganizer.EnsureDependenciesExist(list8, true),
new[] {"A", "B", "E", "F"},
"Missing mod: X required by A\r\n" +
"Missing mod: Y required by B\r\n" +
"Missing mod: Z required by E\r\n" +
"Missing mod: Z required by F");
//multi test case (found)
var list9 = new List<LocalMod> {
Make("A", refs: new[] {"C"}),
Make("B", refs: new[] {"C"}),
Make("C", refs: new[] {"D"}),
Make("D", weakRefs: new[] {"E"}),
Make("E", weakRefs: new[] {"F"}),
Make("F")
};
ModOrganizer.EnsureDependenciesExist(list9, false);
ModOrganizer.EnsureDependenciesExist(list9, true);
}
//test missing dependencies
[TestMethod]
public void TestVersionRequirements() {
//test version on missing mod
var list1 = new List<LocalMod> {
Make("A", refs: new[] {"[email protected]"})
};
ModOrganizer.EnsureTargetVersionsMet(list1);
//test passed version check
var list2 = new List<LocalMod> {
Make("A", refs: new[] {"[email protected]"}),
Make("B", version: "1.2")
};
ModOrganizer.EnsureTargetVersionsMet(list2);
//test failed version check
var list3 = new List<LocalMod> {
Make("A", refs: new[] {"[email protected]"}),
Make("B")
};
AssertModException(
() => ModOrganizer.EnsureTargetVersionsMet(list3),
new[] { "A" },
"A requires version 1.2+ of B but version 1.0.0.0 is installed");
//test one pass, two fail version check
var list4 = new List<LocalMod> {
Make("A"),
Make("B", refs: new[] {"[email protected]"}),
Make("C", refs: new[] {"[email protected]"}),
Make("D", refs: new[] {"[email protected]"})
};
AssertModException(
() => ModOrganizer.EnsureTargetVersionsMet(list4),
new[] { "C", "D" },
"C requires version 1.1+ of A but version 1.0.0.0 is installed\r\n" +
"D requires version 1.0.0.1+ of A but version 1.0.0.0 is installed");
//test weak version check (missing)
var list5 = new List<LocalMod> {
Make("A", weakRefs: new[] {"[email protected]"})
};
ModOrganizer.EnsureDependenciesExist(list5, false);
ModOrganizer.EnsureTargetVersionsMet(list5);
//test weak version check (too low)
var list6 = new List<LocalMod> {
Make("A", weakRefs: new[] {"[email protected]"}),
Make("B")
};
AssertModException(
() => ModOrganizer.EnsureTargetVersionsMet(list6),
new[] { "A" },
"A requires version 1.1+ of B but version 1.0.0.0 is installed");
}
[TestMethod]
public void TestSortOrder() {
//general complex one way edge sort
var list = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new [] {"A"}),
Make("C", sortAfter: new [] {"A"}, sortBefore: new[] {"B"}),
Make("D", sortAfter: new [] {"H"}),
Make("E", sortAfter: new [] {"C"}),
Make("F", sortBefore: new [] {"G"}),
Make("G", sortAfter: new [] {"B", "C"}),
Make("H", sortAfter: new [] {"G"}, sortBefore: new [] {"D"}),
};
AssertSortSatisfied(list);
//mutually satisfiable cycle
var list2 = new List<LocalMod> {
Make("A", sortBefore: new [] {"B"}),
Make("B", sortAfter: new [] {"A"})
};
AssertSortSatisfied(list2);
//direct cycle
var list3 = new List<LocalMod> {
Make("A", sortAfter: new [] {"B"}),
Make("B", sortAfter: new [] {"A"})
};
AssertModException(
() => AssertSortSatisfied(list3),
new[] { "A", "B" },
"Dependency Cycle: A -> B -> A");
//complex unsatisfiable sort
var list4 = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new [] {"A"}),
Make("C", sortBefore: new [] {"A", "D"}),
Make("D", sortAfter: new [] {"I"}),
Make("E", sortAfter: new [] {"C"}),
Make("F", sortBefore: new [] {"A, I"}),
Make("G", sortAfter: new [] {"B", "C"}),
Make("H", sortBefore: new [] {"G", "F"}),
Make("I", sortAfter: new [] {"G", "H"}, sortBefore: new[] {"C"})
};
AssertModException(
() => AssertSortSatisfied(list4),
new[] { "A", "B", "C", "G", "I" },
"Dependency Cycle: A -> C -> I -> G -> B -> A\r\n" +
"Dependency Cycle: C -> I -> G -> C");
}
[TestMethod]
public void TestSidedSorts() {
//basic B is a client mod
var list = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("C", sortAfter: new[] {"B"})
};
AssertModException(
() => AssertSortSatisfied(list),
new[] { "C" },
"C indirectly depends on A via C -> B -> A\r\n"+
"Some of these mods may not exist on both client and server. Add a direct sort entries or weak references.");
//apply above advice
var list2 = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("C", sortAfter: new[] {"B", "A"})
};
AssertSortSatisfied(list2);
//diamond pattern
var list3 = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("C", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("D", sortAfter: new[] {"C", "B"}, side: ModSide.Client),
Make("E", sortAfter: new[] {"D"})
};
AssertModException(
() => AssertSortSatisfied(list3),
new[] { "E" },
"E indirectly depends on A via E -> D -> C -> A\r\n" +
"E indirectly depends on A via E -> D -> B -> A\r\n" +
"Some of these mods may not exist on both client and server. Add a direct sort entries or weak references.");
//diamond pattern (fixed)
var list4 = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("C", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("D", sortAfter: new[] {"C", "B"}, side: ModSide.Client),
Make("E", sortAfter: new[] {"D", "A"})
};
AssertSortSatisfied(list4);
}
[TestMethod]
public void TestSidedSortsMatch() {
//diamond pattern
var list1 = new List<LocalMod> {
Make("A"),
Make("B", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("C", sortAfter: new[] {"A"}, side: ModSide.Client),
Make("D", sortAfter: new[] {"C", "B"}, side: ModSide.Client),
Make("E", sortAfter: new[] {"D", "A"})
};
var s1 = AssertSortSatisfied(list1).Where(m => m.properties.side == ModSide.Both).ToList();
var s2 = AssertSortSatisfied(list1.Where(m => m.properties.side == ModSide.Both).ToList());
Assert.IsTrue(Enumerable.SequenceEqual(s1, s2));
//reverse the order
var list2 = new List<LocalMod> {
Make("E"),
Make("D", sortAfter: new[] {"E"}, side: ModSide.Client),
Make("C", sortAfter: new[] {"E"}, side: ModSide.Client),
Make("B", sortAfter: new[] {"C", "D"}, side: ModSide.Client),
Make("A", sortAfter: new[] {"B", "E"})
};
s1 = AssertSortSatisfied(list2).Where(m => m.properties.side == ModSide.Both).ToList();
s2 = AssertSortSatisfied(list2.Where(m => m.properties.side == ModSide.Both).ToList());
Assert.IsTrue(Enumerable.SequenceEqual(s1, s2));
//mostly independent sort with random client only before/afters
var list3 = new List<LocalMod> {
Make("A"),
Make("B", ModSide.Client, sortBefore: new [] {"A"}),
Make("C"),
Make("D", ModSide.Client, sortAfter: new [] {"F"}),
Make("E", sortAfter: new [] {"G"}),
Make("F", ModSide.Client, sortAfter: new [] {"E, G"}),
Make("G"),
Make("H"),
};
s1 = AssertSortSatisfied(list3).Where(m => m.properties.side == ModSide.Both).ToList();
s2 = AssertSortSatisfied(list3.Where(m => m.properties.side == ModSide.Both).ToList());
Assert.IsTrue(Enumerable.SequenceEqual(s1, s2));
}
}
}