1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15var methods = ['entries', 'keys', 'values', Symbol.iterator]; 16 17methods.forEach(function (method) { 18 try { 19 Map.prototype[method].call(5); 20 assert(false); 21 } catch (e) { 22 assert(e instanceof TypeError); 23 } 24}); 25 26methods.forEach(function (method) { 27 try { 28 Map.prototype[method].call({}); 29 assert(false); 30 } catch (e) { 31 assert(e instanceof TypeError); 32 } 33}); 34 35var testArray = [{0: '0', 1: 0}, 36 {0: '1', 1: 1}, 37 {0: '2', 1: 2}, 38 {0: '3', 1: 3}, 39 {0: '4', 1: 4}, 40 {0: '5', 1: 5}, 41 {0: '6', 1: 6}]; 42 43var m = new Map(testArray); 44 45methods.forEach(function(method) { 46 assert(m[method]().toString() === '[object Map Iterator]'); 47}); 48 49methods.forEach(function (method) { 50 try { 51 m[method].next.call(5); 52 assert(false); 53 } catch (e) { 54 assert(e instanceof TypeError); 55 } 56}); 57 58methods.forEach(function (method) { 59 try { 60 m[method].next.call({}); 61 assert(false); 62 } catch (e) { 63 assert(e instanceof TypeError); 64 } 65}); 66 67var entryIterators = [m.entries(), m[Symbol.iterator]()]; 68var keyIterator = m.keys(); 69var valueIterator = m.values(); 70var elementCount = m.size; 71 72for (var i = 0; i < elementCount; i++) { 73 entryIterators.forEach(function(element) { 74 var next = element.next(); 75 assert(next.done === false); 76 assert(next.value[0] === '' + i); 77 assert(next.value[1] === i); 78 }); 79 80 var next = keyIterator.next(); 81 assert(next.done === false); 82 assert(next.value === '' + i); 83 84 var next = valueIterator.next(); 85 assert(next.done === false); 86 assert(next.value === i); 87} 88 89entryIterators.forEach(function(element) { 90 var next = element.next(); 91 assert(next.done === true); 92 assert(next.value === undefined); 93}); 94 95var next = keyIterator.next(); 96assert(next.done === true); 97assert(next.value === undefined); 98 99next = valueIterator.next(); 100assert(next.done === true); 101assert(next.value === undefined); 102 103var entryIterators = [m.entries(), m[Symbol.iterator]()]; 104var keyIterator = m.keys(); 105var valueIterator = m.values(); 106var elementCount = m.size; 107 108for (var i = 0; i < elementCount; i++) { 109 entryIterators.forEach(function(element) { 110 var next = element.next(); 111 assert(next.done === false); 112 assert(next.value[0] === '' + i); 113 assert(next.value[1] === i); 114 }); 115 116 var next = keyIterator.next(); 117 assert(next.done === false); 118 assert(next.value === '' + i); 119 120 var next = valueIterator.next(); 121 assert(next.done === false); 122 assert(next.value === i); 123 m.delete('' + i); 124} 125 126assert(m.size === 0); 127 128m = new Map(testArray); 129var loopCount = 0; 130var expected = [{0: '0', 1: 0}, 131 {0: '2', 1: 2}, 132 {0: '4', 1: 4}, 133 {0: '6', 1: 6}, 134 {0: '1', 1: 1}, 135 {0: '3', 1: 3}, 136 {0: '5', 1: 5}] 137 138m.forEach(function(value, key) { 139 if (loopCount === 0) { 140 for (i = 0; i < testArray.length; i++) { 141 if (i % 2) { 142 m.delete(testArray[i][0]); 143 m.set(testArray[i][0], testArray[i][1]); 144 } 145 } 146 } 147 148 assert (key === expected[loopCount][0]); 149 assert (value === expected[loopCount][1]); 150 151 loopCount++; 152}); 153 154assert(loopCount === expected.length); 155 156loopCount = 0; 157expected = [{0: '0', 1: 0}, 158 {0: '1', 1: 1}]; 159 160for (var [key, value] of m) { 161 if (loopCount === 0) { 162 m.clear(); 163 m.set('1', 1); 164 } 165 166 assert(key === expected[loopCount][0]); 167 assert(value === expected[loopCount][1]); 168 169 loopCount++; 170} 171 172m = new Map(testArray); 173loopCount = 0; 174 175for (var [key, value] of m) { 176 if (loopCount === 0) { 177 m.delete('' + testArray.length - 1); 178 } 179 180 assert(key === '' + loopCount); 181 assert(value === loopCount); 182 183 loopCount++; 184} 185