|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 * Contributor: |
|
5 * Andreas Gal <gal@mozilla.com> |
|
6 */ |
|
7 |
|
8 //----------------------------------------------------------------------------- |
|
9 var BUGNUMBER = 547941; |
|
10 var summary = 'js weak maps'; |
|
11 var actual = ''; |
|
12 var expect = ''; |
|
13 |
|
14 //----------------------------------------------------------------------------- |
|
15 test(); |
|
16 //----------------------------------------------------------------------------- |
|
17 |
|
18 function test() |
|
19 { |
|
20 enterFunc ('test'); |
|
21 printBugNumber(BUGNUMBER); |
|
22 printStatus(summary); |
|
23 |
|
24 var TestPassCount = 0; |
|
25 var TestFailCount = 0; |
|
26 var TestTodoCount = 0; |
|
27 |
|
28 var TODO = 1; |
|
29 |
|
30 function check(fun, todo) { |
|
31 var thrown = null; |
|
32 var success = false; |
|
33 try { |
|
34 success = fun(); |
|
35 } catch (x) { |
|
36 thrown = x; |
|
37 } |
|
38 |
|
39 if (thrown) |
|
40 success = false; |
|
41 |
|
42 if (todo) { |
|
43 TestTodoCount++; |
|
44 |
|
45 if (success) { |
|
46 var ex = new Error; |
|
47 print ("=== TODO but PASSED? ==="); |
|
48 print (ex.stack); |
|
49 print ("========================"); |
|
50 } |
|
51 |
|
52 return; |
|
53 } |
|
54 |
|
55 if (success) { |
|
56 TestPassCount++; |
|
57 } else { |
|
58 TestFailCount++; |
|
59 |
|
60 var ex = new Error; |
|
61 print ("=== FAILED ==="); |
|
62 print (ex.stack); |
|
63 if (thrown) { |
|
64 print (" threw exception:"); |
|
65 print (thrown); |
|
66 } |
|
67 print ("=============="); |
|
68 } |
|
69 } |
|
70 |
|
71 function checkThrows(fun, todo) { |
|
72 let thrown = false; |
|
73 try { |
|
74 fun(); |
|
75 } catch (x) { |
|
76 thrown = true; |
|
77 } |
|
78 |
|
79 check(function() thrown, todo); |
|
80 } |
|
81 |
|
82 var key = {}; |
|
83 var map = WeakMap(); |
|
84 |
|
85 check(function() !map.has(key)); |
|
86 map.set(key, 42); |
|
87 check(function() map.get(key) == 42); |
|
88 check(function() typeof map.get({}) == "undefined"); |
|
89 check(function() map.get({}, "foo") == "foo"); |
|
90 |
|
91 gc(); gc(); gc(); |
|
92 |
|
93 check(function() map.get(key) == 42); |
|
94 map.delete(key); |
|
95 check(function() typeof map.get(key) == "undefined"); |
|
96 check(function() !map.has(key)); |
|
97 |
|
98 var value = { }; |
|
99 map.set(new Object(), value); |
|
100 gc(); gc(); gc(); |
|
101 |
|
102 print ("done"); |
|
103 |
|
104 reportCompare(0, TestFailCount, "weak map tests"); |
|
105 |
|
106 exitFunc ('test'); |
|
107 } |