Implement parts of linear search and add more test cases for bisect

This commit is contained in:
2025-03-16 23:14:12 +01:00
parent 70198ff293
commit dbfe627877
2 changed files with 126 additions and 3 deletions

View File

@@ -48,11 +48,28 @@ class ModListTroubleshooter {
return currentModList; return currentModList;
} }
ModList linearForward() { ModList linearForward({int stepSize = 20}) {
_endIndex = _startIndex + stepSize;
final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex,
_endIndex,
);
currentModList.disableAll();
currentModList.enableMods(subset);
_startIndex = _endIndex;
return currentModList; return currentModList;
} }
ModList linearBackward() { ModList linearBackward({int stepSize = 20}) {
_startIndex = _endIndex - stepSize;
_endIndex = _startIndex;
final subset = originalModList.activeMods.keys.toList().sublist(
_startIndex,
_endIndex,
);
currentModList.disableAll();
currentModList.enableMods(subset);
_endIndex = _startIndex;
return currentModList; return currentModList;
} }

View File

@@ -49,7 +49,7 @@ Mod makeDummy() {
} }
void main() { void main() {
group('ModListTroubleshooter Bisect Tests', () { group('Bisect Tests', () {
late ModList modList = ModList(); late ModList modList = ModList();
setUp(() { setUp(() {
modList = ModList(); modList = ModList();
@@ -82,19 +82,125 @@ void main() {
var result = troubleshooter.binaryForward(); var result = troubleshooter.binaryForward();
// Half of our initial 30 // Half of our initial 30
expect(result.activeMods.length, equals(15)); expect(result.activeMods.length, equals(15));
expect(result.activeMods.keys.first, equals('test.mod15'));
result = troubleshooter.binaryForward(); result = troubleshooter.binaryForward();
// Half of our previous result // Half of our previous result
expect(result.activeMods.length, equals(8)); expect(result.activeMods.length, equals(8));
expect(result.activeMods.keys.first, equals('test.mod22'));
result = troubleshooter.binaryForward(); result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(4)); expect(result.activeMods.length, equals(4));
expect(result.activeMods.keys.first, equals('test.mod26'));
result = troubleshooter.binaryForward(); result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(2)); expect(result.activeMods.length, equals(2));
expect(result.activeMods.keys.first, equals('test.mod28'));
result = troubleshooter.binaryForward(); result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(1)); expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.first, equals('test.mod29'));
},
);
test(
'Bisect search should end up with half the mods every backward iteration until 1',
() {
final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.binaryBackward();
// Half of our initial 30
expect(result.activeMods.length, equals(15));
expect(result.activeMods.keys.last, equals('test.mod14'));
result = troubleshooter.binaryBackward();
// Half of our previous result
expect(result.activeMods.length, equals(8));
expect(result.activeMods.keys.last, equals('test.mod7'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(4));
expect(result.activeMods.keys.last, equals('test.mod3'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(2));
expect(result.activeMods.keys.last, equals('test.mod1'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.last, equals('test.mod0'));
},
);
test(
'Bisect search should end up with half the mods every iteration until 1',
() {
final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.binaryBackward();
// Half of our initial 30
expect(result.activeMods.length, equals(15));
expect(result.activeMods.keys.last, equals('test.mod14'));
result = troubleshooter.binaryForward();
// Half of our previous result
expect(result.activeMods.length, equals(8));
expect(result.activeMods.keys.first, equals('test.mod7'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(4));
expect(result.activeMods.keys.last, equals('test.mod10'));
result = troubleshooter.binaryForward();
expect(result.activeMods.length, equals(2));
expect(result.activeMods.keys.first, equals('test.mod9'));
result = troubleshooter.binaryBackward();
expect(result.activeMods.length, equals(1));
expect(result.activeMods.keys.last, equals('test.mod9'));
},
);
});
group('Linear Tests', () {
late ModList modList = ModList();
setUp(() {
modList = ModList();
// Add some base mods
for (int i = 0; i < 20; i++) {
final modId = 'test.mod$i';
final mod = makeDummy().copyWith(name: 'Test Mod $i', id: modId);
modList.mods[modId] = mod;
}
// Add some mods with dependencies
for (int i = 20; i < 30; i++) {
final modId = 'test.mod$i';
final mod = makeDummy().copyWith(
name: 'Test Mod $i',
id: modId,
dependencies: ['test.mod${i - 20}'], // Depend on earlier mods
);
modList.mods[modId] = mod;
}
modList.enableAll();
});
test(
'Linear search should end up with 10 mods every forward iteration',
() {
final troubleshooter = ModListTroubleshooter(modList);
var result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod0'));
result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod10'));
result = troubleshooter.linearForward(stepSize: 10);
expect(result.activeMods.length, equals(10));
expect(result.activeMods.keys.first, equals('test.mod20'));
}, },
); );
test( test(