summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_functions.vim
blob: 3bea88df17ce7617dc21fe03335cfe422074e012 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
" Tests for various functions.

source shared.vim
source check.vim
source term_util.vim
source screendump.vim
import './vim9.vim' as v9

" Must be done first, since the alternate buffer must be unset.
func Test_00_bufexists()
  call assert_equal(0, bufexists('does_not_exist'))
  call assert_equal(1, bufexists(bufnr('%')))
  call assert_equal(0, bufexists(0))
  new Xfoo
  let bn = bufnr('%')
  call assert_equal(1, bufexists(bn))
  call assert_equal(1, bufexists('Xfoo'))
  call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
  call assert_equal(1, bufexists(0))
  bw
  call assert_equal(0, bufexists(bn))
  call assert_equal(0, bufexists('Xfoo'))
endfunc

func Test_has()
  call assert_equal(1, has('eval'))
  call assert_equal(1, has('eval', 1))

  if has('unix')
    call assert_equal(1, or(has('ttyin'), 1))
    call assert_equal(0, and(has('ttyout'), 0))
    call assert_equal(1, has('multi_byte_encoding'))
  endif
  call assert_equal(1, has('vcon', 1))
  call assert_equal(1, has('mouse_gpm_enabled', 1))

  call assert_equal(0, has('nonexistent'))
  call assert_equal(0, has('nonexistent', 1))

  " Will we ever have patch 9999?
  let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
  call assert_equal(0, has(ver))

  " There actually isn't a patch 9.0.0, but this is more consistent.
  call assert_equal(1, has('patch-9.0.0'))
endfunc

func Test_empty()
  call assert_equal(1, empty(''))
  call assert_equal(0, empty('a'))

  call assert_equal(1, empty(0))
  call assert_equal(1, empty(-0))
  call assert_equal(0, empty(1))
  call assert_equal(0, empty(-1))

  call assert_equal(1, empty(0.0))
  call assert_equal(1, empty(-0.0))
  call assert_equal(0, empty(1.0))
  call assert_equal(0, empty(-1.0))
  call assert_equal(0, empty(1.0/0.0))
  call assert_equal(0, empty(0.0/0.0))

  call assert_equal(1, empty([]))
  call assert_equal(0, empty(['a']))

  call assert_equal(1, empty({}))
  call assert_equal(0, empty({'a':1}))

  call assert_equal(1, empty(v:null))
  call assert_equal(1, empty(v:none))
  call assert_equal(1, empty(v:false))
  call assert_equal(0, empty(v:true))

  if has('channel')
    call assert_equal(1, empty(test_null_channel()))
  endif
  if has('job')
    call assert_equal(1, empty(test_null_job()))
  endif

  call assert_equal(0, empty(function('Test_empty')))
  call assert_equal(0, empty(function('Test_empty', [0])))

  call assert_fails("call empty(test_void())", 'E685:')
  call assert_fails("call empty(test_unknown())", 'E685:')
endfunc

func Test_test_void()
  call assert_fails('echo 1 == test_void()', 'E1031:')
  call assert_fails('echo 1.0 == test_void()', 'E1031:')
  call assert_fails('let x = json_encode(test_void())', 'E685:')
  call assert_fails('let x = copy(test_void())', 'E685:')
  call assert_fails('let x = copy([test_void()])', 'E1031:')
endfunc

func Test_islocked()
  call assert_fails('call islocked(99)', 'E475:')
  call assert_fails('call islocked("s: x")', 'E488:')
endfunc

func Test_len()
  call assert_equal(1, len(0))
  call assert_equal(2, len(12))

  call assert_equal(0, len(''))
  call assert_equal(2, len('ab'))

  call assert_equal(0, len([]))
  call assert_equal(0, len(test_null_list()))
  call assert_equal(2, len([2, 1]))

  call assert_equal(0, len({}))
  call assert_equal(0, len(test_null_dict()))
  call assert_equal(2, len({'a': 1, 'b': 2}))

  call assert_fails('call len(v:none)', 'E701:')
  call assert_fails('call len({-> 0})', 'E701:')
endfunc

func Test_max()
  call assert_equal(0, max([]))
  call assert_equal(2, max([2]))
  call assert_equal(2, max([1, 2]))
  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_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:'])
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(0, min({}))
  call assert_equal(1, min({'a':1, 'b':2}))

  call assert_fails('call min(1)', 'E712:')
  call assert_fails('call min(v:none)', 'E712:')
  call assert_fails('call min([1, {}])', 'E728:')

  " check we only get one error
  call assert_fails('call min([[1], #{}])', ['E745:', 'E745:'])
  call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:'])
endfunc

func Test_strwidth()
  for aw in ['single', 'double']
    exe 'set ambiwidth=' . aw
    call assert_equal(0, strwidth(''))
    call assert_equal(1, strwidth("\t"))
    call assert_equal(3, strwidth('Vim'))
    call assert_equal(4, strwidth(1234))
    call assert_equal(5, strwidth(-1234))

    call assert_equal(2, strwidth('😉'))
    call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
    call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))

    call assert_fails('call strwidth({->0})', 'E729:')
    call assert_fails('call strwidth([])', 'E730:')
    call assert_fails('call strwidth({})', 'E731:')
  endfor

  call assert_equal(3, strwidth(1.2))
  call v9.CheckDefAndScriptFailure(['echo strwidth(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])

  set ambiwidth&
endfunc

func Test_str2nr()
  call assert_equal(0, str2nr(''))
  call assert_equal(1, str2nr('1'))
  call assert_equal(1, str2nr(' 1 '))

  call assert_equal(1, str2nr('+1'))
  call assert_equal(1, str2nr('+ 1'))
  call assert_equal(1, str2nr(' + 1 '))

  call assert_equal(-1, str2nr('-1'))
  call assert_equal(-1, str2nr('- 1'))
  call assert_equal(-1, str2nr(' - 1 '))

  call assert_equal(123456789, str2nr('123456789'))
  call assert_equal(-123456789, str2nr('-123456789'))

  call assert_equal(5, str2nr('101', 2))
  call assert_equal(5, '0b101'->str2nr(2))
  call assert_equal(5, str2nr('0B101', 2))
  call assert_equal(-5, str2nr('-101', 2))
  call assert_equal(-5, str2nr('-0b101', 2))
  call assert_equal(-5, str2nr('-0B101', 2))

  call assert_equal(65, str2nr('101', 8))
  call assert_equal(65, str2nr('0101', 8))
  call assert_equal(-65, str2nr('-101', 8))
  call assert_equal(-65, str2nr('-0101', 8))
  call assert_equal(65, str2nr('0o101', 8))
  call assert_equal(65, str2nr('0O0101', 8))
  call assert_equal(-65, str2nr('-0O101', 8))
  call assert_equal(-65, str2nr('-0o0101', 8))

  call assert_equal(11259375, str2nr('abcdef', 16))
  call assert_equal(11259375, str2nr('ABCDEF', 16))
  call assert_equal(-11259375, str2nr('-ABCDEF', 16))
  call assert_equal(11259375, str2nr('0xabcdef', 16))
  call assert_equal(11259375, str2nr('0Xabcdef', 16))
  call assert_equal(11259375, str2nr('0XABCDEF', 16))
  call assert_equal(-11259375, str2nr('-0xABCDEF', 16))

  call assert_equal(1, str2nr("1'000'000", 10, 0))
  call assert_equal(256, str2nr("1'0000'0000", 2, 1))
  call assert_equal(262144, str2nr("1'000'000", 8, 1))
  call assert_equal(1000000, str2nr("1'000'000", 10, 1))
  call assert_equal(1000, str2nr("1'000''000", 10, 1))
  call assert_equal(65536, str2nr("1'00'00", 16, 1))

  call assert_equal(0, str2nr('0x10'))
  call assert_equal(0, str2nr('0b10'))
  call assert_equal(0, str2nr('0o10'))
  call assert_equal(1, str2nr('12', 2))
  call assert_equal(1, str2nr('18', 8))
  call assert_equal(1, str2nr('1g', 16))

  call assert_equal(0, str2nr(v:null))
  call assert_equal(0, str2nr(v:none))

  call assert_fails('call str2nr([])', 'E730:')
  call assert_fails('call str2nr({->2})', 'E729:')
  call assert_equal(1, str2nr(1.2))
  call v9.CheckDefAndScriptFailure(['echo str2nr(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
  call assert_fails('call str2nr(10, [])', 'E745:')
endfunc

func Test_strftime()
  CheckFunction strftime

  " Format of strftime() depends on system. We assume
  " that basic formats tested here are available and
  " identical on all systems which support strftime().
  "
  " The 2nd parameter of strftime() is a local time, so the output day
  " of strftime() can be 17 or 18, depending on timezone.
  call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
  "
  call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', '%Y-%m-%d %H:%M:%S'->strftime())

  call assert_fails('call strftime([])', 'E730:')
  call assert_fails('call strftime("%Y", [])', 'E745:')

  " Check that the time changes after we change the timezone
  " Save previous timezone value, if any
  if exists('$TZ')
    let tz = $TZ
  endif

  " Force EST and then UTC, save the current hour (24-hour clock) for each
  let $TZ = 'EST' | let est = strftime('%H')
  let $TZ = 'UTC' | let utc = strftime('%H')

  " Those hours should be two bytes long, and should not be the same; if they
  " are, a tzset(3) call may have failed somewhere
  call assert_equal(strlen(est), 2)
  call assert_equal(strlen(utc), 2)
  " TODO: this fails on MS-Windows
  if has('unix')
    call assert_notequal(est, utc)
  endif

  " If we cached a timezone value, put it back, otherwise clear it
  if exists('tz')
    let $TZ = tz
  else
    unlet $TZ
  endif
endfunc

func Test_strptime()
  CheckFunction strptime

  if exists('$TZ')
    let tz = $TZ
  endif
  let $TZ = 'UTC'

  call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))

  " Force DST and check that it's considered
  let $TZ = 'WINTER0SUMMER,J1,J365'
  call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))

  call assert_fails('call strptime()', 'E119:')
  call assert_fails('call strptime("xxx")', 'E119:')
  call assert_equal(0, strptime("%Y", ''))
  call assert_equal(0, strptime("%Y", "xxx"))

  if exists('tz')
    let $TZ = tz
  else
    unlet $TZ
  endif
endfunc

func Test_resolve_unix()
  CheckUnix

  " Xlink1 -> Xlink2
  " Xlink2 -> Xlink3
  silent !ln -s -f Xlink2 Xlink1
  silent !ln -s -f Xlink3 Xlink2
  call assert_equal('Xlink3', resolve('Xlink1'))
  call assert_equal('./Xlink3', resolve('./Xlink1'))
  call assert_equal('Xlink3/', resolve('Xlink2/'))
  " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
  "call assert_equal('Xlink3/', resolve('Xlink1/'))
  "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
  "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
  call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))

  " Test resolve() with a symlink cycle.
  " Xlink1 -> Xlink2
  " Xlink2 -> Xlink3
  " Xlink3 -> Xlink1
  silent !ln -s -f Xlink1 Xlink3
  call assert_fails('call resolve("Xlink1")',   'E655:')
  call assert_fails('call resolve("./Xlink1")', 'E655:')
  call assert_fails('call resolve("Xlink2")',   'E655:')
  call assert_fails('call resolve("Xlink3")',   'E655:')
  call delete('Xlink1')
  call delete('Xlink2')
  call delete('Xlink3')

  silent !ln -s -f Xresolvedir//Xfile Xresolvelink
  call assert_equal('Xresolvedir/Xfile', resolve('Xresolvelink'))
  call delete('Xresolvelink')

  silent !ln -s -f Xlink2/ Xlink1
  call assert_equal('Xlink2', 'Xlink1'->resolve())
  call assert_equal('Xlink2/', resolve('Xlink1/'))
  call delete('Xlink1')

  silent !ln -s -f ./Xlink2 Xlink1
  call assert_equal('Xlink2', resolve('Xlink1'))
  call assert_equal('./Xlink2', resolve('./Xlink1'))
  call delete('Xlink1')

  call assert_equal('/', resolve('/'))
endfunc

