michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: * Contributor: michael@0: * Andreas Gal michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 547941; michael@0: var summary = 'js weak maps'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: var TestPassCount = 0; michael@0: var TestFailCount = 0; michael@0: var TestTodoCount = 0; michael@0: michael@0: var TODO = 1; michael@0: michael@0: function check(fun, todo) { michael@0: var thrown = null; michael@0: var success = false; michael@0: try { michael@0: success = fun(); michael@0: } catch (x) { michael@0: thrown = x; michael@0: } michael@0: michael@0: if (thrown) michael@0: success = false; michael@0: michael@0: if (todo) { michael@0: TestTodoCount++; michael@0: michael@0: if (success) { michael@0: var ex = new Error; michael@0: print ("=== TODO but PASSED? ==="); michael@0: print (ex.stack); michael@0: print ("========================"); michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: if (success) { michael@0: TestPassCount++; michael@0: } else { michael@0: TestFailCount++; michael@0: michael@0: var ex = new Error; michael@0: print ("=== FAILED ==="); michael@0: print (ex.stack); michael@0: if (thrown) { michael@0: print (" threw exception:"); michael@0: print (thrown); michael@0: } michael@0: print ("=============="); michael@0: } michael@0: } michael@0: michael@0: function checkThrows(fun, todo) { michael@0: let thrown = false; michael@0: try { michael@0: fun(); michael@0: } catch (x) { michael@0: thrown = true; michael@0: } michael@0: michael@0: check(function() thrown, todo); michael@0: } michael@0: michael@0: var key = {}; michael@0: var map = WeakMap(); michael@0: michael@0: check(function() !map.has(key)); michael@0: map.set(key, 42); michael@0: check(function() map.get(key) == 42); michael@0: check(function() typeof map.get({}) == "undefined"); michael@0: check(function() map.get({}, "foo") == "foo"); michael@0: michael@0: gc(); gc(); gc(); michael@0: michael@0: check(function() map.get(key) == 42); michael@0: map.delete(key); michael@0: check(function() typeof map.get(key) == "undefined"); michael@0: check(function() !map.has(key)); michael@0: michael@0: var value = { }; michael@0: map.set(new Object(), value); michael@0: gc(); gc(); gc(); michael@0: michael@0: print ("done"); michael@0: michael@0: reportCompare(0, TestFailCount, "weak map tests"); michael@0: michael@0: exitFunc ('test'); michael@0: }