Skip to content

Commit

Permalink
Merge pull request #103 from c-bata/fix-int-uniform-contains
Browse files Browse the repository at this point in the history
Fix IntUniformDistributions.Contains()
  • Loading branch information
c-bata authored Apr 12, 2020
2 parents ae17410 + 7a8deb4 commit d920a40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func (d *IntUniformDistribution) Single() bool {

// Contains to check a parameter value is contained in the range of this distribution.
func (d *IntUniformDistribution) Contains(ir float64) bool {
value := int(ir)
value := d.ToExternalRepr(ir).(int)
if d.Single() {
return value == d.Low
}
return d.Low <= value && value < d.High
return d.Low <= value && value <= d.High
}

// StepIntUniformDistributionName is the identifier name of IntUniformDistribution
Expand Down
20 changes: 16 additions & 4 deletions distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestDistributionToExternalRepresentation(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.distribution.ToExternalRepr(tt.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UniformDistribution.ToExternalRepr() = %v, want %v", got, tt.want)
t.Errorf("Distribution.ToExternalRepr() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestDistributionSingle(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.distribution.Single(); got != tt.want {
t.Errorf("UniformDistribution.Single() = %v, want %v", got, tt.want)
t.Errorf("Distribution.Single() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -281,11 +281,23 @@ func TestDistributionContains(t *testing.T) {
want: false,
},
{
name: "int uniform distribution true",
name: "int uniform distribution true 1",
distribution: &goptuna.IntUniformDistribution{Low: 0, High: 10},
args: 3,
want: true,
},
{
name: "int uniform distribution true 2",
distribution: &goptuna.IntUniformDistribution{Low: 0, High: 10},
args: -0.4999,
want: true,
},
{
name: "int uniform distribution true 3",
distribution: &goptuna.IntUniformDistribution{Low: 0, High: 10},
args: 10.4999,
want: true,
},
{
name: "int uniform distribution lower",
distribution: &goptuna.IntUniformDistribution{Low: 0, High: 10},
Expand Down Expand Up @@ -374,7 +386,7 @@ func TestDistributionContains(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.distribution.Contains(tt.args); got != tt.want {
t.Errorf("UniformDistribution.ToInternalRepr() = %v, want %v", got, tt.want)
t.Errorf("Distribution.ToInternalRepr() = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit d920a40

Please sign in to comment.