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