Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
115 changes: 59 additions & 56 deletions index_op_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package planout

import (
"testing"
"io/ioutil"
"encoding/json"
"io/ioutil"
"testing"
)

func getInterpreter(filename string) (*Interpreter, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var js map[string]interface{}
err = json.Unmarshal(data, &js)
if err != nil {
return nil, err
}

return &Interpreter{
Name: "the name",
Salt: "the salt",
Evaluated: false,
Inputs: make(map[string]interface{}),
Outputs: make(map[string]interface{}),
Code: js,
}, nil
}

type Inner struct {
Value string
}
Expand All @@ -19,39 +40,24 @@ type NestedStruct struct {
}

func TestNestedIndex(t *testing.T) {
data, err := ioutil.ReadFile("test/nested_index.json")
exp, err := getInterpreter("test/nested_index.json")
if err != nil {
t.Fatal(err)
}
var js map[string]interface{}
err = json.Unmarshal(data, &js)
if (err != nil) {
t.Fatal(err)
}

inputs := make(map[string]interface{})
inputs["s"] = &NestedStruct{
exp.Inputs["s"] = &NestedStruct{
Outer: &Outer{
Inner: &Inner{
Value: "foo",
},
},
}

exp := &Interpreter{
Name: "nested_test",
Salt: "salt123",
Evaluated: false,
Inputs: inputs,
Outputs: make(map[string]interface{}),
Code: js,
}

if _, ok := exp.Run(); !ok {
t.Fatal("Failed to run experiment")
}

if (exp.Outputs["out"] != "foo") {
if exp.Outputs["out"] != "foo" {
t.Fail()
}
}
Expand All @@ -61,30 +67,16 @@ type StructWithArray struct {
}

func TestArrayInStruct(t *testing.T) {
data, err := ioutil.ReadFile("test/array_field_test.json")
exp, err := getInterpreter("test/array_field_test.json")
if err != nil {
t.Fatal(err)
}
var js map[string]interface{}
err = json.Unmarshal(data, &js)
if (err != nil) {
t.Fatal(err)
}

inputs := make(map[string]interface{})
i := 123
inputs["s"] = &StructWithArray{
i := int(123)
exp.Inputs["s"] = &StructWithArray{
Array: []*int{&i},
}

exp := &Interpreter{
Name: "test_array_field",
Salt: "blasdfalks",
Inputs: inputs,
Outputs: make(map[string]interface{}),
Code: js,
}

if _, ok := exp.Run(); !ok {
t.Fatal("Experiment run failed")
}
Expand All @@ -95,40 +87,51 @@ func TestArrayInStruct(t *testing.T) {
}

type StructWithMap struct {
Map map[string]string
Map map[string]int64
}

func TestMapField(t *testing.T) {
data, err := ioutil.ReadFile("test/map_index_test.json")
exp, err := getInterpreter("test/map_index_test.json")
if err != nil {
t.Fatal(err)
}
var js map[string]interface{}
err = json.Unmarshal(data, &js)
if (err != nil) {
t.Fatal(err)
}

inputs := make(map[string]interface{})
mapField := make(map[string]string)
mapField["key"] = "value"
inputs["s"] = &StructWithMap{
mapField := make(map[string]int64)
mapField["key"] = 42
exp.Inputs["s"] = &StructWithMap{
Map: mapField,
}

exp := &Interpreter{
Name: "test_map_index",
Salt: "asdfkhjaslkdfjh",
Inputs: inputs,
Outputs: make(map[string]interface{}),
Code: js,
if _, ok := exp.Run(); !ok {
t.Fatal("Experiment run failed")
}

if elem := exp.Outputs["element"]; elem != int64(42) {
t.Fail()
}

if exp.Outputs["empty"] != nil {
t.Fail()
}
}

type StructWithNilField struct {
None interface{}
}

func TestStructWithNilField(t *testing.T) {
exp, err := getInterpreter("test/struct_with_nil_field.json")
if err != nil {
t.Fatal(err)
}

exp.Inputs["struct"] = &StructWithNilField{}

if _, ok := exp.Run(); !ok {
t.Fatal("Experiment run failed")
}

if elem := exp.Outputs["element"]; elem != "value" {
if exp.Outputs["nil"] != nil {
t.Fail()
}
}
}
4 changes: 4 additions & 0 deletions namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (n *SimpleNamespace) RemoveExperiment(name string) error {
}
}

for _, val := range segmentsToFree {
delete(n.segmentAllocations, uint64(val))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could delete be done inside the loop where values are added tosegmentsToFree? (instead of iterating segmentsToFree outside separately)
https://github.com/biased-unit/planout-golang/blob/master/namespace.go#L98

}

for i := range segmentsToFree {
n.availableSegments = append(n.availableSegments, segmentsToFree[i])
}
Expand Down
6 changes: 5 additions & 1 deletion operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ func unwrapValue(value reflect.Value) interface{} {
case reflect.Bool:
return value.Bool()
default:
return value.Interface()
if value.IsValid() {
return value.Interface()
} else {
return nil
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/map_index_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
},
"index": "key"
}
},
{
"op": "set",
"var": "empty",
"value": {
"op": "index",
"base": {
"op": "get",
"var": "map"
},
"index": "not there"
}
}
]
}
12 changes: 12 additions & 0 deletions test/struct_with_nil_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"op": "set",
"var": "nil",
"value": {
"op": "index",
"base": {
"op": "get",
"var": "struct"
},
"index": "None"
}
}