From cfe1f10e08f1f53fec4ed0c25bef063563c9d653 Mon Sep 17 00:00:00 2001 From: Alun Evans Date: Tue, 24 Sep 2019 15:55:23 -0700 Subject: [PATCH 1/2] Fix starlark map iteration for maps > 64 entries --- _fixtures/starlark_map_iteration.star | 8 +++++--- _fixtures/testvariables2.go | 25 +++++++++++++++++++++++++ pkg/terminal/starbind/conv.go | 2 +- pkg/terminal/starlark_test.go | 5 ++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/_fixtures/starlark_map_iteration.star b/_fixtures/starlark_map_iteration.star index 17c535e399..48e6fede69 100644 --- a/_fixtures/starlark_map_iteration.star +++ b/_fixtures/starlark_map_iteration.star @@ -1,6 +1,8 @@ v = eval(None, "m1").Variable n = 0 +d = {} for k in v.Value: - n = n + 1 - print(k) -print(n) + if not d.get(k): + n = n + 1 + d[k] = True +print("values=", n, sep="") diff --git a/_fixtures/testvariables2.go b/_fixtures/testvariables2.go index 8679f54e1e..0dd6c7bca3 100644 --- a/_fixtures/testvariables2.go +++ b/_fixtures/testvariables2.go @@ -173,6 +173,31 @@ func main() { "tumblers": astruct{}, "horticulturists": astruct{}, "thallium": astruct{}, + "capital": astruct{}, + "paramese": astruct{}, + "vaccinationist": astruct{}, + "shadrach": astruct{}, + "unsincereness": astruct{}, + "undazzled": astruct{}, + "heautomorphism": astruct{}, + "undermeasure": astruct{}, + "intentionally": astruct{}, + "glycine": astruct{}, + "basiliscine": astruct{}, + "preinvolvement": astruct{}, + "adversaria": astruct{}, + "capocchia": astruct{}, + "annunciable": astruct{}, + "unguidableness": astruct{}, + "prankster": astruct{}, + "jagless": astruct{}, + "hormonal": astruct{}, + "crenature": astruct{}, + "unfluttering": astruct{}, + "councilmanic": astruct{}, + "Micraster": astruct{}, + "raphidiferous": astruct{}, + "groomer": astruct{}, } var mnil map[string]astruct = nil m2 := map[int]*astruct{1: &astruct{10, 11}} diff --git a/pkg/terminal/starbind/conv.go b/pkg/terminal/starbind/conv.go index d0d9176367..8385ae667b 100644 --- a/pkg/terminal/starbind/conv.go +++ b/pkg/terminal/starbind/conv.go @@ -573,7 +573,7 @@ func (it *mapVariableAsStarlarkValueIterator) Next(p *starlark.Value) bool { return false } if it.cur >= len(it.v.Children) { - v2 := it.env.autoLoad(fmt.Sprintf("%s[%d:]", varAddrExpr(it.v), len(it.v.Children))) + v2 := it.env.autoLoad(fmt.Sprintf("%s[%d:]", varAddrExpr(it.v), len(it.v.Children)/2)) it.v.Children = append(it.v.Children, v2.Children...) } if it.cur >= len(it.v.Children) { diff --git a/pkg/terminal/starlark_test.go b/pkg/terminal/starlark_test.go index 665c5b4c68..1cdee0cb07 100644 --- a/pkg/terminal/starlark_test.go +++ b/pkg/terminal/starlark_test.go @@ -15,7 +15,7 @@ func TestStarlarkExamples(t *testing.T) { t.Run("linked_list", func(t *testing.T) { testStarlarkExampleLinkedList(t, term) }) t.Run("echo_expr", func(t *testing.T) { testStarlarkEchoExpr(t, term) }) t.Run("find_array", func(t *testing.T) { testStarlarkFindArray(t, term) }) - t.Run("map_iteartion", func(t *testing.T) { testStarlarkMapIteration(t, term) }) + t.Run("map_iteration", func(t *testing.T) { testStarlarkMapIteration(t, term) }) }) } @@ -112,6 +112,9 @@ func testStarlarkFindArray(t *testing.T, term *FakeTerminal) { func testStarlarkMapIteration(t *testing.T, term *FakeTerminal) { out := term.MustExec("source " + findStarFile("starlark_map_iteration")) + if !strings.Contains(out, "values=66") { + t.Fatalf("testStarlarkMapIteration example failed") + } t.Logf("%s", out) } From a13bcd2eb92c5bd44fd598d2fd0074165bd315b8 Mon Sep 17 00:00:00 2001 From: Alun Evans Date: Tue, 24 Sep 2019 16:37:37 -0700 Subject: [PATCH 2/2] Fix TestMapEvaluation --- service/test/variables_test.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/service/test/variables_test.go b/service/test/variables_test.go index 02534c9e3e..46f04df706 100644 --- a/service/test/variables_test.go +++ b/service/test/variables_test.go @@ -689,7 +689,7 @@ func TestEvalExpression(t *testing.T) { {"len(ch1)", false, "4", "4", "", nil}, {"cap(chnil)", false, "0", "0", "", nil}, {"len(chnil)", false, "0", "0", "", nil}, - {"len(m1)", false, "41", "41", "", nil}, + {"len(m1)", false, "66", "66", "", nil}, {"len(mnil)", false, "0", "0", "", nil}, {"imag(cpx1)", false, "2", "2", "", nil}, {"real(cpx1)", false, "1", "1", "", nil}, @@ -892,24 +892,31 @@ func TestMapEvaluation(t *testing.T) { t.Fatalf("Wrong type: %s", m1.Type) } - if len(m1.Children)/2 != 41 { + if len(m1.Children)/2 != 64 { t.Fatalf("Wrong number of children: %d", len(m1.Children)/2) } - found := false - for i := range m1.Children { - if i%2 == 0 && m1.Children[i].Value == "Malone" { - found = true - } + m1sliced, err := evalVariable(p, "m1[64:]", pnormalLoadConfig) + assertNoError(err, t, "EvalVariable(m1[64:])") + if len(m1sliced.Children)/2 != int(m1.Len-64) { + t.Fatalf("Wrong number of children (after slicing): %d", len(m1sliced.Children)/2) } - if !found { - t.Fatalf("Could not find Malone") + + countMalone := func(m *api.Variable) int { + found := 0 + for i := range m.Children { + if i%2 == 0 && m.Children[i].Value == "Malone" { + found++ + } + } + return found } - m1sliced, err := evalVariable(p, "m1[10:]", pnormalLoadConfig) - assertNoError(err, t, "EvalVariable(m1[10:])") - if len(m1sliced.Children)/2 != int(m1.Len-10) { - t.Fatalf("Wrong number of children (after slicing): %d", len(m1sliced.Children)/2) + found := countMalone(m1) + found += countMalone(api.ConvertVar(m1sliced)) + + if found != 1 { + t.Fatalf("Could not find Malone exactly 1 time: found %d", found) } }) }