feat: add remove() method to Map class with native implementation and tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// Make sure it can grow to some size.
|
||||
// This brute force test basically validates that the map can grow and shrink
|
||||
// its capacity while still behaving correctly.
|
||||
|
||||
var fishes = [
|
||||
"Aeneus corydoras", "African glass catfish", "African lungfish",
|
||||
@@ -65,9 +66,16 @@ for (fish in fishes) {
|
||||
|
||||
IO.print(map.count) // expect: 249
|
||||
|
||||
// Re-add some keys.
|
||||
for (n in 20..50) {
|
||||
map[fishes[n]] = n
|
||||
for (n in 0...150) {
|
||||
map.remove(fishes[n])
|
||||
}
|
||||
|
||||
IO.print(map.count) // expect: 249
|
||||
IO.print(map.count) // expect: 99
|
||||
|
||||
// Make sure we can still find all of the remaining ones.
|
||||
var contained = 0
|
||||
for (n in 150...249) {
|
||||
if (map.containsKey(fishes[n])) contained = contained + 1
|
||||
}
|
||||
|
||||
IO.print(contained) // expect: 99
|
||||
@@ -0,0 +1,18 @@
|
||||
var map = {
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3
|
||||
}
|
||||
|
||||
IO.print(map.count) // expect: 3
|
||||
IO.print(map.remove("two")) // expect: 2
|
||||
IO.print(map.count) // expect: 2
|
||||
IO.print(map.remove("three")) // expect: 3
|
||||
IO.print(map.count) // expect: 1
|
||||
|
||||
// Remove an already removed entry.
|
||||
IO.print(map.remove("two")) // expect: null
|
||||
IO.print(map.count) // expect: 1
|
||||
|
||||
IO.print(map.remove("one")) // expect: 1
|
||||
IO.print(map.count) // expect: 0
|
||||
@@ -0,0 +1 @@
|
||||
var result = {}.remove([]) // expect runtime error: Key must be a value type.
|
||||
Reference in New Issue
Block a user