Skip to content

Commit

Permalink
Solve a reflection mistake with unsigned ints
Browse files Browse the repository at this point in the history
Thise closes #1 and URXtech/planout-golang#27
  • Loading branch information
GregBowyer committed Aug 25, 2016
1 parent b65e929 commit 3ab2658
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
52 changes: 52 additions & 0 deletions fixtures_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015 Biasedunit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package planout

import (
"testing"
)

func TestFixture01(t *testing.T) {
testFixture("test/fixtures/1.json", t);
}

func testFixture(fixture string, t *testing.T) {
js := readTest(fixture)

var runs int = 100
var userid int = 123454

for i := 0; i < runs; i++ {
params := make(map[string]interface{})
params["userid"] = userid

expt := &Interpreter{
Salt: "foo",
Evaluated: false,
Inputs: params,
Outputs: map[string]interface{}{},
Overrides: map[string]interface{}{},
Code: js,
}

_, ok := expt.Run()
if !ok {
t.Errorf("Error running experiment %s\n", fixture);
return
}
}
}
59 changes: 59 additions & 0 deletions test/fixtures/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"op": "seq",
"seq": [
{
"op": "set",
"var": "m",
"value": {
"min": 0,
"max": 100,
"unit": {
"op": "get",
"var": "userid"
},
"op": "randomInteger"
}
},
{
"op": "set",
"var": "n",
"value": {
"min": 1,
"max": 10,
"unit": {
"op": "get",
"var": "userid"
},
"op": "randomInteger"
}
},
{
"op": "cond",
"cond": [
{
"if": {
"op": "<",
"left": {
"op": "get",
"var": "n"
},
"right": {
"op": "get",
"var": "m"
}
},
"then": {
"op": "seq",
"seq": [
{
"op": "set",
"var": "m",
"value": 1000
}
]
}
}
]
}
]
}
5 changes: 5 additions & 0 deletions test/fixtures/1.planout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
m = randomInteger(min=0, max=100, unit=userid);
n = randomInteger(min=1, max=10, unit=userid);
if (n < m ) {
m = 1000;
}
5 changes: 4 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ func toNumber(value interface{}) (float64, bool) {
x = float64(value.(float32))
}
return x, true
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
case int, int8, int16, int32, int64:
i := reflect.ValueOf(value)
return float64(i.Int()), true
case uint, uint8, uint16, uint32, uint64:
i := reflect.ValueOf(value)
return float64(i.Uint()), true
case bool:
if value {
return 1, true
Expand Down

0 comments on commit 3ab2658

Please sign in to comment.