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