Ai
12 Star 47 Fork 0

Gitee 极速下载/Vim

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/vim/vim
克隆/下载
test_sort.vim 25.54 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
" Tests for the "sort()" function and for the ":sort" command.
import './util/vim9.vim' as v9
func Compare1(a, b) abort
call sort(range(3), 'Compare2')
return a:a - a:b
endfunc
func Compare2(a, b) abort
return a:a - a:b
endfunc
func Test_sort_strings()
" numbers compared as strings
call assert_equal([1, 2, 3], sort([3, 2, 1]))
call assert_equal([13, 28, 3], sort([3, 28, 13]))
call assert_equal(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'Œ', 'œ'],
\ sort(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'Œ']))
call assert_equal(['A', 'a', 'o', 'O', 'p', 'P', 'Ä', 'Ô', 'ä', 'ô', 'Œ', 'œ'],
\ sort(['A', 'a', 'o', 'O', 'œ', 'Œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'i'))
" This does not appear to work correctly on Mac.
if !has('mac')
if v:collate =~? '^\(en\|fr\)_ca.utf-\?8$'
" with Canadian English capitals come before lower case.
" 'Œ' is omitted because it can sort before or after 'œ'
call assert_equal(['A', 'a', 'Ä', 'ä', 'O', 'o', 'Ô', 'ô', 'œ', 'P', 'p'],
\ sort(['A', 'a', 'o', 'O', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
elseif v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$'
" With the following locales, the accentuated letters are ordered
" similarly to the non-accentuated letters...
call assert_equal(['a', 'A', 'ä', 'Ä', 'o', 'O', 'ô', 'Ô', 'œ', 'Œ', 'p', 'P'],
\ sort(['A', 'a', 'o', 'O', 'œ', 'Œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
elseif v:collate =~? '^sv.*utf-\?8$'
" ... whereas with a Swedish locale, the accentuated letters are ordered
" after Z.
call assert_equal(['a', 'A', 'o', 'O', 'p', 'P', 'ä', 'Ä', 'œ', 'œ', 'ô', 'Ô'],
\ sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
endif
endif
endfunc
func Test_sort_null_string()
" null strings are sorted as empty strings.
call assert_equal(['', 'a', 'b'], sort(['b', test_null_string(), 'a']))
endfunc
func Test_sort_numeric()
call assert_equal([1, 2, 3], sort([3, 2, 1], 'n'))
call assert_equal([3, 13, 28], sort([13, 28, 3], 'n'))
" strings are not sorted
call assert_equal(['13', '28', '3'], sort(['13', '28', '3'], 'n'))
endfunc
func Test_sort_numbers()
call assert_equal([3, 13, 28], sort([13, 28, 3], 'N'))
call assert_equal(['3', '13', '28'], sort(['13', '28', '3'], 'N'))
vim9cmd call assert_equal(['3', '13', '28'], sort(['13', '28', '3'], 'N'))
call assert_equal([3997, 4996], sort([4996, 3997], 'Compare1'))
endfunc
func Test_sort_float()
call assert_equal([0.28, 3, 13.5], sort([13.5, 0.28, 3], 'f'))
endfunc
func Test_sort_nested()
" test ability to call sort() from a compare function
call assert_equal([1, 3, 5], sort([3, 1, 5], 'Compare1'))
endfunc
func Test_sort_default()
" docs say omitted, empty or zero argument sorts on string representation.
call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"]))
call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], ''))
call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 0))
call assert_equal(['2', 'A', 'a', 'AA', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 1))
call assert_fails('call sort([3.3, 1, "2"], 3)', "E474:")
endfunc
" Tests for the ":sort" command.
func Test_sort_cmd()
let tests = [
\ {
\ 'name' : 'Alphabetical sort',
\ 'cmd' : '%sort',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ ' 123b',
\ 'a',
\ 'a122',
\ 'a123',
\ 'a321',
\ 'ab',
\ 'abc',
\ 'b123',
\ 'b321',
\ 'b321',
\ 'b321b',
\ 'b322b',
\ 'c123d',
\ 'c321d'
\ ]
\ },
\ {
\ 'name' : 'Numeric sort',
\ 'cmd' : '%sort n',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'a',
\ 'x-22',
\ 'b321',
\ 'b123',
\ '',
\ 'c123d',
\ '-24',
\ ' 123b',
\ 'c321d',
\ '0',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '-24',
\ 'x-22',
\ '0',
\ 'a122',
\ 'a123',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b321',
\ 'b321b',
\ 'b322b'
\ ]
\ },
\ {
\ 'name' : 'Hexadecimal sort',
\ 'cmd' : '%sort x',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ 'a',
\ 'ab',
\ 'abc',
\ ' 123b',
\ 'a122',
\ 'a123',
\ 'a321',
\ 'b123',
\ 'b321',
\ 'b321',
\ 'b321b',
\ 'b322b',
\ 'c123d',
\ 'c321d'
\ ]
\ },
\ {
\ 'name' : 'Alphabetical unique sort',
\ 'cmd' : '%sort u',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ ' 123b',
\ 'a',
\ 'a122',
\ 'a123',
\ 'a321',
\ 'ab',
\ 'abc',
\ 'b123',
\ 'b321',
\ 'b321b',
\ 'b322b',
\ 'c123d',
\ 'c321d'
\ ]
\ },
\ {
\ 'name' : 'Alphabetical reverse sort',
\ 'cmd' : '%sort!',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ 'c321d',
\ 'c123d',
\ 'b322b',
\ 'b321b',
\ 'b321',
\ 'b321',
\ 'b123',
\ 'abc',
\ 'ab',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'a',
\ ' 123b',
\ ]
\ },
\ {
\ 'name' : 'Numeric reverse sort',
\ 'cmd' : '%sort! n',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ 'b322b',
\ 'b321b',
\ 'b321',
\ 'c321d',
\ 'b321',
\ 'a321',
\ ' 123b',
\ 'c123d',
\ 'b123',
\ 'a123',
\ 'a122',
\ 'a',
\ 'ab',
\ 'abc'
\ ]
\ },
\ {
\ 'name' : 'Unique reverse sort',
\ 'cmd' : 'sort! u',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ],
\ 'expected' : [
\ 'c321d',
\ 'c123d',
\ 'b322b',
\ 'b321b',
\ 'b321',
\ 'b123',
\ 'abc',
\ 'ab',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'a',
\ ' 123b',
\ ]
\ },
\ {
\ 'name' : 'Octal sort',
\ 'cmd' : 'sort o',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a122',
\ 'a123',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b321',
\ 'b321b',
\ 'b322b'
\ ]
\ },
\ {
\ 'name' : 'Reverse hexadecimal sort',
\ 'cmd' : 'sort! x',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'c321d',
\ 'c123d',
\ 'b322b',
\ 'b321b',
\ 'b321',
\ 'b321',
\ 'b123',
\ 'a321',
\ 'a123',
\ 'a122',
\ ' 123b',
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ ''
\ ]
\ },
\ {
\ 'name' : 'Alpha (skip first character) sort',
\ 'cmd' : 'sort/./',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'a',
\ '',
\ '',
\ 'a122',
\ 'a123',
\ 'b123',
\ ' 123b',
\ 'c123d',
\ 'a321',
\ 'b321',
\ 'b321',
\ 'b321b',
\ 'c321d',
\ 'b322b',
\ 'ab',
\ 'abc'
\ ]
\ },
\ {
\ 'name' : 'Alpha (skip first 2 characters) sort',
\ 'cmd' : 'sort/../',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a321',
\ 'b321',
\ 'b321',
\ 'b321b',
\ 'c321d',
\ 'a122',
\ 'b322b',
\ 'a123',
\ 'b123',
\ ' 123b',
\ 'c123d',
\ 'abc'
\ ]
\ },
\ {
\ 'name' : 'alpha, unique, skip first 2 characters',
\ 'cmd' : 'sort/../u',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'ab',
\ 'a',
\ '',
\ 'a321',
\ 'b321',
\ 'b321b',
\ 'c321d',
\ 'a122',
\ 'b322b',
\ 'a123',
\ 'b123',
\ ' 123b',
\ 'c123d',
\ 'abc'
\ ]
\ },
\ {
\ 'name' : 'numeric, skip first character',
\ 'cmd' : 'sort/./n',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a122',
\ 'a123',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b321',
\ 'b321b',
\ 'b322b'
\ ]
\ },
\ {
\ 'name' : 'alpha, sort on first character',
\ 'cmd' : 'sort/./r',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ '',
\ '',
\ ' 123b',
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ 'c123d',
\ 'c321d'
\ ]
\ },
\ {
\ 'name' : 'alpha, sort on first 2 characters',
\ 'cmd' : 'sort/../r',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'a',
\ '',
\ '',
\ ' 123b',
\ 'a123',
\ 'a122',
\ 'a321',
\ 'abc',
\ 'ab',
\ 'b123',
\ 'b321',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ 'c123d',
\ 'c321d'
\ ]
\ },
\ {
\ 'name' : 'numeric, sort on first character',
\ 'cmd' : 'sort/./rn',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ]
\ },
\ {
\ 'name' : 'alpha, skip past first digit',
\ 'cmd' : 'sort/\d/',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a321',
\ 'b321',
\ 'b321',
\ 'b321b',
\ 'c321d',
\ 'a122',
\ 'b322b',
\ 'a123',
\ 'b123',
\ ' 123b',
\ 'c123d'
\ ]
\ },
\ {
\ 'name' : 'alpha, sort on first digit',
\ 'cmd' : 'sort/\d/r',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a123',
\ 'a122',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ]
\ },
\ {
\ 'name' : 'numeric, skip past first digit',
\ 'cmd' : 'sort/\d/n',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b321',
\ 'b321b',
\ 'a122',
\ 'b322b',
\ 'a123',
\ 'b123',
\ 'c123d',
\ ' 123b'
\ ]
\ },
\ {
\ 'name' : 'numeric, sort on first digit',
\ 'cmd' : 'sort/\d/rn',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a123',
\ 'a122',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ]
\ },
\ {
\ 'name' : 'alpha, skip past first 2 digits',
\ 'cmd' : 'sort/\d\d/',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a321',
\ 'b321',
\ 'b321',
\ 'b321b',
\ 'c321d',
\ 'a122',
\ 'b322b',
\ 'a123',
\ 'b123',
\ ' 123b',
\ 'c123d'
\ ]
\ },
\ {
\ 'name' : 'numeric, skip past first 2 digits',
\ 'cmd' : 'sort/\d\d/n',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b321',
\ 'b321b',
\ 'a122',
\ 'b322b',
\ 'a123',
\ 'b123',
\ 'c123d',
\ ' 123b'
\ ]
\ },
\ {
\ 'name' : 'hexadecimal, skip past first 2 digits',
\ 'cmd' : 'sort/\d\d/x',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a321',
\ 'b321',
\ 'b321',
\ 'a122',
\ 'a123',
\ 'b123',
\ 'b321b',
\ 'c321d',
\ 'b322b',
\ ' 123b',
\ 'c123d'
\ ]
\ },
\ {
\ 'name' : 'alpha, sort on first 2 digits',
\ 'cmd' : 'sort/\d\d/r',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a123',
\ 'a122',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ]
\ },
\ {
\ 'name' : 'numeric, sort on first 2 digits',
\ 'cmd' : 'sort/\d\d/rn',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a123',
\ 'a122',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ]
\ },
\ {
\ 'name' : 'hexadecimal, sort on first 2 digits',
\ 'cmd' : 'sort/\d\d/rx',
\ 'input' : [
\ 'abc',
\ 'ab',
\ 'a',
\ 'a321',
\ 'a123',
\ 'a122',
\ 'b321',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b',
\ '',
\ ''
\ ],
\ 'expected' : [
\ 'abc',
\ 'ab',
\ 'a',
\ '',
\ '',
\ 'a123',
\ 'a122',
\ 'b123',
\ 'c123d',
\ ' 123b',
\ 'a321',
\ 'b321',
\ 'c321d',
\ 'b322b',
\ 'b321',
\ 'b321b'
\ ]
\ },
\ {
\ 'name' : 'binary',
\ 'cmd' : 'sort b',
\ 'input' : [
\ '0b111000',
\ '0b101100',
\ '0b101001',
\ '0b101001',
\ '0b101000',
\ '0b000000',
\ '0b001000',
\ '0b010000',
\ '0b101000',
\ '0b100000',
\ '0b101010',
\ '0b100010',
\ '0b100100',
\ '0b100010',
\ '',
\ ''
\ ],
\ 'expected' : [
\ '',
\ '',
\ '0b000000',
\ '0b001000',
\ '0b010000',
\ '0b100000',
\ '0b100010',
\ '0b100010',
\ '0b100100',
\ '0b101000',
\ '0b101000',
\ '0b101001',
\ '0b101001',
\ '0b101010',
\ '0b101100',
\ '0b111000'
\ ]
\ },
\ {
\ 'name' : 'binary with leading characters',
\ 'cmd' : 'sort b',
\ 'input' : [
\ '0b100010',
\ '0b010000',
\ ' 0b101001',
\ 'b0b101100',
\ '0b100010',
\ ' 0b100100',
\ 'a0b001000',
\ '0b101000',
\ '0b101000',
\ 'a0b101001',
\ 'ab0b100000',
\ '0b101010',
\ '0b000000',
\ 'b0b111000',
\ '',
\ ''
\ ],
\ 'expected' : [
\ '',
\ '',
\ '0b000000',
\ 'a0b001000',
\ '0b010000',
\ 'ab0b100000',
\ '0b100010',
\ '0b100010',
\ ' 0b100100',
\ '0b101000',
\ '0b101000',
\ ' 0b101001',
\ 'a0b101001',
\ '0b101010',
\ 'b0b101100',
\ 'b0b111000'
\ ]
\ },
\ {
\ 'name' : 'alphabetical, sorted input',
\ 'cmd' : 'sort',
\ 'input' : [
\ 'a',
\ 'b',
\ 'c',
\ ],
\ 'expected' : [
\ 'a',
\ 'b',
\ 'c',
\ ]
\ },
\ {
\ 'name' : 'alphabetical, sorted input, unique at end',
\ 'cmd' : 'sort u',
\ 'input' : [
\ 'aa',
\ 'bb',
\ 'cc',
\ 'cc',
\ ],
\ 'expected' : [
\ 'aa',
\ 'bb',
\ 'cc',
\ ]
\ },
\ {
\ 'name' : 'sort one line buffer',
\ 'cmd' : 'sort',
\ 'input' : [
\ 'single line'
\ ],
\ 'expected' : [
\ 'single line'
\ ]
\ },
\ {
\ 'name' : 'sort ignoring case',
\ 'cmd' : '%sort i',
\ 'input' : [
\ 'BB',
\ 'Cc',
\ 'aa'
\ ],
\ 'expected' : [
\ 'aa',
\ 'BB',
\ 'Cc'
\ ]
\ },
\ ]
" This does not appear to work correctly on Mac.
if !has('mac')
if v:collate =~? '^\(en\|fr\)_ca.utf-\?8$'
" en_CA.utf-8 sorts capitals before lower case
" 'Œ' is omitted because it can sort before or after 'œ'
let tests += [
\ {
\ 'name' : 'sort with locale ' .. v:collate,
\ 'cmd' : '%sort l',
\ 'input' : [
\ 'A',
\ 'E',
\ 'O',
\ 'À',
\ 'È',
\ 'É',
\ 'Ô',
\ 'Z',
\ 'a',
\ 'e',
\ 'o',
\ 'à',
\ 'è',
\ 'é',
\ 'ô',
\ 'œ',
\ 'z'
\ ],
\ 'expected' : [
\ 'A',
\ 'a',
\ 'À',
\ 'à',
\ 'E',
\ 'e',
\ 'É',
\ 'é',
\ 'È',
\ 'è',
\ 'O',
\ 'o',
\ 'Ô',
\ 'ô',
\ 'œ',
\ 'Z',
\ 'z'
\ ]
\ },
\ ]
elseif v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$'
" With these locales, the accentuated letters are ordered
" similarly to the non-accentuated letters.
let tests += [
\ {
\ 'name' : 'sort with locale ' .. v:collate,
\ 'cmd' : '%sort l',
\ 'input' : [
\ 'A',
\ 'E',
\ 'O',
\ 'À',
\ 'È',
\ 'É',
\ 'Ô',
\ 'Œ',
\ 'Z',
\ 'a',
\ 'e',
\ 'o',
\ 'à',
\ 'è',
\ 'é',
\ 'ô',
\ 'œ',
\ 'z'
\ ],
\ 'expected' : [
\ 'a',
\ 'A',
\ 'à',
\ 'À',
\ 'e',
\ 'E',
\ 'é',
\ 'É',
\ 'è',
\ 'È',
\ 'o',
\ 'O',
\ 'ô',
\ 'Ô',
\ 'œ',
\ 'Œ',
\ 'z',
\ 'Z'
\ ]
\ },
\ ]
endif
endif
let tests += [
\ {
\ 'name' : 'float',
\ 'cmd' : 'sort f',
\ 'input' : [
\ '1.234',
\ '0.88',
\ ' + 123.456',
\ '1.15e-6',
\ '-1.1e3',
\ '-1.01e3',
\ '',
\ ''
\ ],
\ 'expected' : [
\ '',
\ '',
\ '-1.1e3',
\ '-1.01e3',
\ '1.15e-6',
\ '0.88',
\ '1.234',
\ ' + 123.456'
\ ]
\ },
\ ]
for t in tests
enew!
call append(0, t.input)
$delete _
setlocal nomodified
execute t.cmd
call assert_equal(t.expected, getline(1, '$'), t.name)
" Previously, the ":sort" command would set 'modified' even if the buffer
" contents did not change. Here, we check that this problem is fixed.
if t.input == t.expected
call assert_false(&modified, t.name . ': &mod is not correct')
else
call assert_true(&modified, t.name . ': &mod is not correct')
endif
endfor
" Needs at least two lines for this test
call setline(1, ['line1', 'line2'])
call assert_fails('sort no', 'E474:')
call assert_fails('sort c', 'E475:')
call assert_fails('sort #pat%', 'E654:')
call assert_fails('sort /\%(/', 'E53:')
enew!
endfunc
func Test_sort_large_num()
new
a
-2147483648
-2147483647
-1
0
1
-2147483646
2147483646
2147483647
2147483647
-2147483648
abc
.
" Numerical sort. Non-numeric lines are ordered before numerical lines.
" Ordering of non-numerical is stable.
sort n
call assert_equal(['',
\ 'abc',
\ '',
\ '-2147483648',
\ '-2147483648',
\ '-2147483647',
\ '-2147483646',
\ '-1',
\ '0',
\ '1',
\ '2147483646',
\ '2147483647',
\ '2147483647'], getline(1, '$'))
bwipe!
new
a
-9223372036854775808
-9223372036854775807
-1
0
1
-9223372036854775806
9223372036854775806
9223372036854775807
9223372036854775807
-9223372036854775808
abc
.
sort n
call assert_equal(['',
\ 'abc',
\ '',
\ '-9223372036854775808',
\ '-9223372036854775808',
\ '-9223372036854775807',
\ '-9223372036854775806',
\ '-1',
\ '0',
\ '1',
\ '9223372036854775806',
\ '9223372036854775807',
\ '9223372036854775807'], getline(1, '$'))
bwipe!
endfunc
func Test_sort_cmd_report()
enew!
call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
$delete _
setlocal nomodified
let res = execute('%sort u')
call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
call assert_match("6 fewer lines", res)
enew!
call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
$delete _
setlocal nomodified report=10
let res = execute('%sort u')
call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
call assert_equal("", res)
enew!
call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
$delete _
setl report&vim
setlocal nomodified
let res = execute('1g/^/%sort u')
call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
" the output comes from the :g command, not from the :sort
call assert_match("6 fewer lines", res)
enew!
endfunc
" Test for a :sort command followed by another command
func Test_sort_followed_by_cmd()
new
let var = ''
call setline(1, ['cc', 'aa', 'bb'])
%sort | let var = "sortcmdtest"
call assert_equal(var, "sortcmdtest")
call assert_equal(['aa', 'bb', 'cc'], getline(1, '$'))
" Test for :sort followed by a comment
call setline(1, ['3b', '1c', '2a'])
%sort /\d\+/ " sort alphabetically
call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
bw!
endfunc
" Test for :sort using last search pattern
func Test_sort_last_search_pat()
new
let @/ = '\d\+'
call setline(1, ['3b', '1c', '2a'])
sort //
call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
bw!
endfunc
" Test for :sort with no last search pattern
func Test_sort_with_no_last_search_pat()
let lines =<< trim [SCRIPT]
call setline(1, ['3b', '1c', '2a'])
call assert_fails('sort //', 'E35:')
call writefile(v:errors, 'Xresult')
qall!
[SCRIPT]
call writefile(lines, 'Xscript', 'D')
if RunVim([], [], '--clean -S Xscript')
call assert_equal([], readfile('Xresult'))
endif
call delete('Xresult')
endfunc
" Test for retaining marks across a :sort
func Test_sort_with_marks()
new
call setline(1, ['cc', 'aa', 'bb'])
call setpos("'c", [0, 1, 0, 0])
call setpos("'a", [0, 2, 0, 0])
call setpos("'b", [0, 3, 0, 0])
%sort
call assert_equal(['aa', 'bb', 'cc'], getline(1, '$'))
call assert_equal(2, line("'a"))
call assert_equal(3, line("'b"))
call assert_equal(1, line("'c"))
bw!
endfunc
" Test for sort() using a dict function
func Test_sort_using_dict_func()
func DictSort(a, b) dict
if self.order == 'reverse'
return a:b - a:a
else
return a:a - a:b
endif
endfunc
let d = #{order: ''}
call assert_equal([1, 2, 3], sort([2, 1, 3], 'DictSort', d))
let d = #{order: 'reverse'}
call assert_equal([3, 2, 1], sort([2, 1, 3], 'DictSort', d))
delfunc DictSort
endfunc
" Test for using sort() function with a funcref and large numbers
func Test_sort_funcref_with_large_number()
let lines =<< trim END
call assert_equal(
\ [
\ (188325333471071, 188931909913550),
\ (229539777187355, 229539777187355),
\ (245727634348687, 249469249579525),
\ (264028451845520, 265514296554744),
\ (375117820166731, 378942174241518),
\ (487766135067138, 491977135306566),
\ (535474757750378, 535849288071548)
\ ],
\ [
\ (229539777187355, 229539777187355),
\ (487766135067138, 491977135306566),
\ (188325333471071, 188931909913550),
\ (264028451845520, 265514296554744),
\ (245727634348687, 249469249579525),
\ (375117820166731, 378942174241518),
\ (535474757750378, 535849288071548)
\ ]->sort(LSTART a, b LMIDDLE a[0] - b[0] LEND))
END
call v9.CheckSourceLegacyAndVim9Success(lines)
endfunc
" vim: shiftwidth=2 sts=2 expandtab
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/mirrors/vim.git
git@gitee.com:mirrors/vim.git
mirrors
vim
Vim
master

搜索帮助