| // Copyright (C) 2021 Rick Waldron. All rights reserved. |
| // This code is governed by the BSD license found in the LICENSE file. |
| /*--- |
| esid: sec-map.prototype.set |
| description: Observing the expected behavior of valid keys |
| info: | |
| Map.prototype.set ( key , value ) |
| |
| ... |
| Let p be the Record {[[key]]: key, [[value]]: value}. |
| Append p as the last element of entries. |
| ... |
| |
| features: [BigInt, Symbol, TypedArray, WeakRef] |
| ---*/ |
| |
| |
| const negativeZero = -0; |
| const positiveZero = +0; |
| const zero = 0; |
| const one = 1; |
| const twoRaisedToFiftyThreeMinusOne = 2 ** 53 - 1; |
| const int32Array = new Int32Array([zero, one]); |
| const uint32Array = new Uint32Array([zero, one]); |
| const n = 100000000000000000000000000000000000000000000000000000000000000000000000000000000001n; |
| const bigInt = BigInt('100000000000000000000000000000000000000000000000000000000000000000000000000000000001'); |
| const n1 = 1n; |
| const n53 = 9007199254740991n; |
| const fiftyThree = BigInt('9007199254740991'); |
| const bigInt64Array = new BigInt64Array([n1, n53]); |
| const bigUint64Array = new BigUint64Array([n1, n53]); |
| const symbol = Symbol(''); |
| const object = {}; |
| const array = {}; |
| const string = ''; |
| const booleanTrue = true; |
| const booleanFalse = true; |
| const functionExprValue = function() {}; |
| const arrowFunctionValue = () => {}; |
| const classValue = class {}; |
| const map = new Map(); |
| const set = new Set(); |
| const weakMap = new WeakMap(); |
| const weakRef = new WeakRef({}); |
| const weakSet = new WeakSet(); |
| const nullValue = null; |
| const undefinedValue = undefined; |
| let unassigned; |
| |
| { |
| const m = new Map([[negativeZero, negativeZero]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(negativeZero), true); |
| assert.sameValue(m.get(negativeZero), negativeZero); |
| m.delete(negativeZero); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(negativeZero), false); |
| m.set(negativeZero, negativeZero); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(negativeZero), true); |
| assert.sameValue(m.get(negativeZero), negativeZero); |
| }; |
| |
| { |
| const m = new Map([[positiveZero, positiveZero]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(positiveZero), true); |
| assert.sameValue(m.get(positiveZero), positiveZero); |
| m.delete(positiveZero); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(positiveZero), false); |
| m.set(positiveZero, positiveZero); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(positiveZero), true); |
| assert.sameValue(m.get(positiveZero), positiveZero); |
| }; |
| |
| { |
| const m = new Map([[zero, zero]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(zero), true); |
| assert.sameValue(m.get(zero), zero); |
| m.delete(zero); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(zero), false); |
| m.set(zero, zero); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(zero), true); |
| assert.sameValue(m.get(zero), zero); |
| }; |
| |
| { |
| const m = new Map([[one, one]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(one), true); |
| assert.sameValue(m.get(one), one); |
| m.delete(one); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(one), false); |
| m.set(one, one); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(one), true); |
| assert.sameValue(m.get(one), one); |
| }; |
| |
| { |
| const m = new Map([[twoRaisedToFiftyThreeMinusOne, twoRaisedToFiftyThreeMinusOne]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(twoRaisedToFiftyThreeMinusOne), true); |
| assert.sameValue(m.get(twoRaisedToFiftyThreeMinusOne), twoRaisedToFiftyThreeMinusOne); |
| m.delete(twoRaisedToFiftyThreeMinusOne); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(twoRaisedToFiftyThreeMinusOne), false); |
| m.set(twoRaisedToFiftyThreeMinusOne, twoRaisedToFiftyThreeMinusOne); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(twoRaisedToFiftyThreeMinusOne), true); |
| assert.sameValue(m.get(twoRaisedToFiftyThreeMinusOne), twoRaisedToFiftyThreeMinusOne); |
| }; |
| |
| { |
| const m = new Map([[int32Array, int32Array]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(int32Array), true); |
| assert.sameValue(m.get(int32Array), int32Array); |
| m.delete(int32Array); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(int32Array), false); |
| m.set(int32Array, int32Array); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(int32Array), true); |
| assert.sameValue(m.get(int32Array), int32Array); |
| }; |
| |
| { |
| const m = new Map([[uint32Array, uint32Array]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(uint32Array), true); |
| assert.sameValue(m.get(uint32Array), uint32Array); |
| m.delete(uint32Array); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(uint32Array), false); |
| m.set(uint32Array, uint32Array); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(uint32Array), true); |
| assert.sameValue(m.get(uint32Array), uint32Array); |
| }; |
| |
| { |
| const m = new Map([[n, n]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(n), true); |
| assert.sameValue(m.get(n), n); |
| m.delete(n); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(n), false); |
| m.set(n, n); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(n), true); |
| assert.sameValue(m.get(n), n); |
| }; |
| |
| { |
| const m = new Map([[bigInt, bigInt]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(bigInt), true); |
| assert.sameValue(m.get(bigInt), bigInt); |
| m.delete(bigInt); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(bigInt), false); |
| m.set(bigInt, bigInt); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(bigInt), true); |
| assert.sameValue(m.get(bigInt), bigInt); |
| }; |
| |
| { |
| const m = new Map([[n1, n1]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(n1), true); |
| assert.sameValue(m.get(n1), n1); |
| m.delete(n1); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(n1), false); |
| m.set(n1, n1); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(n1), true); |
| assert.sameValue(m.get(n1), n1); |
| }; |
| |
| { |
| const m = new Map([[n53, n53]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(n53), true); |
| assert.sameValue(m.get(n53), n53); |
| m.delete(n53); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(n53), false); |
| m.set(n53, n53); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(n53), true); |
| assert.sameValue(m.get(n53), n53); |
| }; |
| |
| { |
| const m = new Map([[fiftyThree, fiftyThree]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(fiftyThree), true); |
| assert.sameValue(m.get(fiftyThree), fiftyThree); |
| m.delete(fiftyThree); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(fiftyThree), false); |
| m.set(fiftyThree, fiftyThree); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(fiftyThree), true); |
| assert.sameValue(m.get(fiftyThree), fiftyThree); |
| }; |
| |
| { |
| const m = new Map([[bigInt64Array, bigInt64Array]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(bigInt64Array), true); |
| assert.sameValue(m.get(bigInt64Array), bigInt64Array); |
| m.delete(bigInt64Array); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(bigInt64Array), false); |
| m.set(bigInt64Array, bigInt64Array); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(bigInt64Array), true); |
| assert.sameValue(m.get(bigInt64Array), bigInt64Array); |
| }; |
| |
| { |
| const m = new Map([[bigUint64Array, bigUint64Array]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(bigUint64Array), true); |
| assert.sameValue(m.get(bigUint64Array), bigUint64Array); |
| m.delete(bigUint64Array); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(bigUint64Array), false); |
| m.set(bigUint64Array, bigUint64Array); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(bigUint64Array), true); |
| assert.sameValue(m.get(bigUint64Array), bigUint64Array); |
| }; |
| |
| { |
| const m = new Map([[symbol, symbol]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(symbol), true); |
| assert.sameValue(m.get(symbol), symbol); |
| m.delete(symbol); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(symbol), false); |
| m.set(symbol, symbol); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(symbol), true); |
| assert.sameValue(m.get(symbol), symbol); |
| }; |
| |
| { |
| const m = new Map([[object, object]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(object), true); |
| assert.sameValue(m.get(object), object); |
| m.delete(object); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(object), false); |
| m.set(object, object); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(object), true); |
| assert.sameValue(m.get(object), object); |
| }; |
| |
| { |
| const m = new Map([[array, array]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(array), true); |
| assert.sameValue(m.get(array), array); |
| m.delete(array); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(array), false); |
| m.set(array, array); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(array), true); |
| assert.sameValue(m.get(array), array); |
| }; |
| |
| { |
| const m = new Map([[string, string]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(string), true); |
| assert.sameValue(m.get(string), string); |
| m.delete(string); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(string), false); |
| m.set(string, string); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(string), true); |
| assert.sameValue(m.get(string), string); |
| }; |
| |
| { |
| const m = new Map([[booleanTrue, booleanTrue]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(booleanTrue), true); |
| assert.sameValue(m.get(booleanTrue), booleanTrue); |
| m.delete(booleanTrue); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(booleanTrue), false); |
| m.set(booleanTrue, booleanTrue); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(booleanTrue), true); |
| assert.sameValue(m.get(booleanTrue), booleanTrue); |
| }; |
| |
| { |
| const m = new Map([[booleanFalse, booleanFalse]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(booleanFalse), true); |
| assert.sameValue(m.get(booleanFalse), booleanFalse); |
| m.delete(booleanFalse); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(booleanFalse), false); |
| m.set(booleanFalse, booleanFalse); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(booleanFalse), true); |
| assert.sameValue(m.get(booleanFalse), booleanFalse); |
| }; |
| |
| { |
| const m = new Map([[functionExprValue, functionExprValue]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(functionExprValue), true); |
| assert.sameValue(m.get(functionExprValue), functionExprValue); |
| m.delete(functionExprValue); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(functionExprValue), false); |
| m.set(functionExprValue, functionExprValue); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(functionExprValue), true); |
| assert.sameValue(m.get(functionExprValue), functionExprValue); |
| }; |
| |
| { |
| const m = new Map([[arrowFunctionValue, arrowFunctionValue]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(arrowFunctionValue), true); |
| assert.sameValue(m.get(arrowFunctionValue), arrowFunctionValue); |
| m.delete(arrowFunctionValue); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(arrowFunctionValue), false); |
| m.set(arrowFunctionValue, arrowFunctionValue); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(arrowFunctionValue), true); |
| assert.sameValue(m.get(arrowFunctionValue), arrowFunctionValue); |
| }; |
| |
| { |
| const m = new Map([[classValue, classValue]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(classValue), true); |
| assert.sameValue(m.get(classValue), classValue); |
| m.delete(classValue); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(classValue), false); |
| m.set(classValue, classValue); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(classValue), true); |
| assert.sameValue(m.get(classValue), classValue); |
| }; |
| |
| { |
| const m = new Map([[map, map]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(map), true); |
| assert.sameValue(m.get(map), map); |
| m.delete(map); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(map), false); |
| m.set(map, map); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(map), true); |
| assert.sameValue(m.get(map), map); |
| }; |
| |
| { |
| const m = new Map([[set, set]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(set), true); |
| assert.sameValue(m.get(set), set); |
| m.delete(set); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(set), false); |
| m.set(set, set); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(set), true); |
| assert.sameValue(m.get(set), set); |
| }; |
| |
| { |
| const m = new Map([[weakMap, weakMap]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(weakMap), true); |
| assert.sameValue(m.get(weakMap), weakMap); |
| m.delete(weakMap); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(weakMap), false); |
| m.set(weakMap, weakMap); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(weakMap), true); |
| assert.sameValue(m.get(weakMap), weakMap); |
| }; |
| |
| { |
| const m = new Map([[weakRef, weakRef]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(weakRef), true); |
| assert.sameValue(m.get(weakRef), weakRef); |
| m.delete(weakRef); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(weakRef), false); |
| m.set(weakRef, weakRef); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(weakRef), true); |
| assert.sameValue(m.get(weakRef), weakRef); |
| }; |
| |
| { |
| const m = new Map([[weakSet, weakSet]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(weakSet), true); |
| assert.sameValue(m.get(weakSet), weakSet); |
| m.delete(weakSet); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(weakSet), false); |
| m.set(weakSet, weakSet); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(weakSet), true); |
| assert.sameValue(m.get(weakSet), weakSet); |
| }; |
| |
| { |
| const m = new Map([[nullValue, nullValue]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(nullValue), true); |
| assert.sameValue(m.get(nullValue), nullValue); |
| m.delete(nullValue); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(nullValue), false); |
| m.set(nullValue, nullValue); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(nullValue), true); |
| assert.sameValue(m.get(nullValue), nullValue); |
| }; |
| |
| { |
| const m = new Map([[undefinedValue, undefinedValue]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(undefinedValue), true); |
| assert.sameValue(m.get(undefinedValue), undefinedValue); |
| m.delete(undefinedValue); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(undefinedValue), false); |
| m.set(undefinedValue, undefinedValue); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(undefinedValue), true); |
| assert.sameValue(m.get(undefinedValue), undefinedValue); |
| }; |
| |
| { |
| const m = new Map([[unassigned, unassigned]]); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(unassigned), true); |
| assert.sameValue(m.get(unassigned), unassigned); |
| m.delete(unassigned); |
| assert.sameValue(m.size, 0); |
| assert.sameValue(m.has(unassigned), false); |
| m.set(unassigned, unassigned); |
| assert.sameValue(m.size, 1); |
| assert.sameValue(m.has(unassigned), true); |
| assert.sameValue(m.get(unassigned), unassigned); |
| }; |
| |