Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IntUniformDistributions.Contains() #103

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// 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