annotate src/testdir/test_flatten.vim @ 23981:05c1a8485fb9

Added tag v8.2.2532 for changeset bee8c78c0c6aa942c3dfef43ed9095cc12c67731
author Bram Moolenaar <Bram@vim.org>
date Sat, 20 Feb 2021 08:30:04 +0100
parents 525c9e218c69
children 485c7c4afeb7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 " Test for flatting list.
21765
08940efa6b4e patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
2
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 func Test_flatten()
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 call assert_fails('call flatten(1)', 'E686:')
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 call assert_fails('call flatten({})', 'E686:')
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 call assert_fails('call flatten("string")', 'E686:')
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 call assert_fails('call flatten([], [])', 'E745:')
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 call assert_fails('call flatten([], -1)', 'E900: maxdepth')
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 call assert_equal([], flatten([]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 call assert_equal([], flatten([[]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 call assert_equal([], flatten([[[]]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 call assert_equal([1, 2, 3], flatten([1, 2, 3]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 call assert_equal([1, 2, 3], flatten([[1], 2, 3]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 call assert_equal([1, 2, 3], flatten([1, [2], 3]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 call assert_equal([1, 2, 3], flatten([1, 2, [3]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 call assert_equal([1, 2, 3], flatten([[1], [2], 3]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 call assert_equal([1, 2, 3], flatten([1, [2], [3]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 call assert_equal([1, 2, 3], flatten([[1], 2, [3]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 call assert_equal([1, 2, 3], flatten([[1], [2], [3]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 call assert_equal([1, 2, 3], flatten([[1, 2, 3], []]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 call assert_equal([1, 2, 3], flatten([[], [1, 2, 3]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 call assert_equal([1, 2, 3], flatten([[1, 2], [], [3]]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 call assert_equal([1, 2, 3], flatten([[], [1, 2, 3], []]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 call assert_equal([1, 2, 3, 4], flatten(range(1, 4)))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 " example in the help
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 call assert_equal([1, 2, 3, 4, 5], flatten([1, [2, [3, 4]], 5]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 call assert_equal([1, 2, [3, 4], 5], flatten([1, [2, [3, 4]], 5], 1))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 call assert_equal([0, [1], 2, [3], 4], flatten([[0, [1]], 2, [[3], 4]], 1))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 call assert_equal([1, 2, 3], flatten([[[[1]]], [2], [3]], 3))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 call assert_equal([[1], [2], [3]], flatten([[[1], [2], [3]]], 1))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 call assert_equal([[1]], flatten([[1]], 0))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 " Make it flatten if the given maxdepth is larger than actual depth.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 call assert_equal([1, 2, 3], flatten([[1, 2, 3]], 1))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 call assert_equal([1, 2, 3], flatten([[1, 2, 3]], 2))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 let l:list = [[1], [2], [3]]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 call assert_equal([1, 2, 3], flatten(l:list))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 call assert_equal([1, 2, 3], l:list)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 " Tests for checking reference counter works well.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 let l:x = {'foo': 'bar'}
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 call assert_equal([1, 2, l:x, 3], flatten([1, [2, l:x], 3]))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 call test_garbagecollect_now()
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 call assert_equal('bar', l:x.foo)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 let l:list = [[1], [2], [3]]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 call assert_equal([1, 2, 3], flatten(l:list))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 call test_garbagecollect_now()
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 call assert_equal([1, 2, 3], l:list)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 " Tests for checking circular reference list can be flatten.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 let l:x = [1]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 let l:y = [x]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 let l:z = flatten(l:y)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 call assert_equal([1], l:z)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 call test_garbagecollect_now()
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 let l:x[0] = 2
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 call assert_equal([2], l:x)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 call assert_equal([1], l:z) " NOTE: primitive types are copied.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 call assert_equal([1], l:y)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 let l:x = [2]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 let l:y = [1, [l:x], 3] " [1, [[2]], 3]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 let l:z = flatten(l:y, 1)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 call assert_equal([1, [2], 3], l:z)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 let l:x[0] = 9
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 call assert_equal([1, [9], 3], l:z) " Reference to l:x is kept.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 call assert_equal([1, [9], 3], l:y)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 let l:x = [1]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 let l:y = [2]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 call add(x, y) " l:x = [1, [2]]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 call add(y, x) " l:y = [2, [1, [...]]]
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 call assert_equal([1, 2, 1, 2], flatten(l:x, 2))
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 call assert_equal([2, l:x], l:y)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 endfunc
21765
08940efa6b4e patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
83
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
84 func Test_flattennew()
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
85 let l = [1, [2, [3, 4]], 5]
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
86 call assert_equal([1, 2, 3, 4, 5], flattennew(l))
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
87 call assert_equal([1, [2, [3, 4]], 5], l)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
88
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
89 call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
90 call assert_equal([1, [2, [3, 4]], 5], l)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
91 endfunc
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
92
21765
08940efa6b4e patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
93 " vim: shiftwidth=2 sts=2 expandtab