Skip to content

Commit

Permalink
Correctly convert types
Browse files Browse the repository at this point in the history
  • Loading branch information
kairichard committed Dec 16, 2019
1 parent 6499016 commit 043dd1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 18 additions & 2 deletions pkg/elem/rand_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,24 @@ var randRangeCfg = &builtinConfig{
}

data := i.(map[string]interface{})
max := data["max"].(int)
min := data["min"].(int)
maxF, ok := data["max"].(float64)
var (
max int
min int
)
if !ok {
// has to be int at this point
max = data["max"].(int)
} else {
max = int(maxF)
}
minF, ok := data["min"].(float64)
if !ok {
// has to be int at this point
min = data["max"].(int)
} else {
min = int(minF)
}
// This generates values for the following interval [min, max] e.g. {min <= x <= max}
out.Push(min + rand.Intn(max-min+1))
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/elem/rand_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ func Test_Rand_Range(t *testing.T) {
o.Main().In().Push(map[string]interface{}{"min": 0, "max": 0})
a.PortPushes(0, o.Main().Out())

// this breaks type conversion
o = buildRandOperator(t)
o.Main().In().Push(map[string]interface{}{"min": 0.0, "max": 0})
a.PortPushes(nil, o.Main().Out())
a.PortPushes(0, o.Main().Out())

// this breaks type conversion
o = buildRandOperator(t)
o.Main().In().Push(map[string]interface{}{"min": "0.0", "max": 0})
a.PortPushes(nil, o.Main().Out())
a.PortPushes(0, o.Main().Out())
}
func Test_Rand_Range_Negative_Values(t *testing.T) {
a := assertions.New(t)
Expand Down

0 comments on commit 043dd1f

Please sign in to comment.