func s:normalize_fname(fname)
  let ret = substitute(a:fname, '\', '/', 'g')
  let ret = substitute(ret, '//', '/', 'g')
  return ret->tolower()
endfunc

func Test_resolve_win32()
  CheckMSWindows

  " test for shortcut file
  if executable('cscript')
    new Xresfile
    wq
    let lines =<< trim END
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set ws = WScript.CreateObject("WScript.Shell")
	Set shortcut = ws.CreateShortcut("Xlink.lnk")
	shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xresfile")
	shortcut.Save
    END
    call writefile(lines, 'link.vbs')
    silent !cscript link.vbs
    call delete('link.vbs')
    call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
    call delete('Xresfile')

    call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
    call delete('Xlink.lnk')
  else
    echomsg 'skipped test for shortcut file'
  endif

  " remove files
  call delete('Xlink')
  call delete('Xdir', 'd')
  call delete('Xresfile')

  " test for symbolic link to a file
  new Xresfile
  wq
  call assert_equal('Xresfile', resolve('Xresfile'))
  silent !mklink Xlink Xresfile
  if !v:shell_error
    call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink')))
    call delete('Xlink')
  else
    echomsg 'skipped test for symbolic link to a file'
  endif
  call delete('Xresfile')

  " test for junction to a directory
  call mkdir('Xdir')
  silent !mklink /J Xlink Xdir
  if !v:shell_error
    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))

    call delete('Xdir', 'd')

    " test for junction already removed
    call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
    call delete('Xlink')
  else
    echomsg 'skipped test for junction to a directory'
    call delete('Xdir', 'd')
  endif

  " test for symbolic link to a directory
  call mkdir('Xdir')
  silent !mklink /D Xlink Xdir
  if !v:shell_error
    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))

    call delete('Xdir', 'd')

    " test for symbolic link already removed
    call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
    call delete('Xlink')
  else
    echomsg 'skipped test for symbolic link to a directory'
    call delete('Xdir', 'd')
  endif

  " test for buffer name
  new Xbuffile
  wq
  silent !mklink Xlink Xbuffile
  if !v:shell_error
    edit Xlink
    call assert_equal('Xlink', bufname('%'))
    call delete('Xlink')
    bw!
  else
    echomsg 'skipped test for buffer name'
  endif
  call delete('Xbuffile')

  " test for reparse point
  call mkdir('Xdir')
  call assert_equal('Xdir', resolve('Xdir'))
  silent !mklink /D Xdirlink Xdir
  if !v:shell_error
    w Xdir/text.txt
    call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
    call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
    call delete('Xdirlink')
  else
    echomsg 'skipped test for reparse point'
  endif

  call delete('Xdir', 'rf')
endfunc

func Test_simplify()
  call assert_equal('',            simplify(''))
  call assert_equal('/',           simplify('/'))
  call assert_equal('/',           simplify('/.'))
  call assert_equal('/',           simplify('/..'))
  call assert_equal('/...',        simplify('/...'))
  call assert_equal('//path',      simplify('//path'))
  if has('unix')
    call assert_equal('/path',       simplify('///path'))
    call assert_equal('/path',       simplify('////path'))
  endif

  call assert_equal('./dir/file',  './dir/file'->simplify())
  call assert_equal('./dir/file',  simplify('.///dir//file'))
  call assert_equal('./dir/file',  simplify('./dir/./file'))
  call assert_equal('./file',      simplify('./dir/../file'))
  call assert_equal('../dir/file', simplify('dir/../../dir/file'))
  call assert_equal('./file',      simplify('dir/.././file'))
  call assert_equal('../dir',      simplify('./../dir'))
  call assert_equal('..',          simplify('../testdir/..'))
  call mkdir('Xsimpdir')
  call assert_equal('.',           simplify('Xsimpdir/../.'))
  call delete('Xsimpdir', 'd')

  call assert_fails('call simplify({->0})', 'E729:')
  call assert_fails('call simplify([])', 'E730:')
  call assert_fails('call simplify({})', 'E731:')
  call assert_equal('1.2', simplify(1.2))
  call v9.CheckDefAndScriptFailure(['echo simplify(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
endfunc

func Test_pathshorten()
  call assert_equal('', pathshorten(''))
  call assert_equal('foo', pathshorten('foo'))
  call assert_equal('/foo', '/foo'->pathshorten())
  call assert_equal('f/', pathshorten('foo/'))
  call assert_equal('f/bar', pathshorten('foo/bar'))
  call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
  call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
  call assert_equal('.f/bar', pathshorten('.foo/bar'))
  call assert_equal('~f/bar', pathshorten('~foo/bar'))
  call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
  call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
  call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
  call assert_fails('call pathshorten([])', 'E730:')

  " test pathshorten with optional variable to set preferred size of shortening
  call assert_equal('', pathshorten('', 2))
  call assert_equal('foo', pathshorten('foo', 2))
  call assert_equal('/foo', pathshorten('/foo', 2))
  call assert_equal('fo/', pathshorten('foo/', 2))
  call assert_equal('fo/bar', pathshorten('foo/bar', 2))
  call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2))
  call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2))
  call assert_equal('.fo/bar', pathshorten('.foo/bar', 2))
  call assert_equal('~fo/bar', pathshorten('~foo/bar', 2))
  call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2))
  call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2))
  call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2))
  call assert_fails('call pathshorten([],2)', 'E730:')
  call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3))
  call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3))
  call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0))
endfunc

func Test_strpart()
  call assert_equal('de', strpart('abcdefg', 3, 2))
  call assert_equal('ab', strpart('abcdefg', -2, 4))
  call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
  call assert_equal('fg', strpart('abcdefg', 5, 4))
  call assert_equal('defg', strpart('abcdefg', 3))
  call assert_equal('', strpart('abcdefg', 10))
  call assert_fails("let s=strpart('abcdef', [])", 'E745:')

  call assert_equal('lép', strpart('éléphant', 2, 4))
  call assert_equal('léphant', strpart('éléphant', 2))

  call assert_equal('é', strpart('éléphant', 0, 1, 1))
  call assert_equal('ép', strpart('éléphant', 3, 2, v:true))
  call assert_equal('ó', strpart('cómposed', 1, 1, 1))
endfunc

func Test_tolower()
  call assert_equal("", tolower(""))

  " Test with all printable ASCII characters.
  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
          \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))

  " Test with a few uppercase diacritics.
  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
  call assert_equal("bḃḇ", tolower("BḂḆ"))
  call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
  call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
  call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
  call assert_equal("fḟ ", tolower("FḞ "))
  call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
  call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
  call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
  call assert_equal("jĵ", tolower("JĴ"))
  call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
  call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
  call assert_equal("mḿṁ", tolower("MḾṀ"))
  call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
  call assert_equal("pṕṗ", tolower("PṔṖ"))
  call assert_equal("q", tolower("Q"))
  call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
  call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
  call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
  call assert_equal("vṽ", tolower("VṼ"))
  call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
  call assert_equal("xẋẍ", tolower("XẊẌ"))
  call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
  call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))

  " Test with a few lowercase diacritics, which should remain unchanged.
  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
  call assert_equal("bḃḇ", tolower("bḃḇ"))
  call assert_equal("cçćĉċč", tolower("cçćĉċč"))
  call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
  call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
  call assert_equal("fḟ", tolower("fḟ"))
  call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
  call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
  call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
  call assert_equal("jĵǰ", tolower("jĵǰ"))
  call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
  call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
  call assert_equal("mḿṁ ", tolower("mḿṁ "))
  call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
  call assert_equal("pṕṗ", tolower("pṕṗ"))
  call assert_equal("q", tolower("q"))
  call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
  call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
  call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
  call assert_equal("vṽ", tolower("vṽ"))
  call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
  call assert_equal("ẋẍ", tolower("ẋẍ"))
  call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
  call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))

  " According to https://twitter.com/jifa/status/625776454479970304
  " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
  " in length (2 to 3 bytes) when lowercased. So let's test them.
  call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))

  " This call to tolower with invalid utf8 sequence used to cause access to
  " invalid memory.
  call tolower("\xC0\x80\xC0")
  call tolower("123\xC0\x80\xC0")

  " Test in latin1 encoding
  let save_enc = &encoding
  set encoding=latin1
  call assert_equal("abc", tolower("ABC"))
  let &encoding = save_enc
endfunc

func Test_toupper()
  call assert_equal("", toupper(""))

  " Test with all printable ASCII characters.
  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
          \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))

  " Test with a few lowercase diacritics.
  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
  call assert_equal("BḂḆ", toupper("bḃḇ"))
  call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
  call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
  call assert_equal("FḞ", toupper("fḟ"))
  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
  call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
  call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
  call assert_equal("JĴǰ", toupper("jĵǰ"))
  call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
  call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
  call assert_equal("MḾṀ ", toupper("mḿṁ "))
  call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
  call assert_equal("PṔṖ", toupper("pṕṗ"))
  call assert_equal("Q", toupper("q"))
  call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
  call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
  call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
  call assert_equal("VṼ", toupper("vṽ"))
  call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
  call assert_equal("ẊẌ", toupper("ẋẍ"))
  call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
  call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))

  " Test that uppercase diacritics, which should remain unchanged.
  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
  call assert_equal("BḂḆ", toupper("BḂḆ"))
  call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
  call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
  call assert_equal("FḞ ", toupper("FḞ "))
  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
  call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
  call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
  call assert_equal("JĴ", toupper("JĴ"))
  call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
  call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
  call assert_equal("MḾṀ", toupper("MḾṀ"))
  call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
  call assert_equal("PṔṖ", toupper("PṔṖ"))
  call assert_equal("Q", toupper("Q"))
  call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
  call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
  call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
  call assert_equal("VṼ", toupper("VṼ"))
  call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
  call assert_equal("XẊẌ", toupper("XẊẌ"))
  call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
  call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))

  call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))

  " This call to toupper with invalid utf8 sequence used to cause access to
  " invalid memory.
  call toupper("\xC0\x80\xC0")
  call toupper("123\xC0\x80\xC0")

  " Test in latin1 encoding
  let save_enc = &encoding
  set encoding=latin1
  call assert_equal("ABC", toupper("abc"))
  let &encoding = save_enc
endfunc

func Test_tr()
  call assert_equal('foo', tr('bar', 'bar', 'foo'))
  call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
  call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
  call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
  call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
  call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
  set encoding=latin1
  call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
  call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
  call assert_equal('hello', tr('hello', 'xy', 'ab'))
  call assert_fails('call tr("abc", "123", "₁₂")', 'E475:')
  set encoding=utf8
endfunc

" Tests for the mode() function
let current_modes = ''
func Save_mode()
  let g:current_modes = mode(0) . '-' . mode(1)
  return ''
endfunc

