set_test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // Copyright 2006 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. goog.provide('goog.structs.SetTest');
  15. goog.setTestOnly('goog.structs.SetTest');
  16. goog.require('goog.iter');
  17. goog.require('goog.structs');
  18. goog.require('goog.structs.Set');
  19. goog.require('goog.testing.jsunit');
  20. var Set = goog.structs.Set;
  21. function stringifySet(s) {
  22. return goog.structs.getValues(s).join('');
  23. }
  24. function testGetCount() {
  25. var s = new Set;
  26. var a = new String('a');
  27. s.add(a);
  28. var b = new String('b');
  29. s.add(b);
  30. var c = new String('c');
  31. s.add(c);
  32. assertEquals('count, should be 3', s.getCount(), 3);
  33. var d = new String('d');
  34. s.add(d);
  35. assertEquals('count, should be 4', s.getCount(), 4);
  36. s.remove(d);
  37. assertEquals('count, should be 3', s.getCount(), 3);
  38. s = new Set;
  39. s.add('a');
  40. s.add('b');
  41. s.add('c');
  42. assertEquals('count, should be 3', s.getCount(), 3);
  43. s.add('d');
  44. assertEquals('count, should be 4', s.getCount(), 4);
  45. s.remove('d');
  46. assertEquals('count, should be 3', s.getCount(), 3);
  47. }
  48. function testGetValues() {
  49. var s = new Set;
  50. var a = new String('a');
  51. s.add(a);
  52. var b = new String('b');
  53. s.add(b);
  54. var c = new String('c');
  55. s.add(c);
  56. var d = new String('d');
  57. s.add(d);
  58. assertEquals(s.getValues().join(''), 'abcd');
  59. var s = new Set;
  60. s.add('a');
  61. s.add('b');
  62. s.add('c');
  63. s.add('d');
  64. assertEquals(s.getValues().join(''), 'abcd');
  65. }
  66. function testContains() {
  67. var s = new Set;
  68. var a = new String('a');
  69. s.add(a);
  70. var b = new String('b');
  71. s.add(b);
  72. var c = new String('c');
  73. s.add(c);
  74. var d = new String('d');
  75. s.add(d);
  76. var e = new String('e');
  77. assertTrue("contains, Should contain 'a'", s.contains(a));
  78. assertFalse("contains, Should not contain 'e'", s.contains(e));
  79. s = new Set;
  80. s.add('a');
  81. s.add('b');
  82. s.add('c');
  83. s.add('d');
  84. assertTrue("contains, Should contain 'a'", s.contains('a'));
  85. assertFalse("contains, Should not contain 'e'", s.contains('e'));
  86. }
  87. function testContainsFunctionValue() {
  88. var s = new Set;
  89. var fn1 = function() {};
  90. assertFalse(s.contains(fn1));
  91. s.add(fn1);
  92. assertTrue(s.contains(fn1));
  93. var fn2 = function() {};
  94. assertFalse(s.contains(fn2));
  95. s.add(fn2);
  96. assertTrue(s.contains(fn2));
  97. assertEquals(s.getCount(), 2);
  98. }
  99. function testContainsAll() {
  100. var set = new Set([1, 2, 3]);
  101. assertTrue('{1, 2, 3} contains []', set.containsAll([]));
  102. assertTrue('{1, 2, 3} contains [1]', set.containsAll([1]));
  103. assertTrue('{1, 2, 3} contains [1, 1]', set.containsAll([1, 1]));
  104. assertTrue('{1, 2, 3} contains [3, 2, 1]', set.containsAll([3, 2, 1]));
  105. assertFalse("{1, 2, 3} doesn't contain [4]", set.containsAll([4]));
  106. assertFalse("{1, 2, 3} doesn't contain [1, 4]", set.containsAll([1, 4]));
  107. assertTrue('{1, 2, 3} contains {a: 1}', set.containsAll({a: 1}));
  108. assertFalse("{1, 2, 3} doesn't contain {a: 4}", set.containsAll({a: 4}));
  109. assertTrue('{1, 2, 3} contains {1}', set.containsAll(new Set([1])));
  110. assertFalse("{1, 2, 3} doesn't contain {4}", set.containsAll(new Set([4])));
  111. }
  112. function testIntersection() {
  113. var emptySet = new Set;
  114. assertTrue(
  115. 'intersection of empty set and [] should be empty',
  116. emptySet.intersection([]).isEmpty());
  117. assertIntersection(
  118. 'intersection of 2 empty sets should be empty', emptySet, new Set(),
  119. new Set());
  120. var abcdSet = new Set();
  121. abcdSet.add('a');
  122. abcdSet.add('b');
  123. abcdSet.add('c');
  124. abcdSet.add('d');
  125. assertTrue(
  126. 'intersection of populated set and [] should be empty',
  127. abcdSet.intersection([]).isEmpty());
  128. assertIntersection(
  129. 'intersection of populated set and empty set should be empty', abcdSet,
  130. new Set(), new Set());
  131. var bcSet = new Set(['b', 'c']);
  132. assertIntersection(
  133. 'intersection of [a,b,c,d] and [b,c]', abcdSet, bcSet, bcSet);
  134. var bceSet = new Set(['b', 'c', 'e']);
  135. assertIntersection(
  136. 'intersection of [a,b,c,d] and [b,c,e]', abcdSet, bceSet, bcSet);
  137. }
  138. function testDifference() {
  139. var emptySet = new Set;
  140. assertTrue(
  141. 'difference of empty set and [] should be empty',
  142. emptySet.difference([]).isEmpty());
  143. assertTrue(
  144. 'difference of 2 empty sets should be empty',
  145. emptySet.difference(new Set()).isEmpty());
  146. var abcdSet = new Set(['a', 'b', 'c', 'd']);
  147. assertTrue(
  148. 'difference of populated set and [] should be the populated set',
  149. abcdSet.difference([]).equals(abcdSet));
  150. assertTrue(
  151. 'difference of populated set and empty set should be the populated set',
  152. abcdSet.difference(new Set()).equals(abcdSet));
  153. assertTrue(
  154. 'difference of two identical sets should be the empty set',
  155. abcdSet.difference(abcdSet).equals(new Set()));
  156. var bcSet = new Set(['b', 'c']);
  157. assertTrue(
  158. 'difference of [a,b,c,d] and [b,c] shoule be [a,d]',
  159. abcdSet.difference(bcSet).equals(new Set(['a', 'd'])));
  160. assertTrue(
  161. 'difference of [b,c] and [a,b,c,d] should be the empty set',
  162. bcSet.difference(abcdSet).equals(new Set()));
  163. var xyzSet = new Set(['x', 'y', 'z']);
  164. assertTrue(
  165. 'difference of [a,b,c,d] and [x,y,z] should be the [a,b,c,d]',
  166. abcdSet.difference(xyzSet).equals(abcdSet));
  167. }
  168. /**
  169. * Helper function to assert intersection is commutative.
  170. */
  171. function assertIntersection(msg, set1, set2, expectedIntersection) {
  172. assertTrue(
  173. msg + ': set1->set2',
  174. set1.intersection(set2).equals(expectedIntersection));
  175. assertTrue(
  176. msg + ': set2->set1',
  177. set2.intersection(set1).equals(expectedIntersection));
  178. }
  179. function testRemoveAll() {
  180. assertRemoveAll('removeAll of empty set from empty set', [], [], []);
  181. assertRemoveAll(
  182. 'removeAll of empty set from populated set', ['a', 'b', 'c', 'd'], [],
  183. ['a', 'b', 'c', 'd']);
  184. assertRemoveAll(
  185. 'removeAll of [a,d] from [a,b,c,d]', ['a', 'b', 'c', 'd'], ['a', 'd'],
  186. ['b', 'c']);
  187. assertRemoveAll(
  188. 'removeAll of [b,c] from [a,b,c,d]', ['a', 'b', 'c', 'd'], ['b', 'c'],
  189. ['a', 'd']);
  190. assertRemoveAll(
  191. 'removeAll of [b,c,e] from [a,b,c,d]', ['a', 'b', 'c', 'd'],
  192. ['b', 'c', 'e'], ['a', 'd']);
  193. assertRemoveAll(
  194. 'removeAll of [a,b,c,d] from [a,d]', ['a', 'd'], ['a', 'b', 'c', 'd'],
  195. []);
  196. assertRemoveAll(
  197. 'removeAll of [a,b,c,d] from [b,c]', ['b', 'c'], ['a', 'b', 'c', 'd'],
  198. []);
  199. assertRemoveAll(
  200. 'removeAll of [a,b,c,d] from [b,c,e]', ['b', 'c', 'e'],
  201. ['a', 'b', 'c', 'd'], ['e']);
  202. }
  203. /**
  204. * Helper function to test removeAll.
  205. */
  206. function assertRemoveAll(msg, elements1, elements2, expectedResult) {
  207. var set1 = new Set(elements1);
  208. var set2 = new Set(elements2);
  209. set1.removeAll(set2);
  210. assertTrue(
  211. msg + ': set1 count increased after removeAll',
  212. elements1.length >= set1.getCount());
  213. assertEquals(
  214. msg + ': set2 count changed after removeAll', elements2.length,
  215. set2.getCount());
  216. assertTrue(msg + ': wrong set1 after removeAll', set1.equals(expectedResult));
  217. assertIntersection(
  218. msg + ': non-empty intersection after removeAll', set1, set2, []);
  219. }
  220. function testAdd() {
  221. var s = new Set;
  222. var a = new String('a');
  223. var b = new String('b');
  224. s.add(a);
  225. assertTrue(s.contains(a));
  226. s.add(b);
  227. assertTrue(s.contains(b));
  228. s = new Set;
  229. s.add('a');
  230. assertTrue(s.contains('a'));
  231. s.add('b');
  232. assertTrue(s.contains('b'));
  233. s.add(null);
  234. assertTrue('contains null', s.contains(null));
  235. assertFalse('does not contain "null"', s.contains('null'));
  236. }
  237. function testClear() {
  238. var s = new Set;
  239. var a = new String('a');
  240. s.add(a);
  241. var b = new String('b');
  242. s.add(b);
  243. var c = new String('c');
  244. s.add(c);
  245. var d = new String('d');
  246. s.add(d);
  247. s.clear();
  248. assertTrue('cleared so it should be empty', s.isEmpty());
  249. assertTrue("cleared so it should not contain 'a' key", !s.contains(a));
  250. s = new Set;
  251. s.add('a');
  252. s.add('b');
  253. s.add('c');
  254. s.add('d');
  255. s.clear();
  256. assertTrue('cleared so it should be empty', s.isEmpty());
  257. assertTrue("cleared so it should not contain 'a' key", !s.contains('a'));
  258. }
  259. function testAddAll() {
  260. var s = new Set;
  261. var a = new String('a');
  262. var b = new String('b');
  263. var c = new String('c');
  264. var d = new String('d');
  265. s.addAll([a, b, c, d]);
  266. assertTrue('addAll so it should not be empty', !s.isEmpty());
  267. assertTrue("addAll so it should contain 'c' key", s.contains(c));
  268. var s2 = new Set;
  269. s2.addAll(s);
  270. assertTrue('addAll so it should not be empty', !s2.isEmpty());
  271. assertTrue("addAll so it should contain 'c' key", s2.contains(c));
  272. s = new Set;
  273. s.addAll(['a', 'b', 'c', 'd']);
  274. assertTrue('addAll so it should not be empty', !s.isEmpty());
  275. assertTrue("addAll so it should contain 'c' key", s.contains('c'));
  276. s2 = new Set;
  277. s2.addAll(s);
  278. assertTrue('addAll so it should not be empty', !s2.isEmpty());
  279. assertTrue("addAll so it should contain 'c' key", s2.contains('c'));
  280. }
  281. function testConstructor() {
  282. var s = new Set;
  283. var a = new String('a');
  284. s.add(a);
  285. var b = new String('b');
  286. s.add(b);
  287. var c = new String('c');
  288. s.add(c);
  289. var d = new String('d');
  290. s.add(d);
  291. var s2 = new Set(s);
  292. assertFalse('constr with Set so it should not be empty', s2.isEmpty());
  293. assertTrue('constr with Set so it should contain c', s2.contains(c));
  294. s = new Set;
  295. s.add('a');
  296. s.add('b');
  297. s.add('c');
  298. s.add('d');
  299. s2 = new Set(s);
  300. assertFalse('constr with Set so it should not be empty', s2.isEmpty());
  301. assertTrue('constr with Set so it should contain c', s2.contains('c'));
  302. }
  303. function testClone() {
  304. var s = new Set;
  305. var a = new String('a');
  306. s.add(a);
  307. var b = new String('b');
  308. s.add(b);
  309. var c = new String('c');
  310. s.add(c);
  311. var d = new String('d');
  312. s.add(d);
  313. var s2 = s.clone();
  314. assertFalse('clone so it should not be empty', s2.isEmpty());
  315. assertTrue("clone so it should contain 'c' key", s2.contains(c));
  316. var s = new Set;
  317. s.add('a');
  318. s.add('b');
  319. s.add('c');
  320. s.add('d');
  321. s2 = s.clone();
  322. assertFalse('clone so it should not be empty', s2.isEmpty());
  323. assertTrue("clone so it should contain 'c' key", s2.contains('c'));
  324. }
  325. /**
  326. * Helper method for testEquals().
  327. * @param {Object} a First element to use in the tests.
  328. * @param {Object} b Second element to use in the tests.
  329. * @param {Object} c Third element to use in the tests.
  330. * @param {Object} d Fourth element to use in the tests.
  331. */
  332. function helperForTestEquals(a, b, c, d) {
  333. var s = new Set([a, b, c]);
  334. assertTrue('set == itself', s.equals(s));
  335. assertTrue('set == same set', s.equals(new Set([a, b, c])));
  336. assertTrue('set == its clone', s.equals(s.clone()));
  337. assertTrue('set == array of same elements', s.equals([a, b, c]));
  338. assertTrue(
  339. 'set == array of same elements in different order', s.equals([c, b, a]));
  340. assertFalse('set != empty set', s.equals(new Set));
  341. assertFalse('set != its subset', s.equals(new Set([a, c])));
  342. assertFalse('set != its superset', s.equals(new Set([a, b, c, d])));
  343. assertFalse('set != different set', s.equals(new Set([b, c, d])));
  344. assertFalse('set != its subset as array', s.equals([a, c]));
  345. assertFalse('set != its superset as array', s.equals([a, b, c, d]));
  346. assertFalse('set != different set as array', s.equals([b, c, d]));
  347. assertFalse('set != [a, b, c, c]', s.equals([a, b, c, c]));
  348. assertFalse('set != [a, b, b]', s.equals([a, b, b]));
  349. assertFalse('set != [a, a]', s.equals([a, a]));
  350. }
  351. function testEquals() {
  352. helperForTestEquals(1, 2, 3, 4);
  353. helperForTestEquals('a', 'b', 'c', 'd');
  354. helperForTestEquals(
  355. new String('a'), new String('b'), new String('c'), new String('d'));
  356. }
  357. /**
  358. * Helper method for testIsSubsetOf().
  359. * @param {Object} a First element to use in the tests.
  360. * @param {Object} b Second element to use in the tests.
  361. * @param {Object} c Third element to use in the tests.
  362. * @param {Object} d Fourth element to use in the tests.
  363. */
  364. function helperForTestIsSubsetOf(a, b, c, d) {
  365. var s = new Set([a, b, c]);
  366. assertTrue('set <= itself', s.isSubsetOf(s));
  367. assertTrue('set <= same set', s.isSubsetOf(new Set([a, b, c])));
  368. assertTrue('set <= its clone', s.isSubsetOf(s.clone()));
  369. assertTrue('set <= array of same elements', s.isSubsetOf([a, b, c]));
  370. assertTrue(
  371. 'set <= array of same elements in different order', s.equals([c, b, a]));
  372. assertTrue('set <= Set([a, b, c, d])', s.isSubsetOf(new Set([a, b, c, d])));
  373. assertTrue('set <= [a, b, c, d]', s.isSubsetOf([a, b, c, d]));
  374. assertTrue('set <= [a, b, c, c]', s.isSubsetOf([a, b, c, c]));
  375. assertFalse('set !<= Set([a, b])', s.isSubsetOf(new Set([a, b])));
  376. assertFalse('set !<= [a, b]', s.isSubsetOf([a, b]));
  377. assertFalse('set !<= Set([c, d])', s.isSubsetOf(new Set([c, d])));
  378. assertFalse('set !<= [c, d]', s.isSubsetOf([c, d]));
  379. assertFalse('set !<= Set([a, c, d])', s.isSubsetOf(new Set([a, c, d])));
  380. assertFalse('set !<= [a, c, d]', s.isSubsetOf([a, c, d]));
  381. assertFalse('set !<= [a, a, b]', s.isSubsetOf([a, a, b]));
  382. assertFalse('set !<= [a, a, b, b]', s.isSubsetOf([a, a, b, b]));
  383. }
  384. function testIsSubsetOf() {
  385. helperForTestIsSubsetOf(1, 2, 3, 4);
  386. helperForTestIsSubsetOf('a', 'b', 'c', 'd');
  387. helperForTestIsSubsetOf(
  388. new String('a'), new String('b'), new String('c'), new String('d'));
  389. }
  390. function testForEach() {
  391. var s = new Set;
  392. var a = new String('a');
  393. s.add(a);
  394. var b = new String('b');
  395. s.add(b);
  396. var c = new String('c');
  397. s.add(c);
  398. var d = new String('d');
  399. s.add(d);
  400. var str = '';
  401. goog.structs.forEach(s, function(val, key, set) {
  402. assertUndefined(key);
  403. assertEquals(s, set);
  404. str += val;
  405. });
  406. assertEquals(str, 'abcd');
  407. s = new Set;
  408. s.add('a');
  409. s.add('b');
  410. s.add('c');
  411. s.add('d');
  412. str = '';
  413. goog.structs.forEach(s, function(val, key, set) {
  414. assertUndefined(key);
  415. assertEquals(s, set);
  416. str += val;
  417. });
  418. assertEquals(str, 'abcd');
  419. }
  420. function testFilter() {
  421. var s = new Set;
  422. var a = new Number(0);
  423. s.add(a);
  424. var b = new Number(1);
  425. s.add(b);
  426. var c = new Number(2);
  427. s.add(c);
  428. var d = new Number(3);
  429. s.add(d);
  430. var s2 = goog.structs.filter(s, function(val, key, set) {
  431. assertUndefined(key);
  432. assertEquals(s, set);
  433. return val > 1;
  434. });
  435. assertEquals(stringifySet(s2), '23');
  436. s = new Set;
  437. s.add(0);
  438. s.add(1);
  439. s.add(2);
  440. s.add(3);
  441. s2 = goog.structs.filter(s, function(val, key, set) {
  442. assertUndefined(key);
  443. assertEquals(s, set);
  444. return val > 1;
  445. });
  446. assertEquals(stringifySet(s2), '23');
  447. }
  448. function testSome() {
  449. var s = new Set;
  450. var a = new Number(0);
  451. s.add(a);
  452. var b = new Number(1);
  453. s.add(b);
  454. var c = new Number(2);
  455. s.add(c);
  456. var d = new Number(3);
  457. s.add(d);
  458. var b = goog.structs.some(s, function(val, key, s2) {
  459. assertUndefined(key);
  460. assertEquals(s, s2);
  461. return val > 1;
  462. });
  463. assertTrue(b);
  464. var b = goog.structs.some(s, function(val, key, s2) {
  465. assertUndefined(key);
  466. assertEquals(s, s2);
  467. return val > 100;
  468. });
  469. assertFalse(b);
  470. s = new Set;
  471. s.add(0);
  472. s.add(1);
  473. s.add(2);
  474. s.add(3);
  475. b = goog.structs.some(s, function(val, key, s2) {
  476. assertUndefined(key);
  477. assertEquals(s, s2);
  478. return val > 1;
  479. });
  480. assertTrue(b);
  481. b = goog.structs.some(s, function(val, key, s2) {
  482. assertUndefined(key);
  483. assertEquals(s, s2);
  484. return val > 100;
  485. });
  486. assertFalse(b);
  487. }
  488. function testEvery() {
  489. var s = new Set;
  490. var a = new Number(0);
  491. s.add(a);
  492. var b = new Number(1);
  493. s.add(b);
  494. var c = new Number(2);
  495. s.add(c);
  496. var d = new Number(3);
  497. s.add(d);
  498. var b = goog.structs.every(s, function(val, key, s2) {
  499. assertUndefined(key);
  500. assertEquals(s, s2);
  501. return val >= 0;
  502. });
  503. assertTrue(b);
  504. b = goog.structs.every(s, function(val, key, s2) {
  505. assertUndefined(key);
  506. assertEquals(s, s2);
  507. return val > 1;
  508. });
  509. assertFalse(b);
  510. s = new Set;
  511. s.add(0);
  512. s.add(1);
  513. s.add(2);
  514. s.add(3);
  515. b = goog.structs.every(s, function(val, key, s2) {
  516. assertUndefined(key);
  517. assertEquals(s, s2);
  518. return val >= 0;
  519. });
  520. assertTrue(b);
  521. b = goog.structs.every(s, function(val, key, s2) {
  522. assertUndefined(key);
  523. assertEquals(s, s2);
  524. return val > 1;
  525. });
  526. assertFalse(b);
  527. }
  528. function testIterator() {
  529. var s = new Set;
  530. s.add(0);
  531. s.add(1);
  532. s.add(2);
  533. s.add(3);
  534. s.add(4);
  535. assertEquals('01234', goog.iter.join(s, ''));
  536. s.remove(1);
  537. s.remove(3);
  538. assertEquals('024', goog.iter.join(s, ''));
  539. }