stringset_test.js 9.0 KB

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