" Test for the mode() function
func Test_mode()
  new
  call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])

  " Only complete from the current buffer.
  set complete=.

  inoremap <F2> <C-R>=Save_mode()<CR>
  xnoremap <F2> <Cmd>call Save_mode()<CR>

  normal! 3G
  exe "normal i\<F2>\<Esc>"
  call assert_equal('i-i', g:current_modes)
  " i_CTRL-P: Multiple matches
  exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-P: Single match
  exe "normal iBro\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X
  exe "normal iBa\<C-X>\<F2>\<Esc>u"
  call assert_equal('i-ix', g:current_modes)
  " i_CTRL-X CTRL-P: Multiple matches
  exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X CTRL-P: Single match
  exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X CTRL-P + CTRL-P: Single match
  exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X CTRL-L: Multiple matches
  exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X CTRL-L: Single match
  exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-P: No match
  exe "normal iCom\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X CTRL-P: No match
  exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)
  " i_CTRL-X CTRL-L: No match
  exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('i-ic', g:current_modes)

  exe "normal R\<F2>\<Esc>"
  call assert_equal('R-R', g:current_modes)
  " R_CTRL-P: Multiple matches
  exe "normal RBa\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-P: Single match
  exe "normal RBro\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X
  exe "normal RBa\<C-X>\<F2>\<Esc>u"
  call assert_equal('R-Rx', g:current_modes)
  " R_CTRL-X CTRL-P: Multiple matches
  exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X CTRL-P: Single match
  exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X CTRL-P + CTRL-P: Single match
  exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X CTRL-L: Multiple matches
  exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X CTRL-L: Single match
  exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-P: No match
  exe "normal RCom\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X CTRL-P: No match
  exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)
  " R_CTRL-X CTRL-L: No match
  exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('R-Rc', g:current_modes)

  exe "normal gR\<F2>\<Esc>"
  call assert_equal('R-Rv', g:current_modes)
  " gR_CTRL-P: Multiple matches
  exe "normal gRBa\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-P: Single match
  exe "normal gRBro\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X
  exe "normal gRBa\<C-X>\<F2>\<Esc>u"
  call assert_equal('R-Rvx', g:current_modes)
  " gR_CTRL-X CTRL-P: Multiple matches
  exe "normal gRBa\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X CTRL-P: Single match
  exe "normal gRBro\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X CTRL-P + CTRL-P: Single match
  exe "normal gRBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X CTRL-L: Multiple matches
  exe "normal gR\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X CTRL-L: Single match
  exe "normal gRBlu\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-P: No match
  exe "normal gRCom\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X CTRL-P: No match
  exe "normal gRCom\<C-X>\<C-P>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)
  " gR_CTRL-X CTRL-L: No match
  exe "normal gRabc\<C-X>\<C-L>\<F2>\<Esc>u"
  call assert_equal('R-Rvc', g:current_modes)

  call assert_equal('n', 0->mode())
  call assert_equal('n', 1->mode())

  " i_CTRL-O
  exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
  call assert_equal("n-niI", g:current_modes)

  " R_CTRL-O
  exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
  call assert_equal("n-niR", g:current_modes)

  " gR_CTRL-O
  exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
  call assert_equal("n-niV", g:current_modes)

  " How to test operator-pending mode?

  call feedkeys("v", 'xt')
  call assert_equal('v', mode())
  call assert_equal('v', mode(1))
  call feedkeys("\<Esc>V", 'xt')
  call assert_equal('V', mode())
  call assert_equal('V', mode(1))
  call feedkeys("\<Esc>\<C-V>", 'xt')
  call assert_equal("\<C-V>", mode())
  call assert_equal("\<C-V>", mode(1))
  call feedkeys("\<Esc>", 'xt')

  call feedkeys("gh", 'xt')
  call assert_equal('s', mode())
  call assert_equal('s', mode(1))
  call feedkeys("\<Esc>gH", 'xt')
  call assert_equal('S', mode())
  call assert_equal('S', mode(1))
  call feedkeys("\<Esc>g\<C-H>", 'xt')
  call assert_equal("\<C-S>", mode())
  call assert_equal("\<C-S>", mode(1))
  call feedkeys("\<Esc>", 'xt')

  " v_CTRL-O
  exe "normal gh\<C-O>\<F2>\<Esc>"
  call assert_equal("v-vs", g:current_modes)
  exe "normal gH\<C-O>\<F2>\<Esc>"
  call assert_equal("V-Vs", g:current_modes)
  exe "normal g\<C-H>\<C-O>\<F2>\<Esc>"
  call assert_equal("\<C-V>-\<C-V>s", g:current_modes)

  call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
  call assert_equal('c-c', g:current_modes)
  call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
  call assert_equal('c-cv', g:current_modes)
  call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
  call assert_equal('c-ce', g:current_modes)
  " How to test Ex mode?

  " Test mode in operatorfunc (it used to be Operator-pending).
  set operatorfunc=OperatorFunc
  function OperatorFunc(_)
    call Save_mode()
  endfunction
  execute "normal! g@l\<Esc>"
  call assert_equal('n-n', g:current_modes)
  execute "normal! i\<C-o>g@l\<Esc>"
  call assert_equal('n-niI', g:current_modes)
  execute "normal! R\<C-o>g@l\<Esc>"
  call assert_equal('n-niR', g:current_modes)
  execute "normal! gR\<C-o>g@l\<Esc>"
  call assert_equal('n-niV', g:current_modes)

  if has('terminal')
    term
    call feedkeys("\<C-W>N", 'xt')
    call assert_equal('n', mode())
    call assert_equal('nt', mode(1))
    call feedkeys("aexit\<CR>", 'xt')
  endif

  bwipe!
  iunmap <F2>
  xunmap <F2>
  set complete&
  set operatorfunc&
  delfunction OperatorFunc
endfunc

" Test for append()
func Test_append()
  enew!
  split
  call assert_equal(0, append(1, []))
  call assert_equal(0, append(1, test_null_list()))
  call assert_equal(0, append(0, ["foo"]))
  call assert_equal(0, append(1, []))
  call assert_equal(0, append(1, test_null_list()))
  call assert_equal(0, append(8, []))
  call assert_equal(0, append(9, test_null_list()))
  call assert_equal(['foo', ''], getline(1, '$'))
  split
  only
  undo
  undo

  " Using $ instead of '$' must give an error
  call assert_fails("call append($, 'foobar')", 'E116:')

  call assert_fails("call append({}, '')", ['E728:', 'E728:'])
endfunc

" Test for setline()
func Test_setline()
  new
  call setline(0, ["foo"])
  call setline(0, [])
  call setline(0, test_null_list())
  call setline(1, ["bar"])
  call setline(1, [])
  call setline(1, test_null_list())
  call setline(2, [])
  call setline(2, test_null_list())
  call setline(3, [])
  call setline(3, test_null_list())
  call setline(2, ["baz"])
  call assert_equal(['bar', 'baz'], getline(1, '$'))
  close!
endfunc

func Test_getbufvar()
  let bnr = bufnr('%')
  let b:var_num = '1234'
  let def_num = '5678'
  call assert_equal('1234', getbufvar(bnr, 'var_num'))
  call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))

  let bd = getbufvar(bnr, '')
  call assert_equal('1234', bd['var_num'])
  call assert_true(exists("bd['changedtick']"))
  call assert_equal(2, len(bd))

  let bd2 = getbufvar(bnr, '', def_num)
  call assert_equal(bd, bd2)

  unlet b:var_num
  call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
  call assert_equal('', getbufvar(bnr, 'var_num'))

  let bd = getbufvar(bnr, '')
  call assert_equal(1, len(bd))
  let bd = getbufvar(bnr, '',def_num)
  call assert_equal(1, len(bd))

  call assert_equal('', getbufvar(9999, ''))
  call assert_equal(def_num, getbufvar(9999, '', def_num))
  unlet def_num

  call assert_equal(0, getbufvar(bnr, '&autoindent'))
  call assert_equal(0, getbufvar(bnr, '&autoindent', 1))

  " Set and get a buffer-local variable
  call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
  call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))

  " Open new window with forced option values
  set fileformats=unix,dos
  new ++ff=dos ++bin ++enc=iso-8859-2
  call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
  call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
  call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
  close

  " Get the b: dict.
  let b:testvar = 'one'
  new
  let b:testvar = 'two'
  let thebuf = bufnr()
  wincmd w
  call assert_equal('two', getbufvar(thebuf, 'testvar'))
  call assert_equal('two', getbufvar(thebuf, '').testvar)
  bwipe!

  set fileformats&
endfunc

func Test_last_buffer_nr()
  call assert_equal(bufnr('$'), last_buffer_nr())
endfunc

func Test_stridx()
  call assert_equal(-1, stridx('', 'l'))
  call assert_equal(0,  stridx('', ''))
  call assert_equal(0,  'hello'->stridx(''))
  call assert_equal(-1, stridx('hello', 'L'))
  call assert_equal(2,  stridx('hello', 'l', -1))
  call assert_equal(2,  stridx('hello', 'l', 0))
  call assert_equal(2,  'hello'->stridx('l', 1))
  call assert_equal(3,  stridx('hello', 'l', 3))
  call assert_equal(-1, stridx('hello', 'l', 4))
  call assert_equal(-1, stridx('hello', 'l', 10))
  call assert_equal(2,  stridx('hello', 'll'))
  call assert_equal(-1, stridx('hello', 'hello world'))
  call assert_fails("let n=stridx('hello', [])", 'E730:')
  call assert_fails("let n=stridx([], 'l')", 'E730:')
endfunc

func Test_strridx()
  call assert_equal(-1, strridx('', 'l'))
  call assert_equal(0,  strridx('', ''))
  call assert_equal(5,  strridx('hello', ''))
  call assert_equal(-1, strridx('hello', 'L'))
  call assert_equal(3,  'hello'->strridx('l'))
  call assert_equal(3,  strridx('hello', 'l', 10))
  call assert_equal(3,  strridx('hello', 'l', 3))
  call assert_equal(2,  strridx('hello', 'l', 2))
  call assert_equal(-1, strridx('hello', 'l', 1))
  call assert_equal(-1, strridx('hello', 'l', 0))
  call assert_equal(-1, strridx('hello', 'l', -1))
  call assert_equal(2,  strridx('hello', 'll'))
  call assert_equal(-1, strridx('hello', 'hello world'))
  call assert_fails("let n=strridx('hello', [])", 'E730:')
  call assert_fails("let n=strridx([], 'l')", 'E730:')
endfunc

func Test_match_func()
  call assert_equal(4,  match('testing', 'ing'))
  call assert_equal(4,  'testing'->match('ing', 2))
  call assert_equal(-1, match('testing', 'ing', 5))
  call assert_equal(-1, match('testing', 'ing', 8))
  call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
  call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
  call assert_fails("let x=match('vim', [])", 'E730:')
  call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
  call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
  call assert_equal(4,  match('testing', 'ing', -1))
  call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
  call assert_equal(-1, match(test_null_list(), 2))
  call assert_equal(-1, match('abc', '\\%('))
endfunc

func Test_matchend()
  call assert_equal(7,  matchend('testing', 'ing'))
  call assert_equal(7,  'testing'->matchend('ing', 2))
  call assert_equal(-1, matchend('testing', 'ing', 5))
  call assert_equal(-1, matchend('testing', 'ing', 8))
  call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
  call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
endfunc

func Test_matchlist()
  call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
  call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''],  'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
  call assert_equal([],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
endfunc

func Test_matchstr()
  call assert_equal('ing',  matchstr('testing', 'ing'))
  call assert_equal('ing',  'testing'->matchstr('ing', 2))
  call assert_equal('', matchstr('testing', 'ing', 5))
  call assert_equal('', matchstr('testing', 'ing', 8))
  call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
  call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
endfunc

func Test_matchstrpos()
  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
  call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
  call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
  call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
  call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a'))
endfunc

func Test_nextnonblank_prevnonblank()
  new
insert
This


is

a
Test
.
  call assert_equal(0, nextnonblank(-1))
  call assert_equal(0, nextnonblank(0))
  call assert_equal(1, nextnonblank(1))
  call assert_equal(4, 2->nextnonblank())
  call assert_equal(4, nextnonblank(3))
  call assert_equal(4, nextnonblank(4))
  call assert_equal(6, nextnonblank(5))
  call assert_equal(6, nextnonblank(6))
  call assert_equal(7, nextnonblank(7))
  call assert_equal(0, 8->nextnonblank())

  call assert_equal(0, prevnonblank(-1))
  call assert_equal(0, prevnonblank(0))
  call assert_equal(1, 1->prevnonblank())
  call assert_equal(1, prevnonblank(2))
  call assert_equal(1, prevnonblank(3))
  call assert_equal(4, prevnonblank(4))
  call assert_equal(4, 5->prevnonblank())
  call assert_equal(6, prevnonblank(6))
  call assert_equal(7, prevnonblank(7))
  call assert_equal(0, prevnonblank(8))
  bw!
endfunc

func Test_byte2line_line2byte()
  new
  set endofline
  call setline(1, ['a', 'bc', 'd'])

  set fileformat=unix
  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
  \                 map(range(-1, 8), 'byte2line(v:val)'))
  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
  \                 map(range(-1, 5), 'line2byte(v:val)'))

  set fileformat=mac
  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
  \                 map(range(-1, 8), 'v:val->byte2line()'))
  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
  \                 map(range(-1, 5), 'v:val->line2byte()'))

  set fileformat=dos
  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
  \                 map(range(-1, 11), 'byte2line(v:val)'))
  call assert_equal([-1, -1, 1, 4, 8, 11, -1],
  \                 map(range(-1, 5), 'line2byte(v:val)'))

  bw!
  set noendofline nofixendofline
  normal a-
  for ff in ["unix", "mac", "dos"]
    let &fileformat = ff
    call assert_equal(1, line2byte(1))
    call assert_equal(2, line2byte(2))  " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
  endfor

  set endofline& fixendofline& fileformat&
  bw!
