Move GetDuplicateStatus implementations

This commit is contained in:
Matt Nadareski
2025-05-02 19:54:23 -04:00
parent 7d35594040
commit f38fe9b007
8 changed files with 504 additions and 425 deletions

View File

@@ -1869,31 +1869,32 @@ namespace SabreTools.DatFiles.Test
[Fact]
public void ResolveNamesDB_AllDuplicate_Single()
{
DatFile datFile = new Formats.Logiqx(null, useGame: false);
Machine machine = new Machine();
machine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine");
long machineIndex = datFile.AddMachineDB(machine);
Source source = new Source(0);
long sourceIndex = datFile.AddSourceDB(source);
Rom romA = new Rom();
romA.SetName("rom");
romA.SetFieldValue(Models.Metadata.Rom.SizeKey, 12345);
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "crc");
romA.SetFieldValue(DatItem.MachineKey, (Machine)machine.Clone());
romA.SetFieldValue(DatItem.SourceKey, (Source)source.Clone());
long romAIndex = datFile.AddItemDB(romA, machineIndex, sourceIndex, statsOnly: false);
Rom romB = new Rom();
romB.SetName("rom");
romB.SetFieldValue(Models.Metadata.Rom.SizeKey, 12345);
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "crc");
romB.SetFieldValue(DatItem.MachineKey, (Machine)machine.Clone());
romB.SetFieldValue(DatItem.SourceKey, (Source)source.Clone());
long romBIndex = datFile.AddItemDB(romB, machineIndex, sourceIndex, statsOnly: false);
List<KeyValuePair<long, DatItem>> mappings =
[
new KeyValuePair<long, DatItem>(0, romA),
new KeyValuePair<long, DatItem>(1, romB),
new KeyValuePair<long, DatItem>(romAIndex, romA),
new KeyValuePair<long, DatItem>(romBIndex, romB),
];
DatFile datFile = new Formats.Logiqx(null, useGame: false);
List<KeyValuePair<long, DatItem>> actual = datFile.ResolveNamesDB(mappings);
KeyValuePair<long, DatItem> actualItemA = Assert.Single(actual);

View File

@@ -696,6 +696,209 @@ namespace SabreTools.DatFiles.Test
#endregion
#region GetDuplicateStatus
[Fact]
public void GetDuplicateStatus_NullOther_NoDupe()
{
var dict = new ItemDictionaryDB();
Source? selfSource = null;
Source? lastSource = null;
KeyValuePair<long, DatItem>? item = new KeyValuePair<long, DatItem>(0, new Rom());
KeyValuePair<long, DatItem>? lastItem = null;
var actual = dict.GetDuplicateStatus(item, selfSource, lastItem, lastSource);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentTypes_NoDupe()
{
var dict = new ItemDictionaryDB();
Source? selfSource = null;
Source? lastSource = null;
KeyValuePair<long, DatItem>? rom = new KeyValuePair<long, DatItem>(0, new Rom());
KeyValuePair<long, DatItem>? lastItem = new KeyValuePair<long, DatItem>(1, new Disk());
var actual = dict.GetDuplicateStatus(rom, selfSource, lastItem, lastSource);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_MismatchedHashes_NoDupe()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(1);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "BEEFDEAD");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NameMatch_ExternalAll()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(1);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.External | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NoNameMatch_ExternalHash()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(1);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.External | DupeType.Hash, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NameMatch_InternalAll()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(0);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.Internal | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NoNameMatch_InternalHash()
{
var dict = new ItemDictionaryDB();
Source? sourceA = new Source(0);
long sourceAIndex = dict.AddSource(sourceA);
Source? sourceB = new Source(0);
long sourceBIndex = dict.AddSource(sourceB);
Machine? machineA = new Machine();
machineA.SetName("name-same");
long machineAIndex = dict.AddMachine(machineA);
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
long machineBIndex = dict.AddMachine(machineB);
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romAIndex = dict.AddItem(romA, machineAIndex, sourceAIndex);
KeyValuePair<long, DatItem>? romAPair = new KeyValuePair<long, DatItem>(romAIndex, romA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
long romBIndex = dict.AddItem(romB, machineBIndex, sourceBIndex);
KeyValuePair<long, DatItem>? romBPair = new KeyValuePair<long, DatItem>(romBIndex, romB);
var actual = dict.GetDuplicateStatus(romAPair, sourceA, romBPair, sourceB);
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
}
#endregion
#region GetDuplicates
[Theory]

View File

@@ -535,6 +535,165 @@ namespace SabreTools.DatFiles.Test
#endregion
#region GetDuplicateStatus
[Fact]
public void GetDuplicateStatus_NullOther_NoDupe()
{
var dict = new ItemDictionary();
DatItem item = new Rom();
DatItem? lastItem = null;
var actual = dict.GetDuplicateStatus(item, lastItem);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentTypes_NoDupe()
{
var dict = new ItemDictionary();
var rom = new Rom();
DatItem? lastItem = new Disk();
var actual = dict.GetDuplicateStatus(rom, lastItem);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_MismatchedHashes_NoDupe()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "BEEFDEAD");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal((DupeType)0x00, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NameMatch_ExternalAll()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.External | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_DifferentSource_NoNameMatch_ExternalHash()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(1));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.External | DupeType.Hash, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NameMatch_InternalAll()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.Internal | DupeType.All, actual);
}
[Fact]
public void GetDuplicateStatus_SameSource_NoNameMatch_InternalHash()
{
var dict = new ItemDictionary();
Machine? machineA = new Machine();
machineA.SetName("name-same");
Machine? machineB = new Machine();
machineB.SetName("not-name-same");
var romA = new Rom();
romA.SetName("same-name");
romA.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romA.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romA.CopyMachineInformation(machineA);
var romB = new Rom();
romB.SetName("same-name");
romB.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
romB.SetFieldValue<Source?>(DatItem.SourceKey, new Source(0));
romB.CopyMachineInformation(machineB);
var actual = dict.GetDuplicateStatus(romA, romB);
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
}
#endregion
#region GetDuplicates
[Theory]