Skip to content

Commit

Permalink
Add tests for ParameterSet equality (#4550)
Browse files Browse the repository at this point in the history
This commit stabilizes test coverage in ParameterSet.Equals and
ParameterSet.ContainsParamValue.
  • Loading branch information
sharwell authored Dec 11, 2019
1 parent d306ac8 commit cd40a07
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/Microsoft.ML.AutoML.Tests/SweeperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Microsoft.ML.AutoML.Test
Expand Down Expand Up @@ -235,5 +236,41 @@ public void TestStringParameterValue()
Assert.Equal(value1.GetHashCode(), value1.GetHashCode());
Assert.Equal(value1.GetHashCode(), duplicateValue1.GetHashCode());
}

[Fact]
public void TestParameterSetEquality()
{
LongParameterValue value1 = new LongParameterValue(nameof(value1), 1);
LongParameterValue value2 = new LongParameterValue(nameof(value2), 2);
StringParameterValue stringValue1 = new StringParameterValue(nameof(value1), "1");

var parameterSet = new ParameterSet(new[] { value1 });
Assert.False(parameterSet.Equals(null));

// Verify Equals for sets with different hash codes
var parameterSetNewHash = new ParameterSet(new IParameterValue[] { value1 }.ToDictionary(x => x.Name), hash: parameterSet.GetHashCode() + 1);
Assert.NotEqual(parameterSet.GetHashCode(), parameterSetNewHash.GetHashCode());
Assert.False(parameterSet.Equals(parameterSetNewHash));

// Verify Equals for sets with the same hash code, but different number of values
var parameterSetMoreValues = new ParameterSet(new IParameterValue[] { value1, value2 }.ToDictionary(x => x.Name), hash: parameterSet.GetHashCode());
Assert.Equal(parameterSet.GetHashCode(), parameterSetMoreValues.GetHashCode());
Assert.False(parameterSet.Equals(parameterSetMoreValues));

// Verify Equals for sets with the same hash and item counts, but one of the items has a different name
var parameterSetDifferentName = new ParameterSet(new IParameterValue[] { value2 }.ToDictionary(x => x.Name), hash: parameterSet.GetHashCode());
Assert.Equal(parameterSet.GetHashCode(), parameterSetDifferentName.GetHashCode());
Assert.False(parameterSet.Equals(parameterSetDifferentName));

// Verify Equals for sets with the same hash and item names, but one of the items has a different value
var parameterSetDifferentValue = new ParameterSet(new IParameterValue[] { stringValue1 }.ToDictionary(x => x.Name), hash: parameterSet.GetHashCode());
Assert.Equal(parameterSet.GetHashCode(), parameterSetDifferentValue.GetHashCode());
Assert.False(parameterSet.Equals(parameterSetDifferentValue));

// Verify Equals for sets with the same hash and items
var parameterSetSameHash = new ParameterSet(new IParameterValue[] { value1 }.ToDictionary(x => x.Name), hash: parameterSet.GetHashCode());
Assert.Equal(parameterSet.GetHashCode(), parameterSetSameHash.GetHashCode());
Assert.True(parameterSet.Equals(parameterSetSameHash));
}
}
}

0 comments on commit cd40a07

Please sign in to comment.