endfunc

" Test for byteidx() and byteidxcomp() functions
func Test_byteidx()
  let a = '.é.' " one char of two bytes
  call assert_equal(0, byteidx(a, 0))
  call assert_equal(0, byteidxcomp(a, 0))
  call assert_equal(1, byteidx(a, 1))
  call assert_equal(1, byteidxcomp(a, 1))
  call assert_equal(3, byteidx(a, 2))
  call assert_equal(3, byteidxcomp(a, 2))
  call assert_equal(4, byteidx(a, 3))
  call assert_equal(4, byteidxcomp(a, 3))
  call assert_equal(-1, byteidx(a, 4))
  call assert_equal(-1, byteidxcomp(a, 4))

  let b = '.é.' " normal e with composing char
  call assert_equal(0, b->byteidx(0))
  call assert_equal(1, b->byteidx(1))
  call assert_equal(4, b->byteidx(2))
  call assert_equal(5, b->byteidx(3))
  call assert_equal(-1, b->byteidx(4))
  call assert_fails("call byteidx([], 0)", 'E730:')

  call assert_equal(0, b->byteidxcomp(0))
  call assert_equal(1, b->byteidxcomp(1))
  call assert_equal(2, b->byteidxcomp(2))
  call assert_equal(4, b->byteidxcomp(3))
  call assert_equal(5, b->byteidxcomp(4))
  call assert_equal(-1, b->byteidxcomp(5))
  call assert_fails("call byteidxcomp([], 0)", 'E730:')
endfunc

" Test for charidx()
func Test_charidx()
  let a = 'xáb́y'
  call assert_equal(0, charidx(a, 0))
  call assert_equal(1, charidx(a, 3))
  call assert_equal(2, charidx(a, 4))
  call assert_equal(3, charidx(a, 7))
  call assert_equal(-1, charidx(a, 8))
  call assert_equal(-1, charidx(a, -1))
  call assert_equal(-1, charidx('', 0))
  call assert_equal(-1, charidx(test_null_string(), 0))

  " count composing characters
  call assert_equal(0, charidx(a, 0, 1))
  call assert_equal(2, charidx(a, 2, 1))
  call assert_equal(3, charidx(a, 4, 1))
  call assert_equal(5, charidx(a, 7, 1))
  call assert_equal(-1, charidx(a, 8, 1))
  call assert_equal(-1, charidx('', 0, 1))

  call assert_fails('let x = charidx([], 1)', 'E1174:')
  call assert_fails('let x = charidx("abc", [])', 'E1210:')
  call assert_fails('let x = charidx("abc", 1, [])', 'E1212:')
  call assert_fails('let x = charidx("abc", 1, -1)', 'E1212:')
  call assert_fails('let x = charidx("abc", 1, 2)', 'E1212:')
endfunc

func Test_count()
  let l = ['a', 'a', 'A', 'b']
  call assert_equal(2, count(l, 'a'))
  call assert_equal(1, count(l, 'A'))
  call assert_equal(1, count(l, 'b'))
  call assert_equal(0, count(l, 'B'))

  call assert_equal(2, count(l, 'a', 0))
  call assert_equal(1, count(l, 'A', 0))
  call assert_equal(1, count(l, 'b', 0))
  call assert_equal(0, count(l, 'B', 0))

  call assert_equal(3, count(l, 'a', 1))
  call assert_equal(3, count(l, 'A', 1))
  call assert_equal(1, count(l, 'b', 1))
  call assert_equal(1, count(l, 'B', 1))
  call assert_equal(0, count(l, 'c', 1))

  call assert_equal(1, count(l, 'a', 0, 1))
  call assert_equal(2, count(l, 'a', 1, 1))
  call assert_fails('call count(l, "a", 0, 10)', 'E684:')
  call assert_fails('call count(l, "a", [])', 'E745:')

  let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
  call assert_equal(2, count(d, 'a'))
  call assert_equal(1, count(d, 'A'))
  call assert_equal(1, count(d, 'b'))
  call assert_equal(0, count(d, 'B'))

  call assert_equal(2, count(d, 'a', 0))
  call assert_equal(1, count(d, 'A', 0))
  call assert_equal(1, count(d, 'b', 0))
  call assert_equal(0, count(d, 'B', 0))

  call assert_equal(3, count(d, 'a', 1))
  call assert_equal(3, count(d, 'A', 1))
  call assert_equal(1, count(d, 'b', 1))
  call assert_equal(1, count(d, 'B', 1))
  call assert_equal(0, count(d, 'c', 1))

  call assert_fails('call count(d, "a", 0, 1)', 'E474:')

  call assert_equal(0, count("foo", "bar"))
  call assert_equal(1, count("foo", "oo"))
  call assert_equal(2, count("foo", "o"))
  call assert_equal(0, count("foo", "O"))
  call assert_equal(2, count("foo", "O", 1))
  call assert_equal(2, count("fooooo", "oo"))
  call assert_equal(0, count("foo", ""))

  call assert_fails('call count(0, 0)', 'E712:')
endfunc

func Test_changenr()
  new Xchangenr
  call assert_equal(0, changenr())
  norm ifoo
  call assert_equal(1, changenr())
  set undolevels=10
  norm Sbar
  call assert_equal(2, changenr())
  undo
  call assert_equal(1, changenr())
  redo
  call assert_equal(2, changenr())
  bw!
  set undolevels&
endfunc

func Test_filewritable()
  new Xfilewritable
  write!
  call assert_equal(1, filewritable('Xfilewritable'))

  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
  call assert_equal(0, filewritable('Xfilewritable'))

  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
  call assert_equal(1, 'Xfilewritable'->filewritable())

  call assert_equal(0, filewritable('doesnotexist'))

  call mkdir('Xwritedir', 'D')
  call assert_equal(2, filewritable('Xwritedir'))

  call delete('Xfilewritable')
  bw!
endfunc

func Test_Executable()
  if has('win32')
    call assert_equal(1, executable('notepad'))
    call assert_equal(1, 'notepad.exe'->executable())
    call assert_equal(0, executable('notepad.exe.exe'))
    call assert_equal(0, executable('shell32.dll'))
    call assert_equal(0, executable('win.ini'))

    " get "notepad" path and remove the leading drive and sep. (ex. 'C:\')
    let notepadcmd = exepath('notepad.exe')
    let driveroot = notepadcmd[:2]
    let notepadcmd = notepadcmd[3:]
    new
    " check that the relative path works in /
    execute 'lcd' driveroot
    call assert_equal(1, executable(notepadcmd))
    call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath())
    bwipe

    " create "notepad.bat"
    call mkdir('Xnotedir')
    let notepadbat = fnamemodify('Xnotedir/notepad.bat', ':p')
    call writefile([], notepadbat)
    new
    " check that the path and the pathext order is valid
    lcd Xnotedir
    let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd']
    call assert_equal(notepadbat, exepath('notepad'))
    let $PATHEXT = pathext
    bwipe
    eval 'Xnotedir'->delete('rf')
  elseif has('unix')
    call assert_equal(1, 'cat'->executable())
    call assert_equal(0, executable('nodogshere'))

    " get "cat" path and remove the leading /
    let catcmd = exepath('cat')[1:]
    new
    " check that the relative path works in /
    lcd /
    call assert_equal(1, executable(catcmd))
    let result = catcmd->exepath()
    " when using chroot looking for sbin/cat can return bin/cat, that is OK
    if catcmd =~ '\<sbin\>' && result =~ '\<bin\>'
      call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result)
    else
      " /bin/cat and /usr/bin/cat may be hard linked, we could get either
      let result = substitute(result, '/usr/bin/cat', '/bin/cat', '')
      let catcmd = substitute(catcmd, 'usr/bin/cat', 'bin/cat', '')
      call assert_equal('/' .. catcmd, result)
    endif
    bwipe
  else
    throw 'Skipped: does not work on this platform'
  endif
endfunc

func Test_executable_windows_store_apps()
  CheckMSWindows

  " Windows Store apps install some 'decoy' .exe that require some careful
  " handling as they behave similarly to symlinks.
  let app_dir = expand("$LOCALAPPDATA\\Microsoft\\WindowsApps")
  if !isdirectory(app_dir)
    return
  endif

  let save_path = $PATH
  let $PATH = app_dir
  " Ensure executable() finds all the app .exes
  for entry in readdir(app_dir)
    if entry =~ '\.exe$'
      call assert_true(executable(entry))
    endif
  endfor

  let $PATH = save_path
endfunc

func Test_executable_longname()
  CheckMSWindows

  " Create a temporary .bat file with 205 characters in the name.
  " Maximum length of a filename (including the path) on MS-Windows is 259
  " characters.
  " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
  let len = 259 - getcwd()->len() - 6
  if len > 200
    let len = 200
  endif

  let fname = 'X' . repeat('あ', len) . '.bat'
  call writefile([], fname)
  call assert_equal(1, executable(fname))
  call delete(fname)
endfunc

func Test_hostname()
  let hostname_vim = hostname()
  if has('unix')
    let hostname_system = systemlist('uname -n')[0]
    call assert_equal(hostname_vim, hostname_system)
  endif
endfunc

func Test_getpid()
  " getpid() always returns the same value within a vim instance.
  call assert_equal(getpid(), getpid())
  if has('unix')
    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
  endif
endfunc

func Test_hlexists()
  call assert_equal(0, hlexists('does_not_exist'))
  call assert_equal(0, 'Number'->hlexists())
  call assert_equal(0, highlight_exists('does_not_exist'))
  call assert_equal(0, highlight_exists('Number'))
  syntax on
  call assert_equal(0, hlexists('does_not_exist'))
  call assert_equal(1, hlexists('Number'))
  call assert_equal(0, highlight_exists('does_not_exist'))
  call assert_equal(1, highlight_exists('Number'))
  syntax off
endfunc

" Test for the col() function
func Test_col()
  new
  call setline(1, 'abcdef')
  norm gg4|mx6|mY2|
  call assert_equal(2, col('.'))
  call assert_equal(7, col('$'))
  call assert_equal(2, col('v'))
  call assert_equal(4, col("'x"))
  call assert_equal(6, col("'Y"))
  call assert_equal(2, [1, 2]->col())
  call assert_equal(7, col([1, '$']))

  call assert_equal(0, col(''))
  call assert_equal(0, col('x'))
  call assert_equal(0, col([2, '$']))
  call assert_equal(0, col([1, 100]))
  call assert_equal(0, col([1]))
  call assert_equal(0, col(test_null_list()))
  call assert_fails('let c = col({})', 'E1222:')
  call assert_fails('let c = col(".", [])', 'E1210:')

  " test for getting the visual start column
  func T()
    let g:Vcol = col('v')
    return ''
  endfunc
  let g:Vcol = 0
  xmap <expr> <F2> T()
  exe "normal gg3|ve\<F2>"
  call assert_equal(3, g:Vcol)
  xunmap <F2>
  delfunc T

  " Test for the visual line start and end marks '< and '>
  call setline(1, ['one', 'one two', 'one two three'])
  "normal! ggVG
  call feedkeys("ggVG\<Esc>", 'xt')
  call assert_equal(1, col("'<"))
  call assert_equal(14, col("'>"))
  " Delete the last line of the visually selected region
  $d
  call assert_notequal(14, col("'>"))

  " Test with 'virtualedit'
  set virtualedit=all
  call cursor(1, 10)
  call assert_equal(4, col('.'))
  set virtualedit&

  " Test for getting the column number in another window
  let winid = win_getid()
  new
  call win_execute(winid, 'normal 1G$')
  call assert_equal(3, col('.', winid))
  call win_execute(winid, 'normal 2G')
  call assert_equal(8, col('$', winid))
  call assert_equal(0, col('.', 5001))

  bw!
endfunc

