eval/typval: Add tv_list_equal() tests, compare NULL lists equal

This commit is contained in:
ZyX
2016-10-02 04:34:30 +03:00
parent 4f9e784427
commit b3672ae2fc
3 changed files with 102 additions and 37 deletions

View File

@@ -623,13 +623,12 @@ bool tv_list_equal(list_T *const l1, list_T *const l2, const bool ic,
const bool recursive)
FUNC_ATTR_WARN_UNUSED_RESULT
{
if (l1 == NULL || l2 == NULL) {
// FIXME? compare empty list with NULL list equal
return false;
}
if (l1 == l2) {
return true;
}
if (l1 == NULL || l2 == NULL) {
return false;
}
if (tv_list_len(l1) != tv_list_len(l2)) {
return false;
}

View File

@@ -57,12 +57,6 @@ describe('NULL', function()
null_list_expr_test('does not crash extend()', 'extend(L, [1])', 0, 0)
-- FIXME extend() should not return 0 at all
null_list_expr_test('does not crash extend() (second position)', 'extend([1], L)', 0, 0)
-- FIXME Should return 1
null_list_expr_test('is equal to itself', 'L == L', 0, 0)
-- FIXME Should return 0
null_list_expr_test('is not not equal to itself', 'L != L', 0, 1)
-- FIXME Should return 1
null_list_expr_test('counts correctly', 'count([L], L)', 0, 0)
-- FIXME should be accepted by inputlist()
null_list_expr_test('is accepted as an empty list by inputlist()',
'[feedkeys("\\n"), inputlist(L)]', 'E686: Argument of inputlist() must be a List', {0, 0})
@@ -133,5 +127,8 @@ describe('NULL', function()
null_list_expr_test('can be added to itself', '(L + L) is L', 0, 1)
null_list_expr_test('can be added to non-empty list', '([1] + L)', 0, {1})
null_list_expr_test('can be added to non-empty list (reversed)', '(L + [1])', 0, {1})
null_list_expr_test('is equal to itself', 'L == L', 0, 1)
null_list_expr_test('is not not equal to itself', 'L != L', 0, 0)
null_list_expr_test('counts correctly', 'count([L], L)', 0, 1)
end)
end)

View File

@@ -1031,7 +1031,6 @@ describe('typval.c', function()
eq(empty_list, typvalt2lua(rettv4))
end)
end)
end)
describe('join()', function()
local function list_join(l, sep, ret)
local ga = ga_alloc()
@@ -1040,7 +1039,7 @@ describe('typval.c', function()
else return ffi.string(ga.ga_data)
end
end
it('works', function()
itp('works', function()
local l
l = list('boo', 'far')
eq('boo far', list_join(l, ' '))
@@ -1066,4 +1065,74 @@ describe('typval.c', function()
lib.tv_list_free(l, true)
end)
end)
describe('equal()', function()
itp('compares empty and NULL lists correctly', function()
local l = list()
local l2 = list()
-- NULL lists are not equal to empty lists
eq(false, lib.tv_list_equal(l, nil, true, false))
eq(false, lib.tv_list_equal(nil, l, false, false))
eq(false, lib.tv_list_equal(nil, l, false, true))
eq(false, lib.tv_list_equal(l, nil, true, true))
-- Yet NULL lists are equal themselves
eq(true, lib.tv_list_equal(nil, nil, true, false))
eq(true, lib.tv_list_equal(nil, nil, false, false))
eq(true, lib.tv_list_equal(nil, nil, false, true))
eq(true, lib.tv_list_equal(nil, nil, true, true))
-- As well as empty lists
eq(true, lib.tv_list_equal(l, l, true, false))
eq(true, lib.tv_list_equal(l, l2, false, false))
eq(true, lib.tv_list_equal(l2, l, false, true))
eq(true, lib.tv_list_equal(l2, l2, true, true))
end)
-- Must not use recursive=true argument in the following tests because it
-- indicates that tv_equal_recurse_limit and recursive_cnt were set which
-- is essential. This argument will be set when comparing inner lists.
itp('compares lists correctly when case is not ignored', function()
local l1 = list('abc', {1, 2, 'Abc'}, 'def')
local l2 = list('abc', {1, 2, 'Abc'})
local l3 = list('abc', {1, 2, 'Abc'}, 'Def')
local l4 = list('abc', {1, 2, 'Abc', 4}, 'def')
local l5 = list('Abc', {1, 2, 'Abc'}, 'def')
local l6 = list('abc', {1, 2, 'Abc'}, 'def')
local l7 = list('abc', {1, 2, 'abc'}, 'def')
local l8 = list('abc', nil, 'def')
local l9 = list('abc', {1, 2, nil}, 'def')
eq(true, lib.tv_list_equal(l1, l1, false, false))
eq(false, lib.tv_list_equal(l1, l2, false, false))
eq(false, lib.tv_list_equal(l1, l3, false, false))
eq(false, lib.tv_list_equal(l1, l4, false, false))
eq(false, lib.tv_list_equal(l1, l5, false, false))
eq(true, lib.tv_list_equal(l1, l6, false, false))
eq(false, lib.tv_list_equal(l1, l7, false, false))
eq(false, lib.tv_list_equal(l1, l8, false, false))
eq(false, lib.tv_list_equal(l1, l9, false, false))
end)
itp('compares lists correctly when case is ignored', function()
local l1 = list('abc', {1, 2, 'Abc'}, 'def')
local l2 = list('abc', {1, 2, 'Abc'})
local l3 = list('abc', {1, 2, 'Abc'}, 'Def')
local l4 = list('abc', {1, 2, 'Abc', 4}, 'def')
local l5 = list('Abc', {1, 2, 'Abc'}, 'def')
local l6 = list('abc', {1, 2, 'Abc'}, 'def')
local l7 = list('abc', {1, 2, 'abc'}, 'def')
local l8 = list('abc', nil, 'def')
local l9 = list('abc', {1, 2, nil}, 'def')
eq(true, lib.tv_list_equal(l1, l1, true, false))
eq(false, lib.tv_list_equal(l1, l2, true, false))
eq(true, lib.tv_list_equal(l1, l3, true, false))
eq(false, lib.tv_list_equal(l1, l4, true, false))
eq(true, lib.tv_list_equal(l1, l5, true, false))
eq(true, lib.tv_list_equal(l1, l6, true, false))
eq(true, lib.tv_list_equal(l1, l7, true, false))
eq(false, lib.tv_list_equal(l1, l8, true, false))
eq(false, lib.tv_list_equal(l1, l9, true, false))
end)
end)
end)
end)