structs_test.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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.structsTest');
  15. goog.setTestOnly('goog.structsTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.structs');
  20. goog.require('goog.structs.Map');
  21. goog.require('goog.structs.Set'); // needed for filter
  22. goog.require('goog.testing.jsunit');
  23. /*
  24. This one does not test Map or Set
  25. It tests Array, Object, String and a NodeList
  26. */
  27. function stringifyObject(obj) {
  28. var sb = [];
  29. for (var key in obj) {
  30. sb.push(key + obj[key]);
  31. }
  32. return sb.join('');
  33. }
  34. function getTestElement() {
  35. return document.getElementById('test');
  36. }
  37. function getAll() {
  38. return getTestElement().getElementsByTagName('*');
  39. }
  40. var node;
  41. function addNode() {
  42. node = goog.dom.createElement(goog.dom.TagName.SPAN);
  43. getTestElement().appendChild(node);
  44. }
  45. function removeNode() {
  46. getTestElement().removeChild(node);
  47. }
  48. function nodeNames(nl) {
  49. var sb = [];
  50. for (var i = 0, n; n = nl[i]; i++) {
  51. sb.push(n.nodeName);
  52. }
  53. return sb.join(',');
  54. }
  55. var allTagNames1 = 'HR,P,P,P,P,P,P,P,P,H1';
  56. var allTagNames2 = allTagNames1 + ',SPAN';
  57. function testGetCount() {
  58. var arr = ['a', 'b', 'c'];
  59. assertEquals('count, should be 3', 3, goog.structs.getCount(arr));
  60. arr.push('d');
  61. assertEquals('count, should be 4', 4, goog.structs.getCount(arr));
  62. goog.array.remove(arr, 'd');
  63. assertEquals('count, should be 3', 3, goog.structs.getCount(arr));
  64. var obj = {a: 0, b: 1, c: 2};
  65. assertEquals('count, should be 3', 3, goog.structs.getCount(obj));
  66. obj.d = 3;
  67. assertEquals('count, should be 4', 4, goog.structs.getCount(obj));
  68. delete obj.d;
  69. assertEquals('count, should be 3', 3, goog.structs.getCount(obj));
  70. var s = 'abc';
  71. assertEquals('count, should be 3', 3, goog.structs.getCount(s));
  72. s += 'd';
  73. assertEquals('count, should be 4', 4, goog.structs.getCount(s));
  74. var all = getAll();
  75. assertEquals('count, should be 10', 10, goog.structs.getCount(all));
  76. addNode();
  77. assertEquals('count, should be 11', 11, goog.structs.getCount(all));
  78. removeNode();
  79. assertEquals('count, should be 10', 10, goog.structs.getCount(all));
  80. var aMap = new goog.structs.Map({a: 0, b: 1, c: 2});
  81. assertEquals('count, should be 3', 3, goog.structs.getCount(aMap));
  82. aMap.set('d', 3);
  83. assertEquals('count, should be 4', 4, goog.structs.getCount(aMap));
  84. aMap.remove('a');
  85. assertEquals('count, should be 3', 3, goog.structs.getCount(aMap));
  86. var aSet = new goog.structs.Set('abc');
  87. assertEquals('count, should be 3', 3, goog.structs.getCount(aSet));
  88. aSet.add('d');
  89. assertEquals('count, should be 4', 4, goog.structs.getCount(aSet));
  90. aSet.remove('a');
  91. assertEquals('count, should be 3', 3, goog.structs.getCount(aSet));
  92. }
  93. function testGetValues() {
  94. var arr = ['a', 'b', 'c', 'd'];
  95. assertEquals('abcd', goog.structs.getValues(arr).join(''));
  96. var obj = {a: 0, b: 1, c: 2, d: 3};
  97. assertEquals('0123', goog.structs.getValues(obj).join(''));
  98. var s = 'abc';
  99. assertEquals('abc', goog.structs.getValues(s).join(''));
  100. s += 'd';
  101. assertEquals('abcd', goog.structs.getValues(s).join(''));
  102. var all = getAll();
  103. assertEquals(allTagNames1, nodeNames(goog.structs.getValues(all)));
  104. addNode();
  105. assertEquals(allTagNames2, nodeNames(goog.structs.getValues(all)));
  106. removeNode();
  107. assertEquals(allTagNames1, nodeNames(goog.structs.getValues(all)));
  108. var aMap = new goog.structs.Map({a: 1, b: 2, c: 3});
  109. assertEquals('123', goog.structs.getValues(aMap).join(''));
  110. var aSet = new goog.structs.Set([1, 2, 3]);
  111. assertEquals('123', goog.structs.getValues(aMap).join(''));
  112. }
  113. function testGetKeys() {
  114. var arr = ['a', 'b', 'c', 'd'];
  115. assertEquals('0123', goog.structs.getKeys(arr).join(''));
  116. var obj = {a: 0, b: 1, c: 2, d: 3};
  117. assertEquals('abcd', goog.structs.getKeys(obj).join(''));
  118. var s = 'abc';
  119. assertEquals('012', goog.structs.getKeys(s).join(''));
  120. s += 'd';
  121. assertEquals('0123', goog.structs.getKeys(s).join(''));
  122. var all = getAll();
  123. assertEquals('0123456789', goog.structs.getKeys(all).join(''));
  124. addNode();
  125. assertEquals('012345678910', goog.structs.getKeys(all).join(''));
  126. removeNode();
  127. assertEquals('0123456789', goog.structs.getKeys(all).join(''));
  128. var aMap = new goog.structs.Map({a: 1, b: 2, c: 3});
  129. assertEquals('abc', goog.structs.getKeys(aMap).join(''));
  130. var aSet = new goog.structs.Set([1, 2, 3]);
  131. assertUndefined(goog.structs.getKeys(aSet));
  132. }
  133. function testContains() {
  134. var arr = ['a', 'b', 'c', 'd'];
  135. assertTrue("contains, Should contain 'a'", goog.structs.contains(arr, 'a'));
  136. assertFalse(
  137. "contains, Should not contain 'e'", goog.structs.contains(arr, 'e'));
  138. var obj = {a: 0, b: 1, c: 2, d: 3};
  139. assertTrue("contains, Should contain '0'", goog.structs.contains(obj, 0));
  140. assertFalse(
  141. "contains, Should not contain '4'", goog.structs.contains(obj, 4));
  142. var s = 'abc';
  143. assertTrue("contains, Should contain 'a'", goog.structs.contains(s, 'a'));
  144. assertFalse(
  145. "contains, Should not contain 'd'", goog.structs.contains(s, 'd'));
  146. var all = getAll();
  147. assertTrue(
  148. "contains, Should contain 'h1'",
  149. goog.structs.contains(all, document.getElementById('h1')));
  150. assertFalse(
  151. "contains, Should not contain 'document.body'",
  152. goog.structs.contains(all, document.body));
  153. var aMap = new goog.structs.Map({a: 1, b: 2, c: 3});
  154. assertTrue("contains, Should contain '1'", goog.structs.contains(aMap, 1));
  155. assertFalse(
  156. "contains, Should not contain '4'", goog.structs.contains(aMap, 4));
  157. var aSet = new goog.structs.Set([1, 2, 3]);
  158. assertTrue("contains, Should contain '1'", goog.structs.contains(aSet, 1));
  159. assertFalse(
  160. "contains, Should not contain '4'", goog.structs.contains(aSet, 4));
  161. }
  162. function testClear() {
  163. var arr = ['a', 'b', 'c', 'd'];
  164. goog.structs.clear(arr);
  165. assertTrue('cleared so it should be empty', goog.structs.isEmpty(arr));
  166. assertFalse(
  167. "cleared so it should not contain 'a'", goog.structs.contains(arr, 'a'));
  168. var obj = {a: 0, b: 1, c: 2, d: 3};
  169. goog.structs.clear(obj);
  170. assertTrue('cleared so it should be empty', goog.structs.isEmpty(obj));
  171. assertFalse(
  172. "cleared so it should not contain 'a' key",
  173. goog.structs.contains(obj, 0));
  174. var aMap = new goog.structs.Map({a: 1, b: 2, c: 3});
  175. goog.structs.clear(aMap);
  176. assertTrue('cleared map so it should be empty', goog.structs.isEmpty(aMap));
  177. assertFalse(
  178. "cleared map so it should not contain '1' value",
  179. goog.structs.contains(aMap, 1));
  180. var aSet = new goog.structs.Set([1, 2, 3]);
  181. goog.structs.clear(aSet);
  182. assertTrue('cleared set so it should be empty', goog.structs.isEmpty(aSet));
  183. assertFalse(
  184. "cleared set so it should not contain '1'",
  185. goog.structs.contains(aSet, 1));
  186. // cannot clear a string or a NodeList
  187. }
  188. // Map
  189. function testMap() {
  190. var RV = {};
  191. var obj = {
  192. map: function(g) {
  193. assertEquals(f, g);
  194. assertEquals(this, obj);
  195. return RV;
  196. }
  197. };
  198. function f() {}
  199. assertEquals(RV, goog.structs.map(obj, f));
  200. }
  201. function testMap2() {
  202. var THIS_OBJ = {};
  203. var RV = {};
  204. var obj = {
  205. map: function(g, obj2) {
  206. assertEquals(f, g);
  207. assertEquals(this, obj);
  208. assertEquals(THIS_OBJ, obj2);
  209. return RV;
  210. }
  211. };
  212. function f() {}
  213. assertEquals(RV, goog.structs.map(obj, f, THIS_OBJ));
  214. }
  215. function testMapArrayLike() {
  216. var col = [0, 1, 2];
  217. function f(v, i, col2) {
  218. assertEquals(col, col2);
  219. assertEquals('number', typeof i);
  220. return v * v;
  221. }
  222. assertArrayEquals([0, 1, 4], goog.structs.map(col, f));
  223. }
  224. function testMapArrayLike2() {
  225. var THIS_OBJ = {};
  226. var col = [0, 1, 2];
  227. function f(v, i, col2) {
  228. assertEquals(col, col2);
  229. assertEquals('number', typeof i);
  230. assertEquals(THIS_OBJ, this);
  231. return v * v;
  232. }
  233. assertArrayEquals([0, 1, 4], goog.structs.map(col, f, THIS_OBJ));
  234. }
  235. function testMapString() {
  236. var col = '012';
  237. function f(v, i, col2) {
  238. // The SpiderMonkey Array.map for strings turns the string into a String
  239. // so we cannot use assertEquals because it uses ===.
  240. assertTrue(col == col2);
  241. assertEquals('number', typeof i);
  242. return Number(v) * Number(v);
  243. }
  244. assertArrayEquals([0, 1, 4], goog.structs.map(col, f));
  245. }
  246. function testMapString2() {
  247. var THIS_OBJ = {};
  248. var col = '012';
  249. function f(v, i, col2) {
  250. // for some reason the strings are equal but identical???
  251. assertEquals(String(col), String(col2));
  252. assertEquals('number', typeof i);
  253. assertEquals(THIS_OBJ, this);
  254. return Number(v) * Number(v);
  255. }
  256. assertArrayEquals([0, 1, 4], goog.structs.map(col, f, THIS_OBJ));
  257. }
  258. function testMapMap() {
  259. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  260. function f(v, key, col2) {
  261. assertEquals(col, col2);
  262. assertEquals('string', typeof key);
  263. return v * v;
  264. }
  265. assertObjectEquals({a: 0, b: 1, c: 4}, goog.structs.map(col, f));
  266. }
  267. function testMapMap2() {
  268. var THIS_OBJ = {};
  269. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  270. function f(v, key, col2) {
  271. assertEquals(col, col2);
  272. assertEquals('string', typeof key);
  273. assertEquals(THIS_OBJ, this);
  274. return v * v;
  275. }
  276. assertObjectEquals({a: 0, b: 1, c: 4}, goog.structs.map(col, f, THIS_OBJ));
  277. }
  278. function testMapSet() {
  279. var col = new goog.structs.Set([0, 1, 2]);
  280. function f(v, key, col2) {
  281. assertEquals(col, col2);
  282. assertEquals('undefined', typeof key);
  283. return v * v;
  284. }
  285. assertArrayEquals([0, 1, 4], goog.structs.map(col, f));
  286. }
  287. function testMapSet2() {
  288. var THIS_OBJ = {};
  289. var col = new goog.structs.Set([0, 1, 2]);
  290. function f(v, key, col2) {
  291. assertEquals(col, col2);
  292. assertEquals('undefined', typeof key);
  293. assertEquals(THIS_OBJ, this);
  294. return v * v;
  295. }
  296. assertArrayEquals([0, 1, 4], goog.structs.map(col, f, THIS_OBJ));
  297. }
  298. function testMapNodeList() {
  299. var col = getAll();
  300. function f(v, i, col2) {
  301. assertEquals(col, col2);
  302. assertEquals('number', typeof i);
  303. return v.tagName;
  304. }
  305. assertEquals('HRPPPPPPPPH1', goog.structs.map(col, f).join(''));
  306. }
  307. function testMapNodeList2() {
  308. var THIS_OBJ = {};
  309. var col = getAll();
  310. function f(v, i, col2) {
  311. assertEquals(col, col2);
  312. assertEquals('number', typeof i);
  313. assertEquals(THIS_OBJ, this);
  314. return v.tagName;
  315. }
  316. assertEquals('HRPPPPPPPPH1', goog.structs.map(col, f, THIS_OBJ).join(''));
  317. }
  318. // Filter
  319. function testFilter() {
  320. var RV = {};
  321. var obj = {
  322. filter: function(g) {
  323. assertEquals(f, g);
  324. assertEquals(this, obj);
  325. return RV;
  326. }
  327. };
  328. function f() {}
  329. assertEquals(RV, goog.structs.filter(obj, f));
  330. }
  331. function testFilter2() {
  332. var THIS_OBJ = {};
  333. var RV = {};
  334. var obj = {
  335. filter: function(g, obj2) {
  336. assertEquals(f, g);
  337. assertEquals(this, obj);
  338. assertEquals(THIS_OBJ, obj2);
  339. return RV;
  340. }
  341. };
  342. function f() {}
  343. assertEquals(RV, goog.structs.filter(obj, f, THIS_OBJ));
  344. }
  345. function testFilterArrayLike() {
  346. var col = [0, 1, 2];
  347. function f(v, i, col2) {
  348. assertEquals(col, col2);
  349. assertEquals('number', typeof i);
  350. return v > 0;
  351. }
  352. assertArrayEquals([1, 2], goog.structs.filter(col, f));
  353. }
  354. function testFilterArrayLike2() {
  355. var THIS_OBJ = {};
  356. var col = [0, 1, 2];
  357. function f(v, i, col2) {
  358. assertEquals(col, col2);
  359. assertEquals('number', typeof i);
  360. assertEquals(THIS_OBJ, this);
  361. return v > 0;
  362. }
  363. assertArrayEquals([1, 2], goog.structs.filter(col, f, THIS_OBJ));
  364. }
  365. function testFilterString() {
  366. var col = '012';
  367. function f(v, i, col2) {
  368. // for some reason the strings are equal but identical???
  369. assertEquals(String(col), String(col2));
  370. assertEquals('number', typeof i);
  371. return Number(v) > 0;
  372. }
  373. assertArrayEquals(['1', '2'], goog.structs.filter(col, f));
  374. }
  375. function testFilterString2() {
  376. var THIS_OBJ = {};
  377. var col = '012';
  378. function f(v, i, col2) {
  379. // for some reason the strings are equal but identical???
  380. assertEquals(String(col), String(col2));
  381. assertEquals('number', typeof i);
  382. assertEquals(THIS_OBJ, this);
  383. return Number(v) > 0;
  384. }
  385. assertArrayEquals(['1', '2'], goog.structs.filter(col, f, THIS_OBJ));
  386. }
  387. function testFilterMap() {
  388. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  389. function f(v, key, col2) {
  390. assertEquals(col, col2);
  391. assertEquals('string', typeof key);
  392. return v > 0;
  393. }
  394. assertObjectEquals({b: 1, c: 2}, goog.structs.filter(col, f));
  395. }
  396. function testFilterMap2() {
  397. var THIS_OBJ = {};
  398. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  399. function f(v, key, col2) {
  400. assertEquals(col, col2);
  401. assertEquals('string', typeof key);
  402. assertEquals(THIS_OBJ, this);
  403. return v > 0;
  404. }
  405. assertObjectEquals({b: 1, c: 2}, goog.structs.filter(col, f, THIS_OBJ));
  406. }
  407. function testFilterSet() {
  408. var col = new goog.structs.Set([0, 1, 2]);
  409. function f(v, key, col2) {
  410. assertEquals(col, col2);
  411. assertEquals('undefined', typeof key);
  412. return v > 0;
  413. }
  414. assertArrayEquals([1, 2], goog.structs.filter(col, f));
  415. }
  416. function testFilterSet2() {
  417. var THIS_OBJ = {};
  418. var col = new goog.structs.Set([0, 1, 2]);
  419. function f(v, key, col2) {
  420. assertEquals(col, col2);
  421. assertEquals('undefined', typeof key);
  422. assertEquals(THIS_OBJ, this);
  423. return v > 0;
  424. }
  425. assertArrayEquals([1, 2], goog.structs.filter(col, f, THIS_OBJ));
  426. }
  427. function testFilterNodeList() {
  428. var col = getAll();
  429. function f(v, i, col2) {
  430. assertEquals(col, col2);
  431. assertEquals('number', typeof i);
  432. return v.tagName == goog.dom.TagName.P;
  433. }
  434. assertEquals('P,P,P,P,P,P,P,P', nodeNames(goog.structs.filter(col, f)));
  435. }
  436. function testFilterNodeList2() {
  437. var THIS_OBJ = {};
  438. var col = getAll();
  439. function f(v, i, col2) {
  440. assertEquals(col, col2);
  441. assertEquals('number', typeof i);
  442. assertEquals(THIS_OBJ, this);
  443. return v.tagName == goog.dom.TagName.P;
  444. }
  445. assertEquals(
  446. 'P,P,P,P,P,P,P,P', nodeNames(goog.structs.filter(col, f, THIS_OBJ)));
  447. }
  448. // Some
  449. function testSome() {
  450. var RV = {};
  451. var obj = {
  452. some: function(g) {
  453. assertEquals(f, g);
  454. assertEquals(this, obj);
  455. return RV;
  456. }
  457. };
  458. function f() {}
  459. assertEquals(RV, goog.structs.some(obj, f));
  460. }
  461. function testSome2() {
  462. var THIS_OBJ = {};
  463. var RV = {};
  464. var obj = {
  465. some: function(g, obj2) {
  466. assertEquals(f, g);
  467. assertEquals(this, obj);
  468. assertEquals(THIS_OBJ, obj2);
  469. return RV;
  470. }
  471. };
  472. function f() {}
  473. assertEquals(RV, goog.structs.some(obj, f, THIS_OBJ));
  474. }
  475. function testSomeArrayLike() {
  476. var limit = 0;
  477. var col = [0, 1, 2];
  478. function f(v, i, col2) {
  479. assertEquals(col, col2);
  480. assertEquals('number', typeof i);
  481. return v > limit;
  482. }
  483. assertTrue(goog.structs.some(col, f));
  484. limit = 2;
  485. assertFalse(goog.structs.some(col, f));
  486. }
  487. function testSomeArrayLike2() {
  488. var THIS_OBJ = {};
  489. var limit = 0;
  490. var col = [0, 1, 2];
  491. function f(v, i, col2) {
  492. assertEquals(col, col2);
  493. assertEquals('number', typeof i);
  494. assertEquals(THIS_OBJ, this);
  495. return v > limit;
  496. }
  497. assertTrue(goog.structs.some(col, f, THIS_OBJ));
  498. limit = 2;
  499. assertFalse(goog.structs.some(col, f, THIS_OBJ));
  500. }
  501. function testSomeString() {
  502. var limit = 0;
  503. var col = '012';
  504. function f(v, i, col2) {
  505. // for some reason the strings are equal but identical???
  506. assertEquals(String(col), String(col2));
  507. assertEquals('number', typeof i);
  508. return Number(v) > limit;
  509. }
  510. assertTrue(goog.structs.some(col, f));
  511. limit = 2;
  512. assertFalse(goog.structs.some(col, f));
  513. }
  514. function testSomeString2() {
  515. var THIS_OBJ = {};
  516. var limit = 0;
  517. var col = '012';
  518. function f(v, i, col2) {
  519. // for some reason the strings are equal but identical???
  520. assertEquals(String(col), String(col2));
  521. assertEquals('number', typeof i);
  522. assertEquals(THIS_OBJ, this);
  523. return Number(v) > limit;
  524. }
  525. assertTrue(goog.structs.some(col, f, THIS_OBJ));
  526. limit = 2;
  527. assertFalse(goog.structs.some(col, f, THIS_OBJ));
  528. }
  529. function testSomeMap() {
  530. var limit = 0;
  531. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  532. function f(v, key, col2) {
  533. assertEquals(col, col2);
  534. assertEquals('string', typeof key);
  535. return v > limit;
  536. }
  537. assertObjectEquals(true, goog.structs.some(col, f));
  538. limit = 2;
  539. assertFalse(goog.structs.some(col, f));
  540. }
  541. function testSomeMap2() {
  542. var THIS_OBJ = {};
  543. var limit = 0;
  544. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  545. function f(v, key, col2) {
  546. assertEquals(col, col2);
  547. assertEquals('string', typeof key);
  548. assertEquals(THIS_OBJ, this);
  549. return v > limit;
  550. }
  551. assertObjectEquals(true, goog.structs.some(col, f, THIS_OBJ));
  552. limit = 2;
  553. assertFalse(goog.structs.some(col, f, THIS_OBJ));
  554. }
  555. function testSomeSet() {
  556. var limit = 0;
  557. var col = new goog.structs.Set([0, 1, 2]);
  558. function f(v, key, col2) {
  559. assertEquals(col, col2);
  560. assertEquals('undefined', typeof key);
  561. return v > limit;
  562. }
  563. assertTrue(goog.structs.some(col, f));
  564. limit = 2;
  565. assertFalse(goog.structs.some(col, f));
  566. }
  567. function testSomeSet2() {
  568. var THIS_OBJ = {};
  569. var limit = 0;
  570. var col = new goog.structs.Set([0, 1, 2]);
  571. function f(v, key, col2) {
  572. assertEquals(col, col2);
  573. assertEquals('undefined', typeof key);
  574. assertEquals(THIS_OBJ, this);
  575. return v > limit;
  576. }
  577. assertTrue(goog.structs.some(col, f, THIS_OBJ));
  578. limit = 2;
  579. assertFalse(goog.structs.some(col, f, THIS_OBJ));
  580. }
  581. function testSomeNodeList() {
  582. var tagName = goog.dom.TagName.P;
  583. var col = getAll();
  584. function f(v, i, col2) {
  585. assertEquals(col, col2);
  586. assertEquals('number', typeof i);
  587. return v.tagName == tagName;
  588. }
  589. assertTrue(goog.structs.some(col, f));
  590. tagName = 'MARQUEE';
  591. assertFalse(goog.structs.some(col, f));
  592. }
  593. function testSomeNodeList2() {
  594. var THIS_OBJ = {};
  595. var tagName = goog.dom.TagName.P;
  596. var col = getAll();
  597. function f(v, i, col2) {
  598. assertEquals(col, col2);
  599. assertEquals('number', typeof i);
  600. assertEquals(THIS_OBJ, this);
  601. return v.tagName == tagName;
  602. }
  603. assertTrue(goog.structs.some(col, f, THIS_OBJ));
  604. tagName = 'MARQUEE';
  605. assertFalse(goog.structs.some(col, f, THIS_OBJ));
  606. }
  607. // Every
  608. function testEvery() {
  609. var RV = {};
  610. var obj = {
  611. every: function(g) {
  612. assertEquals(f, g);
  613. assertEquals(this, obj);
  614. return RV;
  615. }
  616. };
  617. function f() {}
  618. assertEquals(RV, goog.structs.every(obj, f));
  619. }
  620. function testEvery2() {
  621. var THIS_OBJ = {};
  622. var RV = {};
  623. var obj = {
  624. every: function(g, obj2) {
  625. assertEquals(f, g);
  626. assertEquals(this, obj);
  627. assertEquals(THIS_OBJ, obj2);
  628. return RV;
  629. }
  630. };
  631. function f() {}
  632. assertEquals(RV, goog.structs.every(obj, f, THIS_OBJ));
  633. }
  634. function testEveryArrayLike() {
  635. var limit = -1;
  636. var col = [0, 1, 2];
  637. function f(v, i, col2) {
  638. assertEquals(col, col2);
  639. assertEquals('number', typeof i);
  640. return v > limit;
  641. }
  642. assertTrue(goog.structs.every(col, f));
  643. limit = 1;
  644. assertFalse(goog.structs.every(col, f));
  645. }
  646. function testEveryArrayLike2() {
  647. var THIS_OBJ = {};
  648. var limit = -1;
  649. var col = [0, 1, 2];
  650. function f(v, i, col2) {
  651. assertEquals(col, col2);
  652. assertEquals('number', typeof i);
  653. assertEquals(THIS_OBJ, this);
  654. return v > limit;
  655. }
  656. assertTrue(goog.structs.every(col, f, THIS_OBJ));
  657. limit = 1;
  658. assertFalse(goog.structs.every(col, f, THIS_OBJ));
  659. }
  660. function testEveryString() {
  661. var limit = -1;
  662. var col = '012';
  663. function f(v, i, col2) {
  664. // for some reason the strings are equal but identical???
  665. assertEquals(String(col), String(col2));
  666. assertEquals('number', typeof i);
  667. return Number(v) > limit;
  668. }
  669. assertTrue(goog.structs.every(col, f));
  670. limit = 1;
  671. assertFalse(goog.structs.every(col, f));
  672. }
  673. function testEveryString2() {
  674. var THIS_OBJ = {};
  675. var limit = -1;
  676. var col = '012';
  677. function f(v, i, col2) {
  678. // for some reason the strings are equal but identical???
  679. assertEquals(String(col), String(col2));
  680. assertEquals('number', typeof i);
  681. assertEquals(THIS_OBJ, this);
  682. return Number(v) > limit;
  683. }
  684. assertTrue(goog.structs.every(col, f, THIS_OBJ));
  685. limit = 1;
  686. assertFalse(goog.structs.every(col, f, THIS_OBJ));
  687. }
  688. function testEveryMap() {
  689. var limit = -1;
  690. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  691. function f(v, key, col2) {
  692. assertEquals(col, col2);
  693. assertEquals('string', typeof key);
  694. return v > limit;
  695. }
  696. assertObjectEquals(true, goog.structs.every(col, f));
  697. limit = 1;
  698. assertFalse(goog.structs.every(col, f));
  699. }
  700. function testEveryMap2() {
  701. var THIS_OBJ = {};
  702. var limit = -1;
  703. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  704. function f(v, key, col2) {
  705. assertEquals(col, col2);
  706. assertEquals('string', typeof key);
  707. assertEquals(THIS_OBJ, this);
  708. return v > limit;
  709. }
  710. assertObjectEquals(true, goog.structs.every(col, f, THIS_OBJ));
  711. limit = 1;
  712. assertFalse(goog.structs.every(col, f, THIS_OBJ));
  713. }
  714. function testEverySet() {
  715. var limit = -1;
  716. var col = new goog.structs.Set([0, 1, 2]);
  717. function f(v, key, col2) {
  718. assertEquals(col, col2);
  719. assertEquals('undefined', typeof key);
  720. return v > limit;
  721. }
  722. assertTrue(goog.structs.every(col, f));
  723. limit = 1;
  724. assertFalse(goog.structs.every(col, f));
  725. }
  726. function testEverySet2() {
  727. var THIS_OBJ = {};
  728. var limit = -1;
  729. var col = new goog.structs.Set([0, 1, 2]);
  730. function f(v, key, col2) {
  731. assertEquals(col, col2);
  732. assertEquals('undefined', typeof key);
  733. assertEquals(THIS_OBJ, this);
  734. return v > limit;
  735. }
  736. assertTrue(goog.structs.every(col, f, THIS_OBJ));
  737. limit = 1;
  738. assertFalse(goog.structs.every(col, f, THIS_OBJ));
  739. }
  740. function testEveryNodeList() {
  741. var nodeType = 1; // ELEMENT
  742. var col = getAll();
  743. function f(v, i, col2) {
  744. assertEquals(col, col2);
  745. assertEquals('number', typeof i);
  746. return v.nodeType == nodeType;
  747. }
  748. assertTrue(goog.structs.every(col, f));
  749. nodeType = 3; // TEXT
  750. assertFalse(goog.structs.every(col, f));
  751. }
  752. function testEveryNodeList2() {
  753. var THIS_OBJ = {};
  754. var nodeType = 1; // ELEMENT
  755. var col = getAll();
  756. function f(v, i, col2) {
  757. assertEquals(col, col2);
  758. assertEquals('number', typeof i);
  759. assertEquals(THIS_OBJ, this);
  760. return v.nodeType == nodeType;
  761. }
  762. assertTrue(goog.structs.every(col, f, THIS_OBJ));
  763. nodeType = 3; // TEXT
  764. assertFalse(goog.structs.every(col, f, THIS_OBJ));
  765. }
  766. // For each
  767. function testForEach() {
  768. var called = false;
  769. var obj = {
  770. forEach: function(g) {
  771. assertEquals(f, g);
  772. assertEquals(this, obj);
  773. called = true;
  774. }
  775. };
  776. function f() {}
  777. goog.structs.forEach(obj, f);
  778. assertTrue(called);
  779. }
  780. function testForEach2() {
  781. var called = false;
  782. var THIS_OBJ = {};
  783. var obj = {
  784. forEach: function(g, obj2) {
  785. assertEquals(f, g);
  786. assertEquals(this, obj);
  787. assertEquals(THIS_OBJ, obj2);
  788. called = true;
  789. }
  790. };
  791. function f() {}
  792. goog.structs.forEach(obj, f, THIS_OBJ);
  793. assertTrue(called);
  794. }
  795. function testForEachArrayLike() {
  796. var col = [0, 1, 2];
  797. var values = [];
  798. function f(v, i, col2) {
  799. assertEquals(col, col2);
  800. assertEquals('number', typeof i);
  801. values.push(v * v);
  802. }
  803. goog.structs.forEach(col, f);
  804. assertArrayEquals([0, 1, 4], values);
  805. }
  806. function testForEachArrayLike2() {
  807. var THIS_OBJ = {};
  808. var col = [0, 1, 2];
  809. var values = [];
  810. function f(v, i, col2) {
  811. assertEquals(col, col2);
  812. assertEquals('number', typeof i);
  813. assertEquals(THIS_OBJ, this);
  814. values.push(v * v);
  815. }
  816. goog.structs.forEach(col, f, THIS_OBJ);
  817. assertArrayEquals([0, 1, 4], values);
  818. }
  819. function testForEachString() {
  820. var col = '012';
  821. var values = [];
  822. function f(v, i, col2) {
  823. // for some reason the strings are equal but identical???
  824. assertEquals(String(col), String(col2));
  825. assertEquals('number', typeof i);
  826. values.push(Number(v) * Number(v));
  827. }
  828. goog.structs.forEach(col, f);
  829. assertArrayEquals([0, 1, 4], values);
  830. }
  831. function testForEachString2() {
  832. var THIS_OBJ = {};
  833. var col = '012';
  834. var values = [];
  835. function f(v, i, col2) {
  836. // for some reason the strings are equal but identical???
  837. assertEquals(String(col), String(col2));
  838. assertEquals('number', typeof i);
  839. assertEquals(THIS_OBJ, this);
  840. values.push(Number(v) * Number(v));
  841. }
  842. goog.structs.forEach(col, f, THIS_OBJ);
  843. assertArrayEquals([0, 1, 4], values);
  844. }
  845. function testForEachMap() {
  846. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  847. var values = [];
  848. var keys = [];
  849. function f(v, key, col2) {
  850. assertEquals(col, col2);
  851. assertEquals('string', typeof key);
  852. values.push(v * v);
  853. keys.push(key);
  854. }
  855. goog.structs.forEach(col, f);
  856. assertArrayEquals([0, 1, 4], values);
  857. assertArrayEquals(['a', 'b', 'c'], keys);
  858. }
  859. function testForEachMap2() {
  860. var THIS_OBJ = {};
  861. var col = new goog.structs.Map({a: 0, b: 1, c: 2});
  862. var values = [];
  863. var keys = [];
  864. function f(v, key, col2) {
  865. assertEquals(col, col2);
  866. assertEquals('string', typeof key);
  867. assertEquals(THIS_OBJ, this);
  868. values.push(v * v);
  869. keys.push(key);
  870. }
  871. goog.structs.forEach(col, f, THIS_OBJ);
  872. assertArrayEquals([0, 1, 4], values);
  873. assertArrayEquals(['a', 'b', 'c'], keys);
  874. }
  875. function testForEachSet() {
  876. var col = new goog.structs.Set([0, 1, 2]);
  877. var values = [];
  878. function f(v, key, col2) {
  879. assertEquals(col, col2);
  880. assertEquals('undefined', typeof key);
  881. values.push(v * v);
  882. }
  883. goog.structs.forEach(col, f);
  884. assertArrayEquals([0, 1, 4], values);
  885. }
  886. function testForEachSet2() {
  887. var THIS_OBJ = {};
  888. var col = new goog.structs.Set([0, 1, 2]);
  889. var values = [];
  890. function f(v, key, col2) {
  891. assertEquals(col, col2);
  892. assertEquals('undefined', typeof key);
  893. assertEquals(THIS_OBJ, this);
  894. values.push(v * v);
  895. }
  896. goog.structs.forEach(col, f, THIS_OBJ);
  897. assertArrayEquals([0, 1, 4], values);
  898. }
  899. function testForEachNodeList() {
  900. var values = [];
  901. var col = getAll();
  902. function f(v, i, col2) {
  903. assertEquals(col, col2);
  904. assertEquals('number', typeof i);
  905. values.push(v.tagName);
  906. }
  907. goog.structs.forEach(col, f);
  908. assertEquals('HRPPPPPPPPH1', values.join(''));
  909. }
  910. function testForEachNodeList2() {
  911. var THIS_OBJ = {};
  912. var values = [];
  913. var col = getAll();
  914. function f(v, i, col2) {
  915. assertEquals(col, col2);
  916. assertEquals('number', typeof i);
  917. assertEquals(THIS_OBJ, this);
  918. values.push(v.tagName);
  919. }
  920. goog.structs.forEach(col, f, THIS_OBJ);
  921. assertEquals('HRPPPPPPPPH1', values.join(''));
  922. }