Add more test cases

This commit is contained in:
2025-03-16 23:21:14 +01:00
parent dbfe627877
commit 2df23dde06
2 changed files with 201 additions and 37 deletions

View File

@@ -49,27 +49,54 @@ class ModListTroubleshooter {
} }
ModList linearForward({int stepSize = 20}) { ModList linearForward({int stepSize = 20}) {
_endIndex = _startIndex + stepSize; final totalMods = originalModList.activeMods.length;
if (_startIndex >= totalMods || _startIndex == _endIndex) {
int newStart = totalMods - stepSize;
if (newStart < 0) {
newStart = 0;
}
_startIndex = newStart;
}
_startIndex = _startIndex.clamp(0, totalMods);
_endIndex = (_startIndex + stepSize).clamp(0, totalMods);
final subset = originalModList.activeMods.keys.toList().sublist( final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex, _startIndex,
_endIndex, _endIndex,
); );
currentModList.disableAll(); currentModList.disableAll();
currentModList.enableMods(subset); currentModList.enableMods(subset);
_startIndex = _endIndex; _startIndex = _endIndex;
return currentModList; return currentModList;
} }
ModList linearBackward({int stepSize = 20}) { ModList linearBackward({int stepSize = 20}) {
_startIndex = _endIndex - stepSize; final totalMods = originalModList.activeMods.length;
_endIndex = _startIndex;
if (_endIndex <= 0 || _startIndex == _endIndex) {
int newEnd = stepSize;
if (newEnd > totalMods) {
newEnd = totalMods;
}
_endIndex = newEnd;
}
_endIndex = _endIndex.clamp(0, totalMods);
_startIndex = (_endIndex - stepSize).clamp(0, _endIndex);
final subset = originalModList.activeMods.keys.toList().sublist( final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex, _startIndex,
_endIndex, _endIndex,
); );
currentModList.disableAll(); currentModList.disableAll();
currentModList.enableMods(subset); currentModList.enableMods(subset);
_endIndex = _startIndex; _endIndex = _startIndex;
return currentModList; return currentModList;
} }

View File

