iter_test.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // Copyright 2007 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.iterTest');
  15. goog.setTestOnly('goog.iterTest');
  16. goog.require('goog.iter');
  17. goog.require('goog.iter.Iterator');
  18. goog.require('goog.iter.StopIteration');
  19. goog.require('goog.testing.jsunit');
  20. function ArrayIterator(array) {
  21. this.array_ = array;
  22. this.current_ = 0;
  23. }
  24. goog.inherits(ArrayIterator, goog.iter.Iterator);
  25. ArrayIterator.prototype.next = function() {
  26. if (this.current_ >= this.array_.length) {
  27. throw goog.iter.StopIteration;
  28. }
  29. return this.array_[this.current_++];
  30. };
  31. function testForEach() {
  32. var s = '';
  33. var iter = new ArrayIterator(['a', 'b', 'c', 'd']);
  34. goog.iter.forEach(iter, function(val, index, iter2) {
  35. assertEquals(iter, iter2);
  36. assertEquals('index should be undefined', 'undefined', typeof index);
  37. s += val;
  38. });
  39. assertEquals('abcd', s);
  40. }
  41. function testJoin() {
  42. var iter = new ArrayIterator(['a', 'b', 'c', 'd']);
  43. assertEquals('abcd', goog.iter.join(iter, ''));
  44. iter = new ArrayIterator(['a', 'b', 'c', 'd']);
  45. assertEquals('a,b,c,d', goog.iter.join(iter, ','));
  46. // make sure everything is treated as strings
  47. iter = new ArrayIterator([0, 1, 2, 3]);
  48. assertEquals('0123', goog.iter.join(iter, ''));
  49. iter = new ArrayIterator([0, 1, 2, 3]);
  50. assertEquals('0919293', goog.iter.join(iter, 9));
  51. // Joining an empty iterator should result in an empty string
  52. iter = new ArrayIterator([]);
  53. assertEquals('', goog.iter.join(iter, ','));
  54. }
  55. function testRange() {
  56. var iter = goog.iter.range(0, 5, 1);
  57. assertEquals('01234', goog.iter.join(iter, ''));
  58. iter = goog.iter.range(0, 5, 2);
  59. assertEquals('024', goog.iter.join(iter, ''));
  60. iter = goog.iter.range(0, 5, 5);
  61. assertEquals('0', goog.iter.join(iter, ''));
  62. iter = goog.iter.range(0, 5, 10);
  63. assertEquals('0', goog.iter.join(iter, ''));
  64. // negative step
  65. var iter = goog.iter.range(5, 0, -1);
  66. assertEquals('54321', goog.iter.join(iter, ''));
  67. iter = goog.iter.range(5, 0, -2);
  68. assertEquals('531', goog.iter.join(iter, ''));
  69. iter = goog.iter.range(5, 0, -5);
  70. assertEquals('5', goog.iter.join(iter, ''));
  71. iter = goog.iter.range(5, 0, -10);
  72. assertEquals('5', goog.iter.join(iter, ''));
  73. // wrong direction should result in empty iterator
  74. iter = goog.iter.range(0, 5, -1);
  75. assertEquals('', goog.iter.join(iter, ''));
  76. iter = goog.iter.range(5, 0, 1);
  77. assertEquals('', goog.iter.join(iter, ''));
  78. // a step of 0 is not allowed
  79. goog.iter.range(0, 5, 0);
  80. // test the opt args
  81. iter = goog.iter.range(0, 5);
  82. assertEquals('01234', goog.iter.join(iter, ''));
  83. iter = goog.iter.range(5);
  84. assertEquals('01234', goog.iter.join(iter, ''));
  85. }
  86. function testFilter() {
  87. var iter = goog.iter.range(5);
  88. var iter2 = goog.iter.filter(iter, function(val, index, iter3) {
  89. assertEquals(iter, iter3);
  90. assertEquals('index should be undefined', 'undefined', typeof index);
  91. return val > 1;
  92. });
  93. assertEquals('234', goog.iter.join(iter2, ''));
  94. // Chaining filters
  95. iter = goog.iter.range(10);
  96. var sb = [];
  97. var evens = goog.iter.filter(iter, function(v) {
  98. sb.push('a' + v);
  99. return v % 2 == 0;
  100. });
  101. var evens2 = goog.iter.filter(evens, function(v) {
  102. sb.push('b' + v);
  103. return v >= 5;
  104. });
  105. assertEquals('68', goog.iter.join(evens2, ''));
  106. // Note the order here. The next calls are done lazily.
  107. assertEquals('a0b0a1a2b2a3a4b4a5a6b6a7a8b8a9', sb.join(''));
  108. }
  109. function testFilterFalse() {
  110. var iter = goog.iter.range(5);
  111. var iter2 = goog.iter.filterFalse(iter, function(val, index, iter3) {
  112. assertEquals(iter, iter3);
  113. assertEquals('index should be undefined', 'undefined', typeof index);
  114. return val < 2;
  115. });
  116. assertEquals('234', goog.iter.join(iter2, ''));
  117. // Chaining filters
  118. iter = goog.iter.range(10);
  119. var sb = [];
  120. var odds = goog.iter.filterFalse(iter, function(v) {
  121. sb.push('a' + v);
  122. return v % 2 == 0;
  123. });
  124. var odds2 = goog.iter.filterFalse(odds, function(v) {
  125. sb.push('b' + v);
  126. return v <= 5;
  127. });
  128. assertEquals('79', goog.iter.join(odds2, ''));
  129. // Note the order here. The next calls are done lazily.
  130. assertEquals('a0a1b1a2a3b3a4a5b5a6a7b7a8a9b9', sb.join(''));
  131. }
  132. function testMap() {
  133. var iter = goog.iter.range(4);
  134. var iter2 = goog.iter.map(iter, function(val, index, iter3) {
  135. assertEquals(iter, iter3);
  136. assertEquals('index should be undefined', 'undefined', typeof index);
  137. return val * val;
  138. });
  139. assertEquals('0149', goog.iter.join(iter2, ''));
  140. }
  141. function testReduce() {
  142. var iter = goog.iter.range(1, 5);
  143. assertEquals(
  144. 10, // 1 + 2 + 3 + 4
  145. goog.iter.reduce(iter, function(val, el) { return val + el; }, 0));
  146. }
  147. function testReduce2() {
  148. var iter = goog.iter.range(1, 5);
  149. assertEquals(
  150. 24, // 4!
  151. goog.iter.reduce(iter, function(val, el) { return val * el; }, 1));
  152. }
  153. function testSome() {
  154. var iter = goog.iter.range(5);
  155. var b = goog.iter.some(iter, function(val, index, iter3) {
  156. assertEquals(iter, iter3);
  157. assertEquals('index should be undefined', 'undefined', typeof index);
  158. return val > 1;
  159. });
  160. assertTrue(b);
  161. iter = goog.iter.range(5);
  162. b = goog.iter.some(iter, function(val, index, iter3) {
  163. assertEquals(iter, iter3);
  164. assertEquals('index should be undefined', 'undefined', typeof index);
  165. return val > 100;
  166. });
  167. assertFalse(b);
  168. }
  169. function testEvery() {
  170. var iter = goog.iter.range(5);
  171. var b = goog.iter.every(iter, function(val, index, iter3) {
  172. assertEquals(iter, iter3);
  173. assertEquals('index should be undefined', 'undefined', typeof index);
  174. return val >= 0;
  175. });
  176. assertTrue(b);
  177. iter = goog.iter.range(5);
  178. b = goog.iter.every(iter, function(val, index, iter3) {
  179. assertEquals(iter, iter3);
  180. assertEquals('index should be undefined', 'undefined', typeof index);
  181. return val > 1;
  182. });
  183. assertFalse(b);
  184. }
  185. function testChain() {
  186. var iter = goog.iter.range(0, 2);
  187. var iter2 = goog.iter.range(2, 4);
  188. var iter3 = goog.iter.range(4, 6);
  189. var iter4 = goog.iter.chain(iter, iter2, iter3);
  190. assertEquals('012345', goog.iter.join(iter4, ''));
  191. // empty iter
  192. iter = new goog.iter.Iterator;
  193. iter2 = goog.iter.chain(iter);
  194. assertEquals('', goog.iter.join(iter2, ''));
  195. // no args
  196. iter2 = goog.iter.chain();
  197. assertEquals('', goog.iter.join(iter2, ''));
  198. // arrays
  199. var arr = [0, 1];
  200. var arr2 = [2, 3];
  201. var arr3 = [4, 5];
  202. iter = goog.iter.chain(arr, arr2, arr3);
  203. assertEquals('012345', goog.iter.join(iter, ''));
  204. }
  205. function testChainFromIterable() {
  206. var arg = [0, 1];
  207. var arg2 = [2, 3];
  208. var arg3 = goog.iter.range(4, 6);
  209. var iter = goog.iter.chainFromIterable([arg, arg2, arg3]);
  210. assertEquals('012345', goog.iter.join(iter, ''));
  211. }
  212. function testChainFromIterable2() {
  213. var arg = goog.iter.zip([0, 3], [1, 4], [2, 5]);
  214. var iter = goog.iter.chainFromIterable(arg);
  215. assertEquals('012345', goog.iter.join(iter, ''));
  216. }
  217. function testDropWhile() {
  218. var iter = goog.iter.range(10);
  219. var iter2 = goog.iter.dropWhile(iter, function(val, index, iter3) {
  220. assertEquals(iter, iter3);
  221. assertEquals('index should be undefined', 'undefined', typeof index);
  222. return val < 5;
  223. });
  224. assertEquals('56789', goog.iter.join(iter2, ''));
  225. }
  226. function testDropWhile2() {
  227. var iter = goog.iter.range(10);
  228. var iter2 = goog.iter.dropWhile(iter, function(val, index, iter3) {
  229. assertEquals(iter, iter3);
  230. assertEquals('index should be undefined', 'undefined', typeof index);
  231. return val != 5;
  232. });
  233. assertEquals('56789', goog.iter.join(iter2, ''));
  234. }
  235. function testTakeWhile() {
  236. var iter = goog.iter.range(10);
  237. var iter2 = goog.iter.takeWhile(iter, function(val, index, iter3) {
  238. assertEquals(iter, iter3);
  239. assertEquals('index should be undefined', 'undefined', typeof index);
  240. return val < 5;
  241. });
  242. assertEquals('01234', goog.iter.join(iter2, ''));
  243. // next() should not have been called on iter after the first failure and
  244. // therefore it should contain some elements. 5 failed so we should have
  245. // the rest
  246. assertEquals('6789', goog.iter.join(iter, ''));
  247. }
  248. function testTakeWhile2() {
  249. var iter = goog.iter.range(10);
  250. var iter2 = goog.iter.takeWhile(iter, function(val, index, iter3) {
  251. assertEquals(iter, iter3);
  252. assertEquals('index should be undefined', 'undefined', typeof index);
  253. return val != 5;
  254. });
  255. assertEquals('01234', goog.iter.join(iter2, ''));
  256. // next() should not have been called on iter after the first failure and
  257. // therefore it should contain some elements. 5 failed so we should have
  258. // the rest
  259. assertEquals('6789', goog.iter.join(iter, ''));
  260. }
  261. function testToArray() {
  262. var iter = goog.iter.range(5);
  263. var array = goog.iter.toArray(iter);
  264. assertEquals('01234', array.join(''));
  265. // Empty
  266. iter = new goog.iter.Iterator;
  267. array = goog.iter.toArray(iter);
  268. assertEquals('Empty iterator to array', '', array.join(''));
  269. }
  270. function testToArray2() {
  271. var iterable = [0, 1, 2, 3, 4];
  272. var array = goog.iter.toArray(iterable);
  273. assertEquals('01234', array.join(''));
  274. // Empty
  275. iterable = [];
  276. array = goog.iter.toArray(iterable);
  277. assertEquals('Empty iterator to array', '', array.join(''));
  278. }
  279. function testEquals() {
  280. var iter = goog.iter.range(5);
  281. var iter2 = goog.iter.range(5);
  282. assertTrue('Equal iterators', goog.iter.equals(iter, iter2));
  283. iter = goog.iter.range(4);
  284. iter2 = goog.iter.range(5);
  285. assertFalse('Second one is longer', goog.iter.equals(iter, iter2));
  286. iter = goog.iter.range(5);
  287. iter2 = goog.iter.range(4);
  288. assertFalse('First one is longer', goog.iter.equals(iter, iter2));
  289. // 2 empty iterators
  290. iter = new goog.iter.Iterator;
  291. iter2 = new goog.iter.Iterator;
  292. assertTrue('Two empty iterators are equal', goog.iter.equals(iter, iter2));
  293. iter = goog.iter.range(4);
  294. assertFalse('Same iterator', goog.iter.equals(iter, iter));
  295. // equality function
  296. iter = goog.iter.toIterator(['A', 'B', 'C']);
  297. iter2 = goog.iter.toIterator(['a', 'b', 'c']);
  298. var equalsFn = function(a, b) { return a.toLowerCase() == b.toLowerCase(); };
  299. assertTrue('Case-insensitive equal', goog.iter.equals(iter, iter2, equalsFn));
  300. }
  301. function testToIterator() {
  302. var iter = new goog.iter.range(5);
  303. var iter2 = goog.iter.toIterator(iter);
  304. assertEquals(
  305. 'toIterator on an iterator should return the same obejct', iter, iter2);
  306. var iterLikeObject = {next: function() { throw goog.iter.StopIteration; }};
  307. var obj = {
  308. __iterator__: function(opt_keys) {
  309. assertFalse(
  310. '__iterator__ should always be called with false in toIterator',
  311. opt_keys);
  312. return iterLikeObject;
  313. }
  314. };
  315. assertEquals(
  316. 'Should return the return value of __iterator_(false)', iterLikeObject,
  317. goog.iter.toIterator(obj));
  318. // Array
  319. var array = [0, 1, 2, 3, 4];
  320. iter = goog.iter.toIterator(array);
  321. assertEquals('01234', goog.iter.join(iter, ''));
  322. // Array like
  323. var arrayLike = {'0': 0, '1': 1, '2': 2, length: 3};
  324. iter = goog.iter.toIterator(arrayLike);
  325. assertEquals('012', goog.iter.join(iter, ''));
  326. // DOM
  327. var dom = document.getElementById('t1').childNodes;
  328. iter = goog.iter.toIterator(dom);
  329. iter2 = goog.iter.map(iter, function(el) { return el.innerHTML; });
  330. assertEquals('012', goog.iter.join(iter2, ''));
  331. }
  332. function testNextOrValue() {
  333. var iter = goog.iter.toIterator([1]);
  334. assertEquals(
  335. 'Should return value when iterator is non-empty', 1,
  336. goog.iter.nextOrValue(iter, null));
  337. assertNull(
  338. 'Should return given default when iterator is empty',
  339. goog.iter.nextOrValue(iter, null));
  340. assertEquals(
  341. 'Should return given default when iterator is (still) empty', -1,
  342. goog.iter.nextOrValue(iter, -1));
  343. }
  344. // Return the product of several arrays as an array
  345. function productAsArray(var_args) {
  346. var iter = goog.iter.product.apply(null, arguments);
  347. return goog.iter.toArray(iter);
  348. }
  349. function testProduct() {
  350. assertArrayEquals(
  351. [[1, 3], [1, 4], [2, 3], [2, 4]], productAsArray([1, 2], [3, 4]));
  352. assertArrayEquals(
  353. [
  354. [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6],
  355. [2, 4, 5], [2, 4, 6]
  356. ],
  357. productAsArray([1, 2], [3, 4], [5, 6]));
  358. assertArrayEquals([[1]], productAsArray([1]));
  359. assertArrayEquals([], productAsArray([1], []));
  360. assertArrayEquals([], productAsArray());
  361. var expectedResult = [];
  362. var a = [1, 2, 3];
  363. var b = [4, 5, 6];
  364. var c = [7, 8, 9];
  365. for (var i = 0; i < a.length; i++) {
  366. for (var j = 0; j < b.length; j++) {
  367. for (var k = 0; k < c.length; k++) {
  368. expectedResult.push([a[i], b[j], c[k]]);
  369. }
  370. }
  371. }
  372. assertArrayEquals(expectedResult, productAsArray(a, b, c));
  373. }
  374. function testProductIteration() {
  375. var iter = goog.iter.product([1, 2], [3, 4]);
  376. assertArrayEquals([1, 3], iter.next());
  377. assertArrayEquals([1, 4], iter.next());
  378. assertArrayEquals([2, 3], iter.next());
  379. assertArrayEquals([2, 4], iter.next());
  380. var ex = assertThrows(function() { iter.next() });
  381. assertEquals(goog.iter.StopIteration, ex);
  382. // Ensure the iterator forever throws StopIteration.
  383. for (var i = 0; i < 5; i++) {
  384. var ex = assertThrows(function() { iter.next() });
  385. assertEquals(goog.iter.StopIteration, ex);
  386. }
  387. iter = goog.iter.product();
  388. var ex = assertThrows(function() { iter.next() });
  389. assertEquals(goog.iter.StopIteration, ex);
  390. iter = goog.iter.product([]);
  391. var ex = assertThrows(function() { iter.next() });
  392. assertEquals(goog.iter.StopIteration, ex);
  393. }
  394. function testCycle() {
  395. var regularArray = [1, 2, 3];
  396. var iter = goog.iter.cycle(regularArray);
  397. // Test 3 cycles to ensure proper cache behavior
  398. var values = [];
  399. for (var i = 0; i < 9; i++) {
  400. values.push(iter.next());
  401. }
  402. assertArrayEquals([1, 2, 3, 1, 2, 3, 1, 2, 3], values);
  403. }
  404. function testCycleSingleItemIterable() {
  405. var singleItemArray = [1];
  406. var iter = goog.iter.cycle(singleItemArray);
  407. var values = [];
  408. for (var i = 0; i < 5; i++) {
  409. values.push(iter.next());
  410. }
  411. assertArrayEquals([1, 1, 1, 1, 1], values);
  412. }
  413. function testCycleEmptyIterable() {
  414. var emptyArray = [];
  415. var iter = goog.iter.cycle(emptyArray);
  416. var ex = assertThrows(function() { iter.next(); });
  417. assertEquals(goog.iter.StopIteration, ex);
  418. }
  419. function testCountNoArgs() {
  420. var iter = goog.iter.count();
  421. var values = goog.iter.limit(iter, 5);
  422. assertArrayEquals([0, 1, 2, 3, 4], goog.iter.toArray(values));
  423. }
  424. function testCountStart() {
  425. var iter = goog.iter.count(10);
  426. var values = goog.iter.limit(iter, 5);
  427. assertArrayEquals([10, 11, 12, 13, 14], goog.iter.toArray(values));
  428. }
  429. function testCountStep() {
  430. var iter = goog.iter.count(10, 2);
  431. var values = goog.iter.limit(iter, 5);
  432. assertArrayEquals([10, 12, 14, 16, 18], goog.iter.toArray(values));
  433. }
  434. function testCountNegativeStep() {
  435. var iter = goog.iter.count(10, -2);
  436. var values = goog.iter.limit(iter, 5);
  437. assertArrayEquals([10, 8, 6, 4, 2], goog.iter.toArray(values));
  438. }
  439. function testCountZeroStep() {
  440. var iter = goog.iter.count(42, 0);
  441. assertEquals(42, iter.next());
  442. assertEquals(42, iter.next());
  443. assertEquals(42, iter.next());
  444. }
  445. function testCountFloat() {
  446. var iter = goog.iter.count(1.5, 0.5);
  447. var values = goog.iter.limit(iter, 5);
  448. assertArrayEquals([1.5, 2.0, 2.5, 3.0, 3.5], goog.iter.toArray(values));
  449. }
  450. function testRepeat() {
  451. var obj = {foo: 'bar'};
  452. var iter = goog.iter.repeat(obj);
  453. assertEquals(obj, iter.next());
  454. assertEquals(obj, iter.next());
  455. assertEquals(obj, iter.next());
  456. }
  457. function testAccumulateArray() {
  458. var iter = goog.iter.accumulate([1, 2, 3, 4, 5]);
  459. assertArrayEquals([1, 3, 6, 10, 15], goog.iter.toArray(iter));
  460. }
  461. function testAccumulateIterator() {
  462. var iter = goog.iter.accumulate(goog.iter.range(1, 6));
  463. assertArrayEquals([1, 3, 6, 10, 15], goog.iter.toArray(iter));
  464. }
  465. function testAccumulateFloat() {
  466. var iter = goog.iter.accumulate([1.0, 2.5, 0.5, 1.5, 0.5]);
  467. assertArrayEquals([1.0, 3.5, 4.0, 5.5, 6.0], goog.iter.toArray(iter));
  468. }
  469. function testZipArrays() {
  470. var iter = goog.iter.zip([1, 2, 3], [4, 5, 6], [7, 8, 9]);
  471. assertArrayEquals([1, 4, 7], iter.next());
  472. assertArrayEquals([2, 5, 8], iter.next());
  473. assertArrayEquals([3, 6, 9], iter.next());
  474. var ex = assertThrows(function() { iter.next() });
  475. assertEquals(goog.iter.StopIteration, ex);
  476. }
  477. function testZipSingleArg() {
  478. var iter = goog.iter.zip([1, 2, 3]);
  479. assertArrayEquals([1], iter.next());
  480. assertArrayEquals([2], iter.next());
  481. assertArrayEquals([3], iter.next());
  482. var ex = assertThrows(function() { iter.next() });
  483. assertEquals(goog.iter.StopIteration, ex);
  484. }
  485. function testZipUnevenArgs() {
  486. var iter = goog.iter.zip([1, 2, 3], [4, 5], [7]);
  487. assertArrayEquals([1, 4, 7], iter.next());
  488. var ex = assertThrows(function() { iter.next() });
  489. assertEquals(goog.iter.StopIteration, ex);
  490. }
  491. function testZipNoArgs() {
  492. var iter = goog.iter.zip();
  493. var ex = assertThrows(function() { iter.next() });
  494. assertEquals(goog.iter.StopIteration, ex);
  495. }
  496. function testZipIterators() {
  497. var iter = goog.iter.zip(goog.iter.count(), goog.iter.repeat('foo'));
  498. assertArrayEquals([0, 'foo'], iter.next());
  499. assertArrayEquals([1, 'foo'], iter.next());
  500. assertArrayEquals([2, 'foo'], iter.next());
  501. assertArrayEquals([3, 'foo'], iter.next());
  502. }
  503. function testZipLongestArrays() {
  504. var iter = goog.iter.zipLongest('-', 'ABCD'.split(''), 'xy'.split(''));
  505. assertArrayEquals(['A', 'x'], iter.next());
  506. assertArrayEquals(['B', 'y'], iter.next());
  507. assertArrayEquals(['C', '-'], iter.next());
  508. assertArrayEquals(['D', '-'], iter.next());
  509. var ex = assertThrows(function() { iter.next() });
  510. assertEquals(goog.iter.StopIteration, ex);
  511. }
  512. function testZipLongestSingleArg() {
  513. var iter = goog.iter.zipLongest('-', 'ABCD'.split(''));
  514. assertArrayEquals(['A'], iter.next());
  515. assertArrayEquals(['B'], iter.next());
  516. assertArrayEquals(['C'], iter.next());
  517. assertArrayEquals(['D'], iter.next());
  518. var ex = assertThrows(function() { iter.next() });
  519. assertEquals(goog.iter.StopIteration, ex);
  520. }
  521. function testZipLongestNoArgs() {
  522. var iter = goog.iter.zipLongest();
  523. assertArrayEquals([], goog.iter.toArray(iter));
  524. var iter = goog.iter.zipLongest('fill');
  525. assertArrayEquals([], goog.iter.toArray(iter));
  526. }
  527. function testZipLongestIterators() {
  528. var iter = goog.iter.zipLongest(null, goog.iter.range(3), goog.iter.range(5));
  529. assertArrayEquals([0, 0], iter.next());
  530. assertArrayEquals([1, 1], iter.next());
  531. assertArrayEquals([2, 2], iter.next());
  532. assertArrayEquals([null, 3], iter.next());
  533. assertArrayEquals([null, 4], iter.next());
  534. var ex = assertThrows(function() { iter.next() });
  535. assertEquals(goog.iter.StopIteration, ex);
  536. }
  537. function testCompressArray() {
  538. var iter = goog.iter.compress('ABCDEF'.split(''), [1, 0, 1, 0, 1, 1]);
  539. assertEquals('ACEF', goog.iter.join(iter, ''));
  540. }
  541. function testCompressUnevenArgs() {
  542. var iter = goog.iter.compress('ABCDEF'.split(''), [false, true, true]);
  543. assertEquals('BC', goog.iter.join(iter, ''));
  544. }
  545. function testCompressIterators() {
  546. var iter = goog.iter.compress(goog.iter.range(10), goog.iter.cycle([0, 1]));
  547. assertArrayEquals([1, 3, 5, 7, 9], goog.iter.toArray(iter));
  548. }
  549. function testGroupByNoKeyFunc() {
  550. var iter = goog.iter.groupBy('AAABBBBCDD'.split(''));
  551. assertArrayEquals(['A', ['A', 'A', 'A']], iter.next());
  552. assertArrayEquals(['B', ['B', 'B', 'B', 'B']], iter.next());
  553. assertArrayEquals(['C', ['C']], iter.next());
  554. assertArrayEquals(['D', ['D', 'D']], iter.next());
  555. var ex = assertThrows(function() { iter.next() });
  556. assertEquals(goog.iter.StopIteration, ex);
  557. }
  558. function testGroupByKeyFunc() {
  559. var keyFunc = function(x) { return x.toLowerCase(); };
  560. var iter = goog.iter.groupBy('AaAABBbbBCccddDD'.split(''), keyFunc);
  561. assertArrayEquals(['a', ['A', 'a', 'A', 'A']], iter.next());
  562. assertArrayEquals(['b', ['B', 'B', 'b', 'b', 'B']], iter.next());
  563. assertArrayEquals(['c', ['C', 'c', 'c']], iter.next());
  564. assertArrayEquals(['d', ['d', 'd', 'D', 'D']], iter.next());
  565. var ex = assertThrows(function() { iter.next() });
  566. assertEquals(goog.iter.StopIteration, ex);
  567. }
  568. function testStarMap() {
  569. var iter = goog.iter.starMap([[2, 5], [3, 2], [10, 3]], Math.pow);
  570. assertEquals(32, iter.next());
  571. assertEquals(9, iter.next());
  572. assertEquals(1000, iter.next());
  573. var ex = assertThrows(function() { iter.next() });
  574. assertEquals(goog.iter.StopIteration, ex);
  575. }
  576. function testStarMapExtraArgs() {
  577. var func = function(string, radix, undef, iterator) {
  578. assertEquals('undef should be undefined', 'undefined', typeof undef);
  579. assertTrue(iterator instanceof goog.iter.Iterator);
  580. return parseInt(string, radix);
  581. };
  582. var iter = goog.iter.starMap([['42', 10], ['0xFF', 16], ['101', 2]], func);
  583. assertEquals(42, iter.next());
  584. assertEquals(255, iter.next());
  585. assertEquals(5, iter.next());
  586. var ex = assertThrows(function() { iter.next() });
  587. assertEquals(goog.iter.StopIteration, ex);
  588. }
  589. function testTeeArray() {
  590. var iters = goog.iter.tee('ABC'.split(''));
  591. assertEquals(2, iters.length);
  592. var it0 = iters[0], it1 = iters[1];
  593. assertEquals('A', it0.next());
  594. assertEquals('A', it1.next());
  595. assertEquals('B', it0.next());
  596. assertEquals('B', it1.next());
  597. assertEquals('C', it0.next());
  598. assertEquals('C', it1.next());
  599. var ex = assertThrows(function() { it0.next() });
  600. assertEquals(goog.iter.StopIteration, ex);
  601. ex = assertThrows(function() { it1.next() });
  602. assertEquals(goog.iter.StopIteration, ex);
  603. }
  604. function testTeeIterator() {
  605. var iters = goog.iter.tee(goog.iter.count(), 3);
  606. assertEquals(3, iters.length);
  607. var it0 = iters[0], it1 = iters[1], it2 = iters[2];
  608. assertEquals(0, it0.next());
  609. assertEquals(1, it0.next());
  610. assertEquals(0, it1.next());
  611. assertEquals(1, it1.next());
  612. assertEquals(2, it1.next());
  613. assertEquals(2, it0.next());
  614. assertEquals(0, it2.next());
  615. assertEquals(1, it2.next());
  616. assertEquals(2, it2.next());
  617. assertEquals(3, it0.next());
  618. assertEquals(3, it1.next());
  619. assertEquals(3, it2.next());
  620. }
  621. function testEnumerateNoStart() {
  622. var iter = goog.iter.enumerate('ABC'.split(''));
  623. assertArrayEquals([0, 'A'], iter.next());
  624. assertArrayEquals([1, 'B'], iter.next());
  625. assertArrayEquals([2, 'C'], iter.next());
  626. var ex = assertThrows(function() { iter.next() });
  627. assertEquals(goog.iter.StopIteration, ex);
  628. }
  629. function testEnumerateStart() {
  630. var iter = goog.iter.enumerate('DEF'.split(''), 3);
  631. assertArrayEquals([3, 'D'], iter.next());
  632. assertArrayEquals([4, 'E'], iter.next());
  633. assertArrayEquals([5, 'F'], iter.next());
  634. var ex = assertThrows(function() { iter.next() });
  635. assertEquals(goog.iter.StopIteration, ex);
  636. }
  637. function testLimitLess() {
  638. var iter = goog.iter.limit('ABCDEFG'.split(''), 3);
  639. assertEquals('ABC', goog.iter.join(iter, ''));
  640. }
  641. function testLimitGreater() {
  642. var iter = goog.iter.limit('ABCDEFG'.split(''), 10);
  643. assertEquals('ABCDEFG', goog.iter.join(iter, ''));
  644. }
  645. function testConsumeLess() {
  646. var iter = goog.iter.consume('ABCDEFG'.split(''), 3);
  647. assertEquals('DEFG', goog.iter.join(iter, ''));
  648. }
  649. function testConsumeGreater() {
  650. var iter = goog.iter.consume('ABCDEFG'.split(''), 10);
  651. var ex = assertThrows(function() { iter.next() });
  652. assertEquals(goog.iter.StopIteration, ex);
  653. }
  654. function testSliceStart() {
  655. var iter = goog.iter.slice('ABCDEFG'.split(''), 2);
  656. assertEquals('CDEFG', goog.iter.join(iter, ''));
  657. }
  658. function testSliceStop() {
  659. var iter = goog.iter.slice('ABCDEFG'.split(''), 2, 4);
  660. assertEquals('CD', goog.iter.join(iter, ''));
  661. }
  662. function testSliceStartStopEqual() {
  663. var iter = goog.iter.slice('ABCDEFG'.split(''), 1, 1);
  664. var ex = assertThrows(function() { iter.next() });
  665. assertEquals(goog.iter.StopIteration, ex);
  666. }
  667. function testSliceIterator() {
  668. var iter = goog.iter.slice(goog.iter.count(20), 0, 5);
  669. assertArrayEquals([20, 21, 22, 23, 24], goog.iter.toArray(iter));
  670. }
  671. function testSliceStartGreater() {
  672. var iter = goog.iter.slice('ABCDEFG'.split(''), 10);
  673. var ex = assertThrows(function() { iter.next() });
  674. assertEquals(goog.iter.StopIteration, ex);
  675. }
  676. function testPermutationsNoLength() {
  677. var iter = goog.iter.permutations(goog.iter.range(3));
  678. assertArrayEquals([0, 1, 2], iter.next());
  679. assertArrayEquals([0, 2, 1], iter.next());
  680. assertArrayEquals([1, 0, 2], iter.next());
  681. assertArrayEquals([1, 2, 0], iter.next());
  682. assertArrayEquals([2, 0, 1], iter.next());
  683. assertArrayEquals([2, 1, 0], iter.next());
  684. var ex = assertThrows(function() { iter.next() });
  685. assertEquals(goog.iter.StopIteration, ex);
  686. }
  687. function testPermutationsLength() {
  688. var iter = goog.iter.permutations('ABC'.split(''), 2);
  689. assertArrayEquals(['A', 'B'], iter.next());
  690. assertArrayEquals(['A', 'C'], iter.next());
  691. assertArrayEquals(['B', 'A'], iter.next());
  692. assertArrayEquals(['B', 'C'], iter.next());
  693. assertArrayEquals(['C', 'A'], iter.next());
  694. assertArrayEquals(['C', 'B'], iter.next());
  695. }
  696. function testCombinations() {
  697. var iter = goog.iter.combinations(goog.iter.range(4), 3);
  698. assertArrayEquals([0, 1, 2], iter.next());
  699. assertArrayEquals([0, 1, 3], iter.next());
  700. assertArrayEquals([0, 2, 3], iter.next());
  701. assertArrayEquals([1, 2, 3], iter.next());
  702. var ex = assertThrows(function() { iter.next() });
  703. assertEquals(goog.iter.StopIteration, ex);
  704. }
  705. function testCombinationsWithReplacement() {
  706. var iter = goog.iter.combinationsWithReplacement('ABC'.split(''), 2);
  707. assertArrayEquals(['A', 'A'], iter.next());
  708. assertArrayEquals(['A', 'B'], iter.next());
  709. assertArrayEquals(['A', 'C'], iter.next());
  710. assertArrayEquals(['B', 'B'], iter.next());
  711. assertArrayEquals(['B', 'C'], iter.next());
  712. assertArrayEquals(['C', 'C'], iter.next());
  713. var ex = assertThrows(function() { iter.next() });
  714. assertEquals(goog.iter.StopIteration, ex);
  715. }