" Test for input()
func Test_input_func()
  " Test for prompt with multiple lines
  redir => v
  call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
  redir END
  call assert_equal("B", c)
  call assert_equal(['A', 'B', 'C'], split(v, "\n"))

  " Test for default value
  call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
  call assert_equal('red', c)

  " Test for completion at the input prompt
  func! Tcomplete(arglead, cmdline, pos)
    return "item1\nitem2\nitem3"
  endfunc
  call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>"
        \ .. "\<C-A>\<CR>", 'xt')
  delfunc Tcomplete
  call assert_equal('item1 item2 item3', c)

  " Test for using special characters as default input
  call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt')
  call assert_equal('y', c)

  " Test for using text with composing characters as default input
  call feedkeys(":let c = input('name? ', \"ã̳\")\<CR>\<CR>", 'xt')
  call assert_equal('ã̳', c)

  " Test for using <CR> as default input
  call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt')
  call assert_equal(' x', c)

  call assert_fails("call input('F:', '', 'invalid')", 'E180:')
  call assert_fails("call input('F:', '', [])", 'E730:')
endfunc

" Test for the inputdialog() function
func Test_inputdialog()
  set timeout timeoutlen=10
  if has('gui_running')
    call assert_fails('let v=inputdialog([], "xx")', 'E730:')
    call assert_fails('let v=inputdialog("Q", [])', 'E730:')
  else
    call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
    call assert_equal('xx', v)
    call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
    call assert_equal('yy', v)
  endif
  set timeout& timeoutlen&
endfunc

" Test for inputlist()
func Test_inputlist()
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
  call assert_equal(1, c)
  call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
  call assert_equal(2, c)
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
  call assert_equal(3, c)

  " CR to cancel
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx')
  call assert_equal(0, c)

  " Esc to cancel
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx')
  call assert_equal(0, c)

  " q to cancel
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx')
  call assert_equal(0, c)

  " Cancel after inputting a number
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx')
  call assert_equal(0, c)

  " Use backspace to delete characters in the prompt
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
  call assert_equal(2, c)

  " Use mouse to make a selection
  call test_setmouse(&lines - 3, 2)
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
  call assert_equal(1, c)
  " Mouse click outside of the list
  call test_setmouse(&lines - 6, 2)
  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
  call assert_equal(-2, c)

  call assert_fails('call inputlist("")', 'E686:')
  call assert_fails('call inputlist(test_null_list())', 'E686:')
endfunc

func Test_range_inputlist()
  " flush out any garbage left in the buffer
  while getchar(0)
  endwhile

  call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
  call assert_equal(1, result)
  call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
  call assert_equal(1, result)

  unlet result
endfunc

func Test_balloon_show()
  CheckFeature balloon_eval

  " This won't do anything but must not crash either.
  call balloon_show('hi!')
  if !has('gui_running')
    call balloon_show(range(3))
    call balloon_show([])
  endif
endfunc

func Test_setbufvar_options()
  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
  " window layout and cursor position.
  call assert_equal(1, winnr('$'))
  split dummy_preview
  resize 2
  set winfixheight winfixwidth
  let prev_id = win_getid()

  wincmd j
  let wh = winheight(0)
  let dummy_buf = bufnr('dummy_buf1', v:true)
  call setbufvar(dummy_buf, '&buftype', 'nofile')
  execute 'belowright vertical split #' . dummy_buf
  call assert_equal(wh, winheight(0))
  let dum1_id = win_getid()
  call setline(1, 'foo')
  normal! V$
  call assert_equal(4, col('.'))
  call setbufvar('dummy_preview', '&buftype', 'nofile')
  call assert_equal(4, col('.'))

  wincmd h
  let wh = winheight(0)
  call setline(1, 'foo')
  normal! V$
  call assert_equal(4, col('.'))
  let dummy_buf = bufnr('dummy_buf2', v:true)
  eval 'nofile'->setbufvar(dummy_buf, '&buftype')
  call assert_equal(4, col('.'))
  execute 'belowright vertical split #' . dummy_buf
  call assert_equal(wh, winheight(0))

  bwipe!
  call win_gotoid(prev_id)
  bwipe!
  call win_gotoid(dum1_id)
  bwipe!
endfunc

func Test_setbufvar_keep_window_title()
  CheckRunVimInTerminal
  if !has('title') || empty(&t_ts)
    throw "Skipped: can't get/set title"
  endif

  let lines =<< trim END
      set title
      edit Xa.txt
      let g:buf = bufadd('Xb.txt')
      inoremap <F2> <C-R>=setbufvar(g:buf, '&autoindent', 1) ?? ''<CR>
  END
  call writefile(lines, 'Xsetbufvar', 'D')
  let buf = RunVimInTerminal('-S Xsetbufvar', {})
  call WaitForAssert({-> assert_match('Xa.txt', term_gettitle(buf))}, 1000)

  call term_sendkeys(buf, "i\<F2>")
  call TermWait(buf)
  call term_sendkeys(buf, "\<Esc>")
  call TermWait(buf)
  call assert_match('Xa.txt', term_gettitle(buf))

  call StopVimInTerminal(buf)
endfunc

func Test_redo_in_nested_functions()
  nnoremap g. :set opfunc=Operator<CR>g@
  function Operator( type, ... )
     let @x = 'XXX'
     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
  endfunction

  function! Apply()
      5,6normal! .
  endfunction

  new
  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
  1normal g.i"
  call assert_equal('some "XXX" text', getline(1))
  3,4normal .
  call assert_equal('some "XXX" text', getline(3))
  call assert_equal('more "XXX" text', getline(4))
  call Apply()
  call assert_equal('some "XXX" text', getline(5))
  call assert_equal('more "XXX" text', getline(6))
  bwipe!

  nunmap g.
  delfunc Operator
  delfunc Apply
endfunc

func Test_trim()
  call assert_equal("Testing", trim("  \t\r\r\x0BTesting  \t\n\r\n\t\x0B\x0B"))
  call assert_equal("Testing", "  \t  \r\r\n\n\x0BTesting  \t\n\r\n\t\x0B\x0B"->trim())
  call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
  call assert_equal("wRE    \tSERVEzyww", trim("wRE    \tSERVEzyww"))
  call assert_equal("abcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail"))
  call assert_equal("\tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", " "))
  call assert_equal(" \tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", "abx"))
  call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
  call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
  call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r   你好您R E SER V E早好你你    \t  \x0B", ))
  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    你好您R E SER V E早好你你    \t  \x0B", " 你好"))
  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", " 你好tes"))
  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", "   你你你好好好tttsses"))
  call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
  call assert_equal("", trim("", ""))
  call assert_equal("a", trim("a", ""))
  call assert_equal("", trim("", "a"))

  call assert_equal("vim", trim("  vim  ", " ", 0))
  call assert_equal("vim  ", trim("  vim  ", " ", 1))
  call assert_equal("  vim", trim("  vim  ", " ", 2))
  call assert_fails('eval trim("  vim  ", " ", [])', 'E745:')
  call assert_fails('eval trim("  vim  ", " ", -1)', 'E475:')
  call assert_fails('eval trim("  vim  ", " ", 3)', 'E475:')
  call assert_fails('eval trim("  vim  ", 0)', 'E1174:')

  let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
  call assert_equal("x", trim(chars . "x" . chars))

  call assert_fails('let c=trim([])', 'E730:')
endfunc

" Test for reg_recording() and reg_executing()
func Test_reg_executing_and_recording()
  let s:reg_stat = ''
  func s:save_reg_stat()
    let s:reg_stat = reg_recording() . ':' . reg_executing()
    return ''
  endfunc

  new
  call s:save_reg_stat()
  call assert_equal(':', s:reg_stat)
  call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
  call assert_equal('a:', s:reg_stat)
  call feedkeys("@a", 'xt')
  call assert_equal(':a', s:reg_stat)
  call feedkeys("qb@aq", 'xt')
  call assert_equal('b:a', s:reg_stat)
  call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
  call assert_equal('":', s:reg_stat)

  " :normal command saves and restores reg_executing
  let s:reg_stat = ''
  let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
  func TestFunc() abort
    normal! ia
  endfunc
  call feedkeys("@q", 'xt')
  call assert_equal(':q', s:reg_stat)
  delfunc TestFunc

  " getchar() command saves and restores reg_executing
  map W :call TestFunc()<CR>
  let @q = "W"
  let g:typed = ''
  let g:regs = []
  func TestFunc() abort
    let g:regs += [reg_executing()]
    let g:typed = getchar(0)
    let g:regs += [reg_executing()]
  endfunc
  call feedkeys("@qy", 'xt')
  call assert_equal(char2nr("y"), g:typed)
  call assert_equal(['q', 'q'], g:regs)
  delfunc TestFunc
  unmap W
  unlet g:typed
  unlet g:regs

  " input() command saves and restores reg_executing
  map W :call TestFunc()<CR>
  let @q = "W"
  let g:typed = ''
  let g:regs = []
  func TestFunc() abort
    let g:regs += [reg_executing()]
    let g:typed = '?'->input()
    let g:regs += [reg_executing()]
  endfunc
  call feedkeys("@qy\<CR>", 'xt')
  call assert_equal("y", g:typed)
  call assert_equal(['q', 'q'], g:regs)
  delfunc TestFunc
  unmap W
  unlet g:typed
  unlet g:regs

  bwipe!
  delfunc s:save_reg_stat
  unlet s:reg_stat
endfunc

func Test_inputsecret()
  map W :call TestFunc()<CR>
  let @q = "W"
  let g:typed1 = ''
  let g:typed2 = ''
  let g:regs = []
  func TestFunc() abort
    let g:typed1 = '?'->inputsecret()
    let g:typed2 = inputsecret('password: ')
  endfunc
  call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
  call assert_equal("something", g:typed1)
  call assert_equal("else", g:typed2)
  delfunc TestFunc
  unmap W
  unlet g:typed1
  unlet g:typed2
endfunc

func Test_getchar()
  call feedkeys('a', '')
  call assert_equal(char2nr('a'), getchar())
  call assert_equal(0, getchar(0))
  call assert_equal(0, getchar(1))

  call feedkeys('a', '')
  call assert_equal('a', getcharstr())
  call assert_equal('', getcharstr(0))
  call assert_equal('', getcharstr(1))

  call feedkeys("\<M-F2>", '')
  call assert_equal("\<M-F2>", getchar(0))
  call assert_equal(0, getchar(0))

  call setline(1, 'xxxx')
  call test_setmouse(1, 3)
  let v:mouse_win = 9
  let v:mouse_winid = 9
  let v:mouse_lnum = 9
  let v:mouse_col = 9
  call feedkeys("\<S-LeftMouse>", '')
  call assert_equal("\<S-LeftMouse>", getchar())
  call assert_equal(1, v:mouse_win)
  call assert_equal(win_getid(1), v:mouse_winid)
  call assert_equal(1, v:mouse_lnum)
  call assert_equal(3, v:mouse_col)
  enew!
endfunc

func Test_libcall_libcallnr()
  CheckFeature libcall

  if has('win32')
    let libc = 'msvcrt.dll'
  elseif has('mac')
    let libc = 'libSystem.B.dylib'
  elseif executable('ldd')
    let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
  endif
  if get(l:, 'libc', '') ==# ''
    " On Unix, libc.so can be in various places.
    if has('linux')
      " There is not documented but regarding the 1st argument of glibc's
      " dlopen an empty string and nullptr are equivalent, so using an empty
      " string for the 1st argument of libcall allows to call functions.
      let libc = ''
    elseif has('sun')
      " Set the path to libc.so according to the architecture.
      let test_bits = system('file ' . GetVimProg())
      let test_arch = system('uname -p')
      if test_bits =~ '64-bit' && test_arch =~ 'sparc'
        let libc = '/usr/lib/sparcv9/libc.so'
      elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
        let libc = '/usr/lib/amd64/libc.so'
      else
        let libc = '/usr/lib/libc.so'
      endif
    else
      " Unfortunately skip this test until a good way is found.
      return
    endif
  endif

  if has('win32')
    call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
  else
    call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
  endif

  " If function returns NULL, libcall() should return an empty string.
  call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))

  " Test libcallnr() with string and integer argument.
  call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
  call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))

  call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
  call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])

  call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:'])
  call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:'])
endfunc

sandbox function Fsandbox()
  normal ix
endfunc

