From dbfe6278773c3cdc904c873d2c5b6ee50b30f9ee Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 16 Mar 2025 23:14:12 +0100 Subject: [PATCH] Implement parts of linear search and add more test cases for bisect --- lib/mod_list_troubleshooter.dart | 21 ++++- test/mod_list_troubleshooter_test.dart | 108 ++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 3 deletions(-) diff --git a/lib/mod_list_troubleshooter.dart b/lib/mod_list_troubleshooter.dart index d21b53c..fb343cb 100644 --- a/lib/mod_list_troubleshooter.dart +++ b/lib/mod_list_troubleshooter.dart @@ -48,11 +48,28 @@ class ModListTroubleshooter { 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; } - 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; } diff --git a/test/mod_list_troubleshooter_test.dart b/test/mod_list_troubleshooter_test.dart index fbef505..fcf3392 100644 --- a/test/mod_list_troubleshooter_test.dart +++ b/test/mod_list_troubleshooter_test.dart @@ -49,7 +49,7 @@ Mod makeDummy() { } void main() { - group('ModListTroubleshooter Bisect Tests', () { + group('Bisect Tests', () { late ModList modList = ModList(); setUp(() { modList = ModList(); @@ -82,19 +82,125 @@ void main() { var result = troubleshooter.binaryForward(); // Half of our initial 30 expect(result.activeMods.length, equals(15)); + expect(result.activeMods.keys.first, equals('test.mod15')); result = troubleshooter.binaryForward(); // Half of our previous result expect(result.activeMods.length, equals(8)); + expect(result.activeMods.keys.first, equals('test.mod22')); result = troubleshooter.binaryForward(); expect(result.activeMods.length, equals(4)); + expect(result.activeMods.keys.first, equals('test.mod26')); result = troubleshooter.binaryForward(); expect(result.activeMods.length, equals(2)); + expect(result.activeMods.keys.first, equals('test.mod28')); result = troubleshooter.binaryForward(); 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(