Add some unit tests for the BinarySearch function.

This commit is contained in:
w0rp 2016-10-13 21:24:47 +01:00
parent f506887b28
commit a089fabb5c
3 changed files with 62 additions and 36 deletions

View File

@ -18,41 +18,6 @@ function! s:GetMessage(linter, type, text) abort
return printf(l:msg, l:text) return printf(l:msg, l:text)
endfunction endfunction
" This function will perform a binary search to find a message from the
" loclist to echo when the cursor moves.
function! s:BinarySearch(loclist, line, column) abort
let l:min = 0
let l:max = len(a:loclist) - 1
let l:last_column_match = -1
while 1
if l:max < l:min
return l:last_column_match
endif
let l:mid = (l:min + l:max) / 2
let l:obj = a:loclist[l:mid]
" Binary search to get on the same line
if a:loclist[l:mid]['lnum'] < a:line
let l:min = l:mid + 1
elseif a:loclist[l:mid]['lnum'] > a:line
let l:max = l:mid - 1
else
let l:last_column_match = l:mid
" Binary search to get the same column, or near it
if a:loclist[l:mid]['col'] < a:column
let l:min = l:mid + 1
elseif a:loclist[l:mid]['col'] > a:column
let l:max = l:mid - 1
else
return l:mid
endif
endif
endwhile
endfunction
function! ale#cursor#TruncatedEcho(message) abort function! ale#cursor#TruncatedEcho(message) abort
let l:message = a:message let l:message = a:message
" Change tabs to spaces. " Change tabs to spaces.
@ -87,7 +52,7 @@ function! ale#cursor#EchoCursorWarning(...) abort
let l:pos = getcurpos() let l:pos = getcurpos()
let l:loclist = g:ale_buffer_loclist_map[l:buffer] let l:loclist = g:ale_buffer_loclist_map[l:buffer]
let l:index = s:BinarySearch(l:loclist, l:pos[1], l:pos[2]) let l:index = ale#util#BinarySearch(l:loclist, l:pos[1], l:pos[2])
if l:index >= 0 if l:index >= 0
let l:loc = l:loclist[l:index] let l:loc = l:loclist[l:index]

View File

@ -63,3 +63,38 @@ function! ale#util#LocItemCompare(left, right) abort
return 0 return 0
endfunction endfunction
" This function will perform a binary search to find a message from the
" loclist to echo when the cursor moves.
function! ale#util#BinarySearch(loclist, line, column) abort
let l:min = 0
let l:max = len(a:loclist) - 1
let l:last_column_match = -1
while 1
if l:max < l:min
return l:last_column_match
endif
let l:mid = (l:min + l:max) / 2
let l:obj = a:loclist[l:mid]
" Binary search to get on the same line
if a:loclist[l:mid]['lnum'] < a:line
let l:min = l:mid + 1
elseif a:loclist[l:mid]['lnum'] > a:line
let l:max = l:mid - 1
else
let l:last_column_match = l:mid
" Binary search to get the same column, or near it
if a:loclist[l:mid]['col'] < a:column
let l:min = l:mid + 1
elseif a:loclist[l:mid]['col'] > a:column
let l:max = l:mid - 1
else
return l:mid
endif
endif
endwhile
endfunction

View File

@ -0,0 +1,26 @@
Before:
let g:loclist = [
\ {'lnum': 2, 'col': 10},
\ {'lnum': 3, 'col': 2},
\ {'lnum': 3, 'col': 10},
\ {'lnum': 3, 'col': 12},
\ {'lnum': 3, 'col': 25},
\ {'lnum': 5, 'col': 4},
\ {'lnum': 5, 'col': 5},
\]
Execute (Exact column matches should be correct):
AssertEqual ale#util#BinarySearch(g:loclist, 3, 2), 1
Execute (Off lines, there should be no match):
AssertEqual ale#util#BinarySearch(g:loclist, 4, 2), -1
Execute (Near column matches should be taken):
AssertEqual ale#util#BinarySearch(g:loclist, 3, 11), 2
AssertEqual ale#util#BinarySearch(g:loclist, 3, 13), 4
Execute (Columns before should be taken when the cursor is far ahead):
AssertEqual ale#util#BinarySearch(g:loclist, 3, 300), 4
After:
unlet g:loclist