func Test_func_sandbox()
  sandbox let F = {-> 'hello'}
  call assert_equal('hello', F())

  sandbox let F = {-> "normal ix\<Esc>"->execute()}
  call assert_fails('call F()', 'E48:')
  unlet F

  call assert_fails('call Fsandbox()', 'E48:')
  delfunc Fsandbox

  " From a sandbox try to set a predefined variable (which cannot be modified
  " from a sandbox)
  call assert_fails('sandbox let v:lnum = 10', 'E794:')
endfunc

func EditAnotherFile()
  let word = expand('<cword>')
  edit Xfuncrange2
endfunc

func Test_func_range_with_edit()
  " Define a function that edits another buffer, then call it with a range that
  " is invalid in that buffer.
  call writefile(['just one line'], 'Xfuncrange2', 'D')
  new
  eval 10->range()->setline(1)
  write Xfuncrange1
  call assert_fails('5,8call EditAnotherFile()', 'E16:')

  call delete('Xfuncrange1')
  bwipe!
endfunc

func Test_func_exists_on_reload()
  call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists', 'D')
  call assert_equal(0, exists('*ExistingFunction'))
  source Xfuncexists
  call assert_equal(1, '*ExistingFunction'->exists())
  " Redefining a function when reloading a script is OK.
  source Xfuncexists
  call assert_equal(1, exists('*ExistingFunction'))

  " But redefining in another script is not OK.
  call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2', 'D')
  call assert_fails('source Xfuncexists2', 'E122:')

  " Defining a new function from the cmdline should fail if the function is
  " already defined
  call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:')

  delfunc ExistingFunction
  call assert_equal(0, exists('*ExistingFunction'))
  call writefile([
	\ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
	\ 'func ExistingFunction()', 'echo "no"', 'endfunc',
	\ ], 'Xfuncexists')
  call assert_fails('source Xfuncexists', 'E122:')
  call assert_equal(1, exists('*ExistingFunction'))

  delfunc ExistingFunction
endfunc

" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
func Test_confirm()
  CheckUnix
  CheckNotGui

  call feedkeys('o', 'L')
  let a = confirm('Press O to proceed')
  call assert_equal(1, a)

  call feedkeys('y', 'L')
  let a = 'Are you sure?'->confirm("&Yes\n&No")
  call assert_equal(1, a)

  call feedkeys('n', 'L')
  let a = confirm('Are you sure?', "&Yes\n&No")
  call assert_equal(2, a)

  " confirm() should return 0 when pressing CTRL-C.
  call feedkeys("\<C-C>", 'L')
  let a = confirm('Are you sure?', "&Yes\n&No")
  call assert_equal(0, a)

  " <Esc> requires another character to avoid it being seen as the start of an
  " escape sequence.  Zero should be harmless.
  eval "\<Esc>0"->feedkeys('L')
  let a = confirm('Are you sure?', "&Yes\n&No")
  call assert_equal(0, a)

  " Default choice is returned when pressing <CR>.
  call feedkeys("\<CR>", 'L')
  let a = confirm('Are you sure?', "&Yes\n&No")
  call assert_equal(1, a)

  call feedkeys("\<CR>", 'L')
  let a = confirm('Are you sure?', "&Yes\n&No", 2)
  call assert_equal(2, a)

  call feedkeys("\<CR>", 'L')
  let a = confirm('Are you sure?', "&Yes\n&No", 0)
  call assert_equal(0, a)

  " Test with the {type} 4th argument
  for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
    call feedkeys('y', 'L')
    let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
    call assert_equal(1, a)
  endfor

  call assert_fails('call confirm([])', 'E730:')
  call assert_fails('call confirm("Are you sure?", [])', 'E730:')
  call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
  call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
endfunc

func Test_platform_name()
  " The system matches at most only one name.
  let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
  call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))

  " Is Unix?
  call assert_equal(has('bsd'), has('bsd') && has('unix'))
  call assert_equal(has('hpux'), has('hpux') && has('unix'))
  call assert_equal(has('linux'), has('linux') && has('unix'))
  call assert_equal(has('mac'), has('mac') && has('unix'))
  call assert_equal(has('qnx'), has('qnx') && has('unix'))
  call assert_equal(has('sun'), has('sun') && has('unix'))
  call assert_equal(has('win32'), has('win32') && !has('unix'))
  call assert_equal(has('win32unix'), has('win32unix') && has('unix'))

  if has('unix') && executable('uname')
    let uname = system('uname')
    " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
    call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
    call assert_equal(uname =~? 'HP-UX', has('hpux'))
    call assert_equal(uname =~? 'Linux', has('linux'))
    call assert_equal(uname =~? 'Darwin', has('mac'))
    call assert_equal(uname =~? 'QNX', has('qnx'))
    call assert_equal(uname =~? 'SunOS', has('sun'))
    call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
  endif
endfunc

func Test_readdir()
  call mkdir('Xreaddir', 'R')
  call writefile([], 'Xreaddir/foo.txt')
  call writefile([], 'Xreaddir/bar.txt')
  call mkdir('Xreaddir/dir')

  " All results
  let files = readdir('Xreaddir')
  call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))

  " Only results containing "f"
  let files = 'Xreaddir'->readdir({ x -> stridx(x, 'f') != -1 })
  call assert_equal(['foo.txt'], sort(files))

  " Only .txt files
  let files = readdir('Xreaddir', { x -> x =~ '.txt$' })
  call assert_equal(['bar.txt', 'foo.txt'], sort(files))

  " Only .txt files with string
  let files = readdir('Xreaddir', 'v:val =~ ".txt$"')
  call assert_equal(['bar.txt', 'foo.txt'], sort(files))

  " Limit to 1 result.
  let l = []
  let files = readdir('Xreaddir', {x -> len(add(l, x)) == 2 ? -1 : 1})
  call assert_equal(1, len(files))

  " Nested readdir() must not crash
  let files = readdir('Xreaddir', 'readdir("Xreaddir", "1") != []')
  call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
endfunc

