array_test.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  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.arrayTest');
  15. goog.setTestOnly('goog.arrayTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.testing.PropertyReplacer');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.recordFunction');
  22. function testArrayLast() {
  23. assertEquals(goog.array.last([1, 2, 3]), 3);
  24. assertEquals(goog.array.last([1]), 1);
  25. assertUndefined(goog.array.last([]));
  26. }
  27. function testArrayLastWhenDeleted() {
  28. var a = [1, 2, 3];
  29. delete a[2];
  30. assertUndefined(goog.array.last(a));
  31. }
  32. function testArrayIndexOf() {
  33. assertEquals(goog.array.indexOf([0, 1, 2, 3], 1), 1);
  34. assertEquals(goog.array.indexOf([0, 1, 1, 1], 1), 1);
  35. assertEquals(goog.array.indexOf([0, 1, 2, 3], 4), -1);
  36. assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, 1), 1);
  37. assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, 2), -1);
  38. assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, -3), 1);
  39. assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, -2), -1);
  40. }
  41. function testArrayIndexOfOmitsDeleted() {
  42. var a = [0, 1, 2, 3];
  43. delete a[1];
  44. delete a[3];
  45. assertEquals(goog.array.indexOf(a, undefined), -1);
  46. }
  47. function testArrayIndexOfString() {
  48. assertEquals(goog.array.indexOf('abcd', 'd'), 3);
  49. assertEquals(goog.array.indexOf('abbb', 'b', 2), 2);
  50. assertEquals(goog.array.indexOf('abcd', 'e'), -1);
  51. assertEquals(goog.array.indexOf('abcd', 'cd'), -1);
  52. assertEquals(goog.array.indexOf('0123', 1), -1);
  53. }
  54. function testArrayLastIndexOf() {
  55. assertEquals(goog.array.lastIndexOf([0, 1, 2, 3], 1), 1);
  56. assertEquals(goog.array.lastIndexOf([0, 1, 1, 1], 1), 3);
  57. assertEquals(goog.array.lastIndexOf([0, 1, 1, 1], 1, 2), 2);
  58. }
  59. function testArrayLastIndexOfOmitsDeleted() {
  60. var a = [0, 1, 2, 3];
  61. delete a[1];
  62. delete a[3];
  63. assertEquals(goog.array.lastIndexOf(a, undefined), -1);
  64. }
  65. function testArrayLastIndexOfString() {
  66. assertEquals(goog.array.lastIndexOf('abcd', 'b'), 1);
  67. assertEquals(goog.array.lastIndexOf('abbb', 'b'), 3);
  68. assertEquals(goog.array.lastIndexOf('abbb', 'b', 2), 2);
  69. assertEquals(goog.array.lastIndexOf('abcd', 'cd'), -1);
  70. assertEquals(goog.array.lastIndexOf('0123', 1), -1);
  71. }
  72. function testArrayForEachBasic() {
  73. var s = '';
  74. var a = ['a', 'b', 'c', 'd'];
  75. goog.array.forEach(a, function(val, index, a2) {
  76. assertEquals(a, a2);
  77. assertEquals('Index is not a number', 'number', typeof index);
  78. s += val + index;
  79. });
  80. assertEquals('a0b1c2d3', s);
  81. }
  82. function testArrayForEachWithEmptyArray() {
  83. var a = new Array(100);
  84. goog.array.forEach(a, function(val, index, a2) {
  85. fail('The function should not be called since no values were assigned.');
  86. });
  87. }
  88. function testArrayForEachWithOnlySomeValuesAsigned() {
  89. var count = 0;
  90. var a = new Array(1000);
  91. a[100] = undefined;
  92. goog.array.forEach(a, function(val, index, a2) {
  93. assertEquals(100, index);
  94. count++;
  95. });
  96. assertEquals(
  97. 'Should only call function when a value of array was assigned.', 1,
  98. count);
  99. }
  100. function testArrayForEachWithArrayLikeObject() {
  101. var counter = goog.testing.recordFunction();
  102. var a = {'length': 1, '0': 0, '100': 100, '101': 102};
  103. goog.array.forEach(a, counter);
  104. assertEquals(
  105. 'Number of calls should not exceed the value of its length', 1,
  106. counter.getCallCount());
  107. }
  108. function testArrayForEachOmitsDeleted() {
  109. var s = '';
  110. var a = ['a', 'b', 'c', 'd'];
  111. delete a[1];
  112. delete a[3];
  113. goog.array.forEach(a, function(val, index, a2) {
  114. assertEquals(a, a2);
  115. assertEquals('number', typeof index);
  116. s += val + index;
  117. });
  118. assertEquals('a0c2', s);
  119. }
  120. function testArrayForEachScope() {
  121. var scope = {};
  122. var a = ['a', 'b', 'c', 'd'];
  123. goog.array.forEach(a, function(val, index, a2) {
  124. assertEquals(a, a2);
  125. assertEquals('number', typeof index);
  126. assertEquals(this, scope);
  127. }, scope);
  128. }
  129. function testArrayForEachRight() {
  130. var s = '';
  131. var a = ['a', 'b', 'c', 'd'];
  132. goog.array.forEachRight(a, function(val, index, a2) {
  133. assertEquals(a, a2);
  134. assertEquals('number', typeof index);
  135. s += val + String(index);
  136. });
  137. assertEquals('d3c2b1a0', s);
  138. }
  139. function testArrayForEachRightOmitsDeleted() {
  140. var s = '';
  141. var a = ['a', 'b', 'c', 'd'];
  142. delete a[1];
  143. delete a[3];
  144. goog.array.forEachRight(a, function(val, index, a2) {
  145. assertEquals(a, a2);
  146. assertEquals('number', typeof index);
  147. assertEquals('string', typeof val);
  148. s += val + String(index);
  149. });
  150. assertEquals('c2a0', s);
  151. }
  152. function testArrayFilter() {
  153. var a = [0, 1, 2, 3];
  154. a = goog.array.filter(a, function(val, index, a2) {
  155. assertEquals(a, a2);
  156. assertEquals('index is not a number', 'number', typeof index);
  157. return val > 1;
  158. });
  159. assertArrayEquals([2, 3], a);
  160. }
  161. function testArrayFilterOmitsDeleted() {
  162. var a = [0, 1, 2, 3];
  163. delete a[1];
  164. delete a[3];
  165. a = goog.array.filter(a, function(val, index, a2) {
  166. assertEquals(a, a2);
  167. assertEquals('number', typeof val);
  168. assertEquals('index is not a number', 'number', typeof index);
  169. return val > 1;
  170. });
  171. assertArrayEquals([2], a);
  172. }
  173. function testArrayFilterPreservesValues() {
  174. var a = [0, 1, 2, 3];
  175. a = goog.array.filter(a, function(val, index, a2) {
  176. assertEquals(a, a2);
  177. // sometimes functions might be evil and do something like this, but we
  178. // should still use the original values when returning the filtered array
  179. a2[index] = a2[index] - 1;
  180. return a2[index] >= 1;
  181. });
  182. assertArrayEquals([2, 3], a);
  183. }
  184. function testArrayMap() {
  185. var a = [0, 1, 2, 3];
  186. var result = goog.array.map(a, function(val, index, a2) {
  187. assertEquals(a, a2);
  188. assertEquals('index is not a number', 'number', typeof index);
  189. return val * val;
  190. });
  191. assertArrayEquals([0, 1, 4, 9], result);
  192. }
  193. function testArrayMapOmitsDeleted() {
  194. var a = [0, 1, 2, 3];
  195. delete a[1];
  196. delete a[3];
  197. var result = goog.array.map(a, function(val, index, a2) {
  198. assertEquals(a, a2);
  199. assertEquals('number', typeof val);
  200. assertEquals('index is not a number', 'number', typeof index);
  201. return val * val;
  202. });
  203. var expected = [0, 1, 4, 9];
  204. delete expected[1];
  205. delete expected[3];
  206. assertArrayEquals(expected, result);
  207. assertFalse('1' in result);
  208. assertFalse('3' in result);
  209. }
  210. function testArrayReduce() {
  211. var a = [0, 1, 2, 3];
  212. assertEquals(6, goog.array.reduce(a, function(rval, val, i, arr) {
  213. assertEquals('number', typeof i);
  214. assertEquals(a, arr);
  215. return rval + val;
  216. }, 0));
  217. var scope = {
  218. last: 0,
  219. testFn: function(r, v, i, arr) {
  220. assertEquals('number', typeof i);
  221. assertEquals(a, arr);
  222. var l = this.last;
  223. this.last = r + v;
  224. return this.last + l;
  225. }
  226. };
  227. assertEquals(10, goog.array.reduce(a, scope.testFn, 0, scope));
  228. }
  229. function testArrayReduceOmitDeleted() {
  230. var a = [0, 1, 2, 3];
  231. delete a[1];
  232. delete a[3];
  233. assertEquals(2, goog.array.reduce(a, function(rval, val, i, arr) {
  234. assertEquals('number', typeof i);
  235. assertEquals(a, arr);
  236. return rval + val;
  237. }, 0));
  238. var scope = {
  239. last: 0,
  240. testFn: function(r, v, i, arr) {
  241. assertEquals('number', typeof i);
  242. assertEquals(a, arr);
  243. var l = this.last;
  244. this.last = r + v;
  245. return this.last + l;
  246. }
  247. };
  248. assertEquals(2, goog.array.reduce(a, scope.testFn, 0, scope));
  249. }
  250. function testArrayReduceRight() {
  251. var a = [0, 1, 2, 3, 4];
  252. assertEquals('43210', goog.array.reduceRight(a, function(rval, val, i, arr) {
  253. assertEquals('number', typeof i);
  254. assertEquals(a, arr);
  255. return rval + val;
  256. }, ''));
  257. var scope = {
  258. last: '',
  259. testFn: function(r, v, i, arr) {
  260. assertEquals('number', typeof i);
  261. assertEquals(a, arr);
  262. var l = this.last;
  263. this.last = v;
  264. return r + v + l;
  265. }
  266. };
  267. a = ['a', 'b', 'c'];
  268. assertEquals('_cbcab', goog.array.reduceRight(a, scope.testFn, '_', scope));
  269. }
  270. function testArrayReduceRightOmitsDeleted() {
  271. var a = [0, 1, 2, 3, 4];
  272. delete a[1];
  273. delete a[4];
  274. assertEquals('320', goog.array.reduceRight(a, function(rval, val, i, arr) {
  275. assertEquals('number', typeof i);
  276. assertEquals(a, arr);
  277. return rval + val;
  278. }, ''));
  279. scope = {
  280. last: '',
  281. testFn: function(r, v, i, arr) {
  282. assertEquals('number', typeof i);
  283. assertEquals(a, arr);
  284. var l = this.last;
  285. this.last = v;
  286. return r + v + l;
  287. }
  288. };
  289. a = ['a', 'b', 'c', 'd'];
  290. delete a[1];
  291. delete a[3];
  292. assertEquals('_cac', goog.array.reduceRight(a, scope.testFn, '_', scope));
  293. }
  294. function testArrayFind() {
  295. var a = [0, 1, 2, 3];
  296. var b = goog.array.find(a, function(val, index, a2) {
  297. assertEquals(a, a2);
  298. assertEquals('index is not a number', 'number', typeof index);
  299. return val > 1;
  300. });
  301. assertEquals(2, b);
  302. b = goog.array.find(a, function(val, index, a2) {
  303. assertEquals(a, a2);
  304. assertEquals('index is not a number', 'number', typeof index);
  305. return val > 100;
  306. });
  307. assertNull(b);
  308. a = 'abCD';
  309. b = goog.array.find(a, function(val, index, a2) {
  310. assertEquals(a, a2);
  311. assertEquals('index is not a number', 'number', typeof index);
  312. return val >= 'A' && val <= 'Z';
  313. });
  314. assertEquals('C', b);
  315. a = 'abcd';
  316. b = goog.array.find(a, function(val, index, a2) {
  317. assertEquals(a, a2);
  318. assertEquals('index is not a number', 'number', typeof index);
  319. return val >= 'A' && val <= 'Z';
  320. });
  321. assertNull(b);
  322. }
  323. function testArrayFindOmitsDeleted() {
  324. var a = [0, 1, 2, 3];
  325. delete a[1];
  326. delete a[3];
  327. var b = goog.array.find(a, function(val, index, a2) {
  328. assertEquals(a, a2);
  329. assertEquals('index is not a number', 'number', typeof index);
  330. return val > 1;
  331. });
  332. assertEquals(2, b);
  333. b = goog.array.find(a, function(val, index, a2) {
  334. assertEquals(a, a2);
  335. assertEquals('index is not a number', 'number', typeof index);
  336. return val > 100;
  337. });
  338. assertNull(b);
  339. }
  340. function testArrayFindIndex() {
  341. var a = [0, 1, 2, 3];
  342. var b = goog.array.findIndex(a, function(val, index, a2) {
  343. assertEquals(a, a2);
  344. assertEquals('index is not a number', 'number', typeof index);
  345. return val > 1;
  346. });
  347. assertEquals(2, b);
  348. b = goog.array.findIndex(a, function(val, index, a2) {
  349. assertEquals(a, a2);
  350. assertEquals('index is not a number', 'number', typeof index);
  351. return val > 100;
  352. });
  353. assertEquals(-1, b);
  354. a = 'abCD';
  355. b = goog.array.findIndex(a, function(val, index, a2) {
  356. assertEquals(a, a2);
  357. assertEquals('index is not a number', 'number', typeof index);
  358. return val >= 'A' && val <= 'Z';
  359. });
  360. assertEquals(2, b);
  361. a = 'abcd';
  362. b = goog.array.findIndex(a, function(val, index, a2) {
  363. assertEquals(a, a2);
  364. assertEquals('index is not a number', 'number', typeof index);
  365. return val >= 'A' && val <= 'Z';
  366. });
  367. assertEquals(-1, b);
  368. }
  369. function testArrayFindIndexOmitsDeleted() {
  370. var a = [0, 1, 2, 3];
  371. delete a[1];
  372. delete a[3];
  373. var b = goog.array.findIndex(a, function(val, index, a2) {
  374. assertEquals(a, a2);
  375. assertEquals('index is not a number', 'number', typeof index);
  376. return val > 1;
  377. });
  378. assertEquals(2, b);
  379. b = goog.array.findIndex(a, function(val, index, a2) {
  380. assertEquals(a, a2);
  381. assertEquals('index is not a number', 'number', typeof index);
  382. return val > 100;
  383. });
  384. assertEquals(-1, b);
  385. }
  386. function testArrayFindRight() {
  387. var a = [0, 1, 2, 3];
  388. var b = goog.array.findRight(a, function(val, index, a2) {
  389. assertEquals(a, a2);
  390. assertEquals('index is not a number', 'number', typeof index);
  391. return val < 3;
  392. });
  393. assertEquals(2, b);
  394. b = goog.array.findRight(a, function(val, index, a2) {
  395. assertEquals(a, a2);
  396. assertEquals('index is not a number', 'number', typeof index);
  397. return val > 100;
  398. });
  399. assertNull(b);
  400. }
  401. function testArrayFindRightOmitsDeleted() {
  402. var a = [0, 1, 2, 3];
  403. delete a[1];
  404. delete a[3];
  405. var b = goog.array.findRight(a, function(val, index, a2) {
  406. assertEquals(a, a2);
  407. assertEquals('index is not a number', 'number', typeof index);
  408. return val < 3;
  409. });
  410. assertEquals(2, b);
  411. b = goog.array.findRight(a, function(val, index, a2) {
  412. assertEquals(a, a2);
  413. assertEquals('index is not a number', 'number', typeof index);
  414. return val > 100;
  415. });
  416. assertNull(b);
  417. }
  418. function testArrayFindIndexRight() {
  419. var a = [0, 1, 2, 3];
  420. var b = goog.array.findIndexRight(a, function(val, index, a2) {
  421. assertEquals(a, a2);
  422. assertEquals('index is not a number', 'number', typeof index);
  423. return val < 3;
  424. });
  425. assertEquals(2, b);
  426. b = goog.array.findIndexRight(a, function(val, index, a2) {
  427. assertEquals(a, a2);
  428. assertEquals('index is not a number', 'number', typeof index);
  429. return val > 100;
  430. });
  431. assertEquals(-1, b);
  432. a = 'abCD';
  433. b = goog.array.findIndexRight(a, function(val, index, a2) {
  434. assertEquals(a, a2);
  435. assertEquals('index is not a number', 'number', typeof index);
  436. return val >= 'a' && val <= 'z';
  437. });
  438. assertEquals(1, b);
  439. a = 'abcd';
  440. b = goog.array.findIndexRight(a, function(val, index, a2) {
  441. assertEquals(a, a2);
  442. assertEquals('index is not a number', 'number', typeof index);
  443. return val >= 'A' && val <= 'Z';
  444. });
  445. assertEquals(-1, b);
  446. }
  447. function testArrayFindIndexRightOmitsDeleted() {
  448. var a = [0, 1, 2, 3];
  449. delete a[1];
  450. delete a[3];
  451. var b = goog.array.findIndexRight(a, function(val, index, a2) {
  452. assertEquals(a, a2);
  453. assertEquals('index is not a number', 'number', typeof index);
  454. return val < 3;
  455. });
  456. assertEquals(2, b);
  457. b = goog.array.findIndexRight(a, function(val, index, a2) {
  458. assertEquals(a, a2);
  459. assertEquals('index is not a number', 'number', typeof index);
  460. return val > 100;
  461. });
  462. assertEquals(-1, b);
  463. }
  464. function testArraySome() {
  465. var a = [0, 1, 2, 3];
  466. var b = goog.array.some(a, function(val, index, a2) {
  467. assertEquals(a, a2);
  468. assertEquals('index is not a number', 'number', typeof index);
  469. return val > 1;
  470. });
  471. assertTrue(b);
  472. b = goog.array.some(a, function(val, index, a2) {
  473. assertEquals(a, a2);
  474. assertEquals('index is not a number', 'number', typeof index);
  475. return val > 100;
  476. });
  477. assertFalse(b);
  478. }
  479. function testArraySomeOmitsDeleted() {
  480. var a = [0, 1, 2, 3];
  481. delete a[1];
  482. delete a[3];
  483. var b = goog.array.some(a, function(val, index, a2) {
  484. assertEquals(a, a2);
  485. assertEquals('number', typeof val);
  486. assertEquals('index is not a number', 'number', typeof index);
  487. return val > 1;
  488. });
  489. assertTrue(b);
  490. b = goog.array.some(a, function(val, index, a2) {
  491. assertEquals(a, a2);
  492. assertEquals('number', typeof val);
  493. assertEquals('index is not a number', 'number', typeof index);
  494. return val > 100;
  495. });
  496. assertFalse(b);
  497. }
  498. function testArrayEvery() {
  499. var a = [0, 1, 2, 3];
  500. var b = goog.array.every(a, function(val, index, a2) {
  501. assertEquals(a, a2);
  502. assertEquals('index is not a number', 'number', typeof index);
  503. return val >= 0;
  504. });
  505. assertTrue(b);
  506. b = goog.array.every(a, function(val, index, a2) {
  507. assertEquals(a, a2);
  508. assertEquals('index is not a number', 'number', typeof index);
  509. return val > 1;
  510. });
  511. assertFalse(b);
  512. }
  513. function testArrayEveryOmitsDeleted() {
  514. var a = [0, 1, 2, 3];
  515. delete a[1];
  516. delete a[3];
  517. var b = goog.array.every(a, function(val, index, a2) {
  518. assertEquals(a, a2);
  519. assertEquals('number', typeof val);
  520. assertEquals('index is not a number', 'number', typeof index);
  521. return val >= 0;
  522. });
  523. assertTrue(b);
  524. b = goog.array.every(a, function(val, index, a2) {
  525. assertEquals(a, a2);
  526. assertEquals('number', typeof val);
  527. assertEquals('index is not a number', 'number', typeof index);
  528. return val > 1;
  529. });
  530. assertFalse(b);
  531. }
  532. function testArrayCount() {
  533. var a = [0, 1, 2, 3, 4];
  534. var context = {};
  535. assertEquals(3, goog.array.count(a, function(element, index, array) {
  536. assertTrue(goog.isNumber(index));
  537. assertEquals(a, array);
  538. assertEquals(context, this);
  539. return element % 2 == 0;
  540. }, context));
  541. delete a[2];
  542. assertEquals('deleted element is ignored', 4, goog.array.count(a, function() {
  543. return true;
  544. }));
  545. }
  546. function testArrayContains() {
  547. var a = [0, 1, 2, 3];
  548. assertTrue('contain, Should contain 3', goog.array.contains(a, 3));
  549. assertFalse('contain, Should not contain 4', goog.array.contains(a, 4));
  550. var s = 'abcd';
  551. assertTrue('contain, Should contain d', goog.array.contains(s, 'd'));
  552. assertFalse('contain, Should not contain e', goog.array.contains(s, 'e'));
  553. }
  554. function testArrayContainsOmitsDeleted() {
  555. var a = [0, 1, 2, 3];
  556. delete a[1];
  557. delete a[3];
  558. assertFalse(
  559. 'should not contain undefined', goog.array.contains(a, undefined));
  560. }
  561. function testArrayInsert() {
  562. var a = [0, 1, 2, 3];
  563. goog.array.insert(a, 4);
  564. assertEquals('insert, Should append 4', a[4], 4);
  565. goog.array.insert(a, 3);
  566. assertEquals('insert, Should not append 3', a.length, 5);
  567. assertNotEquals('insert, Should not append 3', a[a.length - 1], 3);
  568. }
  569. function testArrayInsertAt() {
  570. var a = [0, 1, 2, 3];
  571. goog.array.insertAt(a, 4, 2);
  572. assertArrayEquals('insertAt, insert in middle', [0, 1, 4, 2, 3], a);
  573. goog.array.insertAt(a, 5, 10);
  574. assertArrayEquals(
  575. 'insertAt, too large value should append', [0, 1, 4, 2, 3, 5], a);
  576. goog.array.insertAt(a, 6);
  577. assertArrayEquals(
  578. 'insertAt, null/undefined value should insert at 0',
  579. [6, 0, 1, 4, 2, 3, 5], a);
  580. goog.array.insertAt(a, 7, -2);
  581. assertArrayEquals(
  582. 'insertAt, negative values start from end', [6, 0, 1, 4, 2, 7, 3, 5], a);
  583. }
  584. function testArrayInsertArrayAt() {
  585. var a = [2, 5];
  586. goog.array.insertArrayAt(a, [3, 4], 1);
  587. assertArrayEquals('insertArrayAt, insert in middle', [2, 3, 4, 5], a);
  588. goog.array.insertArrayAt(a, [0, 1], 0);
  589. assertArrayEquals(
  590. 'insertArrayAt, insert at beginning', [0, 1, 2, 3, 4, 5], a);
  591. goog.array.insertArrayAt(a, [6, 7], 6);
  592. assertArrayEquals(
  593. 'insertArrayAt, insert at end', [0, 1, 2, 3, 4, 5, 6, 7], a);
  594. goog.array.insertArrayAt(a, ['x'], 4);
  595. assertArrayEquals(
  596. 'insertArrayAt, insert one element', [0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
  597. goog.array.insertArrayAt(a, [], 4);
  598. assertArrayEquals(
  599. 'insertArrayAt, insert 0 elements', [0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
  600. goog.array.insertArrayAt(a, ['y', 'z']);
  601. assertArrayEquals(
  602. 'insertArrayAt, undefined value should insert at 0',
  603. ['y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
  604. goog.array.insertArrayAt(a, ['a'], null);
  605. assertArrayEquals(
  606. 'insertArrayAt, null value should insert at 0',
  607. ['a', 'y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
  608. goog.array.insertArrayAt(a, ['b'], 100);
  609. assertArrayEquals(
  610. 'insertArrayAt, too large value should append',
  611. ['a', 'y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 7, 'b'], a);
  612. goog.array.insertArrayAt(a, ['c', 'd'], -2);
  613. assertArrayEquals(
  614. 'insertArrayAt, negative values start from end',
  615. ['a', 'y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 'c', 'd', 7, 'b'], a);
  616. }
  617. function testArrayInsertBefore() {
  618. var a = ['a', 'b', 'c', 'd'];
  619. goog.array.insertBefore(a, 'e', 'b');
  620. assertArrayEquals(
  621. 'insertBefore, with existing element', ['a', 'e', 'b', 'c', 'd'], a);
  622. goog.array.insertBefore(a, 'f', 'x');
  623. assertArrayEquals(
  624. 'insertBefore, with non existing element', ['a', 'e', 'b', 'c', 'd', 'f'],
  625. a);
  626. }
  627. function testArrayRemove() {
  628. var a = ['a', 'b', 'c', 'd'];
  629. goog.array.remove(a, 'c');
  630. assertArrayEquals('remove, remove existing element', ['a', 'b', 'd'], a);
  631. goog.array.remove(a, 'x');
  632. assertArrayEquals('remove, remove non existing element', ['a', 'b', 'd'], a);
  633. }
  634. function testArrayRemoveLast() {
  635. var a = ['c', 'a', 'b', 'c', 'd', 'a'];
  636. goog.array.removeLast(a, 'c');
  637. var temp = ['c', 'a', 'b', 'd', 'a'];
  638. assertArrayEquals('remove, remove existing element', temp, a);
  639. goog.array.removeLast(a, 'a');
  640. temp = ['c', 'a', 'b', 'd'];
  641. assertArrayEquals('remove, remove existing element', temp, a);
  642. goog.array.removeLast(a, 'y');
  643. temp = ['c', 'a', 'b', 'd'];
  644. assertArrayEquals('remove, remove non existing element', temp, a);
  645. }
  646. function testArrayRemoveAt() {
  647. var a = [0, 1, 2, 3];
  648. goog.array.removeAt(a, 2);
  649. assertArrayEquals('removeAt, remove existing index', [0, 1, 3], a);
  650. a = [0, 1, 2, 3];
  651. goog.array.removeAt(a, 10);
  652. assertArrayEquals('removeAt, remove non existing index', [0, 1, 2, 3], a);
  653. a = [0, 1, 2, 3];
  654. goog.array.removeAt(a, -2);
  655. assertArrayEquals('removeAt, remove with negative index', [0, 1, 3], a);
  656. }
  657. function testArrayRemoveIf() {
  658. var a = [0, 1, 2, 3];
  659. var b = goog.array.removeIf(a, function(val, index, a2) {
  660. assertEquals(a, a2);
  661. assertEquals('index is not a number', 'number', typeof index);
  662. return val > 1;
  663. });
  664. assertArrayEquals('removeIf, remove existing element', [0, 1, 3], a);
  665. a = [0, 1, 2, 3];
  666. var b = goog.array.removeIf(a, function(val, index, a2) {
  667. assertEquals(a, a2);
  668. assertEquals('index is not a number', 'number', typeof index);
  669. return val > 100;
  670. });
  671. assertArrayEquals('removeIf, remove non-existing element', [0, 1, 2, 3], a);
  672. }
  673. function testArrayClone() {
  674. var a = [0, 1, 2, 3];
  675. var a2 = goog.array.clone(a);
  676. assertArrayEquals('clone, should be equal', a, a2);
  677. var b = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
  678. var b2 = goog.array.clone(b);
  679. for (var i = 0; i < b.length; i++) {
  680. assertEquals('clone, should be equal', b[i], b2[i]);
  681. }
  682. }
  683. function testToArray() {
  684. var a = [0, 1, 2, 3];
  685. var a2 = goog.array.toArray(a);
  686. assertArrayEquals('toArray, should be equal', a, a2);
  687. var b = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
  688. var b2 = goog.array.toArray(b);
  689. for (var i = 0; i < b.length; i++) {
  690. assertEquals('toArray, should be equal', b[i], b2[i]);
  691. }
  692. }
  693. function testToArrayOnNonArrayLike() {
  694. var nonArrayLike = {};
  695. assertArrayEquals(
  696. 'toArray on non ArrayLike should return an empty array', [],
  697. goog.array.toArray(nonArrayLike));
  698. var nonArrayLike2 = {length: 'hello world'};
  699. assertArrayEquals(
  700. 'toArray on non ArrayLike should return an empty array', [],
  701. goog.array.toArray(nonArrayLike2));
  702. }
  703. function testExtend() {
  704. var a = [0, 1];
  705. goog.array.extend(a, [2, 3]);
  706. var a2 = [0, 1, 2, 3];
  707. assertArrayEquals('extend, should be equal', a, a2);
  708. var b = [0, 1];
  709. goog.array.extend(b, 2);
  710. var b2 = [0, 1, 2];
  711. assertArrayEquals('extend, should be equal', b, b2);
  712. a = [0, 1];
  713. goog.array.extend(a, [2, 3], [4, 5]);
  714. a2 = [0, 1, 2, 3, 4, 5];
  715. assertArrayEquals('extend, should be equal', a, a2);
  716. b = [0, 1];
  717. goog.array.extend(b, 2, 3);
  718. b2 = [0, 1, 2, 3];
  719. assertArrayEquals('extend, should be equal', b, b2);
  720. var c = [0, 1];
  721. goog.array.extend(c, 2, [3, 4], 5, [6]);
  722. var c2 = [0, 1, 2, 3, 4, 5, 6];
  723. assertArrayEquals('extend, should be equal', c, c2);
  724. var d = [0, 1];
  725. var arrayLikeObject = {0: 2, 1: 3, length: 2};
  726. goog.array.extend(d, arrayLikeObject);
  727. var d2 = [0, 1, 2, 3];
  728. assertArrayEquals('extend, should be equal', d, d2);
  729. var e = [0, 1];
  730. var emptyArrayLikeObject = {length: 0};
  731. goog.array.extend(e, emptyArrayLikeObject);
  732. assertArrayEquals('extend, should be equal', e, e);
  733. var f = [0, 1];
  734. var length3ArrayLikeObject = {0: 2, 1: 4, 2: 8, length: 3};
  735. goog.array.extend(f, length3ArrayLikeObject, length3ArrayLikeObject);
  736. var f2 = [0, 1, 2, 4, 8, 2, 4, 8];
  737. assertArrayEquals('extend, should be equal', f2, f);
  738. var result = [];
  739. var i = 1000000;
  740. var bigArray = Array(i);
  741. while (i--) {
  742. bigArray[i] = i;
  743. }
  744. goog.array.extend(result, bigArray);
  745. assertArrayEquals(bigArray, result);
  746. }
  747. function testExtendWithArguments() {
  748. function f() { return arguments; }
  749. var a = [0];
  750. var a2 = [0, 1, 2, 3, 4, 5];
  751. goog.array.extend(a, f(1, 2, 3), f(4, 5));
  752. assertArrayEquals('extend, should be equal', a, a2);
  753. }
  754. function testExtendWithQuerySelector() {
  755. var a = [0];
  756. var d = goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.DIV, 'foo');
  757. goog.array.extend(a, d);
  758. assertEquals(2, a.length);
  759. }
  760. function testArraySplice() {
  761. var a = [0, 1, 2, 3];
  762. goog.array.splice(a, 1, 0, 4);
  763. assertArrayEquals([0, 4, 1, 2, 3], a);
  764. goog.array.splice(a, 1, 1, 5);
  765. assertArrayEquals([0, 5, 1, 2, 3], a);
  766. goog.array.splice(a, 1, 1);
  767. assertArrayEquals([0, 1, 2, 3], a);
  768. // var args
  769. goog.array.splice(a, 1, 1, 4, 5, 6);
  770. assertArrayEquals([0, 4, 5, 6, 2, 3], a);
  771. }
  772. function testArraySlice() {
  773. var a = [0, 1, 2, 3];
  774. a = goog.array.slice(a, 1, 3);
  775. assertArrayEquals([1, 2], a);
  776. a = [0, 1, 2, 3];
  777. a = goog.array.slice(a, 1, 6);
  778. assertArrayEquals('slice, with too large end', [1, 2, 3], a);
  779. a = [0, 1, 2, 3];
  780. a = goog.array.slice(a, 1, -1);
  781. assertArrayEquals('slice, with negative end', [1, 2], a);
  782. a = [0, 1, 2, 3];
  783. a = goog.array.slice(a, -2, 3);
  784. assertArrayEquals('slice, with negative start', [2], a);
  785. }
  786. function assertRemovedDuplicates(expected, original) {
  787. var tempArr = goog.array.clone(original);
  788. goog.array.removeDuplicates(tempArr);
  789. assertArrayEquals(expected, tempArr);
  790. }
  791. function testRemoveDuplicates() {
  792. assertRemovedDuplicates([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]);
  793. assertRemovedDuplicates(
  794. [9, 4, 2, 1, 3, 6, 0, -9], [9, 4, 2, 4, 4, 2, 9, 1, 3, 6, 0, -9]);
  795. assertRemovedDuplicates(
  796. ['four', 'one', 'two', 'three', 'THREE'],
  797. ['four', 'one', 'two', 'one', 'three', 'THREE', 'four', 'two']);
  798. assertRemovedDuplicates([], []);
  799. assertRemovedDuplicates(
  800. ['abc', 'hasOwnProperty', 'toString'],
  801. ['abc', 'hasOwnProperty', 'toString', 'abc']);
  802. var o1 = {}, o2 = {}, o3 = {}, o4 = {};
  803. assertRemovedDuplicates([o1, o2, o3, o4], [o1, o1, o2, o3, o2, o4]);
  804. // Mixed object types.
  805. assertRemovedDuplicates([1, '1', 2, '2'], [1, '1', 2, '2']);
  806. assertRemovedDuplicates(
  807. [true, 'true', false, 'false'], [true, 'true', false, 'false']);
  808. assertRemovedDuplicates(['foo'], [String('foo'), 'foo']);
  809. assertRemovedDuplicates([12], [Number(12), 12]);
  810. var obj = {};
  811. var uid = goog.getUid(obj);
  812. assertRemovedDuplicates([obj, uid], [obj, uid]);
  813. }
  814. function testRemoveDuplicates_customHashFn() {
  815. var object1 = {key: 'foo'};
  816. var object2 = {key: 'bar'};
  817. var dupeObject = {key: 'foo'};
  818. var array = [object1, object2, dupeObject, 'bar'];
  819. var hashFn = function(object) {
  820. return goog.isObject(object) ? object.key :
  821. (typeof object).charAt(0) + object;
  822. };
  823. goog.array.removeDuplicates(array, /* opt_rv */ undefined, hashFn);
  824. assertArrayEquals([object1, object2, 'bar'], array);
  825. }
  826. function testBinaryInsertRemove() {
  827. var makeChecker = function(array, fn, opt_compareFn) {
  828. return function(value, expectResult, expectArray) {
  829. var result = fn(array, value, opt_compareFn);
  830. assertEquals(expectResult, result);
  831. assertArrayEquals(expectArray, array);
  832. }
  833. };
  834. var a = [];
  835. var check = makeChecker(a, goog.array.binaryInsert);
  836. check(3, true, [3]);
  837. check(3, false, [3]);
  838. check(1, true, [1, 3]);
  839. check(5, true, [1, 3, 5]);
  840. check(2, true, [1, 2, 3, 5]);
  841. check(2, false, [1, 2, 3, 5]);
  842. check = makeChecker(a, goog.array.binaryRemove);
  843. check(0, false, [1, 2, 3, 5]);
  844. check(3, true, [1, 2, 5]);
  845. check(1, true, [2, 5]);
  846. check(5, true, [2]);
  847. check(2, true, []);
  848. check(2, false, []);
  849. // test with custom comparison function, which reverse orders numbers
  850. var revNumCompare = function(a, b) { return b - a; };
  851. check = makeChecker(a, goog.array.binaryInsert, revNumCompare);
  852. check(3, true, [3]);
  853. check(3, false, [3]);
  854. check(1, true, [3, 1]);
  855. check(5, true, [5, 3, 1]);
  856. check(2, true, [5, 3, 2, 1]);
  857. check(2, false, [5, 3, 2, 1]);
  858. check = makeChecker(a, goog.array.binaryRemove, revNumCompare);
  859. check(0, false, [5, 3, 2, 1]);
  860. check(3, true, [5, 2, 1]);
  861. check(1, true, [5, 2]);
  862. check(5, true, [2]);
  863. check(2, true, []);
  864. check(2, false, []);
  865. }
  866. function testBinarySearch() {
  867. var insertionPoint = function(position) { return -(position + 1) };
  868. var pos;
  869. // test default comparison on array of String(s)
  870. var a = [
  871. '1000', '9', 'AB', 'ABC', 'ABCABC', 'ABD', 'ABDA', 'B',
  872. 'B', 'B', 'C', 'CA', 'CC', 'ZZZ', 'ab', 'abc',
  873. 'abcabc', 'abd', 'abda', 'b', 'c', 'ca', 'cc', 'zzz'
  874. ];
  875. assertEquals(
  876. '\'1000\' should be found at index 0', 0,
  877. goog.array.binarySearch(a, '1000'));
  878. assertEquals(
  879. '\'zzz\' should be found at index ' + (a.length - 1), a.length - 1,
  880. goog.array.binarySearch(a, 'zzz'));
  881. assertEquals(
  882. '\'C\' should be found at index 10', 10, goog.array.binarySearch(a, 'C'));
  883. pos = goog.array.binarySearch(a, 'B');
  884. assertTrue(
  885. '\'B\' should be found at index 7 || 8 || 9',
  886. pos == 7 || pos == 8 || pos == 9);
  887. pos = goog.array.binarySearch(a, '100');
  888. assertTrue('\'100\' should not be found', pos < 0);
  889. assertEquals(
  890. '\'100\' should have an insertion point of 0', 0, insertionPoint(pos));
  891. pos = goog.array.binarySearch(a, 'zzz0');
  892. assertTrue('\'zzz0\' should not be found', pos < 0);
  893. assertEquals(
  894. '\'zzz0\' should have an insertion point of ' + (a.length), a.length,
  895. insertionPoint(pos));
  896. pos = goog.array.binarySearch(a, 'BA');
  897. assertTrue('\'BA\' should not be found', pos < 0);
  898. assertEquals(
  899. '\'BA\' should have an insertion point of 10', 10, insertionPoint(pos));
  900. // test 0 length array with default comparison
  901. var b = [];
  902. pos = goog.array.binarySearch(b, 'a');
  903. assertTrue('\'a\' should not be found', pos < 0);
  904. assertEquals(
  905. '\'a\' should have an insertion point of 0', 0, insertionPoint(pos));
  906. // test single element array with default lexiographical comparison
  907. var c = ['only item'];
  908. assertEquals(
  909. '\'only item\' should be found at index 0', 0,
  910. goog.array.binarySearch(c, 'only item'));
  911. pos = goog.array.binarySearch(c, 'a');
  912. assertTrue('\'a\' should not be found', pos < 0);
  913. assertEquals(
  914. '\'a\' should have an insertion point of 0', 0, insertionPoint(pos));
  915. pos = goog.array.binarySearch(c, 'z');
  916. assertTrue('\'z\' should not be found', pos < 0);
  917. assertEquals(
  918. '\'z\' should have an insertion point of 1', 1, insertionPoint(pos));
  919. // test default comparison on array of Number(s)
  920. var d = [
  921. -897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0, 0.31255, 5,
  922. 142.88888708, 334, 342, 453, 54254
  923. ];
  924. assertEquals(
  925. '-897123.9 should be found at index 0', 0,
  926. goog.array.binarySearch(d, -897123.9));
  927. assertEquals(
  928. '54254 should be found at index ' + (a.length - 1), d.length - 1,
  929. goog.array.binarySearch(d, 54254));
  930. assertEquals(
  931. '-3 should be found at index 5', 5, goog.array.binarySearch(d, -3));
  932. pos = goog.array.binarySearch(d, 0);
  933. assertTrue(
  934. '0 should be found at index 6 || 7 || 8',
  935. pos == 6 || pos == 7 || pos == 8);
  936. pos = goog.array.binarySearch(d, -900000);
  937. assertTrue('-900000 should not be found', pos < 0);
  938. assertEquals(
  939. '-900000 should have an insertion point of 0', 0, insertionPoint(pos));
  940. pos = goog.array.binarySearch(d, 54255);
  941. assertTrue('54255 should not be found', pos < 0);
  942. assertEquals(
  943. '54255 should have an insertion point of ' + (d.length), d.length,
  944. insertionPoint(pos));
  945. pos = goog.array.binarySearch(d, 1.1);
  946. assertTrue('1.1 should not be found', pos < 0);
  947. assertEquals(
  948. '1.1 should have an insertion point of 10', 10, insertionPoint(pos));
  949. // test with custom comparison function, which reverse orders numbers
  950. var revNumCompare = function(a, b) { return b - a; };
  951. var e = [
  952. 54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, 0, 0, -3, -9, -324,
  953. -1321.3124, -321434.58758, -897123.9
  954. ];
  955. assertEquals(
  956. '54254 should be found at index 0', 0,
  957. goog.array.binarySearch(e, 54254, revNumCompare));
  958. assertEquals(
  959. '-897123.9 should be found at index ' + (e.length - 1), e.length - 1,
  960. goog.array.binarySearch(e, -897123.9, revNumCompare));
  961. assertEquals(
  962. '-3 should be found at index 10', 10,
  963. goog.array.binarySearch(e, -3, revNumCompare));
  964. pos = goog.array.binarySearch(e, 0, revNumCompare);
  965. assertTrue(
  966. '0 should be found at index 7 || 8 || 9',
  967. pos == 7 || pos == 8 || pos == 9);
  968. pos = goog.array.binarySearch(e, 54254.1, revNumCompare);
  969. assertTrue('54254.1 should not be found', pos < 0);
  970. assertEquals(
  971. '54254.1 should have an insertion point of 0', 0, insertionPoint(pos));
  972. pos = goog.array.binarySearch(e, -897124, revNumCompare);
  973. assertTrue('-897124 should not be found', pos < 0);
  974. assertEquals(
  975. '-897124 should have an insertion point of ' + (e.length), e.length,
  976. insertionPoint(pos));
  977. pos = goog.array.binarySearch(e, 1.1, revNumCompare);
  978. assertTrue('1.1 should not be found', pos < 0);
  979. assertEquals(
  980. '1.1 should have an insertion point of 6', 6, insertionPoint(pos));
  981. // test 0 length array with custom comparison function
  982. var f = [];
  983. pos = goog.array.binarySearch(f, 0, revNumCompare);
  984. assertTrue('0 should not be found', pos < 0);
  985. assertEquals('0 should have an insertion point of 0', 0, insertionPoint(pos));
  986. // test single element array with custom comparison function
  987. var g = [1];
  988. assertEquals(
  989. '1 should be found at index 0', 0,
  990. goog.array.binarySearch(g, 1, revNumCompare));
  991. pos = goog.array.binarySearch(g, 2, revNumCompare);
  992. assertTrue('2 should not be found', pos < 0);
  993. assertEquals('2 should have an insertion point of 0', 0, insertionPoint(pos));
  994. pos = goog.array.binarySearch(g, 0, revNumCompare);
  995. assertTrue('0 should not be found', pos < 0);
  996. assertEquals('0 should have an insertion point of 1', 1, insertionPoint(pos));
  997. assertEquals(
  998. 'binarySearch should find the index of the first 0', 0,
  999. goog.array.binarySearch([0, 0, 1], 0));
  1000. assertEquals(
  1001. 'binarySearch should find the index of the first 1', 1,
  1002. goog.array.binarySearch([0, 1, 1], 1));
  1003. }
  1004. function testBinarySearchPerformance() {
  1005. // Ensure that Array#slice, Function#apply and Function#call are not called
  1006. // from within binarySearch, since they have performance implications in IE.
  1007. var propertyReplacer = new goog.testing.PropertyReplacer();
  1008. propertyReplacer.replace(Array.prototype, 'slice', function() {
  1009. fail('Should not call Array#slice from binary search.');
  1010. });
  1011. propertyReplacer.replace(Function.prototype, 'apply', function() {
  1012. fail('Should not call Function#apply from binary search.');
  1013. });
  1014. propertyReplacer.replace(Function.prototype, 'call', function() {
  1015. fail('Should not call Function#call from binary search.');
  1016. });
  1017. try {
  1018. var array = [1, 5, 7, 11, 13, 16, 19, 24, 28, 31, 33, 36, 40, 50, 52, 55];
  1019. // Test with the default comparison function.
  1020. goog.array.binarySearch(array, 48);
  1021. // Test with a custom comparison function.
  1022. goog.array.binarySearch(
  1023. array, 13, function(a, b) { return a > b ? 1 : a < b ? -1 : 0; });
  1024. } finally {
  1025. // The test runner uses Function.prototype.apply to call tearDown in the
  1026. // global context so it has to be reset here.
  1027. propertyReplacer.reset();
  1028. }
  1029. }
  1030. function testBinarySelect() {
  1031. var insertionPoint = function(position) { return -(position + 1) };
  1032. var numbers = [
  1033. -897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0, 0.31255, 5,
  1034. 142.88888708, 334, 342, 453, 54254
  1035. ];
  1036. var objects = goog.array.map(numbers, function(n) { return {n: n}; });
  1037. function makeEvaluator(target) {
  1038. return function(obj, i, arr) {
  1039. assertEquals(objects, arr);
  1040. assertEquals(obj, arr[i]);
  1041. return target - obj.n;
  1042. };
  1043. }
  1044. assertEquals(
  1045. '{n:-897123.9} should be found at index 0', 0,
  1046. goog.array.binarySelect(objects, makeEvaluator(-897123.9)));
  1047. assertEquals(
  1048. '{n:54254} should be found at index ' + (objects.length - 1),
  1049. objects.length - 1,
  1050. goog.array.binarySelect(objects, makeEvaluator(54254)));
  1051. assertEquals(
  1052. '{n:-3} should be found at index 5', 5,
  1053. goog.array.binarySelect(objects, makeEvaluator(-3)));
  1054. pos = goog.array.binarySelect(objects, makeEvaluator(0));
  1055. assertTrue(
  1056. '{n:0} should be found at index 6 || 7 || 8',
  1057. pos == 6 || pos == 7 || pos == 8);
  1058. pos = goog.array.binarySelect(objects, makeEvaluator(-900000));
  1059. assertTrue('{n:-900000} should not be found', pos < 0);
  1060. assertEquals(
  1061. '{n:-900000} should have an insertion point of 0', 0,
  1062. insertionPoint(pos));
  1063. pos = goog.array.binarySelect(objects, makeEvaluator('54255'));
  1064. assertTrue('{n:54255} should not be found', pos < 0);
  1065. assertEquals(
  1066. '{n:54255} should have an insertion point of ' + (objects.length),
  1067. objects.length, insertionPoint(pos));
  1068. pos = goog.array.binarySelect(objects, makeEvaluator(1.1));
  1069. assertTrue('{n:1.1} should not be found', pos < 0);
  1070. assertEquals(
  1071. '{n:1.1} should have an insertion point of 10', 10, insertionPoint(pos));
  1072. }
  1073. function testArrayEquals() {
  1074. // Test argument types.
  1075. assertFalse('array == not array', goog.array.equals([], null));
  1076. assertFalse('not array == array', goog.array.equals(null, []));
  1077. assertFalse('not array == not array', goog.array.equals(null, null));
  1078. // Test with default comparison function.
  1079. assertTrue('[] == []', goog.array.equals([], []));
  1080. assertTrue('[1] == [1]', goog.array.equals([1], [1]));
  1081. assertTrue('["1"] == ["1"]', goog.array.equals(['1'], ['1']));
  1082. assertFalse('[1] == ["1"]', goog.array.equals([1], ['1']));
  1083. assertTrue('[null] == [null]', goog.array.equals([null], [null]));
  1084. assertFalse('[null] == [undefined]', goog.array.equals([null], [undefined]));
  1085. assertTrue('[1, 2] == [1, 2]', goog.array.equals([1, 2], [1, 2]));
  1086. assertFalse('[1, 2] == [2, 1]', goog.array.equals([1, 2], [2, 1]));
  1087. assertFalse('[1, 2] == [1]', goog.array.equals([1, 2], [1]));
  1088. assertFalse('[1] == [1, 2]', goog.array.equals([1], [1, 2]));
  1089. assertFalse('[{}] == [{}]', goog.array.equals([{}], [{}]));
  1090. // Test with custom comparison function.
  1091. var cmp = function(a, b) { return typeof a == typeof b; };
  1092. assertTrue('[] cmp []', goog.array.equals([], [], cmp));
  1093. assertTrue('[1] cmp [1]', goog.array.equals([1], [1], cmp));
  1094. assertTrue('[1] cmp [2]', goog.array.equals([1], [2], cmp));
  1095. assertTrue('["1"] cmp ["1"]', goog.array.equals(['1'], ['1'], cmp));
  1096. assertTrue('["1"] cmp ["2"]', goog.array.equals(['1'], ['2'], cmp));
  1097. assertFalse('[1] cmp ["1"]', goog.array.equals([1], ['1'], cmp));
  1098. assertTrue('[1, 2] cmp [3, 4]', goog.array.equals([1, 2], [3, 4], cmp));
  1099. assertFalse('[1] cmp [2, 3]', goog.array.equals([1], [2, 3], cmp));
  1100. assertTrue('[{}] cmp [{}]', goog.array.equals([{}], [{}], cmp));
  1101. assertTrue('[{}] cmp [{a: 1}]', goog.array.equals([{}], [{a: 1}], cmp));
  1102. // Test with array-like objects.
  1103. assertTrue('[5] == obj [5]', goog.array.equals([5], {0: 5, length: 1}));
  1104. assertTrue('obj [5] == [5]', goog.array.equals({0: 5, length: 1}, [5]));
  1105. assertTrue(
  1106. '["x"] == obj ["x"]', goog.array.equals(['x'], {0: 'x', length: 1}));
  1107. assertTrue(
  1108. 'obj ["x"] == ["x"]', goog.array.equals({0: 'x', length: 1}, ['x']));
  1109. assertTrue(
  1110. '[5] == {0: 5, 1: 6, length: 1}',
  1111. goog.array.equals([5], {0: 5, 1: 6, length: 1}));
  1112. assertTrue(
  1113. '{0: 5, 1: 6, length: 1} == [5]',
  1114. goog.array.equals({0: 5, 1: 6, length: 1}, [5]));
  1115. assertFalse(
  1116. '[5, 6] == {0: 5, 1: 6, length: 1}',
  1117. goog.array.equals([5, 6], {0: 5, 1: 6, length: 1}));
  1118. assertFalse(
  1119. '{0: 5, 1: 6, length: 1}, [5, 6]',
  1120. goog.array.equals({0: 5, 1: 6, length: 1}, [5, 6]));
  1121. assertTrue(
  1122. '[5, 6] == obj [5, 6]',
  1123. goog.array.equals([5, 6], {0: 5, 1: 6, length: 2}));
  1124. assertTrue(
  1125. 'obj [5, 6] == [5, 6]',
  1126. goog.array.equals({0: 5, 1: 6, length: 2}, [5, 6]));
  1127. assertFalse(
  1128. '{0: 5, 1: 6} == [5, 6]', goog.array.equals({0: 5, 1: 6}, [5, 6]));
  1129. }
  1130. function testArrayCompare3Basic() {
  1131. assertEquals(0, goog.array.compare3([], []));
  1132. assertEquals(0, goog.array.compare3(['111', '222'], ['111', '222']));
  1133. assertEquals(-1, goog.array.compare3(['111', '222'], ['1111', '']));
  1134. assertEquals(1, goog.array.compare3(['111', '222'], ['111']));
  1135. assertEquals(1, goog.array.compare3(['11', '222', '333'], []));
  1136. assertEquals(-1, goog.array.compare3([], ['11', '222', '333']));
  1137. }
  1138. function testArrayCompare3ComparatorFn() {
  1139. function cmp(a, b) { return a - b; };
  1140. assertEquals(0, goog.array.compare3([], [], cmp));
  1141. assertEquals(0, goog.array.compare3([8, 4], [8, 4], cmp));
  1142. assertEquals(-1, goog.array.compare3([4, 3], [5, 0]));
  1143. assertEquals(1, goog.array.compare3([6, 2], [6]));
  1144. assertEquals(1, goog.array.compare3([1, 2, 3], []));
  1145. assertEquals(-1, goog.array.compare3([], [1, 2, 3]));
  1146. }
  1147. function testSort() {
  1148. // Test sorting empty array
  1149. var a = [];
  1150. goog.array.sort(a);
  1151. assertEquals(
  1152. 'Sorted empty array is still an empty array (length 0)', 0, a.length);
  1153. // Test sorting homogenous array of String(s) of length > 1
  1154. var b = [
  1155. 'JUST', '1', 'test', 'Array', 'to', 'test', 'array', 'Sort', 'about', 'NOW',
  1156. '!!'
  1157. ];
  1158. var bSorted = [
  1159. '!!', '1', 'Array', 'JUST', 'NOW', 'Sort', 'about', 'array', 'test', 'test',
  1160. 'to'
  1161. ];
  1162. goog.array.sort(b);
  1163. assertArrayEquals(bSorted, b);
  1164. // Test sorting already sorted array of String(s) of length > 1
  1165. goog.array.sort(b);
  1166. assertArrayEquals(bSorted, b);
  1167. // Test sorting homogenous array of integer Number(s) of length > 1
  1168. var c = [
  1169. 100, 1, 2000, -1, 0, 1000023, 12312512, -12331, 123, 54325, -38104783,
  1170. 93708, 908, -213, -4, 5423, 0
  1171. ];
  1172. var cSorted = [
  1173. -38104783, -12331, -213, -4, -1, 0, 0, 1, 100, 123, 908, 2000, 5423, 54325,
  1174. 93708, 1000023, 12312512
  1175. ];
  1176. goog.array.sort(c);
  1177. assertArrayEquals(cSorted, c);
  1178. // Test sorting already sorted array of integer Number(s) of length > 1
  1179. goog.array.sort(c);
  1180. assertArrayEquals(cSorted, c);
  1181. // Test sorting homogenous array of Number(s) of length > 1
  1182. var e = [
  1183. -1321.3124, 0.31255, 54254, 0, 142.88888708, -321434.58758, -324, 453, 334,
  1184. -3, 5, -9, 342, -897123.9
  1185. ];
  1186. var eSorted = [
  1187. -897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0.31255, 5,
  1188. 142.88888708, 334, 342, 453, 54254
  1189. ];
  1190. goog.array.sort(e);
  1191. assertArrayEquals(eSorted, e);
  1192. // Test sorting already sorted array of Number(s) of length > 1
  1193. goog.array.sort(e);
  1194. assertArrayEquals(eSorted, e);
  1195. // Test sorting array of Number(s) of length > 1,
  1196. // using custom comparison function which does reverse ordering
  1197. var f = [
  1198. -1321.3124, 0.31255, 54254, 0, 142.88888708, -321434.58758, -324, 453, 334,
  1199. -3, 5, -9, 342, -897123.9
  1200. ];
  1201. var fSorted = [
  1202. 54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, -3, -9, -324, -1321.3124,
  1203. -321434.58758, -897123.9
  1204. ];
  1205. goog.array.sort(f, function(a, b) { return b - a; });
  1206. assertArrayEquals(fSorted, f);
  1207. // Test sorting already sorted array of Number(s) of length > 1
  1208. // using custom comparison function which does reverse ordering
  1209. goog.array.sort(f, function(a, b) { return b - a; });
  1210. assertArrayEquals(fSorted, f);
  1211. // Test sorting array of custom Object(s) of length > 1 that have
  1212. // an overriden toString
  1213. function ComparedObject(value) { this.value = value; };
  1214. ComparedObject.prototype.toString = function() { return this.value; };
  1215. var co1 = new ComparedObject('a');
  1216. var co2 = new ComparedObject('b');
  1217. var co3 = new ComparedObject('c');
  1218. var co4 = new ComparedObject('d');
  1219. var g = [co3, co4, co2, co1];
  1220. var gSorted = [co1, co2, co3, co4];
  1221. goog.array.sort(g);
  1222. assertArrayEquals(gSorted, g);
  1223. // Test sorting already sorted array of custom Object(s) of length > 1
  1224. // that have an overriden toString
  1225. goog.array.sort(g);
  1226. assertArrayEquals(gSorted, g);
  1227. // Test sorting an array of custom Object(s) of length > 1 using
  1228. // a custom comparison function
  1229. var h = [co4, co2, co1, co3];
  1230. var hSorted = [co1, co2, co3, co4];
  1231. goog.array.sort(h, function(a, b) {
  1232. return a.value > b.value ? 1 : a.value < b.value ? -1 : 0;
  1233. });
  1234. assertArrayEquals(hSorted, h);
  1235. // Test sorting already sorted array of custom Object(s) of length > 1
  1236. // using a custom comparison function
  1237. goog.array.sort(h);
  1238. assertArrayEquals(hSorted, h);
  1239. // Test sorting arrays of length 1
  1240. var i = ['one'];
  1241. var iSorted = ['one'];
  1242. goog.array.sort(i);
  1243. assertArrayEquals(iSorted, i);
  1244. var j = [1];
  1245. var jSorted = [1];
  1246. goog.array.sort(j);
  1247. assertArrayEquals(jSorted, j);
  1248. var k = [1.1];
  1249. var kSorted = [1.1];
  1250. goog.array.sort(k);
  1251. assertArrayEquals(kSorted, k);
  1252. var l = [co3];
  1253. var lSorted = [co3];
  1254. goog.array.sort(l);
  1255. assertArrayEquals(lSorted, l);
  1256. var m = [co2];
  1257. var mSorted = [co2];
  1258. goog.array.sort(m, function(a, b) {
  1259. return a.value > b.value ? 1 : a.value < b.value ? -1 : 0;
  1260. });
  1261. assertArrayEquals(mSorted, m);
  1262. }
  1263. function testStableSort() {
  1264. // Test array with custom comparison function
  1265. var arr = [
  1266. {key: 3, val: 'a'}, {key: 2, val: 'b'}, {key: 3, val: 'c'},
  1267. {key: 4, val: 'd'}, {key: 3, val: 'e'}
  1268. ];
  1269. var arrClone = goog.array.clone(arr);
  1270. function comparisonFn(obj1, obj2) { return obj1.key - obj2.key; }
  1271. goog.array.stableSort(arr, comparisonFn);
  1272. var sortedValues = [];
  1273. for (var i = 0; i < arr.length; i++) {
  1274. sortedValues.push(arr[i].val);
  1275. }
  1276. var wantedSortedValues = ['b', 'a', 'c', 'e', 'd'];
  1277. assertArrayEquals(wantedSortedValues, sortedValues);
  1278. // Test array without custom comparison function
  1279. var arr2 = [];
  1280. for (var i = 0; i < arrClone.length; i++) {
  1281. arr2.push({
  1282. val: arrClone[i].val,
  1283. toString: goog.partial(function(index) { return arrClone[index].key; }, i)
  1284. });
  1285. }
  1286. goog.array.stableSort(arr2);
  1287. var sortedValues2 = [];
  1288. for (var i = 0; i < arr2.length; i++) {
  1289. sortedValues2.push(arr2[i].val);
  1290. }
  1291. assertArrayEquals(wantedSortedValues, sortedValues2);
  1292. }
  1293. function testSortByKey() {
  1294. function Item(value) {
  1295. this.getValue = function() { return value; };
  1296. }
  1297. var keyFn = function(item) { return item.getValue(); };
  1298. // Test without custom key comparison function
  1299. var arr1 = [new Item(3), new Item(2), new Item(1), new Item(5), new Item(4)];
  1300. goog.array.sortByKey(arr1, keyFn);
  1301. var wantedSortedValues1 = [1, 2, 3, 4, 5];
  1302. for (var i = 0; i < arr1.length; i++) {
  1303. assertEquals(wantedSortedValues1[i], arr1[i].getValue());
  1304. }
  1305. // Test with custom key comparison function
  1306. var arr2 = [new Item(3), new Item(2), new Item(1), new Item(5), new Item(4)];
  1307. function comparisonFn(key1, key2) { return -(key1 - key2); }
  1308. goog.array.sortByKey(arr2, keyFn, comparisonFn);
  1309. var wantedSortedValues2 = [5, 4, 3, 2, 1];
  1310. for (var i = 0; i < arr2.length; i++) {
  1311. assertEquals(wantedSortedValues2[i], arr2[i].getValue());
  1312. }
  1313. }
  1314. function testArrayBucketModulus() {
  1315. // bucket things by modulus
  1316. var a = {};
  1317. var b = [];
  1318. function modFive(num) { return num % 5; }
  1319. for (var i = 0; i < 20; i++) {
  1320. var mod = modFive(i);
  1321. a[mod] = a[mod] || [];
  1322. a[mod].push(i);
  1323. b.push(i);
  1324. }
  1325. var buckets = goog.array.bucket(b, modFive);
  1326. for (var i = 0; i < 5; i++) {
  1327. // The order isn't defined, but they should be the same sorted.
  1328. goog.array.sort(a[i]);
  1329. goog.array.sort(buckets[i]);
  1330. assertArrayEquals(a[i], buckets[i]);
  1331. }
  1332. }
  1333. function testArrayBucketEvenOdd() {
  1334. var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  1335. // test even/odd
  1336. function isEven(value, index, array) {
  1337. assertEquals(value, array[index]);
  1338. assertEquals('number', typeof index);
  1339. assertEquals(a, array);
  1340. return value % 2 == 0;
  1341. }
  1342. var b = goog.array.bucket(a, isEven);
  1343. assertArrayEquals(b[true], [2, 4, 6, 8]);
  1344. assertArrayEquals(b[false], [1, 3, 5, 7, 9]);
  1345. }
  1346. function testArrayBucketUsingThisObject() {
  1347. var a = [1, 2, 3, 4, 5];
  1348. var obj = {specialValue: 2};
  1349. function isSpecialValue(value, index, array) {
  1350. return value == this.specialValue ? 1 : 0;
  1351. }
  1352. var b = goog.array.bucket(a, isSpecialValue, obj);
  1353. assertArrayEquals(b[0], [1, 3, 4, 5]);
  1354. assertArrayEquals(b[1], [2]);
  1355. }
  1356. function testArrayToObject() {
  1357. var a = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}];
  1358. function getName(value, index, array) {
  1359. assertEquals(value, array[index]);
  1360. assertEquals('number', typeof index);
  1361. assertEquals(a, array);
  1362. return value.name;
  1363. }
  1364. var b = goog.array.toObject(a, getName);
  1365. for (var i = 0; i < a.length; i++) {
  1366. assertEquals(a[i], b[a[i].name]);
  1367. }
  1368. }
  1369. function testRange() {
  1370. assertArrayEquals([], goog.array.range(0));
  1371. assertArrayEquals([], goog.array.range(5, 5, 5));
  1372. assertArrayEquals([], goog.array.range(-3, -3));
  1373. assertArrayEquals([], goog.array.range(10, undefined, -1));
  1374. assertArrayEquals([], goog.array.range(8, 0));
  1375. assertArrayEquals([], goog.array.range(-5, -10, 3));
  1376. assertArrayEquals([0], goog.array.range(1));
  1377. assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], goog.array.range(10));
  1378. assertArrayEquals([1], goog.array.range(1, 2));
  1379. assertArrayEquals([-3, -2, -1, 0, 1, 2], goog.array.range(-3, 3));
  1380. assertArrayEquals([4], goog.array.range(4, 40, 400));
  1381. assertArrayEquals([5, 8, 11, 14], goog.array.range(5, 15, 3));
  1382. assertArrayEquals([1, -1, -3], goog.array.range(1, -5, -2));
  1383. assertElementsRoughlyEqual([.2, .3, .4], goog.array.range(.2, .5, .1), 0.001);
  1384. assertArrayEquals([0], goog.array.range(7, undefined, 9));
  1385. assertArrayEquals([0, 2, 4, 6], goog.array.range(8, undefined, 2));
  1386. }
  1387. function testArrayRepeat() {
  1388. assertArrayEquals([], goog.array.repeat(3, 0));
  1389. assertArrayEquals([], goog.array.repeat(3, -1));
  1390. assertArrayEquals([3], goog.array.repeat(3, 1));
  1391. assertArrayEquals([3, 3, 3], goog.array.repeat(3, 3));
  1392. assertArrayEquals([null, null], goog.array.repeat(null, 2));
  1393. }
  1394. function testArrayFlatten() {
  1395. assertArrayEquals([1, 2, 3, 4, 5], goog.array.flatten(1, 2, 3, 4, 5));
  1396. assertArrayEquals([1, 2, 3, 4, 5], goog.array.flatten(1, [2, [3, [4, 5]]]));
  1397. assertArrayEquals([1, 2, 3, 4], goog.array.flatten(1, [2, [3, [4]]]));
  1398. assertArrayEquals([1, 2, 3, 4], goog.array.flatten([[[1], 2], 3], 4));
  1399. assertArrayEquals([1], goog.array.flatten([[1]]));
  1400. assertArrayEquals([], goog.array.flatten());
  1401. assertArrayEquals([], goog.array.flatten([]));
  1402. assertArrayEquals(
  1403. goog.array.repeat(3, 180002),
  1404. goog.array.flatten(3, goog.array.repeat(3, 180000), 3));
  1405. assertArrayEquals(
  1406. goog.array.repeat(3, 180000),
  1407. goog.array.flatten([goog.array.repeat(3, 180000)]));
  1408. }
  1409. function testSortObjectsByKey() {
  1410. var sortedArray = buildSortedObjectArray(4);
  1411. var objects =
  1412. [sortedArray[1], sortedArray[2], sortedArray[0], sortedArray[3]];
  1413. goog.array.sortObjectsByKey(objects, 'name');
  1414. validateObjectArray(sortedArray, objects);
  1415. }
  1416. function testSortObjectsByKeyWithCompareFunction() {
  1417. var sortedArray = buildSortedObjectArray(4);
  1418. var objects =
  1419. [sortedArray[1], sortedArray[2], sortedArray[0], sortedArray[3]];
  1420. var descSortedArray =
  1421. [sortedArray[3], sortedArray[2], sortedArray[1], sortedArray[0]];
  1422. function descCompare(a, b) { return a < b ? 1 : a > b ? -1 : 0; };
  1423. goog.array.sortObjectsByKey(objects, 'name', descCompare);
  1424. validateObjectArray(descSortedArray, objects);
  1425. }
  1426. function buildSortedObjectArray(size) {
  1427. var objectArray = [];
  1428. for (var i = 0; i < size; i++) {
  1429. objectArray.push({'name': 'name_' + i, 'id': 'id_' + (size - i)});
  1430. }
  1431. return objectArray;
  1432. }
  1433. function validateObjectArray(expected, actual) {
  1434. assertEquals(expected.length, actual.length);
  1435. for (var i = 0; i < expected.length; i++) {
  1436. assertEquals(expected[i].name, actual[i].name);
  1437. assertEquals(expected[i].id, actual[i].id);
  1438. }
  1439. }
  1440. function testIsSorted() {
  1441. assertTrue(goog.array.isSorted([1, 2, 3]));
  1442. assertTrue(goog.array.isSorted([1, 2, 2]));
  1443. assertFalse(goog.array.isSorted([1, 2, 1]));
  1444. assertTrue(goog.array.isSorted([1, 2, 3], null, true));
  1445. assertFalse(goog.array.isSorted([1, 2, 2], null, true));
  1446. assertFalse(goog.array.isSorted([1, 2, 1], null, true));
  1447. function compare(a, b) { return b - a; }
  1448. assertFalse(goog.array.isSorted([1, 2, 3], compare));
  1449. assertTrue(goog.array.isSorted([3, 2, 2], compare));
  1450. }
  1451. function assertRotated(expect, array, rotate) {
  1452. assertArrayEquals(expect, goog.array.rotate(array, rotate));
  1453. }
  1454. function testRotate() {
  1455. assertRotated([], [], 3);
  1456. assertRotated([1], [1], 3);
  1457. assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], -6);
  1458. assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], -5);
  1459. assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], -4);
  1460. assertRotated([3, 4, 0, 1, 2], [0, 1, 2, 3, 4], -3);
  1461. assertRotated([2, 3, 4, 0, 1], [0, 1, 2, 3, 4], -2);
  1462. assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], -1);
  1463. assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 0);
  1464. assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], 1);
  1465. assertRotated([3, 4, 0, 1, 2], [0, 1, 2, 3, 4], 2);
  1466. assertRotated([2, 3, 4, 0, 1], [0, 1, 2, 3, 4], 3);
  1467. assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], 4);
  1468. assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 5);
  1469. assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], 6);
  1470. }
  1471. function testMoveItemWithArray() {
  1472. var arr = [0, 1, 2, 3];
  1473. goog.array.moveItem(arr, 1, 3); // toIndex > fromIndex
  1474. assertArrayEquals([0, 2, 3, 1], arr);
  1475. goog.array.moveItem(arr, 2, 0); // toIndex < fromIndex
  1476. assertArrayEquals([3, 0, 2, 1], arr);
  1477. goog.array.moveItem(arr, 1, 1); // toIndex == fromIndex
  1478. assertArrayEquals([3, 0, 2, 1], arr);
  1479. // Out-of-bounds indexes throw assertion errors.
  1480. assertThrows(function() { goog.array.moveItem(arr, -1, 1); });
  1481. assertThrows(function() { goog.array.moveItem(arr, 4, 1); });
  1482. assertThrows(function() { goog.array.moveItem(arr, 1, -1); });
  1483. assertThrows(function() { goog.array.moveItem(arr, 1, 4); });
  1484. // The array should not be modified by the out-of-bound calls.
  1485. assertArrayEquals([3, 0, 2, 1], arr);
  1486. }
  1487. function testMoveItemWithArgumentsObject() {
  1488. var f = function() {
  1489. goog.array.moveItem(arguments, 0, 1);
  1490. return arguments;
  1491. };
  1492. assertArrayEquals([1, 0], goog.array.toArray(f(0, 1)));
  1493. }
  1494. function testConcat() {
  1495. var a1 = [1, 2, 3];
  1496. var a2 = [4, 5, 6];
  1497. var a3 = goog.array.concat(a1, a2);
  1498. a1.push(1);
  1499. a2.push(5);
  1500. assertArrayEquals([1, 2, 3, 4, 5, 6], a3);
  1501. }
  1502. function testConcatWithNoSecondArg() {
  1503. var a1 = [1, 2, 3, 4];
  1504. var a2 = goog.array.concat(a1);
  1505. a1.push(5);
  1506. assertArrayEquals([1, 2, 3, 4], a2);
  1507. }
  1508. function testConcatWithNonArrayArgs() {
  1509. var a1 = [1, 2, 3, 4];
  1510. var o = {0: 'a', 1: 'b', length: 2};
  1511. var a2 = goog.array.concat(a1, 5, '10', o);
  1512. assertArrayEquals([1, 2, 3, 4, 5, '10', o], a2);
  1513. }
  1514. function testConcatWithNull() {
  1515. var a1 = goog.array.concat(null, [1, 2, 3]);
  1516. var a2 = goog.array.concat([1, 2, 3], null);
  1517. assertArrayEquals([null, 1, 2, 3], a1);
  1518. assertArrayEquals([1, 2, 3, null], a2);
  1519. }
  1520. function testZip() {
  1521. var a1 = goog.array.zip([1, 2, 3], [3, 2, 1]);
  1522. var a2 = goog.array.zip([1, 2], [3, 2, 1]);
  1523. var a3 = goog.array.zip();
  1524. assertArrayEquals([[1, 3], [2, 2], [3, 1]], a1);
  1525. assertArrayEquals([[1, 3], [2, 2]], a2);
  1526. assertArrayEquals([], a3);
  1527. }
  1528. function testShuffle() {
  1529. // Test array. This array should have unique values for the purposes of this
  1530. // test case.
  1531. var testArray = [1, 2, 3, 4, 5];
  1532. var testArrayCopy = goog.array.clone(testArray);
  1533. // Custom random function, which always returns a value approaching 1,
  1534. // resulting in a "shuffle" that preserves the order of original array
  1535. // (for array sizes that we work with here).
  1536. var noChangeShuffleFunction = function() { return .999999; };
  1537. goog.array.shuffle(testArray, noChangeShuffleFunction);
  1538. assertArrayEquals(testArrayCopy, testArray);
  1539. // Custom random function, which always returns 0, resulting in a
  1540. // deterministic "shuffle" that is predictable but differs from the
  1541. // original order of the array.
  1542. var testShuffleFunction = function() { return 0; };
  1543. goog.array.shuffle(testArray, testShuffleFunction);
  1544. assertArrayEquals([2, 3, 4, 5, 1], testArray);
  1545. // Test the use of a real random function(no optional RNG is specified).
  1546. goog.array.shuffle(testArray);
  1547. // Ensure the shuffled array comprises the same elements (without regard to
  1548. // order).
  1549. assertSameElements(testArrayCopy, testArray);
  1550. }
  1551. function testRemoveAllIf() {
  1552. var testArray = [9, 1, 9, 2, 9, 3, 4, 9, 9, 9, 5];
  1553. var expectedArray = [1, 2, 3, 4, 5];
  1554. var actualOutput =
  1555. goog.array.removeAllIf(testArray, function(el) { return el == 9; });
  1556. assertEquals(6, actualOutput);
  1557. assertArrayEquals(expectedArray, testArray);
  1558. }
  1559. function testRemoveAllIf_noMatches() {
  1560. var testArray = [1];
  1561. var expectedArray = [1];
  1562. var actualOutput =
  1563. goog.array.removeAllIf(testArray, function(el) { return false; });
  1564. assertEquals(0, actualOutput);
  1565. assertArrayEquals(expectedArray, testArray);
  1566. }
  1567. function testCopyByIndex() {
  1568. var testArray = [1, 2, 'a', 'b', 'c', 'd'];
  1569. var copyIndexes = [1, 3, 0, 0, 2];
  1570. var expectedArray = [2, 'b', 1, 1, 'a'];
  1571. var actualOutput = goog.array.copyByIndex(testArray, copyIndexes);
  1572. assertArrayEquals(expectedArray, actualOutput);
  1573. }
  1574. function testComparators() {
  1575. var greater = 42;
  1576. var smaller = 13;
  1577. assertTrue(goog.array.defaultCompare(smaller, greater) < 0);
  1578. assertEquals(0, goog.array.defaultCompare(smaller, smaller));
  1579. assertTrue(goog.array.defaultCompare(greater, smaller) > 0);
  1580. assertTrue(goog.array.inverseDefaultCompare(greater, smaller) < 0);
  1581. assertEquals(0, goog.array.inverseDefaultCompare(greater, greater));
  1582. assertTrue(goog.array.inverseDefaultCompare(smaller, greater) > 0);
  1583. }
  1584. function testConcatMap() {
  1585. var a = [0, 1, 2, 0];
  1586. var context = {};
  1587. var arraysToReturn = [['x', 'y', 'z'], [], ['a', 'b']];
  1588. var timesCalled = 0;
  1589. var result = goog.array.concatMap(a, function(val, index, a2) {
  1590. assertEquals(a, a2);
  1591. assertEquals(context, this);
  1592. assertEquals(timesCalled++, index);
  1593. assertEquals(a[index], val);
  1594. return arraysToReturn[val];
  1595. }, context);
  1596. assertArrayEquals(['x', 'y', 'z', 'a', 'b', 'x', 'y', 'z'], result);
  1597. }