unit-tests.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>js-naturalsort test suite</title>
  5. <script src='http://code.jquery.com/jquery-latest.js'></script>
  6. <link rel='stylesheet' href='http://code.jquery.com/qunit/git/qunit.css' type='text/css' media='screen' />
  7. <script type='text/javascript' src='http://code.jquery.com/qunit/git/qunit.js'></script>
  8. <script src='naturalSort.js' type='text/javascript'></script>
  9. <script type='text/javascript'>
  10. $(function () {
  11. test('different values types', function () {
  12. wrapTest(
  13. ['a',1],
  14. [1,'a'],
  15. 'number always comes first');
  16. wrapTest(
  17. ['1',1],
  18. ['1',1],
  19. 'number vs numeric string - should remain unchanged (error in chrome)');
  20. wrapTest(
  21. ['02',3,2,'01'],
  22. ['01','02',2,3],
  23. 'padding numeric string vs number');
  24. });
  25. test('datetime', function () {
  26. wrapTest(
  27. ['10/12/2008','10/11/2008','10/11/2007','10/12/2007'],
  28. ['10/11/2007','10/12/2007','10/11/2008','10/12/2008'],
  29. 'similar dates');
  30. wrapTest(
  31. ['01/01/2008','01/10/2008','01/01/1992','01/01/1991'],
  32. ['01/01/1991','01/01/1992','01/01/2008','01/10/2008'],
  33. 'similar dates');
  34. wrapTest(
  35. [
  36. 'Wed Jan 01 2010 00:00:00 GMT-0800 (Pacific Standard Time)',
  37. 'Thu Dec 31 2009 00:00:00 GMT-0800 (Pacific Standard Time)',
  38. 'Wed Jan 01 2010 00:00:00 GMT-0500 (Eastern Standard Time)'
  39. ],[
  40. 'Thu Dec 31 2009 00:00:00 GMT-0800 (Pacific Standard Time)',
  41. 'Wed Jan 01 2010 00:00:00 GMT-0500 (Eastern Standard Time)',
  42. 'Wed Jan 01 2010 00:00:00 GMT-0800 (Pacific Standard Time)'
  43. ], 'javascript toString(), different timezones');
  44. wrapTest(
  45. [
  46. 'Saturday, July 3, 2010',
  47. 'Monday, August 2, 2010',
  48. 'Monday, May 3, 2010'
  49. ],[
  50. 'Monday, May 3, 2010',
  51. 'Saturday, July 3, 2010',
  52. 'Monday, August 2, 2010'
  53. ], 'Date.toString(), Date.toLocaleString()');
  54. wrapTest(
  55. [
  56. 'Mon, 15 Jun 2009 20:45:30 GMT',
  57. 'Mon, 3 May 2010 17:45:30 GMT',
  58. 'Mon, 15 Jun 2009 17:45:30 GMT'
  59. ],[
  60. 'Mon, 15 Jun 2009 17:45:30 GMT',
  61. 'Mon, 15 Jun 2009 20:45:30 GMT',
  62. 'Mon, 3 May 2010 17:45:30 GMT'
  63. ], 'Date.toUTCString()');
  64. wrapTest(
  65. [
  66. 'Saturday, July 3, 2010 1:45 PM',
  67. 'Saturday, July 3, 2010 1:45 AM',
  68. 'Monday, August 2, 2010 1:45 PM',
  69. 'Monday, May 3, 2010 1:45 PM'
  70. ],[
  71. 'Monday, May 3, 2010 1:45 PM',
  72. 'Saturday, July 3, 2010 1:45 AM',
  73. 'Saturday, July 3, 2010 1:45 PM',
  74. 'Monday, August 2, 2010 1:45 PM'
  75. ], '');
  76. wrapTest(
  77. [
  78. 'Saturday, July 3, 2010 1:45:30 PM',
  79. 'Saturday, July 3, 2010 1:45:29 PM',
  80. 'Monday, August 2, 2010 1:45:01 PM',
  81. 'Monday, May 3, 2010 1:45:00 PM'
  82. ],[
  83. 'Monday, May 3, 2010 1:45:00 PM',
  84. 'Saturday, July 3, 2010 1:45:29 PM',
  85. 'Saturday, July 3, 2010 1:45:30 PM',
  86. 'Monday, August 2, 2010 1:45:01 PM'
  87. ], '');
  88. wrapTest(
  89. [
  90. '2/15/2009 1:45 PM',
  91. '1/15/2009 1:45 PM',
  92. '2/15/2009 1:45 AM'
  93. ],[
  94. '1/15/2009 1:45 PM',
  95. '2/15/2009 1:45 AM',
  96. '2/15/2009 1:45 PM'
  97. ], '');
  98. wrapTest(
  99. [
  100. '2010-06-15T13:45:30',
  101. '2009-06-15T13:45:30',
  102. '2009-06-15T01:45:30.2',
  103. '2009-01-15T01:45:30'
  104. ],[
  105. '2009-01-15T01:45:30',
  106. '2009-06-15T01:45:30.2',
  107. '2009-06-15T13:45:30',
  108. '2010-06-15T13:45:30'
  109. ], 'ISO8601 Dates');
  110. wrapTest(
  111. [
  112. '2010-06-15 13:45:30',
  113. '2009-06-15 13:45:30',
  114. '2009-01-15 01:45:30'
  115. ],[
  116. '2009-01-15 01:45:30',
  117. '2009-06-15 13:45:30',
  118. '2010-06-15 13:45:30'
  119. ], 'ISO8601-ish YYYY-MM-DDThh:mm:ss - which does not parse into a Date instance');
  120. wrapTest(
  121. [
  122. 'Mon, 15 Jun 2009 20:45:30 GMT',
  123. 'Mon, 15 Jun 2009 20:45:30 PDT',
  124. 'Mon, 15 Jun 2009 20:45:30 EST',
  125. ],[
  126. 'Mon, 15 Jun 2009 20:45:30 GMT',
  127. 'Mon, 15 Jun 2009 20:45:30 EST',
  128. 'Mon, 15 Jun 2009 20:45:30 PDT'
  129. ], 'RFC1123 testing different timezones');
  130. wrapTest(
  131. [
  132. '1245098730000',
  133. '14330728000',
  134. '1245098728000'
  135. ],[
  136. '14330728000',
  137. '1245098728000',
  138. '1245098730000'
  139. ], 'unix epoch, Date.getTime()');
  140. });
  141. test('version number strings', function () {
  142. wrapTest(
  143. ['1.0.2','1.0.1','1.0.0','1.0.9'],
  144. ['1.0.0','1.0.1','1.0.2','1.0.9'],
  145. 'close version numbers');
  146. wrapTest(
  147. ['1.0.03','1.0.003','1.0.002','1.0.0001'],
  148. ['1.0.0001','1.0.002','1.0.003','1.0.03'],
  149. 'multi-digit branch release');
  150. wrapTest(
  151. ['1.1beta','1.1.2alpha3','1.0.2alpha3','1.0.2alpha1','1.0.1alpha4','2.1.2','2.1.1'],
  152. ['1.0.1alpha4','1.0.2alpha1','1.0.2alpha3','1.1.2alpha3','1.1beta','2.1.1','2.1.2'],
  153. 'close version numbers');
  154. wrapTest(
  155. ['myrelease-1.1.3','myrelease-1.2.3','myrelease-1.1.4','myrelease-1.1.1','myrelease-1.0.5'],
  156. ['myrelease-1.0.5','myrelease-1.1.1','myrelease-1.1.3','myrelease-1.1.4','myrelease-1.2.3'],
  157. 'string first');
  158. });
  159. test('numerics', function () {
  160. wrapTest(
  161. ['10',9,2,'1','4'],
  162. ['1',2,'4',9,'10'],
  163. 'string vs number');
  164. wrapTest(
  165. ['0001','002','001'],
  166. ['0001','001','002'],
  167. '0 left-padded numbers');
  168. wrapTest(
  169. [2,1,'1','0001','002','02','001'],
  170. ['0001','001','002','02',1,'1',2],
  171. '0 left-padded numbers and regular numbers');
  172. wrapTest(
  173. ['10.0401',10.022,10.042,'10.021999'],
  174. ['10.021999',10.022,'10.0401',10.042],
  175. 'decimal string vs decimal, different precision');
  176. wrapTest(
  177. ['10.04',10.02,10.03,'10.01'],
  178. ['10.01',10.02,10.03,'10.04'],
  179. 'decimal string vs decimal, same precision');
  180. wrapTest(
  181. ['10.04f','10.039F','10.038d','10.037D'],
  182. ['10.037D','10.038d','10.039F','10.04f'],
  183. 'float/decimal with \'F\' or \'D\' notation');
  184. wrapTest(
  185. ['10.004Z','10.039T','10.038ooo','10.037g'],
  186. ['10.004Z','10.037g','10.038ooo','10.039T'],
  187. 'not foat/decimal notation');
  188. wrapTest(
  189. ['1.528535047e5','1.528535047e7','1.52e15','1.528535047e3','1.59e-3'],
  190. ['1.59e-3','1.528535047e3','1.528535047e5','1.528535047e7','1.52e15'],
  191. 'scientific notation');
  192. wrapTest(
  193. ['-1','-2','4','-3','0','-5'],
  194. ['-5','-3','-2','-1','0','4'],
  195. 'negative numbers as strings');
  196. wrapTest(
  197. [-1,'-2',4,-3,'0','-5'],
  198. ['-5',-3,'-2',-1,'0',4],
  199. 'negative numbers as strings - mixed input type, string + numeric');
  200. wrapTest(
  201. [-2.01,-2.1,4.144,4.1,-2.001,-5],
  202. [-5,-2.1,-2.01,-2.001,4.1,4.144],
  203. 'negative floats - all numeric');
  204. });
  205. test('IP addresses', function () {
  206. wrapTest(
  207. [
  208. '192.168.0.100',
  209. '192.168.0.1',
  210. '192.168.1.1',
  211. '192.168.0.250',
  212. '192.168.1.123',
  213. '10.0.0.2',
  214. '10.0.0.1'
  215. ],
  216. [
  217. '10.0.0.1',
  218. '10.0.0.2',
  219. '192.168.0.1',
  220. '192.168.0.100',
  221. '192.168.0.250',
  222. '192.168.1.1',
  223. '192.168.1.123'
  224. ]);
  225. });
  226. test('filenames', function () {
  227. wrapTest(
  228. ['img12.png','img10.png','img2.png','img1.png'],
  229. ['img1.png','img2.png','img10.png','img12.png'],
  230. 'simple image filenames');
  231. wrapTest(
  232. ['car.mov','01alpha.sgi','001alpha.sgi','my.string_41299.tif','organic2.0001.sgi'],
  233. ['001alpha.sgi','01alpha.sgi','car.mov','my.string_41299.tif','organic2.0001.sgi'],
  234. 'complex filenames');
  235. wrapTest([
  236. './system/kernel/js/01_ui.core.js',
  237. './system/kernel/js/00_jquery-1.3.2.js',
  238. './system/kernel/js/02_my.desktop.js'
  239. ],[
  240. './system/kernel/js/00_jquery-1.3.2.js',
  241. './system/kernel/js/01_ui.core.js',
  242. './system/kernel/js/02_my.desktop.js'
  243. ], 'unix filenames');
  244. });
  245. test('space(s) as first character(s)', function () {
  246. wrapTest(
  247. ['alpha',' 1',' 3',' 2',0],
  248. [0,' 1',' 2',' 3','alpha']);
  249. });
  250. test('empty strings and space character', function () {
  251. wrapTest(
  252. ['10023','999','',2,5.663,5.6629],
  253. ['',2,5.6629,5.663,'999','10023']);
  254. wrapTest(
  255. [0,'0',''],
  256. [0,'0','']);
  257. });
  258. test('hex', function () {
  259. wrapTest(
  260. [ '0xA','0x9','0x99' ],
  261. [ '0x9','0xA','0x99'],
  262. 'real hex numbers');
  263. wrapTest(
  264. [ '0xZZ','0xVVV','0xVEV','0xUU' ],
  265. [ '0xUU','0xVEV','0xVVV','0xZZ' ],'fake hex numbers');
  266. });
  267. test('unicode', function () {
  268. wrapTest(
  269. [ '\u0044', '\u0055', '\u0054', '\u0043' ],
  270. [ '\u0043', '\u0044', '\u0054', '\u0055' ],
  271. 'basic latin');
  272. });
  273. test('sparse array sort', function () {
  274. var sarray = [3, 2]
  275. sarrayOutput = [1, 2, 3];
  276. sarray[10] = 1;
  277. for (var i=0; i < 8; i++)
  278. sarrayOutput.push(undefined);
  279. wrapTest(sarray, sarrayOutput,'simple sparse array');
  280. });
  281. test('case insensitive support', function () {
  282. naturalSort.insensitive = true;
  283. wrapTest(
  284. ['A', 'b', 'C', 'd', 'E', 'f'],
  285. ['A', 'b', 'C', 'd', 'E', 'f'],
  286. 'case sensitive pre-sorted array');
  287. wrapTest(
  288. ['A', 'C', 'E', 'b', 'd', 'f'],
  289. ['A', 'b', 'C', 'd', 'E', 'f'],
  290. 'case sensitive un-sorted array');
  291. naturalSort.insensitive = false;
  292. wrapTest(
  293. ['A', 'C', 'E', 'b', 'd', 'f'],
  294. ['A', 'C', 'E', 'b', 'd', 'f'],
  295. 'case sensitive pre-sorted array');
  296. wrapTest(
  297. ['A', 'b', 'C', 'd', 'E', 'f'],
  298. ['A', 'C', 'E', 'b', 'd', 'f'],
  299. 'case sensitive un-sorted array');
  300. });
  301. test('contributed tests', function () {
  302. wrapTest(
  303. ['T78','U17','U10','U12','U14','745','U7','01','485','S16','S2','S22','1081','S25','1055','779','776','771','44','4','87','1091','42','480','952','951','756','1000','824','770','666','633','619','1','991','77H','PIER-7','47','29','9','77L','433'],
  304. ['01','1','4','9','29','42','44','47','77H','77L','87','433','480','485','619','633','666','745','756','770','771','776','779','824','951','952','991','1000','1055','1081','1091','PIER-7','S2','S16','S22','S25','T78','U7','U10','U12','U14','U17'],
  305. 'contributed by Bob Zeiner');
  306. wrapTest(
  307. [
  308. 'FSI stop, Position: 5',
  309. 'Mail Group stop, Position: 5',
  310. 'Mail Group stop, Position: 5',
  311. 'FSI stop, Position: 6',
  312. 'FSI stop, Position: 6',
  313. 'Newsstand stop, Position: 4',
  314. 'Newsstand stop, Position: 4',
  315. 'FSI stop, Position: 5'
  316. ],[
  317. 'FSI stop, Position: 5',
  318. 'FSI stop, Position: 5',
  319. 'FSI stop, Position: 6',
  320. 'FSI stop, Position: 6',
  321. 'Mail Group stop, Position: 5',
  322. 'Mail Group stop, Position: 5',
  323. 'Newsstand stop, Position: 4',
  324. 'Newsstand stop, Position: 4'
  325. ],'contributed by Scott');
  326. wrapTest(
  327. [2, 10, 1, 'azd', undefined, 'asd'],
  328. [1, 2, 10, 'asd', 'azd', undefined],
  329. 'issue #2 - undefined support - jarvinen pekka');
  330. wrapTest(
  331. [undefined, undefined, undefined, 1, undefined],
  332. [1, undefined, undefined, undefined],
  333. 'issue #2 - undefined support - jarvinen pekka');
  334. wrapTest(
  335. ['-1', '-2', '4', '-3', '0', '-5'],
  336. ['-5', '-3', '-2', '-1', '0', '4'],
  337. 'issue #3 - invalid numeric string sorting - guilermo.dev');
  338. naturalSort.insensitive = true;
  339. wrapTest(
  340. ['9','11','22','99','A','aaaa','bbbb','Aaaa','aAaa','aa','AA','Aa','aA','BB','bB','aaA','AaA','aaa'],
  341. ['9', '11', '22', '99', 'A', 'aa', 'AA', 'Aa', 'aA', 'aaA', 'AaA', 'aaa', 'aaaa', 'Aaaa', 'aAaa', 'BB', 'bB', 'bbbb'],
  342. 'issue #5 - invalid sort order - Howie Schecter');
  343. naturalSort.insensitive = false;
  344. wrapTest(
  345. ['9','11','22','99','A','aaaa','bbbb','Aaaa','aAaa','aa','AA','Aa','aA','BB','bB','aaA','AaA','aaa'],
  346. ['9', '11', '22', '99', 'A', 'AA', 'Aa', 'AaA', 'Aaaa', 'BB', 'aA', 'aAaa', 'aa', 'aaA', 'aaa', 'aaaa', 'bB', 'bbbb'],
  347. 'issue #5 - invalid sort order - Howie Schecter');
  348. wrapTest(
  349. ['5D','1A','2D','33A','5E','33K','33D','5S','2C','5C','5F','1D','2M'],
  350. ['1A','1D','2C','2D','2M','5C','5D','5E','5F','5S','33A','33D','33K'],
  351. 'alphanumeric - number first');
  352. });
  353. });
  354. function wrapTest(origArray, sortArray, message) {
  355. deepEqual($.extend([], origArray).sort(naturalSort), sortArray, (message ? message + ' - ' : '') + QUnit.jsDump.parse(origArray));
  356. }
  357. </script>
  358. </head>
  359. <body>
  360. <h1 id='qunit-header'>js-naturalsort test suite</h1>
  361. <h2 id='qunit-banner'></h2>
  362. <h2 id='qunit-userAgent'></h2>
  363. <ol id='qunit-tests' />
  364. </body>
  365. </html>