pkg/terminal: Fix starlark map iteration for maps > 64 entries (#1699)

* Fix starlark map iteration for maps > 64 entries

* Fix TestMapEvaluation
This commit is contained in:
Alun Evans 2019-10-07 09:35:58 -07:00 committed by Derek Parker
parent bd6a8e56d5
commit 36d688bf14
5 changed files with 56 additions and 19 deletions

@ -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="")

@ -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}}

@ -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) {

@ -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)
}

@ -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
}
}
if !found {
t.Fatalf("Could not find Malone")
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)
}
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)
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
}
found := countMalone(m1)
found += countMalone(api.ConvertVar(m1sliced))
if found != 1 {
t.Fatalf("Could not find Malone exactly 1 time: found %d", found)
}
})
}