1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 10:33:07 +08:00

Merge pull request #11085 from bdach/sampleinfo-consistent-hashcode

This commit is contained in:
Dean Herbert 2020-12-04 10:31:44 +09:00 committed by GitHub
commit 3b38e0259f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 1 deletions

View File

@ -0,0 +1,78 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Game.Audio;
namespace osu.Game.Tests.Audio
{
[TestFixture]
public class SampleInfoEqualityTest
{
[Test]
public void TestSameSingleSamplesAreEqual()
{
var first = new SampleInfo("sample");
var second = new SampleInfo("sample");
assertEquality(first, second);
}
[Test]
public void TestDifferentSingleSamplesAreNotEqual()
{
var first = new SampleInfo("first");
var second = new SampleInfo("second");
assertNonEquality(first, second);
}
[Test]
public void TestDifferentCountSampleSetsAreNotEqual()
{
var first = new SampleInfo("sample", "extra");
var second = new SampleInfo("sample");
assertNonEquality(first, second);
}
[Test]
public void TestDifferentSampleSetsOfSameCountAreNotEqual()
{
var first = new SampleInfo("first", "common");
var second = new SampleInfo("common", "second");
assertNonEquality(first, second);
}
[Test]
public void TestSameOrderSameSampleSetsAreEqual()
{
var first = new SampleInfo("first", "second");
var second = new SampleInfo("first", "second");
assertEquality(first, second);
}
[Test]
public void TestDifferentOrderSameSampleSetsAreEqual()
{
var first = new SampleInfo("first", "second");
var second = new SampleInfo("second", "first");
assertEquality(first, second);
}
private void assertEquality(SampleInfo first, SampleInfo second)
{
Assert.That(first.Equals(second), Is.True);
Assert.That(first.GetHashCode(), Is.EqualTo(second.GetHashCode()));
}
private void assertNonEquality(SampleInfo first, SampleInfo second)
{
Assert.That(first.Equals(second), Is.False);
Assert.That(first.GetHashCode(), Is.Not.EqualTo(second.GetHashCode()));
}
}
}

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@ -17,6 +18,7 @@ namespace osu.Game.Audio
public SampleInfo(params string[] sampleNames)
{
this.sampleNames = sampleNames;
Array.Sort(sampleNames);
}
public IEnumerable<string> LookupNames => sampleNames;
@ -25,7 +27,9 @@ namespace osu.Game.Audio
public override int GetHashCode()
{
return HashCode.Combine(sampleNames, Volume);
return HashCode.Combine(
StructuralComparisons.StructuralEqualityComparer.GetHashCode(sampleNames),
Volume);
}
public bool Equals(SampleInfo other)