Debian Codegraph @

closure-library/closure/goog/structs/stringset_test.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
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
15 goog.provide('goog.structs.StringSetTest');
16 goog.setTestOnly('goog.structs.StringSetTest');
17
18 goog.require('goog.array');
19 goog.require('goog.iter');
20 goog.require('goog.structs.StringSet');
21 goog.require('goog.testing.asserts');
22 goog.require('goog.testing.jsunit');
23
24 var TEST_VALUES = [
25 '', ' ', ' ', 'true', 'null', 'undefined', '0', 'new', 'constructor',
26 'prototype', '__proto__', 'set', 'hasOwnProperty', 'toString', 'valueOf'
27 ];
28
29 var TEST_VALUES_WITH_DUPLICATES = [
30 '', '',
31 ' ', ' ',
32 'true', true,
33 'null', null,
34 'undefined', undefined,
35 '0', 0,
36 'new', 'constructor',
37 'prototype', '__proto__',
38 'set', 'hasOwnProperty',
39 'toString', 'valueOf'
40 ];
41
42 function testConstructor() {
43 var empty = new goog.structs.StringSet;
44 assertSameElements('elements in empty set', [], empty.getValues());
45
46 var s = new goog.structs.StringSet(TEST_VALUES_WITH_DUPLICATES);
47 assertSameElements(
48 'duplicates are filtered out by their string value', TEST_VALUES,
49 s.getValues());
50 }
51
52 function testConstructorAssertsThatObjectPrototypeHasNoEnumerableKeys() {
53 assertNotThrows(goog.structs.StringSet);
54 Object.prototype.foo = 0;
55 try {
56 assertThrows(goog.structs.StringSet);
57 } finally {
58 delete Object.prototype.foo;
59 }
60 assertNotThrows(goog.structs.StringSet);
61 }
62
63 function testOverridingObjectPrototypeToStringIsSafe() {
64 var originalToString = Object.prototype.toString;
65 Object.prototype.toString = function() { return 'object'; };
66 try {
67 assertEquals(0, new goog.structs.StringSet().getCount());
68 assertFalse(new goog.structs.StringSet().contains('toString'));
69 } finally {
70 Object.prototype.toString = originalToString;
71 }
72 }
73
74 function testAdd() {
75 var s = new goog.structs.StringSet;
76 goog.array.forEach(TEST_VALUES_WITH_DUPLICATES, s.add, s);
77 assertSameElements(TEST_VALUES, s.getValues());
78 }
79
80 function testAddArray() {
81 var s = new goog.structs.StringSet;
82 s.addArray(TEST_VALUES_WITH_DUPLICATES);
83 assertSameElements('added elements from array', TEST_VALUES, s.getValues());
84 }
85
86 function testAddSet() {
87 var s = new goog.structs.StringSet;
88 s.addSet(new goog.structs.StringSet([1, 2]));
89 assertSameElements('empty set + {1, 2}', ['1', '2'], s.getValues());
90 s.addSet(new goog.structs.StringSet([2, 3]));
91 assertSameElements('{1, 2} + {2, 3}', ['1', '2', '3'], s.getValues());
92 }
93
94 function testClear() {
95 var s = new goog.structs.StringSet([1, 2]);
96 s.clear();
97 assertSameElements('cleared set', [], s.getValues());
98 }
99
100 function testClone() {
101 var s = new goog.structs.StringSet([1, 2]);
102 var c = s.clone();
103 assertSameElements('elements in clone', ['1', '2'], c.getValues());
104 s.add(3);
105 assertSameElements(
106 'changing the original set does not affect the clone', ['1', '2'],
107 c.getValues());
108 }
109
110 function testContains() {
111 var e = new goog.structs.StringSet;
112 goog.array.forEach(TEST_VALUES, function(element) {
113 assertFalse('empty set does not contain ' + element, e.contains(element));
114 });
115
116 var s = new goog.structs.StringSet(TEST_VALUES);
117 goog.array.forEach(TEST_VALUES_WITH_DUPLICATES, function(element) {
118 assertTrue('s contains ' + element, s.contains(element));
119 });
120 assertFalse('s does not contain 42', s.contains(42));
121 }
122
123 function testContainsArray() {
124 var s = new goog.structs.StringSet(TEST_VALUES);
125 assertTrue('set contains empty array', s.containsArray([]));
126 assertTrue(
127 'set contains all elements of itself with some duplication',
128 s.containsArray(TEST_VALUES_WITH_DUPLICATES));
129 assertFalse('set does not contain 42', s.containsArray([42]));
130 }
131
132 function testEquals() {
133 var s = new goog.structs.StringSet([1, 2]);
134 assertTrue('set equals to itself', s.equals(s));
135 assertTrue('set equals to its clone', s.equals(s.clone()));
136 assertFalse(
137 'set does not equal to its subset',
138 s.equals(new goog.structs.StringSet([1])));
139 assertFalse(
140 'set does not equal to its superset',
141 s.equals(new goog.structs.StringSet([1, 2, 3])));
142 }
143
144 function testForEach() {
145 var s = new goog.structs.StringSet(TEST_VALUES);
146 var values = [];
147 var context = {};
148 s.forEach(function(value, key, stringSet) {
149 assertEquals('context of forEach callback', context, this);
150 values.push(value);
151 assertUndefined('key argument of forEach callback', key);
152 assertEquals('set argument of forEach callback', s, stringSet);
153 }, context);
154 assertSameElements('values passed to forEach callback', TEST_VALUES, values);
155 }
156
157 function testGetCount() {
158 var empty = new goog.structs.StringSet;
159 assertEquals('count(empty set)', 0, empty.getCount());
160
161 var s = new goog.structs.StringSet(TEST_VALUES);
162 assertEquals('count(non-empty set)', TEST_VALUES.length, s.getCount());
163 }
164
165 function testGetDifference() {
166 var s1 = new goog.structs.StringSet([1, 2]);
167 var s2 = new goog.structs.StringSet([2, 3]);
168 assertSameElements(
169 '{1, 2} - {2, 3}', ['1'], s1.getDifference(s2).getValues());
170 }
171
172 function testGetIntersection() {
173 var s1 = new goog.structs.StringSet([1, 2]);
174 var s2 = new goog.structs.StringSet([2, 3]);
175 assertSameElements(
176 '{1, 2} * {2, 3}', ['2'], s1.getIntersection(s2).getValues());
177 }
178
179 function testGetSymmetricDifference() {
180 var s1 = new goog.structs.StringSet([1, 2]);
181 var s2 = new goog.structs.StringSet([2, 3]);
182 assertSameElements(
183 '{1, 2} sym.diff. {2, 3}', ['1', '3'],
184 s1.getSymmetricDifference(s2).getValues());
185 }
186
187 function testGetUnion() {
188 var s1 = new goog.structs.StringSet([1, 2]);
189 var s2 = new goog.structs.StringSet([2, 3]);
190 assertSameElements(
191 '{1, 2} + {2, 3}', ['1', '2', '3'], s1.getUnion(s2).getValues());
192 }
193
194 function testIsDisjoint() {
195 var s = new goog.structs.StringSet;
196 var s12 = new goog.structs.StringSet([1, 2]);
197 var s23 = new goog.structs.StringSet([2, 3]);
198 var s34 = new goog.structs.StringSet([3, 4]);
199
200 assertTrue('{} and {1, 2} are disjoint', s.isDisjoint(s12));
201 assertTrue('{1, 2} and {3, 4} are disjoint', s12.isDisjoint(s34));
202 assertFalse('{1, 2} and {2, 3} are not disjoint', s12.isDisjoint(s23));
203 }
204
205 function testIsEmpty() {
206 assertTrue('empty set', new goog.structs.StringSet().isEmpty());
207 assertFalse('non-empty set', new goog.structs.StringSet(['']).isEmpty());
208 }
209
210 function testIsSubsetOf() {
211 var s1 = new goog.structs.StringSet([1]);
212 var s12 = new goog.structs.StringSet([1, 2]);
213 var s123 = new goog.structs.StringSet([1, 2, 3]);
214 var s23 = new goog.structs.StringSet([2, 3]);
215
216 assertTrue('{1, 2} is subset of {1, 2}', s12.isSubsetOf(s12));
217 assertTrue('{1, 2} is subset of {1, 2, 3}', s12.isSubsetOf(s123));
218 assertFalse('{1, 2} is not subset of {1}', s12.isSubsetOf(s1));
219 assertFalse('{1, 2} is not subset of {2, 3}', s12.isSubsetOf(s23));
220 }
221
222 function testIsSupersetOf() {
223 var s1 = new goog.structs.StringSet([1]);
224 var s12 = new goog.structs.StringSet([1, 2]);
225 var s123 = new goog.structs.StringSet([1, 2, 3]);
226 var s23 = new goog.structs.StringSet([2, 3]);
227
228 assertTrue('{1, 2} is superset of {1}', s12.isSupersetOf(s1));
229 assertTrue('{1, 2} is superset of {1, 2}', s12.isSupersetOf(s12));
230 assertFalse('{1, 2} is not superset of {1, 2, 3}', s12.isSupersetOf(s123));
231 assertFalse('{1, 2} is not superset of {2, 3}', s12.isSupersetOf(s23));
232 }
233
234 function testRemove() {
235 var n = new goog.structs.StringSet([1, 2]);
236 assertFalse('3 not removed from {1, 2}', n.remove(3));
237 assertSameElements('set == {1, 2}', ['1', '2'], n.getValues());
238 assertTrue('2 removed from {1, 2}', n.remove(2));
239 assertSameElements('set == {1}', ['1'], n.getValues());
240 assertTrue('"1" removed from {1}', n.remove('1'));
241 assertSameElements('set == {}', [], n.getValues());
242
243 var s = new goog.structs.StringSet(TEST_VALUES);
244 goog.array.forEach(TEST_VALUES, s.remove, s);
245 assertSameElements('all special values have been removed', [], s.getValues());
246 }
247
248 function testRemoveArray() {
249 var s = new goog.structs.StringSet(TEST_VALUES);
250 s.removeArray(TEST_VALUES.slice(0, TEST_VALUES.length - 2));
251 assertSameElements(
252 'removed elements from array', TEST_VALUES.slice(TEST_VALUES.length - 2),
253 s.getValues());
254 }
255
256 function testRemoveSet() {
257 var s1 = new goog.structs.StringSet([1, 2]);
258 var s2 = new goog.structs.StringSet([2, 3]);
259 s1.removeSet(s2);
260 assertSameElements('{1, 2} - {2, 3}', ['1'], s1.getValues());
261 }
262
263 function testIterator() {
264 var s = new goog.structs.StringSet(TEST_VALUES_WITH_DUPLICATES);
265 var values = [];
266 goog.iter.forEach(s, function(value) { values.push(value); });
267 assertSameElements(
268 '__iterator__ takes all elements once', TEST_VALUES, values);
269 }