xref: /vim-8.2.3635/src/testdir/test_sort.vim (revision 55e29611)
1" Tests for the "sort()" function and for the ":sort" command.
2
3source check.vim
4
5func Compare1(a, b) abort
6  call sort(range(3), 'Compare2')
7  return a:a - a:b
8endfunc
9
10func Compare2(a, b) abort
11  return a:a - a:b
12endfunc
13
14func Test_sort_strings()
15  " numbers compared as strings
16  call assert_equal([1, 2, 3], sort([3, 2, 1]))
17  call assert_equal([13, 28, 3], sort([3, 28, 13]))
18
19  call assert_equal(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'œ'],
20  \            sort(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'œ']))
21
22  call assert_equal(['A', 'a', 'o', 'O', 'p', 'P', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'œ'],
23  \            sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'i'))
24
25  let lc = execute('language collate')
26  " With the following locales, the accentuated letters are ordered
27  " similarly to the non-accentuated letters...
28  if lc =~? '"\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8"'
29    call assert_equal(['a', 'A', 'ä', 'Ä', 'o', 'O', 'ô', 'Ô', 'œ', 'œ', 'p', 'P'],
30    \            sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
31  " ... whereas with a Swedish locale, the accentuated letters are ordered
32  " after Z.
33  elseif lc =~? '"sv.*utf-\?8"'
34    call assert_equal(['a', 'A', 'o', 'O', 'p', 'P', 'ä', 'Ä', 'œ', 'œ', 'ô', 'Ô'],
35    \            sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
36  endif
37endfunc
38
39func Test_sort_numeric()
40  call assert_equal([1, 2, 3], sort([3, 2, 1], 'n'))
41  call assert_equal([3, 13, 28], sort([13, 28, 3], 'n'))
42  " strings are not sorted
43  call assert_equal(['13', '28', '3'], sort(['13', '28', '3'], 'n'))
44endfunc
45
46func Test_sort_numbers()
47  call assert_equal([3, 13, 28], sort([13, 28, 3], 'N'))
48  call assert_equal(['3', '13', '28'], sort(['13', '28', '3'], 'N'))
49endfunc
50
51func Test_sort_float()
52  CheckFeature float
53  call assert_equal([0.28, 3, 13.5], sort([13.5, 0.28, 3], 'f'))
54endfunc
55
56func Test_sort_nested()
57  " test ability to call sort() from a compare function
58  call assert_equal([1, 3, 5], sort([3, 1, 5], 'Compare1'))
59endfunc
60
61func Test_sort_default()
62  CheckFeature float
63
64  " docs say omitted, empty or zero argument sorts on string representation.
65  call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"]))
66  call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], ''))
67  call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 0))
68  call assert_equal(['2', 'A', 'a', 'AA', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 1))
69  call assert_fails('call sort([3.3, 1, "2"], 3)', "E474:")
70endfunc
71
72" Tests for the ":sort" command.
73func Test_sort_cmd()
74  let tests = [
75	\ {
76	\    'name' : 'Alphabetical sort',
77	\    'cmd' : '%sort',
78	\    'input' : [
79	\	'abc',
80	\	'ab',
81	\	'a',
82	\	'a321',
83	\	'a123',
84	\	'a122',
85	\	'b321',
86	\	'b123',
87	\	'c123d',
88	\	' 123b',
89	\	'c321d',
90	\	'b322b',
91	\	'b321',
92	\	'b321b'
93	\    ],
94	\    'expected' : [
95	\	' 123b',
96	\	'a',
97	\	'a122',
98	\	'a123',
99	\	'a321',
100	\	'ab',
101	\	'abc',
102	\	'b123',
103	\	'b321',
104	\	'b321',
105	\	'b321b',
106	\	'b322b',
107	\	'c123d',
108	\	'c321d'
109	\    ]
110	\ },
111	\ {
112	\    'name' : 'Numeric sort',
113	\    'cmd' : '%sort n',
114	\    'input' : [
115	\	'abc',
116	\	'ab',
117	\	'a321',
118	\	'a123',
119	\	'a122',
120	\	'a',
121	\	'x-22',
122	\	'b321',
123	\	'b123',
124	\	'',
125	\	'c123d',
126	\	'-24',
127	\	' 123b',
128	\	'c321d',
129	\	'0',
130	\	'b322b',
131	\	'b321',
132	\	'b321b'
133	\    ],
134	\    'expected' : [
135	\	'abc',
136	\	'ab',
137	\	'a',
138	\	'',
139	\	'-24',
140	\	'x-22',
141	\	'0',
142	\	'a122',
143	\	'a123',
144	\	'b123',
145	\	'c123d',
146	\	' 123b',
147	\	'a321',
148	\	'b321',
149	\	'c321d',
150	\	'b321',
151	\	'b321b',
152	\	'b322b'
153	\    ]
154	\ },
155	\ {
156	\    'name' : 'Hexadecimal sort',
157	\    'cmd' : '%sort x',
158	\    'input' : [
159	\	'abc',
160	\	'ab',
161	\	'a',
162	\	'a321',
163	\	'a123',
164	\	'a122',
165	\	'b321',
166	\	'b123',
167	\	'c123d',
168	\	' 123b',
169	\	'c321d',
170	\	'b322b',
171	\	'b321',
172	\	'b321b'
173	\    ],
174	\    'expected' : [
175	\	'a',
176	\	'ab',
177	\	'abc',
178	\	' 123b',
179	\	'a122',
180	\	'a123',
181	\	'a321',
182	\	'b123',
183	\	'b321',
184	\	'b321',
185	\	'b321b',
186	\	'b322b',
187	\	'c123d',
188	\	'c321d'
189	\    ]
190	\ },
191	\ {
192	\    'name' : 'Alphabetical unique sort',
193	\    'cmd' : '%sort u',
194	\    'input' : [
195	\	'abc',
196	\	'ab',
197	\	'a',
198	\	'a321',
199	\	'a123',
200	\	'a122',
201	\	'b321',
202	\	'b123',
203	\	'c123d',
204	\	' 123b',
205	\	'c321d',
206	\	'b322b',
207	\	'b321',
208	\	'b321b'
209	\    ],
210	\    'expected' : [
211	\	' 123b',
212	\	'a',
213	\	'a122',
214	\	'a123',
215	\	'a321',
216	\	'ab',
217	\	'abc',
218	\	'b123',
219	\	'b321',
220	\	'b321b',
221	\	'b322b',
222	\	'c123d',
223	\	'c321d'
224	\    ]
225	\ },
226	\ {
227	\    'name' : 'Alphabetical reverse sort',
228	\    'cmd' : '%sort!',
229	\    'input' : [
230	\	'abc',
231	\	'ab',
232	\	'a',
233	\	'a321',
234	\	'a123',
235	\	'a122',
236	\	'b321',
237	\	'b123',
238	\	'c123d',
239	\	' 123b',
240	\	'c321d',
241	\	'b322b',
242	\	'b321',
243	\	'b321b'
244	\    ],
245	\    'expected' : [
246	\	'c321d',
247	\	'c123d',
248	\	'b322b',
249	\	'b321b',
250	\	'b321',
251	\	'b321',
252	\	'b123',
253	\	'abc',
254	\	'ab',
255	\	'a321',
256	\	'a123',
257	\	'a122',
258	\	'a',
259	\	' 123b',
260	\    ]
261	\ },
262	\ {
263	\    'name' : 'Numeric reverse sort',
264	\    'cmd' : '%sort! n',
265	\    'input' : [
266	\	'abc',
267	\	'ab',
268	\	'a',
269	\	'a321',
270	\	'a123',
271	\	'a122',
272	\	'b321',
273	\	'b123',
274	\	'c123d',
275	\	' 123b',
276	\	'c321d',
277	\	'b322b',
278	\	'b321',
279	\	'b321b'
280	\    ],
281	\    'expected' : [
282	\	'b322b',
283	\	'b321b',
284	\	'b321',
285	\	'c321d',
286	\	'b321',
287	\	'a321',
288	\	' 123b',
289	\	'c123d',
290	\	'b123',
291	\	'a123',
292	\	'a122',
293	\	'a',
294	\	'ab',
295	\	'abc'
296	\    ]
297	\ },
298	\ {
299	\    'name' : 'Unique reverse sort',
300	\    'cmd' : 'sort! u',
301	\    'input' : [
302	\	'abc',
303	\	'ab',
304	\	'a',
305	\	'a321',
306	\	'a123',
307	\	'a122',
308	\	'b321',
309	\	'b123',
310	\	'c123d',
311	\	' 123b',
312	\	'c321d',
313	\	'b322b',
314	\	'b321',
315	\	'b321b'
316	\    ],
317	\    'expected' : [
318	\	'c321d',
319	\	'c123d',
320	\	'b322b',
321	\	'b321b',
322	\	'b321',
323	\	'b123',
324	\	'abc',
325	\	'ab',
326	\	'a321',
327	\	'a123',
328	\	'a122',
329	\	'a',
330	\	' 123b',
331	\    ]
332	\ },
333	\ {
334	\    'name' : 'Octal sort',
335	\    'cmd' : 'sort o',
336	\    'input' : [
337	\	'abc',
338	\	'ab',
339	\	'a',
340	\	'a321',
341	\	'a123',
342	\	'a122',
343	\	'b321',
344	\	'b123',
345	\	'c123d',
346	\	' 123b',
347	\	'c321d',
348	\	'b322b',
349	\	'b321',
350	\	'b321b',
351	\	'',
352	\	''
353	\    ],
354	\    'expected' : [
355	\	'abc',
356	\	'ab',
357	\	'a',
358	\	'',
359	\	'',
360	\	'a122',
361	\	'a123',
362	\	'b123',
363	\	'c123d',
364	\	' 123b',
365	\	'a321',
366	\	'b321',
367	\	'c321d',
368	\	'b321',
369	\	'b321b',
370	\	'b322b'
371	\    ]
372	\ },
373	\ {
374	\    'name' : 'Reverse hexadecimal sort',
375	\    'cmd' : 'sort! x',
376	\    'input' : [
377	\	'abc',
378	\	'ab',
379	\	'a',
380	\	'a321',
381	\	'a123',
382	\	'a122',
383	\	'b321',
384	\	'b123',
385	\	'c123d',
386	\	' 123b',
387	\	'c321d',
388	\	'b322b',
389	\	'b321',
390	\	'b321b',
391	\	'',
392	\	''
393	\    ],
394	\    'expected' : [
395	\	'c321d',
396	\	'c123d',
397	\	'b322b',
398	\	'b321b',
399	\	'b321',
400	\	'b321',
401	\	'b123',
402	\	'a321',
403	\	'a123',
404	\	'a122',
405	\	' 123b',
406	\	'abc',
407	\	'ab',
408	\	'a',
409	\	'',
410	\	''
411	\    ]
412	\ },
413	\ {
414	\    'name' : 'Alpha (skip first character) sort',
415	\    'cmd' : 'sort/./',
416	\    'input' : [
417	\	'abc',
418	\	'ab',
419	\	'a',
420	\	'a321',
421	\	'a123',
422	\	'a122',
423	\	'b321',
424	\	'b123',
425	\	'c123d',
426	\	' 123b',
427	\	'c321d',
428	\	'b322b',
429	\	'b321',
430	\	'b321b',
431	\	'',
432	\	''
433	\    ],
434	\    'expected' : [
435	\	'a',
436	\	'',
437	\	'',
438	\	'a122',
439	\	'a123',
440	\	'b123',
441	\	' 123b',
442	\	'c123d',
443	\	'a321',
444	\	'b321',
445	\	'b321',
446	\	'b321b',
447	\	'c321d',
448	\	'b322b',
449	\	'ab',
450	\	'abc'
451	\    ]
452	\ },
453	\ {
454	\    'name' : 'Alpha (skip first 2 characters) sort',
455	\    'cmd' : 'sort/../',
456	\    'input' : [
457	\	'abc',
458	\	'ab',
459	\	'a',
460	\	'a321',
461	\	'a123',
462	\	'a122',
463	\	'b321',
464	\	'b123',
465	\	'c123d',
466	\	' 123b',
467	\	'c321d',
468	\	'b322b',
469	\	'b321',
470	\	'b321b',
471	\	'',
472	\	''
473	\    ],
474	\    'expected' : [
475	\	'ab',
476	\	'a',
477	\	'',
478	\	'',
479	\	'a321',
480	\	'b321',
481	\	'b321',
482	\	'b321b',
483	\	'c321d',
484	\	'a122',
485	\	'b322b',
486	\	'a123',
487	\	'b123',
488	\	' 123b',
489	\	'c123d',
490	\	'abc'
491	\    ]
492	\ },
493	\ {
494	\    'name' : 'alpha, unique, skip first 2 characters',
495	\    'cmd' : 'sort/../u',
496	\    'input' : [
497	\	'abc',
498	\	'ab',
499	\	'a',
500	\	'a321',
501	\	'a123',
502	\	'a122',
503	\	'b321',
504	\	'b123',
505	\	'c123d',
506	\	' 123b',
507	\	'c321d',
508	\	'b322b',
509	\	'b321',
510	\	'b321b',
511	\	'',
512	\	''
513	\    ],
514	\    'expected' : [
515	\	'ab',
516	\	'a',
517	\	'',
518	\	'a321',
519	\	'b321',
520	\	'b321b',
521	\	'c321d',
522	\	'a122',
523	\	'b322b',
524	\	'a123',
525	\	'b123',
526	\	' 123b',
527	\	'c123d',
528	\	'abc'
529	\    ]
530	\ },
531	\ {
532	\    'name' : 'numeric, skip first character',
533	\    'cmd' : 'sort/./n',
534	\    'input' : [
535	\	'abc',
536	\	'ab',
537	\	'a',
538	\	'a321',
539	\	'a123',
540	\	'a122',
541	\	'b321',
542	\	'b123',
543	\	'c123d',
544	\	' 123b',
545	\	'c321d',
546	\	'b322b',
547	\	'b321',
548	\	'b321b',
549	\	'',
550	\	''
551	\    ],
552	\    'expected' : [
553	\	'abc',
554	\	'ab',
555	\	'a',
556	\	'',
557	\	'',
558	\	'a122',
559	\	'a123',
560	\	'b123',
561	\	'c123d',
562	\	' 123b',
563	\	'a321',
564	\	'b321',
565	\	'c321d',
566	\	'b321',
567	\	'b321b',
568	\	'b322b'
569	\    ]
570	\ },
571	\ {
572	\    'name' : 'alpha, sort on first character',
573	\    'cmd' : 'sort/./r',
574	\    'input' : [
575	\	'abc',
576	\	'ab',
577	\	'a',
578	\	'a321',
579	\	'a123',
580	\	'a122',
581	\	'b321',
582	\	'b123',
583	\	'c123d',
584	\	' 123b',
585	\	'c321d',
586	\	'b322b',
587	\	'b321',
588	\	'b321b',
589	\	'',
590	\	''
591	\    ],
592	\    'expected' : [
593	\	'',
594	\	'',
595	\	' 123b',
596	\	'abc',
597	\	'ab',
598	\	'a',
599	\	'a321',
600	\	'a123',
601	\	'a122',
602	\	'b321',
603	\	'b123',
604	\	'b322b',
605	\	'b321',
606	\	'b321b',
607	\	'c123d',
608	\	'c321d'
609	\    ]
610	\ },
611	\ {
612	\    'name' : 'alpha, sort on first 2 characters',
613	\    'cmd' : 'sort/../r',
614	\    'input' : [
615	\	'abc',
616	\	'ab',
617	\	'a',
618	\	'a321',
619	\	'a123',
620	\	'a122',
621	\	'b321',
622	\	'b123',
623	\	'c123d',
624	\	' 123b',
625	\	'c321d',
626	\	'b322b',
627	\	'b321',
628	\	'b321b',
629	\	'',
630	\	''
631	\    ],
632	\    'expected' : [
633	\	'a',
634	\	'',
635	\	'',
636	\	' 123b',
637	\	'a123',
638	\	'a122',
639	\	'a321',
640	\	'abc',
641	\	'ab',
642	\	'b123',
643	\	'b321',
644	\	'b322b',
645	\	'b321',
646	\	'b321b',
647	\	'c123d',
648	\	'c321d'
649	\    ]
650	\ },
651	\ {
652	\    'name' : 'numeric, sort on first character',
653	\    'cmd' : 'sort/./rn',
654	\    'input' : [
655	\	'abc',
656	\	'ab',
657	\	'a',
658	\	'a321',
659	\	'a123',
660	\	'a122',
661	\	'b321',
662	\	'b123',
663	\	'c123d',
664	\	' 123b',
665	\	'c321d',
666	\	'b322b',
667	\	'b321',
668	\	'b321b',
669	\	'',
670	\	''
671	\    ],
672	\    'expected' : [
673	\	'abc',
674	\	'ab',
675	\	'a',
676	\	'a321',
677	\	'a123',
678	\	'a122',
679	\	'b321',
680	\	'b123',
681	\	'c123d',
682	\	' 123b',
683	\	'c321d',
684	\	'b322b',
685	\	'b321',
686	\	'b321b',
687	\	'',
688	\	''
689	\    ]
690	\ },
691	\ {
692	\    'name' : 'alpha, skip past first digit',
693	\    'cmd' : 'sort/\d/',
694	\    'input' : [
695	\	'abc',
696	\	'ab',
697	\	'a',
698	\	'a321',
699	\	'a123',
700	\	'a122',
701	\	'b321',
702	\	'b123',
703	\	'c123d',
704	\	' 123b',
705	\	'c321d',
706	\	'b322b',
707	\	'b321',
708	\	'b321b',
709	\	'',
710	\	''
711	\    ],
712	\    'expected' : [
713	\	'abc',
714	\	'ab',
715	\	'a',
716	\	'',
717	\	'',
718	\	'a321',
719	\	'b321',
720	\	'b321',
721	\	'b321b',
722	\	'c321d',
723	\	'a122',
724	\	'b322b',
725	\	'a123',
726	\	'b123',
727	\	' 123b',
728	\	'c123d'
729	\    ]
730	\ },
731	\ {
732	\    'name' : 'alpha, sort on first digit',
733	\    'cmd' : 'sort/\d/r',
734	\    'input' : [
735	\	'abc',
736	\	'ab',
737	\	'a',
738	\	'a321',
739	\	'a123',
740	\	'a122',
741	\	'b321',
742	\	'b123',
743	\	'c123d',
744	\	' 123b',
745	\	'c321d',
746	\	'b322b',
747	\	'b321',
748	\	'b321b',
749	\	'',
750	\	''
751	\    ],
752	\    'expected' : [
753	\	'abc',
754	\	'ab',
755	\	'a',
756	\	'',
757	\	'',
758	\	'a123',
759	\	'a122',
760	\	'b123',
761	\	'c123d',
762	\	' 123b',
763	\	'a321',
764	\	'b321',
765	\	'c321d',
766	\	'b322b',
767	\	'b321',
768	\	'b321b'
769	\    ]
770	\ },
771	\ {
772	\    'name' : 'numeric, skip past first digit',
773	\    'cmd' : 'sort/\d/n',
774	\    'input' : [
775	\	'abc',
776	\	'ab',
777	\	'a',
778	\	'a321',
779	\	'a123',
780	\	'a122',
781	\	'b321',
782	\	'b123',
783	\	'c123d',
784	\	' 123b',
785	\	'c321d',
786	\	'b322b',
787	\	'b321',
788	\	'b321b',
789	\	'',
790	\	''
791	\    ],
792	\    'expected' : [
793	\	'abc',
794	\	'ab',
795	\	'a',
796	\	'',
797	\	'',
798	\	'a321',
799	\	'b321',
800	\	'c321d',
801	\	'b321',
802	\	'b321b',
803	\	'a122',
804	\	'b322b',
805	\	'a123',
806	\	'b123',
807	\	'c123d',
808	\	' 123b'
809	\    ]
810	\ },
811	\ {
812	\    'name' : 'numeric, sort on first digit',
813	\    'cmd' : 'sort/\d/rn',
814	\    'input' : [
815	\	'abc',
816	\	'ab',
817	\	'a',
818	\	'a321',
819	\	'a123',
820	\	'a122',
821	\	'b321',
822	\	'b123',
823	\	'c123d',
824	\	' 123b',
825	\	'c321d',
826	\	'b322b',
827	\	'b321',
828	\	'b321b',
829	\	'',
830	\	''
831	\    ],
832	\    'expected' : [
833	\	'abc',
834	\	'ab',
835	\	'a',
836	\	'',
837	\	'',
838	\	'a123',
839	\	'a122',
840	\	'b123',
841	\	'c123d',
842	\	' 123b',
843	\	'a321',
844	\	'b321',
845	\	'c321d',
846	\	'b322b',
847	\	'b321',
848	\	'b321b'
849	\    ]
850	\ },
851	\ {
852	\    'name' : 'alpha, skip past first 2 digits',
853	\    'cmd' : 'sort/\d\d/',
854	\    'input' : [
855	\	'abc',
856	\	'ab',
857	\	'a',
858	\	'a321',
859	\	'a123',
860	\	'a122',
861	\	'b321',
862	\	'b123',
863	\	'c123d',
864	\	' 123b',
865	\	'c321d',
866	\	'b322b',
867	\	'b321',
868	\	'b321b',
869	\	'',
870	\	''
871	\    ],
872	\    'expected' : [
873	\	'abc',
874	\	'ab',
875	\	'a',
876	\	'',
877	\	'',
878	\	'a321',
879	\	'b321',
880	\	'b321',
881	\	'b321b',
882	\	'c321d',
883	\	'a122',
884	\	'b322b',
885	\	'a123',
886	\	'b123',
887	\	' 123b',
888	\	'c123d'
889	\    ]
890	\ },
891	\ {
892	\    'name' : 'numeric, skip past first 2 digits',
893	\    'cmd' : 'sort/\d\d/n',
894	\    'input' : [
895	\	'abc',
896	\	'ab',
897	\	'a',
898	\	'a321',
899	\	'a123',
900	\	'a122',
901	\	'b321',
902	\	'b123',
903	\	'c123d',
904	\	' 123b',
905	\	'c321d',
906	\	'b322b',
907	\	'b321',
908	\	'b321b',
909	\	'',
910	\	''
911	\    ],
912	\    'expected' : [
913	\	'abc',
914	\	'ab',
915	\	'a',
916	\	'',
917	\	'',
918	\	'a321',
919	\	'b321',
920	\	'c321d',
921	\	'b321',
922	\	'b321b',
923	\	'a122',
924	\	'b322b',
925	\	'a123',
926	\	'b123',
927	\	'c123d',
928	\	' 123b'
929	\    ]
930	\ },
931	\ {
932	\    'name' : 'hexadecimal, skip past first 2 digits',
933	\    'cmd' : 'sort/\d\d/x',
934	\    'input' : [
935	\	'abc',
936	\	'ab',
937	\	'a',
938	\	'a321',
939	\	'a123',
940	\	'a122',
941	\	'b321',
942	\	'b123',
943	\	'c123d',
944	\	' 123b',
945	\	'c321d',
946	\	'b322b',
947	\	'b321',
948	\	'b321b',
949	\	'',
950	\	''
951	\    ],
952	\    'expected' : [
953	\	'abc',
954	\	'ab',
955	\	'a',
956	\	'',
957	\	'',
958	\	'a321',
959	\	'b321',
960	\	'b321',
961	\	'a122',
962	\	'a123',
963	\	'b123',
964	\	'b321b',
965	\	'c321d',
966	\	'b322b',
967	\	' 123b',
968	\	'c123d'
969	\    ]
970	\ },
971	\ {
972	\    'name' : 'alpha, sort on first 2 digits',
973	\    'cmd' : 'sort/\d\d/r',
974	\    'input' : [
975	\	'abc',
976	\	'ab',
977	\	'a',
978	\	'a321',
979	\	'a123',
980	\	'a122',
981	\	'b321',
982	\	'b123',
983	\	'c123d',
984	\	' 123b',
985	\	'c321d',
986	\	'b322b',
987	\	'b321',
988	\	'b321b',
989	\	'',
990	\	''
991	\    ],
992	\    'expected' : [
993	\	'abc',
994	\	'ab',
995	\	'a',
996	\	'',
997	\	'',
998	\	'a123',
999	\	'a122',
1000	\	'b123',
1001	\	'c123d',
1002	\	' 123b',
1003	\	'a321',
1004	\	'b321',
1005	\	'c321d',
1006	\	'b322b',
1007	\	'b321',
1008	\	'b321b'
1009	\    ]
1010	\ },
1011	\ {
1012	\    'name' : 'numeric, sort on first 2 digits',
1013	\    'cmd' : 'sort/\d\d/rn',
1014	\    'input' : [
1015	\	'abc',
1016	\	'ab',
1017	\	'a',
1018	\	'a321',
1019	\	'a123',
1020	\	'a122',
1021	\	'b321',
1022	\	'b123',
1023	\	'c123d',
1024	\	' 123b',
1025	\	'c321d',
1026	\	'b322b',
1027	\	'b321',
1028	\	'b321b',
1029	\	'',
1030	\	''
1031	\    ],
1032	\    'expected' : [
1033	\	'abc',
1034	\	'ab',
1035	\	'a',
1036	\	'',
1037	\	'',
1038	\	'a123',
1039	\	'a122',
1040	\	'b123',
1041	\	'c123d',
1042	\	' 123b',
1043	\	'a321',
1044	\	'b321',
1045	\	'c321d',
1046	\	'b322b',
1047	\	'b321',
1048	\	'b321b'
1049	\    ]
1050	\ },
1051	\ {
1052	\    'name' : 'hexadecimal, sort on first 2 digits',
1053	\    'cmd' : 'sort/\d\d/rx',
1054	\    'input' : [
1055	\	'abc',
1056	\	'ab',
1057	\	'a',
1058	\	'a321',
1059	\	'a123',
1060	\	'a122',
1061	\	'b321',
1062	\	'b123',
1063	\	'c123d',
1064	\	' 123b',
1065	\	'c321d',
1066	\	'b322b',
1067	\	'b321',
1068	\	'b321b',
1069	\	'',
1070	\	''
1071	\    ],
1072	\    'expected' : [
1073	\	'abc',
1074	\	'ab',
1075	\	'a',
1076	\	'',
1077	\	'',
1078	\	'a123',
1079	\	'a122',
1080	\	'b123',
1081	\	'c123d',
1082	\	' 123b',
1083	\	'a321',
1084	\	'b321',
1085	\	'c321d',
1086	\	'b322b',
1087	\	'b321',
1088	\	'b321b'
1089	\    ]
1090	\ },
1091	\ {
1092	\    'name' : 'binary',
1093	\    'cmd' : 'sort b',
1094	\    'input' : [
1095	\	'0b111000',
1096	\	'0b101100',
1097	\	'0b101001',
1098	\	'0b101001',
1099	\	'0b101000',
1100	\	'0b000000',
1101	\	'0b001000',
1102	\	'0b010000',
1103	\	'0b101000',
1104	\	'0b100000',
1105	\	'0b101010',
1106	\	'0b100010',
1107	\	'0b100100',
1108	\	'0b100010',
1109	\	'',
1110	\	''
1111	\    ],
1112	\    'expected' : [
1113	\	'',
1114	\	'',
1115	\	'0b000000',
1116	\	'0b001000',
1117	\	'0b010000',
1118	\	'0b100000',
1119	\	'0b100010',
1120	\	'0b100010',
1121	\	'0b100100',
1122	\	'0b101000',
1123	\	'0b101000',
1124	\	'0b101001',
1125	\	'0b101001',
1126	\	'0b101010',
1127	\	'0b101100',
1128	\	'0b111000'
1129	\    ]
1130	\ },
1131	\ {
1132	\    'name' : 'binary with leading characters',
1133	\    'cmd' : 'sort b',
1134	\    'input' : [
1135	\	'0b100010',
1136	\	'0b010000',
1137	\	' 0b101001',
1138	\	'b0b101100',
1139	\	'0b100010',
1140	\	' 0b100100',
1141	\	'a0b001000',
1142	\	'0b101000',
1143	\	'0b101000',
1144	\	'a0b101001',
1145	\	'ab0b100000',
1146	\	'0b101010',
1147	\	'0b000000',
1148	\	'b0b111000',
1149	\	'',
1150	\	''
1151	\    ],
1152	\    'expected' : [
1153	\	'',
1154	\	'',
1155	\	'0b000000',
1156	\	'a0b001000',
1157	\	'0b010000',
1158	\	'ab0b100000',
1159	\	'0b100010',
1160	\	'0b100010',
1161	\	' 0b100100',
1162	\	'0b101000',
1163	\	'0b101000',
1164	\	' 0b101001',
1165	\	'a0b101001',
1166	\	'0b101010',
1167	\	'b0b101100',
1168	\	'b0b111000'
1169	\    ]
1170	\ },
1171	\ {
1172	\    'name' : 'alphabetical, sorted input',
1173	\    'cmd' : 'sort',
1174	\    'input' : [
1175	\	'a',
1176	\	'b',
1177	\	'c',
1178	\    ],
1179	\    'expected' : [
1180	\	'a',
1181	\	'b',
1182	\	'c',
1183	\    ]
1184	\ },
1185	\ {
1186	\    'name' : 'alphabetical, sorted input, unique at end',
1187	\    'cmd' : 'sort u',
1188	\    'input' : [
1189	\	'aa',
1190	\	'bb',
1191	\	'cc',
1192	\	'cc',
1193	\    ],
1194	\    'expected' : [
1195	\	'aa',
1196	\	'bb',
1197	\	'cc',
1198	\    ]
1199	\ },
1200	\ {
1201	\    'name' : 'sort one line buffer',
1202	\    'cmd' : 'sort',
1203	\    'input' : [
1204	\	'single line'
1205	\    ],
1206	\    'expected' : [
1207	\	'single line'
1208	\    ]
1209	\ },
1210	\ {
1211	\    'name' : 'sort ignoring case',
1212	\    'cmd' : '%sort i',
1213	\    'input' : [
1214	\	'BB',
1215	\	'Cc',
1216	\	'aa'
1217	\    ],
1218	\    'expected' : [
1219	\	'aa',
1220	\	'BB',
1221	\	'Cc'
1222	\    ]
1223	\ },
1224	\ ]
1225
1226    " With the following locales, the accentuated letters are ordered
1227    " similarly to the non-accentuated letters...
1228    let lc = execute('language collate')
1229    if lc =~? '"\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8"'
1230      let tests += [
1231	\ {
1232	\    'name' : 'sort with locale',
1233	\    'cmd' : '%sort l',
1234	\    'input' : [
1235	\	'A',
1236	\	'E',
1237	\	'O',
1238	\	'À',
1239	\	'È',
1240	\	'É',
1241	\	'Ô',
1242	\	'Œ',
1243	\	'Z',
1244	\	'a',
1245	\	'e',
1246	\	'o',
1247	\	'à',
1248	\	'è',
1249	\	'é',
1250	\	'ô',
1251	\	'œ',
1252	\	'z'
1253	\    ],
1254	\    'expected' : [
1255	\	'a',
1256	\	'A',
1257	\	'à',
1258	\	'À',
1259	\	'e',
1260	\	'E',
1261	\	'é',
1262	\	'É',
1263	\	'è',
1264	\	'È',
1265	\	'o',
1266	\	'O',
1267	\	'ô',
1268	\	'Ô',
1269	\	'œ',
1270	\	'Œ',
1271	\	'z',
1272	\	'Z'
1273	\    ]
1274	\ },
1275	\ ]
1276  endif
1277  if has('float')
1278    let tests += [
1279          \ {
1280          \    'name' : 'float',
1281          \    'cmd' : 'sort f',
1282          \    'input' : [
1283          \	'1.234',
1284          \	'0.88',
1285          \	'  +  123.456',
1286          \	'1.15e-6',
1287          \	'-1.1e3',
1288          \	'-1.01e3',
1289          \	'',
1290          \	''
1291          \    ],
1292          \    'expected' : [
1293          \	'',
1294          \	'',
1295          \	'-1.1e3',
1296          \	'-1.01e3',
1297          \	'1.15e-6',
1298          \	'0.88',
1299          \	'1.234',
1300          \	'  +  123.456'
1301          \    ]
1302          \ },
1303          \ ]
1304  endif
1305
1306  for t in tests
1307    enew!
1308    call append(0, t.input)
1309    $delete _
1310    setlocal nomodified
1311    execute t.cmd
1312
1313    call assert_equal(t.expected, getline(1, '$'), t.name)
1314
1315    " Previously, the ":sort" command would set 'modified' even if the buffer
1316    " contents did not change.  Here, we check that this problem is fixed.
1317    if t.input == t.expected
1318      call assert_false(&modified, t.name . ': &mod is not correct')
1319    else
1320      call assert_true(&modified, t.name . ': &mod is not correct')
1321    endif
1322  endfor
1323
1324  " Needs atleast two lines for this test
1325  call setline(1, ['line1', 'line2'])
1326  call assert_fails('sort no', 'E474:')
1327  call assert_fails('sort c', 'E475:')
1328  call assert_fails('sort #pat%', 'E654:')
1329  call assert_fails('sort /\%(/', 'E53:')
1330
1331  enew!
1332endfunc
1333
1334func Test_sort_large_num()
1335  new
1336  a
1337-2147483648
1338-2147483647
1339
1340-1
13410
13421
1343-2147483646
13442147483646
13452147483647
13462147483647
1347-2147483648
1348abc
1349
1350.
1351  " Numerical sort. Non-numeric lines are ordered before numerical lines.
1352  " Ordering of non-numerical is stable.
1353  sort n
1354  call assert_equal(['',
1355  \                  'abc',
1356  \                  '',
1357  \                  '-2147483648',
1358  \                  '-2147483648',
1359  \                  '-2147483647',
1360  \                  '-2147483646',
1361  \                  '-1',
1362  \                  '0',
1363  \                  '1',
1364  \                  '2147483646',
1365  \                  '2147483647',
1366  \                  '2147483647'], getline(1, '$'))
1367  bwipe!
1368
1369  new
1370  a
1371-9223372036854775808
1372-9223372036854775807
1373
1374-1
13750
13761
1377-9223372036854775806
13789223372036854775806
13799223372036854775807
13809223372036854775807
1381-9223372036854775808
1382abc
1383
1384.
1385  sort n
1386  call assert_equal(['',
1387  \                  'abc',
1388  \                  '',
1389  \                  '-9223372036854775808',
1390  \                  '-9223372036854775808',
1391  \                  '-9223372036854775807',
1392  \                  '-9223372036854775806',
1393  \                  '-1',
1394  \                  '0',
1395  \                  '1',
1396  \                  '9223372036854775806',
1397  \                  '9223372036854775807',
1398  \                  '9223372036854775807'], getline(1, '$'))
1399  bwipe!
1400endfunc
1401
1402
1403func Test_sort_cmd_report()
1404    enew!
1405    call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
1406    $delete _
1407    setlocal nomodified
1408    let res = execute('%sort u')
1409
1410    call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
1411    call assert_match("6 fewer lines", res)
1412    enew!
1413    call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
1414    $delete _
1415    setlocal nomodified report=10
1416    let res = execute('%sort u')
1417
1418    call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
1419    call assert_equal("", res)
1420    enew!
1421    call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
1422    $delete _
1423    setl report&vim
1424    setlocal nomodified
1425    let res = execute('1g/^/%sort u')
1426
1427    call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
1428    " the output comes from the :g command, not from the :sort
1429    call assert_match("6 fewer lines", res)
1430    enew!
1431endfunc
1432
1433" Test for a :sort command followed by another command
1434func Test_sort_followed_by_cmd()
1435  new
1436  let var = ''
1437  call setline(1, ['cc', 'aa', 'bb'])
1438  %sort | let var = "sortcmdtest"
1439  call assert_equal(var, "sortcmdtest")
1440  call assert_equal(['aa', 'bb', 'cc'], getline(1, '$'))
1441  " Test for :sort followed by a comment
1442  call setline(1, ['3b', '1c', '2a'])
1443  %sort /\d\+/ " sort alphabetically
1444  call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
1445  close!
1446endfunc
1447
1448" Test for :sort using last search pattern
1449func Test_sort_last_search_pat()
1450  new
1451  let @/ = '\d\+'
1452  call setline(1, ['3b', '1c', '2a'])
1453  sort //
1454  call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
1455  close!
1456endfunc
1457
1458" Test for :sort with no last search pattern
1459func Test_sort_with_no_last_search_pat()
1460  let lines =<< trim [SCRIPT]
1461    call setline(1, ['3b', '1c', '2a'])
1462    call assert_fails('sort //', 'E35:')
1463    call writefile(v:errors, 'Xresult')
1464    qall!
1465  [SCRIPT]
1466  call writefile(lines, 'Xscript')
1467  if RunVim([], [], '--clean -S Xscript')
1468    call assert_equal([], readfile('Xresult'))
1469  endif
1470  call delete('Xscript')
1471  call delete('Xresult')
1472endfunc
1473
1474" Test for retaining marks across a :sort
1475func Test_sort_with_marks()
1476  new
1477  call setline(1, ['cc', 'aa', 'bb'])
1478  call setpos("'c", [0, 1, 0, 0])
1479  call setpos("'a", [0, 2, 0, 0])
1480  call setpos("'b", [0, 3, 0, 0])
1481  %sort
1482  call assert_equal(['aa', 'bb', 'cc'], getline(1, '$'))
1483  call assert_equal(2, line("'a"))
1484  call assert_equal(3, line("'b"))
1485  call assert_equal(1, line("'c"))
1486  close!
1487endfunc
1488
1489" vim: shiftwidth=2 sts=2 expandtab
1490