func Test_readdirex()
  call mkdir('Xexdir', 'R')
  call writefile(['foo'], 'Xexdir/foo.txt')
  call writefile(['barbar'], 'Xexdir/bar.txt')
  call mkdir('Xexdir/dir')

  " All results
  let files = readdirex('Xexdir')->map({-> v:val.name})
  call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
  let sizes = readdirex('Xexdir')->map({-> v:val.size})
  call assert_equal([0, 4, 7], sort(sizes))

  " Only results containing "f"
  let files = 'Xexdir'->readdirex({ e -> stridx(e.name, 'f') != -1 })
			  \ ->map({-> v:val.name})
  call assert_equal(['foo.txt'], sort(files))

  " Only .txt files
  let files = readdirex('Xexdir', { e -> e.name =~ '.txt$' })
			  \ ->map({-> v:val.name})
  call assert_equal(['bar.txt', 'foo.txt'], sort(files))

  " Only .txt files with string
  let files = readdirex('Xexdir', 'v:val.name =~ ".txt$"')
			  \ ->map({-> v:val.name})
  call assert_equal(['bar.txt', 'foo.txt'], sort(files))

  " Limit to 1 result.
  let l = []
  let files = readdirex('Xexdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1})
			  \ ->map({-> v:val.name})
  call assert_equal(1, len(files))

  " Nested readdirex() must not crash
  let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
			  \ ->map({-> v:val.name})
  call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])

  " report broken link correctly
  if has("unix")
    call writefile([], 'Xexdir/abc.txt')
    call system("ln -s Xexdir/abc.txt Xexdir/link")
    call delete('Xexdir/abc.txt')
    let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
			  \ ->map({-> v:val.name .. '_' .. v:val.type})
    call sort(files)->assert_equal(
        \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link'])
  endif

  call assert_fails('call readdirex("doesnotexist")', 'E484:')
endfunc

func Test_readdirex_sort()
  CheckUnix
  " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing)
  if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix")
    throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem'
  endif
  let _collate = v:collate
  call mkdir('Xsortdir2', 'R')
  call writefile(['1'], 'Xsortdir2/README.txt')
  call writefile(['2'], 'Xsortdir2/Readme.txt')
  call writefile(['3'], 'Xsortdir2/readme.txt')

  " 1) default
  let files = readdirex('Xsortdir2')->map({-> v:val.name})
  let default = copy(files)
  call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default')

  " 2) no sorting
  let files = readdirex('Xsortdir2', 1, #{sort: 'none'})->map({-> v:val.name})
  let unsorted = copy(files)
  call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted')
  call assert_fails("call readdirex('Xsortdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required')

  " 3) sort by case (same as default)
  let files = readdirex('Xsortdir2', 1, #{sort: 'case'})->map({-> v:val.name})
  call assert_equal(default, files, 'sort by case')

  " 4) sort by ignoring case
  let files = readdirex('Xsortdir2', 1, #{sort: 'icase'})->map({-> v:val.name})
  call assert_equal(unsorted->sort('i'), files, 'sort by icase')

  " 5) Default Collation
  let collate = v:collate
  lang collate C
  let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
  call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation')

  " 6) Collation de_DE
  " Switch locale, this may not work on the CI system, if the locale isn't
  " available
  try
    lang collate de_DE
    let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
    call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation')
  catch
    throw 'Skipped: de_DE collation is not available'

  finally
    exe 'lang collate' collate
  endtry
endfunc

func Test_readdir_sort()
  " some more cases for testing sorting for readdirex
  let dir = 'Xsortdir3'
  call mkdir(dir, 'R')
  call writefile(['1'], dir .. '/README.txt')
  call writefile(['2'], dir .. '/Readm.txt')
  call writefile(['3'], dir .. '/read.txt')
  call writefile(['4'], dir .. '/Z.txt')
  call writefile(['5'], dir .. '/a.txt')
  call writefile(['6'], dir .. '/b.txt')

  " 1) default
  let files = readdir(dir)
  let default = copy(files)
  call assert_equal(default->sort(), files, 'sort using default')

  " 2) sort by case (same as default)
  let files = readdir(dir, '1', #{sort: 'case'})
  call assert_equal(default, files, 'sort using default')

  " 3) sort by ignoring case
  let files = readdir(dir, '1', #{sort: 'icase'})
  call assert_equal(default->sort('i'), files, 'sort by ignoring case')

  " 4) collation
  let collate = v:collate
  lang collate C
  let files = readdir(dir, 1, #{sort: 'collate'})
  call assert_equal(default->sort(), files, 'sort by C collation')
  exe "lang collate" collate

  " 5) Errors
  call assert_fails('call readdir(dir, 1, 1)', 'E1206:')
  call assert_fails('call readdir(dir, 1, #{sorta: 1})')
  call assert_fails('call readdir(dir, 1, test_null_dict())', 'E1297:')
  call assert_fails('call readdirex(dir, 1, 1)', 'E1206:')
  call assert_fails('call readdirex(dir, 1, #{sorta: 1})')
  call assert_fails('call readdirex(dir, 1, test_null_dict())', 'E1297:')

  " 6) ignore other values in dict
  let files = readdir(dir, '1', #{sort: 'c'})
  call assert_equal(default, files, 'sort using default2')

  " Cleanup
  exe "lang collate" collate
endfunc

func Test_delete_rf()
  call mkdir('Xrfdir')
  call writefile([], 'Xrfdir/foo.txt')
  call writefile([], 'Xrfdir/bar.txt')
  call mkdir('Xrfdir/[a-1]')  " issue #696
  call writefile([], 'Xrfdir/[a-1]/foo.txt')
  call writefile([], 'Xrfdir/[a-1]/bar.txt')
  call assert_true(filereadable('Xrfdir/foo.txt'))
  call assert_true('Xrfdir/[a-1]/foo.txt'->filereadable())

  call assert_equal(0, delete('Xrfdir', 'rf'))
  call assert_false(filereadable('Xrfdir/foo.txt'))
  call assert_false(filereadable('Xrfdir/[a-1]/foo.txt'))

  if has('unix')
    call mkdir('Xrfdir/Xdir2', 'p')
    silent !chmod 555 Xrfdir
    call assert_equal(-1, delete('Xrfdir/Xdir2', 'rf'))
    call assert_equal(-1, delete('Xrfdir', 'rf'))
    silent !chmod 755 Xrfdir
    call assert_equal(0, delete('Xrfdir', 'rf'))
  endif
endfunc

func Test_call()
  call assert_equal(3, call('len', [123]))
  call assert_equal(3, 'len'->call([123]))
  call assert_fails("call call('len', 123)", 'E1211:')
  call assert_equal(0, call('', []))
  call assert_equal(0, call('len', test_null_list()))

  function Mylen() dict
     return len(self.data)
  endfunction
  let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
  eval mydict.len->call([], mydict)->assert_equal(4)
  call assert_fails("call call('Mylen', [], 0)", 'E1206:')
  call assert_fails('call foo', 'E107:')

  " These once caused a crash.
  call call(test_null_function(), [])
  call call(test_null_partial(), [])
  call assert_fails('call test_null_function()()', 'E1192:')
  call assert_fails('call test_null_partial()()', 'E117:')

  let lines =<< trim END
      let Time = 'localtime'
      call Time()
  END
  call v9.CheckScriptFailure(lines, 'E1085:')
endfunc

func Test_char2nr()
  call assert_equal(12354, char2nr('あ', 1))
  call assert_equal(120, 'x'->char2nr())
  set encoding=latin1
  call assert_equal(120, 'x'->char2nr())
  set encoding=utf-8
endfunc

func Test_charclass()
  call assert_equal(0, charclass(' '))
  call assert_equal(1, charclass('.'))
  call assert_equal(2, charclass('x'))
  call assert_equal(3, charclass("\u203c"))
  " this used to crash vim
  call assert_equal(0, "xxx"[-1]->charclass())
endfunc

func Test_eventhandler()
  call assert_equal(0, eventhandler())
endfunc

func Test_bufadd_bufload()
  call assert_equal(0, bufexists('someName'))
  let buf = bufadd('someName')
  call assert_notequal(0, buf)
  call assert_equal(1, bufexists('someName'))
  call assert_equal(0, getbufvar(buf, '&buflisted'))
  call assert_equal(0, bufloaded(buf))
  call bufload(buf)
  call assert_equal(1, bufloaded(buf))
  call assert_equal([''], getbufline(buf, 1, '$'))

  let curbuf = bufnr('')
  eval ['some', 'text']->writefile('XotherName')
  let buf = 'XotherName'->bufadd()
  call assert_notequal(0, buf)
  eval 'XotherName'->bufexists()->assert_equal(1)
  call assert_equal(0, getbufvar(buf, '&buflisted'))
  call assert_equal(0, bufloaded(buf))
  eval buf->bufload()
  call assert_equal(1, bufloaded(buf))
  call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
  call assert_equal(curbuf, bufnr(''))

  let buf1 = bufadd('')
  let buf2 = bufadd('')
  call assert_notequal(0, buf1)
  call assert_notequal(0, buf2)
  call assert_notequal(buf1, buf2)
  call assert_equal(1, bufexists(buf1))
  call assert_equal(1, bufexists(buf2))
  call assert_equal(0, bufloaded(buf1))
  exe 'bwipe ' .. buf1
  call assert_equal(0, bufexists(buf1))
  call assert_equal(1, bufexists(buf2))
  exe 'bwipe ' .. buf2
  call assert_equal(0, bufexists(buf2))

  " When 'buftype' is "nofile" then bufload() does not read the file.
  " Other values too.
  for val in [['nofile', 0],
            \ ['nowrite', 1],
            \ ['acwrite', 1],
            \ ['quickfix', 0],
            \ ['help', 1],
            \ ['terminal', 0],
            \ ['prompt', 0],
            \ ['popup', 0],
            \ ]
    bwipe! XotherName
    let buf = bufadd('XotherName')
    call setbufvar(buf, '&bt', val[0])
    call bufload(buf)
    call assert_equal(val[1] ? ['some', 'text'] : [''], getbufline(buf, 1, '$'), val[0])
  endfor

  bwipe someName
  bwipe XotherName
  call assert_equal(0, bufexists('someName'))
  call delete('XotherName')
endfunc

func Test_state()
  CheckRunVimInTerminal

  let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"

  let lines =<< trim END
	call setline(1, ['one', 'two', 'three'])
	map ;; gg
	set complete=.
	func RunTimer()
	  call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
	endfunc
	au Filetype foobar let g:state = state()|let g:mode = mode()
  END
  call writefile(lines, 'XState')
  let buf = RunVimInTerminal('-S XState', #{rows: 6})

  " Using a ":" command Vim is busy, thus "S" is returned
  call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
  call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
  call term_sendkeys(buf, ":\<CR>")

  " Using a timer callback
  call term_sendkeys(buf, ":call RunTimer()\<CR>")
  call TermWait(buf, 25)
  call term_sendkeys(buf, getstate)
  call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)

  " Halfway a mapping
  call term_sendkeys(buf, ":call RunTimer()\<CR>;")
  call TermWait(buf, 25)
  call term_sendkeys(buf, ";")
  call term_sendkeys(buf, getstate)
  call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)

  " Insert mode completion (bit slower on Mac)
  call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
  call TermWait(buf, 25)
  call term_sendkeys(buf, "\<Esc>")
  call term_sendkeys(buf, getstate)
  call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)

  " Autocommand executing
  call term_sendkeys(buf, ":set filetype=foobar\<CR>")
  call TermWait(buf, 25)
  call term_sendkeys(buf, getstate)
  call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)

  " Todo: "w" - waiting for ch_evalexpr()

  " messages scrolled
  call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
  call TermWait(buf, 25)
  call term_sendkeys(buf, "\<CR>")
  call term_sendkeys(buf, getstate)
  call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)

  call StopVimInTerminal(buf)
  call delete('XState')
endfunc

func Test_range()
  " destructuring
  let [x, y] = range(2)
  call assert_equal([0, 1], [x, y])

  " index
  call assert_equal(4, range(1, 10)[3])

  " add()
  call assert_equal([0, 1, 2, 3], add(range(3), 3))
  call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
  call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))

  " append()
  new
  call append('.', range(5))
  call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
  bwipe!

  " appendbufline()
  new
  call appendbufline(bufnr(''), '.', range(5))
  call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
  bwipe!

  " call()
  func TwoArgs(a, b)
    return [a:a, a:b]
  endfunc
  call assert_equal([0, 1], call('TwoArgs', range(2)))

  " col()
  new
  call setline(1, ['foo', 'bar'])
  call assert_equal(2, col(range(1, 2)))
  bwipe!

  " complete()
  execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
  " complete_info()
  execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"

  " copy()
  call assert_equal([1, 2, 3], copy(range(1, 3)))

  " count()
  call assert_equal(0, count(range(0), 3))
  call assert_equal(0, count(range(2), 3))
  call assert_equal(1, count(range(5), 3))

  " cursor()
  new
  call setline(1, ['aaa', 'bbb', 'ccc'])
  call cursor(range(1, 2))
  call assert_equal([2, 1], [col('.'), line('.')])
  bwipe!

  " deepcopy()
  call assert_equal([1, 2, 3], deepcopy(range(1, 3)))

  " empty()
  call assert_true(empty(range(0)))
  call assert_false(empty(range(2)))

  " execute()
  new
  call setline(1, ['aaa', 'bbb', 'ccc'])
  call execute(range(3))
  call assert_equal(2, line('.'))
  bwipe!

  " extend()
  call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
  call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
  call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))

  " filter()
  call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
  call assert_equal([1, 5, 7, 11, 13], filter(filter(range(15), 'v:val % 2'), 'v:val % 3'))

  " funcref()
  call assert_equal([0, 1], funcref('TwoArgs', range(2))())

  " function()
  call assert_equal([0, 1], function('TwoArgs', range(2))())

  " garbagecollect()
  let thelist = [1, range(2), 3]
  let otherlist = range(3)
  call test_garbagecollect_now()

  " get()
  call assert_equal(4, get(range(1, 10), 3))
  call assert_equal(-1, get(range(1, 10), 42, -1))

  " index()
  call assert_equal(1, index(range(1, 5), 2))
  call assert_fails("echo index([1, 2], 1, [])", 'E745:')

  " insert()
  call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
  call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
  call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
  call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
  call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
  call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))

  " join()
  call assert_equal('0 1 2 3 4', join(range(5)))

  " json_encode()
  call assert_equal('[0,1,2,3]', json_encode(range(4)))

  " len()
  call assert_equal(0, len(range(0)))
  call assert_equal(2, len(range(2)))
  call assert_equal(5, len(range(0, 12, 3)))
  call assert_equal(4, len(range(3, 0, -1)))

  " list2str()
  call assert_equal('ABC', list2str(range(65, 67)))
  call assert_fails('let s = list2str(5)', 'E1211:')

  " lock()
  let thelist = range(5)
  lockvar thelist

  " map()
  call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
  call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3'))
  call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2'))
  call assert_equal([2, 4, 8], filter(map(range(5), 'v:val * 2'), 'v:val % 3'))

  " match()
  call assert_equal(3, match(range(5), 3))

  " matchaddpos()
  highlight MyGreenGroup ctermbg=green guibg=green
  call matchaddpos('MyGreenGroup', range(line('.'), line('.')))

  " matchend()
  call assert_equal(4, matchend(range(5), '4'))
  call assert_equal(3, matchend(range(1, 5), '4'))
  call assert_equal(-1, matchend(range(1, 5), '42'))

  " matchstrpos()
  call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
  call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
  call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))

  " max() reverse()
  call assert_equal(0, max(range(0)))
  call assert_equal(0, max(range(10, 9)))
  call assert_equal(9, max(range(10)))
  call assert_equal(18, max(range(0, 20, 3)))
  call assert_equal(20, max(range(20, 0, -3)))
  call assert_equal(99999, max(range(100000)))
  call assert_equal(99999, max(range(99999, 0, -1)))
  call assert_equal(99999, max(reverse(range(100000))))
  call assert_equal(99999, max(reverse(range(99999, 0, -1))))

  " min() reverse()
  call assert_equal(0, min(range(0)))
  call assert_equal(0, min(range(10, 9)))
  call assert_equal(5, min(range(5, 10)))
  call assert_equal(5, min(range(5, 10, 3)))
  call assert_equal(2, min(range(20, 0, -3)))
  call assert_equal(0, min(range(100000)))
  call assert_equal(0, min(range(99999, 0, -1)))
  call assert_equal(0, min(reverse(range(100000))))
  call assert_equal(0, min(reverse(range(99999, 0, -1))))

  " remove()
  call assert_equal(1, remove(range(1, 10), 0))
  call assert_equal(2, remove(range(1, 10), 1))
  call assert_equal(9, remove(range(1, 10), 8))
  call assert_equal(10, remove(range(1, 10), 9))
  call assert_equal(10, remove(range(1, 10), -1))
  call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))

  " repeat()
  call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
  call assert_equal([0, 1, 2], repeat(range(3), 1))
  call assert_equal([], repeat(range(3), 0))
  call assert_equal([], repeat(range(5, 4), 2))
  call assert_equal([], repeat(range(5, 4), 0))

  " reverse()
  call assert_equal([2, 1, 0], reverse(range(3)))
  call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
  call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
  call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
  call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
  call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
  call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
  call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
  call assert_equal([], reverse(range(0)))

  " TODO: setpos()
  " new
  " call setline(1, repeat([''], bufnr('')))
  " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
  " call setpos('x', range(bufnr(''), bufnr('') + 3))
  " bwipe!

  " setreg()
  call setreg('a', range(3))
  call assert_equal("0\n1\n2\n", getreg('a'))

  " settagstack()
  call settagstack(1, #{items : range(4)})

  " sign_define()
  call assert_fails("call sign_define(range(5))", "E715:")
  call assert_fails("call sign_placelist(range(5))", "E715:")

  " sign_undefine()
  call assert_fails("call sign_undefine(range(5))", "E908:")

  " sign_unplacelist()
  call assert_fails("call sign_unplacelist(range(5))", "E715:")

  " sort()
  call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))

  " string()
  call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))

  " taglist() with 'tagfunc'
  func TagFunc(pattern, flags, info)
    return range(10)
  endfunc
  set tagfunc=TagFunc
  call assert_fails("call taglist('asdf')", 'E987:')
  set tagfunc=

  " term_start()
  if has('terminal') && has('termguicolors')
    call assert_fails('call term_start(range(3, 4))', 'E474:')
    let g:terminal_ansi_colors = range(16)
    if has('win32')
      let cmd = "cmd /c dir"
    else
      let cmd = "ls"
    endif
    call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"'
        \ .. ', ansi_colors: range(16)})', 'E475:')
    unlet g:terminal_ansi_colors
  endif

  " type()
  call assert_equal(v:t_list, type(range(5)))

  " uniq()
  call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))

  " errors
  call assert_fails('let x=range(2, 8, 0)', 'E726:')
  call assert_fails('let x=range(3, 1)', 'E727:')
  call assert_fails('let x=range(1, 3, -2)', 'E727:')
  call assert_fails('let x=range([])', 'E745:')
  call assert_fails('let x=range(1, [])', 'E745:')
  call assert_fails('let x=range(1, 4, [])', 'E745:')
