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 starlark map iteration for maps > 64 entries #1699

Merged
merged 2 commits into from Oct 7, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions _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="")
25 changes: 25 additions & 0 deletions _fixtures/testvariables2.go
Expand Up @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/terminal/starbind/conv.go
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/terminal/starlark_test.go
Expand Up @@ -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) })
})
}

Expand Down Expand Up @@ -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)
}

Expand Down
33 changes: 20 additions & 13 deletions service/test/variables_test.go
Expand Up @@ -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},
Expand Down Expand Up @@ -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)
}
})
}
Expand Down