summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/lua/list_spec.lua
blob: 7534505ef24ad9a469f440a202c1fd727ed5fe92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- Test suite for vim.list
local t = require('test.testutil')
local eq = t.eq

describe('vim.list', function()
  it('vim.list.unique()', function()
    eq({ 1, 2, 3, 4, 5 }, vim.list.unique({ 1, 2, 2, 3, 4, 4, 5 }))
    eq({ 1, 2, 3, 4, 5 }, vim.list.unique({ 1, 2, 3, 4, 4, 5, 1, 2, 3, 2, 1, 2, 3, 4, 5 }))
    eq({ 1, 2, 3, 4, 5, field = 1 }, vim.list.unique({ 1, 2, 2, 3, 4, 4, 5, field = 1 }))

    -- Not properly defined, but test anyway
    -- luajit evaluates #t as 7, whereas Lua 5.1 evaluates it as 12
    local r = vim.list.unique({ 1, 2, 2, 3, 4, 4, 5, nil, 6, 6, 7, 7 })
    if jit then
      eq({ 1, 2, 3, 4, 5, nil, nil, nil, 6, 6, 7, 7 }, r)
    else
      eq({ 1, 2, 3, 4, 5, nil, 6, 7 }, r)
    end

    eq(
      { { 1 }, { 2 }, { 3 } },
      vim.list.unique({ { 1 }, { 1 }, { 2 }, { 2 }, { 3 }, { 3 } }, function(x)
        return x[1]
      end)
    )
  end)

  --- Generate a list like { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ...}.
  ---@param num integer
  local function gen_list(num)
    ---@type integer[]
    local list = {}
    for i = 1, num do
      for _ = 1, i do
        list[#list + 1] = i
      end
    end
    return list
  end

  --- Index of the last {num}.
  --- Mathematically, a triangular number.
  ---@param num integer
  local function index(num)
    return math.floor((math.pow(num, 2) + num) / 2)
  end

  it("vim.list.bisect(..., { bound = 'lower' })", function()
    local num = math.random(100)
    local list = gen_list(num)

    local target = math.random(num)
    eq(vim.list.bisect(list, target, { bound = 'lower' }), index(target - 1) + 1)
    eq(vim.list.bisect(list, num + 1, { bound = 'lower' }), index(num) + 1)
  end)

  it("vim.list.bisect(..., bound = { 'upper' })", function()
    local num = math.random(100)
    local list = gen_list(num)

    local target = math.random(num)
    eq(vim.list.bisect(list, target, { bound = 'upper' }), index(target) + 1)
    eq(vim.list.bisect(list, num + 1, { bound = 'upper' }), index(num) + 1)
  end)
end)