@@ -158,6 +158,45 @@ void main() {
expect(result.activeMods.keys.last, equals('test.mod9')); expect(result.activeMods.keys.last, equals('test.mod9'));
}, },
); );
test('Bisect should handle abuse gracefully', () {
final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(15));
result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(8));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(4));
result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(2));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod9'));
result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod9'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod9'));
result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod9'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod9'));
result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod9'));
});
}); });
group('Linear Tests', () { group('Linear Tests', () {
@@ -193,61 +232,159 @@ void main() {
var result = troubleshooter.linearForward(stepSize: 10); var result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10)); expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod0')); expect(result.activeMods.keys.first, equals('test.mod0'));
expect(result.activeMods.keys.last, equals('test.mod9'));
result = troubleshooter.linearForward(stepSize: 10); result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10)); expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod10')); expect(result.activeMods.keys.first, equals('test.mod10'));
expect(result.activeMods.keys.last, equals('test.mod19'));
result = troubleshooter.linearForward(stepSize: 10); result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10)); expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod20')); expect(result.activeMods.keys.first, equals('test.mod20'));
expect(result.activeMods.keys.last, equals('test.mod29'));
}, },
); );
test( test(
'Bisect search should end up with half the mods every backward iteration until 1', 'Linear search should end up with 10 mods every backward iteration',
() { () {
final troubleshooter = ModListTroubleshooter(modList); final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.binaryBackward(); var result = troubleshooter.linearBackward(stepSize: 10);
// Half of our initial 30 expect(result.activeMods.length, equals(10));
expect(result.activeMods.length, equals(15)); expect(result.activeMods.keys.first, equals('test.mod20'));
expect(result.activeMods.keys.last, equals('test.mod29'));
result = troubleshooter.binaryBackward(); result = troubleshooter.linearBackward(stepSize: 10);
// Half of our previous result expect(result.activeMods.length, equals(10));
expect(result.activeMods.length, equals(8)); expect(result.activeMods.keys.first, equals('test.mod10'));
expect(result.activeMods.keys.last, equals('test.mod19'));
result = troubleshooter.binaryBackward(); result = troubleshooter.linearBackward(stepSize: 10);
expect(result.activeMods.length, equals(4)); expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod0'));
result = troubleshooter.binaryBackward(); expect(result.activeMods.keys.last, equals('test.mod9'));
expect(result.activeMods.length, equals(2));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(1));
}, },
); );
test( test('Linear search should end up with 10 mods every iteration', () {
'Bisect search should end up with half the mods every iteration until 1',
() {
final troubleshooter = ModListTroubleshooter(modList); final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.binaryBackward(); var result = troubleshooter.linearBackward(stepSize: 10);
// Half of our initial 30 expect(result.activeMods.length, equals(10));
expect(result.activeMods.length, equals(15)); expect(result.activeMods.keys.first, equals('test.mod20'));
expect(result.activeMods.keys.last, equals('test.mod29'));
result = troubleshooter.binaryForward(); result = troubleshooter.linearForward(stepSize: 10);
// Half of our previous result expect(result.activeMods.length, equals(10));
expect(result.activeMods.length, equals(8)); expect(result.activeMods.keys.first, equals('test.mod20'));
expect(result.activeMods.keys.last, equals('test.mod29'));
result = troubleshooter.binaryBackward(); result = troubleshooter.linearBackward(stepSize: 10);
expect(result.activeMods.length, equals(4)); expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod20'));
expect(result.activeMods.keys.last, equals('test.mod29'));
});
test('Linear search should handle abuse gracefully', () {
final troubleshooter = ModListTroubleshooter(modList);
result = troubleshooter.binaryForward(); var result = troubleshooter.linearBackward(stepSize: 10);
expect(result.activeMods.length, equals(2)); expect(result.activeMods.length, equals(10));
result = troubleshooter.binaryBackward(); result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(1)); expect(result.activeMods.length, equals(10));
},
); result = troubleshooter.linearBackward(stepSize: 10);
expect(result.activeMods.length, equals(10));
result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10));
result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10));
});
test('Linear search cannot return more items than there are', () {
final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.linearBackward(stepSize: 10000);
expect(result.activeMods.length, equals(30));
result = troubleshooter.linearForward(stepSize: 10000);
expect(result.activeMods.length, equals(30));
});
});
group('Loading dependencies', () {
late ModList modList = ModList();
setUp(() {
modList = ModList();
for (int i = 0; i < 20; i++) {
final modId = 'test.mod$i';
var mod = makeDummy().copyWith(name: 'Test Mod $i', id: modId);
if (i % 3 == 0) {
mod = mod.copyWith(dependencies: ['test.mod${i + 1}']);
}
modList.mods[modId] = mod;
}
// Dependencies are:
// 0 -> 1
// 3 -> 4
// 6 -> 7
// 9 -> 10
// 12 -> 13
// 15 -> 16
// 18 -> 19
modList.enableAll();
});
// Not that it has any reason to since they're completely detached...
test('Loading dependencies should not fuck up troubleshooter', () {
final troubleshooter = ModListTroubleshooter(modList);
final expectedFirst = [
'test.mod1',
// 0 depends on 1
'test.mod0',
'test.mod2',
// 3 depends on 4
'test.mod4',
'test.mod3',
'test.mod5',
// 6 depends on 7
'test.mod7',
'test.mod6',
'test.mod8',
// 9 depends on 10
'test.mod10',
'test.mod9',
];
var result = troubleshooter.linearForward(stepSize: 10);
var loadOrder = result.loadRequired();
expect(loadOrder.length, equals(11));
for (int i = 0; i < expectedFirst.length; i++) {
expect(loadOrder[i], equals(expectedFirst[i]));
}
final expectedSecond = [
'test.mod10',
'test.mod11',
// 12 depends on 13
'test.mod13',
'test.mod12',
'test.mod14',
// 15 depends on 16
'test.mod16',
'test.mod15',
'test.mod17',
// 18 depends on 19
'test.mod19',
'test.mod18',
];
result = troubleshooter.linearForward(stepSize: 10);
loadOrder = result.loadRequired();
expect(loadOrder.length, equals(10));
for (int i = 0; i < expectedSecond.length; i++) {
expect(loadOrder[i], equals(expectedSecond[i]));
}
});
}); });
} }