endfunc

func Test_garbagecollect_now_fails()
  let v:testing = 0
  call assert_fails('call test_garbagecollect_now()', 'E1142:')
  let v:testing = 1
endfunc

func Test_echoraw()
  CheckScreendump

  " Normally used for escape codes, but let's test with a CR.
  let lines =<< trim END
    call echoraw("hello\<CR>x")
  END
  call writefile(lines, 'XTest_echoraw')
  let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
  call VerifyScreenDump(buf, 'Test_functions_echoraw', {})

  " clean up
  call StopVimInTerminal(buf)
  call delete('XTest_echoraw')
endfunc

" Test for echo highlighting
func Test_echohl()
  echohl Search
  echo 'Vim'
  call assert_equal('Vim', Screenline(&lines))
  " TODO: How to check the highlight group used by echohl?
  " ScreenAttrs() returns all zeros.
  echohl None
endfunc

" Test for the eval() function
func Test_eval()
  call assert_fails("call eval('5 a')", 'E488:')
endfunc

" Test for the keytrans() function
func Test_keytrans()
  call assert_equal('<Space>', keytrans(' '))
  call assert_equal('<lt>', keytrans('<'))
  call assert_equal('<lt>Tab>', keytrans('<Tab>'))
  call assert_equal('<Tab>', keytrans("\<Tab>"))
  call assert_equal('<C-V>', keytrans("\<C-V>"))
  call assert_equal('<BS>', keytrans("\<BS>"))
  call assert_equal('<Home>', keytrans("\<Home>"))
  call assert_equal('<C-Home>', keytrans("\<C-Home>"))
  call assert_equal('<M-Home>', keytrans("\<M-Home>"))
  call assert_equal('<C-Space>', keytrans("\<C-Space>"))
  call assert_equal('<M-Space>', keytrans("\<*M-Space>"))
  call assert_equal('<M-x>', "\<*M-x>"->keytrans())
  call assert_equal('<C-I>', "\<*C-I>"->keytrans())
  call assert_equal('<S-3>', "\<*S-3>"->keytrans())
  call assert_equal('π', 'π'->keytrans())
  call assert_equal('<M-π>', "\<M-π>"->keytrans())
  call assert_equal('ě', 'ě'->keytrans())
  call assert_equal('<M-ě>', "\<M-ě>"->keytrans())
  call assert_equal('', ''->keytrans())
  call assert_equal('', test_null_string()->keytrans())
  call assert_fails('call keytrans(1)', 'E1174:')
  call assert_fails('call keytrans()', 'E119:')
endfunc

" Test for the nr2char() function
func Test_nr2char()
  set encoding=latin1
  call assert_equal('@', nr2char(64))
  set encoding=utf8
  call assert_equal('a', nr2char(97, 1))
  call assert_equal('a', nr2char(97, 0))

  call assert_equal("\x80\xfc\b" .. nr2char(0x100000), eval('"\<M-' .. nr2char(0x100000) .. '>"'))
  call assert_equal("\x80\xfc\b" .. nr2char(0x40000000), eval('"\<M-' .. nr2char(0x40000000) .. '>"'))
endfunc

" Test for screenattr(), screenchar() and screenchars() functions
func Test_screen_functions()
  call assert_equal(-1, screenattr(-1, -1))
  call assert_equal(-1, screenchar(-1, -1))
  call assert_equal([], screenchars(-1, -1))
endfunc

" Test for getcurpos() and setpos()
func Test_getcurpos_setpos()
  new
  call setline(1, ['012345678', '012345678'])
  normal gg6l
  let sp = getcurpos()
  normal 0
  call setpos('.', sp)
  normal jyl
  call assert_equal('6', @")
  call assert_equal(-1, setpos('.', test_null_list()))
  call assert_equal(-1, setpos('.', {}))

  let winid = win_getid()
  normal G$
  let pos = getcurpos()
  wincmd w
  call assert_equal(pos, getcurpos(winid))

  wincmd w
  close!

  call assert_equal(getcurpos(), getcurpos(0))
  call assert_equal([0, 0, 0, 0, 0], getcurpos(-1))
  call assert_equal([0, 0, 0, 0, 0], getcurpos(1999))
endfunc

func Test_getmousepos()
  enew!
  call setline(1, "\t\t\t1234")
  call test_setmouse(1, 1)
  call assert_equal(#{
        \ screenrow: 1,
        \ screencol: 1,
        \ winid: win_getid(),
        \ winrow: 1,
        \ wincol: 1,
        \ line: 1,
        \ column: 1,
        \ }, getmousepos())
  call test_setmouse(1, 25)
  call assert_equal(#{
        \ screenrow: 1,
        \ screencol: 25,
        \ winid: win_getid(),
        \ winrow: 1,
        \ wincol: 25,
        \ line: 1,
        \ column: 4,
        \ }, getmousepos())
  call test_setmouse(1, 50)
  call assert_equal(#{
        \ screenrow: 1,
        \ screencol: 50,
        \ winid: win_getid(),
        \ winrow: 1,
        \ wincol: 50,
        \ line: 1,
        \ column: 8,
        \ }, getmousepos())

  " If the mouse is positioned past the last buffer line, "line" and "column"
  " should act like it's positioned on the last buffer line.
  call test_setmouse(2, 25)
  call assert_equal(#{
        \ screenrow: 2,
        \ screencol: 25,
        \ winid: win_getid(),
        \ winrow: 2,
        \ wincol: 25,
        \ line: 1,
        \ column: 4,
        \ }, getmousepos())
  call test_setmouse(2, 50)
  call assert_equal(#{
        \ screenrow: 2,
        \ screencol: 50,
        \ winid: win_getid(),
        \ winrow: 2,
        \ wincol: 50,
        \ line: 1,
        \ column: 8,
        \ }, getmousepos())
  bwipe!
endfunc

func Test_getmouseshape()
  CheckFeature mouseshape

  call assert_equal('arrow', getmouseshape())
endfunc

" Test for glob()
func Test_glob()
  call assert_equal('', glob(test_null_string()))
  call assert_equal('', globpath(test_null_string(), test_null_string()))
  call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:')

  call writefile([], 'Xglob1')
  call writefile([], 'XGLOB2')
  set wildignorecase
  " Sort output of glob() otherwise we end up with different
  " ordering depending on whether file system is case-sensitive.
  call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1)))
  " wildignorecase shall be applied even when the pattern contains no wildcards.
  call assert_equal('XGLOB2', glob('xglob2'))
  set wildignorecase&

  call delete('Xglob1')
  call delete('XGLOB2')

  call assert_fails("call glob('*', 0, {})", 'E728:')
endfunc

" Test for browse()
func Test_browse()
  CheckFeature browse
  call assert_fails('call browse([], "open", "x", "a.c")', 'E745:')
endfunc

" Test for browsedir()
func Test_browsedir()
  CheckFeature browse
  call assert_fails('call browsedir("open", [])', 'E730:')
endfunc

func HasDefault(msg = 'msg')
  return a:msg
endfunc

func Test_default_arg_value()
  call assert_equal('msg', HasDefault())
endfunc

" Test for gettext()
func Test_gettext()
  call assert_fails('call gettext(1)', 'E1174:')
endfunc

func Test_builtin_check()
  call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:')
  call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
  call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:')
  call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:')
  let lines =<< trim END
    vim9script
    var trim = (x) => " " .. x
  END
  call v9.CheckScriptFailure(lines, 'E704:')

  call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:')
  let g:bar = 123
  call extend(g:, #{bar: { -> "foo" }}, "keep")
  call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
  unlet g:bar

  call assert_fails('call extend(l:, #{foo: { -> "foo" }})', 'E704:')
  let bar = 123
  call extend(l:, #{bar: { -> "foo" }}, "keep")
  call assert_fails('call extend(l:, #{bar: { -> "foo" }}, "force")', 'E704:')
  unlet bar

  call assert_fails('call extend(g:, #{foo: function("extend")})', 'E704:')
  let g:bar = 123
  call extend(g:, #{bar: function("extend")}, "keep")
  call assert_fails('call extend(g:, #{bar: function("extend")}, "force")', 'E704:')
  unlet g:bar

  call assert_fails('call extend(l:, #{foo: function("extend")})', 'E704:')
  let bar = 123
  call extend(l:, #{bar: function("extend")}, "keep")
  call assert_fails('call extend(l:, #{bar: function("extend")}, "force")', 'E704:')
  unlet bar
endfunc

func Test_funcref_to_string()
  let Fn = funcref('g:Test_funcref_to_string')
  call assert_equal("function('g:Test_funcref_to_string')", string(Fn))
endfunc

" Test for isabsolutepath()
func Test_isabsolutepath()
  call assert_false(isabsolutepath(''))
  call assert_false(isabsolutepath('.'))
  call assert_false(isabsolutepath('../Foo'))
  call assert_false(isabsolutepath('Foo/'))
  if has('win32')
    call assert_true(isabsolutepath('A:\'))
    call assert_true(isabsolutepath('A:\Foo'))
    call assert_true(isabsolutepath('A:/Foo'))
    call assert_false(isabsolutepath('A:Foo'))
    call assert_false(isabsolutepath('\Windows'))
    call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt'))
  else
    call assert_true(isabsolutepath('/'))
    call assert_true(isabsolutepath('/usr/share/'))
  endif
endfunc

" Test for exepath()
func Test_exepath()
  if has('win32')
    call assert_notequal(exepath('cmd'), '')

    let oldNoDefaultCurrentDirectoryInExePath = $NoDefaultCurrentDirectoryInExePath
    call writefile(['@echo off', 'echo Evil'], 'vim-test-evil.bat')
    let $NoDefaultCurrentDirectoryInExePath = ''
    call assert_notequal(exepath("vim-test-evil.bat"), '')
    let $NoDefaultCurrentDirectoryInExePath = '1'
    call assert_equal(exepath("vim-test-evil.bat"), '')
    let $NoDefaultCurrentDirectoryInExePath = oldNoDefaultCurrentDirectoryInExePath
    call delete('vim-test-evil.bat')
  else
    call assert_notequal(exepath('sh'), '')
  endif
endfunc

" Test for virtcol()
func Test_virtcol()
  enew!
  call setline(1, "the\tquick\tbrown\tfox")
  norm! 4|
  call assert_equal(8, virtcol('.'))
  call assert_equal(8, virtcol('.', v:false))
  call assert_equal([4, 8], virtcol('.', v:true))
  bwipe!
endfunc

func Test_delfunc_while_listing()
  CheckRunVimInTerminal

  let lines =<< trim END
      set nocompatible
      for i in range(1, 999)
        exe 'func ' .. 'MyFunc' .. i .. '()'
        endfunc
      endfor
      au CmdlineLeave : call timer_start(0, {-> execute('delfunc MyFunc622')})
  END
  call writefile(lines, 'Xfunctionclear', 'D')
  let buf = RunVimInTerminal('-S Xfunctionclear', {'rows': 12})

  " This was using freed memory.  The height of the terminal must be so that
  " the next function to be listed with "j" is the one that is deleted in the
  " timer callback, tricky!
  call term_sendkeys(buf, ":func /MyFunc\<CR>")
  call TermWait(buf, 50)
  call term_sendkeys(buf, "j")
  call TermWait(buf, 50)
  call term_sendkeys(buf, "\<CR>")

  call StopVimInTerminal(buf)
endfunc


" vim: shiftwidth=2 sts=2 expandtab