1
0
forked from Mirror/wren

Add MapEntry to core to let maps be directly iterated.

Fix #372.
This commit is contained in:
Bob Nystrom
2016-07-06 07:17:07 -07:00
parent fcfb053dc1
commit 5418e4f8f3
13 changed files with 129 additions and 9 deletions

View File

@ -0,0 +1,2 @@
System.print({}.isEmpty) // expect: true
System.print({1: 1}.isEmpty) // expect: false

View File

@ -0,0 +1,45 @@
var a = {"one": 1, "two": 2, "three": 3, "four": 4}
// The precise numeric values aren't defined since they are indexes into the
// entry table and the hashing process isn't specified. So we just validate
// what we can assume about them.
System.print(a.iterate(null) is Num) // expect: true
System.print(a.iterate(null) >= 0) // expect: true
System.print(a.iterate(0) is Num) // expect: true
System.print(a.iterate(0) > 0) // expect: true
System.print(a.iterate(1) is Num) // expect: true
System.print(a.iterate(1) > 0) // expect: true
System.print(a.iterate(2) is Num) // expect: true
System.print(a.iterate(2) > 0) // expect: true
System.print(a.iterate(3) is Num) // expect: true
System.print(a.iterate(3) > 0) // expect: true
var previous = -1
var iterator = a.iterate(null)
while (iterator) {
System.print(iterator > previous)
System.print(iterator is Num)
previous = iterator
iterator = a.iterate(iterator)
}
// First entry:
// expect: true
// expect: true
// Second entry:
// expect: true
// expect: true
// Third entry:
// expect: true
// expect: true
// Fourth entry:
// expect: true
// expect: true
// Out of bounds.
System.print(a.iterate(16)) // expect: false
System.print(a.iterate(-1)) // expect: false
// Nothing to iterate in an empty map.
System.print({}.iterate(null)) // expect: false

View File

@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.iterate(1.5) // expect runtime error: Iterator must be an integer.

View File

@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.iterate("2") // expect runtime error: Iterator must be a number.

View File

@ -0,0 +1,13 @@
var a = {1: "one"}
// The actual iterator values are implementation specific, so ask the map.
var iterator = a.iterate(null)
var value = a.iteratorValue(iterator)
System.print(value is MapEntry) // expect: true
System.print(value.key) // expect: 1
System.print(value.value) // expect: one
// The entry does not track the underlying map.
a[1] = "updated"
System.print(value.value) // expect: one

View File

@ -0,0 +1,2 @@
var a = {1: "one"}
a.iteratorValue(1.5) // expect runtime error: Iterator must be an integer.

View File

@ -0,0 +1,2 @@
var a = {1: "one"}
a.iteratorValue("2") // expect runtime error: Iterator must be a number.

View File

@ -0,0 +1,6 @@
var a = {1: "one"}
// The maximum value is based on the map's capacity, not its count, so use a
// sufficiently large enough value for the test to make not affected by growth
// strategy.
a.iteratorValue(9999) // expect runtime error: Iterator out of bounds.

View File

@ -0,0 +1,6 @@
var a = {1: "one"}
// The maximum value is based on the map's capacity, not its count, so use a
// sufficiently large enough value for the test to make not affected by growth
// strategy.
a.iteratorValue(-9999) // expect runtime error: Iterator out of bounds.

View File

@ -0,0 +1,4 @@
var entry = MapEntry.new("key", "value")
System.print(entry.key) // expect: key
System.print(entry.value) // expect: value