mirror of
https://github.com/vim/vim.git
synced 2025-10-26 09:14:23 -04:00
patch 9.1.1684: min()/max() does not handle float data types
Problem: min()/max() does not handle float data types
(ubaldot)
Solution: Extend min() and max() to every comparable type
(LemonBoy)
Re-use the logic used for plain old comparison operators, this way we
gain support for float values and unify the logic handling the
comparisons.
fixes: #18052
closes: 18055
Signed-off-by: LemonBoy <thatlemon@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
b922b30cfe
commit
3b3b936125
@@ -133,35 +133,57 @@ func Test_max()
|
||||
call assert_equal(0, max([]))
|
||||
call assert_equal(2, max([2]))
|
||||
call assert_equal(2, max([1, 2]))
|
||||
call assert_equal(3, max([1.0, 2, 3]))
|
||||
call assert_equal(3.0, max([1, 2, 3.0]))
|
||||
call assert_equal(2, max([1, 2, v:null]))
|
||||
|
||||
call assert_equal(0, max(()))
|
||||
call assert_equal(2, max((2, )))
|
||||
call assert_equal(2, max((1, 2)))
|
||||
call assert_equal(3, max((1.0, 2, 3)))
|
||||
call assert_equal(3.0, max((1, 2, 3.0)))
|
||||
call assert_equal(2, max((1, 2, v:null)))
|
||||
|
||||
call assert_equal(0, max({}))
|
||||
call assert_equal(2, max({'a':1, 'b':2}))
|
||||
|
||||
call assert_equal('abz', max(['abc', 'aba', 'abz']))
|
||||
|
||||
call assert_fails('call max(1)', 'E712:')
|
||||
call assert_fails('call max(v:none)', 'E712:')
|
||||
|
||||
" check we only get one error
|
||||
call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:'])
|
||||
call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:'])
|
||||
call assert_fails('call max([#{}, [1]])', ['E691:', 'E691:'])
|
||||
call assert_fails('call max(#{a: {}, b: [1]})', ['E691:', 'E691:'])
|
||||
endfunc
|
||||
|
||||
func Test_min()
|
||||
call assert_equal(0, min([]))
|
||||
call assert_equal(2, min([2]))
|
||||
call assert_equal(1, min([1, 2]))
|
||||
call assert_equal(0, min([1, 2, v:null]))
|
||||
call assert_equal(1, min([1, 2, 3.0]))
|
||||
call assert_equal(1.0, min([1.0, 2]))
|
||||
call assert_equal(v:null, min([1, 2, v:null]))
|
||||
|
||||
call assert_equal(0, min(()))
|
||||
call assert_equal(2, min((2, )))
|
||||
call assert_equal(1, min((1, 2)))
|
||||
call assert_equal(1, min((1, 2, 3.0)))
|
||||
call assert_equal(1.0, min((1.0, 2)))
|
||||
call assert_equal(v:null, min((1, 2, v:null)))
|
||||
|
||||
call assert_equal(0, min({}))
|
||||
call assert_equal(1, min({'a':1, 'b':2}))
|
||||
|
||||
call assert_equal('aba', min(['abc', 'aba', 'abz']))
|
||||
|
||||
call assert_fails('call min(1)', 'E712:')
|
||||
call assert_fails('call min(v:none)', 'E712:')
|
||||
call assert_fails('call min([1, {}])', 'E728:')
|
||||
call assert_fails('call min([1, {}])', 'E735:')
|
||||
|
||||
" check we only get one error
|
||||
call assert_fails('call min([[1], #{}])', ['E745:', 'E745:'])
|
||||
call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:'])
|
||||
call assert_fails('call min([[1], #{}])', ['E691:', 'E691:'])
|
||||
call assert_fails('call min(#{a: [1], b: #{}})', ['E691:', 'E691:'])
|
||||
endfunc
|
||||
|
||||
func Test_strwidth()
|
||||
|
||||
@@ -1994,22 +1994,13 @@ func Test_tuple_max()
|
||||
END
|
||||
call v9.CheckSourceFailure(lines, 'E1030: Using a String as a Number: "b"')
|
||||
|
||||
let lines =<< trim END
|
||||
vim9script
|
||||
def Fn()
|
||||
var x = max(('a', 'b'))
|
||||
enddef
|
||||
Fn()
|
||||
END
|
||||
call v9.CheckSourceFailure(lines, 'E1030: Using a String as a Number: "a"')
|
||||
|
||||
let lines =<< trim END
|
||||
echo max([('a', 'b'), 20])
|
||||
END
|
||||
call v9.CheckSourceLegacyAndVim9Failure(lines, [
|
||||
\ 'E1520: Using a Tuple as a Number',
|
||||
\ 'E1520: Using a Tuple as a Number',
|
||||
\ 'E1520: Using a Tuple as a Number'])
|
||||
\ 'E1517: Can only compare Tuple with Tuple',
|
||||
\ 'E1517: Can only compare Tuple with Tuple',
|
||||
\ 'E1517: Can only compare Tuple with Tuple'])
|
||||
endfunc
|
||||
|
||||
" Test for min()
|
||||
@@ -2035,16 +2026,6 @@ func Test_tuple_min()
|
||||
var x = min((1, 'b'))
|
||||
END
|
||||
call v9.CheckSourceFailure(lines, 'E1030: Using a String as a Number: "b"')
|
||||
|
||||
|
||||
let lines =<< trim END
|
||||
vim9script
|
||||
def Fn()
|
||||
var x = min(('a', 'b'))
|
||||
enddef
|
||||
Fn()
|
||||
END
|
||||
call v9.CheckSourceFailure(lines, 'E1030: Using a String as a Number: "a"')
|
||||
endfunc
|
||||
|
||||
" Test for reduce()
|
||||
|
||||
@@ -3229,7 +3229,7 @@ def Test_type_check()
|
||||
assert_fails('N = d', 'E1012: Type mismatch; expected number but got dict<number>')
|
||||
assert_fails('N = l', 'E1012: Type mismatch; expected number but got list<number>')
|
||||
assert_fails('N = b', 'E1012: Type mismatch; expected number but got blob')
|
||||
assert_fails('N = Fn', 'E1012: Type mismatch; expected number but got func([unknown]): number')
|
||||
assert_fails('N = Fn', 'E1012: Type mismatch; expected number but got func([unknown]): any')
|
||||
assert_fails('N = A', 'E1405: Class "A" cannot be used as a value')
|
||||
assert_fails('N = o', 'E1012: Type mismatch; expected number but got object<A>')
|
||||
assert_fails('N = T', 'E1403: Type alias "T" cannot be used as a value')
|
||||
@@ -3247,7 +3247,7 @@ def Test_type_check()
|
||||
assert_fails('var [X1: number, Y: number] = [1, d]', 'E1012: Type mismatch; expected number but got dict<number>')
|
||||
assert_fails('var [X2: number, Y: number] = [1, l]', 'E1012: Type mismatch; expected number but got list<number>')
|
||||
assert_fails('var [X3: number, Y: number] = [1, b]', 'E1012: Type mismatch; expected number but got blob')
|
||||
assert_fails('var [X4: number, Y: number] = [1, Fn]', 'E1012: Type mismatch; expected number but got func([unknown]): number')
|
||||
assert_fails('var [X4: number, Y: number] = [1, Fn]', 'E1012: Type mismatch; expected number but got func([unknown]): any')
|
||||
assert_fails('var [X7: number, Y: number] = [1, A]', 'E1405: Class "A" cannot be used as a value')
|
||||
assert_fails('var [X8: number, Y: number] = [1, o]', 'E1012: Type mismatch; expected number but got object<A>')
|
||||
assert_fails('var [X8: number, Y: number] = [1, T]', 'E1403: Type alias "T" cannot be used as a value')
|
||||
@@ -3345,7 +3345,7 @@ def Test_func_argtype_check()
|
||||
assert_fails('IntArg(d)', 'E1013: Argument 1: type mismatch, expected number but got dict<number>')
|
||||
assert_fails('IntArg(l)', 'E1013: Argument 1: type mismatch, expected number but got list<number>')
|
||||
assert_fails('IntArg(b)', 'E1013: Argument 1: type mismatch, expected number but got blob')
|
||||
assert_fails('IntArg(Fn)', 'E1013: Argument 1: type mismatch, expected number but got func([unknown]): number')
|
||||
assert_fails('IntArg(Fn)', 'E1013: Argument 1: type mismatch, expected number but got func([unknown]): any')
|
||||
if has('channel')
|
||||
var j: job = test_null_job()
|
||||
var ch: channel = test_null_channel()
|
||||
|
||||
@@ -4910,8 +4910,8 @@ def Test_typename()
|
||||
if has('channel')
|
||||
assert_equal('channel', test_null_channel()->typename())
|
||||
endif
|
||||
var l: list<func(list<number>): number> = [function('min')]
|
||||
assert_equal('list<func(list<number>): number>', typename(l))
|
||||
var l: list<func(list<number>): any> = [function('min')]
|
||||
assert_equal('list<func(list<number>): any>', typename(l))
|
||||
enddef
|
||||
|
||||
def Test_undofile()
|
||||
|
||||
@@ -4111,7 +4111,7 @@ enddef
|
||||
def Test_source_func_script_var()
|
||||
var lines =<< trim END
|
||||
vim9script noclear
|
||||
var Fn: func(list<any>): number
|
||||
var Fn: func(list<any>): any
|
||||
Fn = function('min')
|
||||
assert_equal(2, Fn([4, 2]))
|
||||
END
|
||||
|
||||
Reference in New Issue
Block a user