robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0937: sort() with a numeric option converts each item on every comparison (#41286)f33c92348a

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