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