hexicon-vm 0.2.0

The Hexicon virtual machine: runs the games Hexicon builds
Documentation
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
//! Unit tests. The programs here are assembled by hand into small images,
//! so the tests need nothing outside this repository.

use hexicon_bytecode::Opcode as Op;
use hexicon_host::BufferHost;
use hexicon_image::Image;

use crate::console::{Formatter, Newline};
use crate::error::{ErrParam, VmError, code, format_message};
use crate::object::{Object, ObjectTable};
use crate::program_object::{self, ProgramObject};
use crate::value::{ObjId, Value, type_code};
use crate::vm::Vm;

// ---- building images ----

/// Index of each built-in class in the test images' dependency list.
const META_PROGRAM_OBJECT: u8 = 0;

/// Index of each function set in the test images' dependency list.
const SET_GEN: u8 = 1;
const SET_IO: u8 = 2;

/// Property IDs the test images use.
const PROP_CONSTRUCT: u16 = 1;
const PROP_MESSAGE: u16 = 2;
const PROP_NAME: u16 = 10;
const PROP_GREET: u16 = 11;
const PROP_LENGTH: u16 = 20;

/// Assembles a code pool. Functions are appended one after another.
#[derive(Default)]
struct Code {
    bytes: Vec<u8>,
}

impl Code {
    /// Begin a function and return its address.
    fn function(&mut self, params: u8, locals: u16) -> u32 {
        let address = self.bytes.len() as u32;
        self.bytes.push(params);
        self.bytes.push(0);
        self.bytes.extend_from_slice(&locals.to_le_bytes());
        // Stack depth, exception table offset, debug records offset.
        self.bytes.extend_from_slice(&[16, 0, 0, 0, 0, 0]);
        address
    }

    fn op(&mut self, opcode: Op) -> &mut Self {
        self.bytes.push(opcode as u8);
        self
    }

    fn u8(&mut self, value: u8) -> &mut Self {
        self.bytes.push(value);
        self
    }

    fn u16(&mut self, value: u16) -> &mut Self {
        self.bytes.extend_from_slice(&value.to_le_bytes());
        self
    }

    fn u32(&mut self, value: u32) -> &mut Self {
        self.bytes.extend_from_slice(&value.to_le_bytes());
        self
    }

    /// A branch operand to an absolute address.
    fn branch_to(&mut self, target: usize) -> &mut Self {
        let offset = target as i64 - self.bytes.len() as i64;
        self.u16(offset as i16 as u16)
    }

    /// A branch operand to be filled in later. Returns its position.
    fn branch_later(&mut self) -> usize {
        let at = self.bytes.len();
        self.u16(0);
        at
    }

    /// Point an earlier branch operand at the current position.
    fn land(&mut self, operand: usize) {
        let offset = (self.bytes.len() as i64 - operand as i64) as i16;
        self.bytes[operand..operand + 2].copy_from_slice(&offset.to_le_bytes());
    }

    fn here(&self) -> usize {
        self.bytes.len()
    }

    /// Display the value on top of the stack with `print`.
    fn say(&mut self) -> &mut Self {
        self.op(Op::Builtin1).u8(1).u8(0).u8(SET_IO)
    }

    /// Push a string constant and display it.
    fn say_str(&mut self, offset: u32) -> &mut Self {
        self.op(Op::PushStr).u32(offset).say()
    }
}

/// Assembles a constant pool.
#[derive(Default)]
struct Consts {
    bytes: Vec<u8>,
}

impl Consts {
    fn string(&mut self, text: &str) -> u32 {
        let offset = self.bytes.len() as u32;
        self.bytes
            .extend_from_slice(&(text.len() as u16).to_le_bytes());
        self.bytes.extend_from_slice(text.as_bytes());
        offset
    }

    fn list(&mut self, items: &[[u8; 5]]) -> u32 {
        let offset = self.bytes.len() as u32;
        self.bytes
            .extend_from_slice(&(items.len() as u16).to_le_bytes());
        for item in items {
            self.bytes.extend_from_slice(item);
        }
        offset
    }
}

/// A value in the five-byte form images use.
fn holder(type_code: i32, value: u32) -> [u8; 5] {
    let v = value.to_le_bytes();
    [type_code as u8, v[0], v[1], v[2], v[3]]
}

/// The image data of a program-defined object.
fn program_object_data(superclasses: &[u32], is_class: bool, props: &[(u16, [u8; 5])]) -> Vec<u8> {
    let mut data = Vec::new();
    data.extend_from_slice(&(superclasses.len() as u16).to_le_bytes());
    data.extend_from_slice(&(props.len() as u16).to_le_bytes());
    data.extend_from_slice(&u16::from(is_class).to_le_bytes());
    for superclass in superclasses {
        data.extend_from_slice(&superclass.to_le_bytes());
    }
    for (prop, value) in props {
        data.extend_from_slice(&prop.to_le_bytes());
        data.extend_from_slice(value);
    }
    data
}

/// Assembles an image file.
#[derive(Default)]
struct ImageBuilder {
    objects: Vec<(u32, Vec<u8>)>,
    symbols: Vec<(String, [u8; 5])>,
}

impl ImageBuilder {
    fn object(mut self, id: u32, data: Vec<u8>) -> Self {
        self.objects.push((id, data));
        self
    }

    fn build(self, entry: u32, code: &Code, consts: &Consts) -> Image {
        Image::parse(&self.build_bytes(entry, code, consts)).expect("the test image is well formed")
    }

    /// The image file's bytes.
    fn build_bytes(self, entry: u32, code: &Code, consts: &Consts) -> Vec<u8> {
        fn block(data: &mut Vec<u8>, kind: &[u8; 4], body: &[u8]) {
            data.extend_from_slice(kind);
            data.extend_from_slice(&(body.len() as u32).to_le_bytes());
            data.extend_from_slice(&1u16.to_le_bytes());
            data.extend_from_slice(body);
        }
        fn pool(data: &mut Vec<u8>, id: u16, contents: &[u8]) {
            let mut body = id.to_le_bytes().to_vec();
            body.extend_from_slice(&1u32.to_le_bytes());
            body.extend_from_slice(&4096u32.to_le_bytes());
            block(data, b"CPDF", &body);
            let mut body = id.to_le_bytes().to_vec();
            body.extend_from_slice(&0u32.to_le_bytes());
            body.push(0);
            body.extend_from_slice(contents);
            block(data, b"CPPG", &body);
        }

        let mut data = hexicon_image::SIGNATURE.to_vec();
        data.extend_from_slice(&2u16.to_le_bytes());
        data.extend_from_slice(&[0; 32]);
        data.extend_from_slice(b"Sun Aug 01 17:05:20 1999");

        let mut body = entry.to_le_bytes().to_vec();
        for size in [10u16, 10, 10, 6, 6, 2, 8] {
            body.extend_from_slice(&size.to_le_bytes());
        }
        block(&mut data, b"ENTP", &body);

        // The string class has one method, `length`.
        let metaclasses: [(&str, &[u16]); 4] = [
            ("hexicon-object/030005", &[]),
            ("string/030008", &[PROP_LENGTH]),
            ("list/030008", &[]),
            ("intrinsic-class/030001", &[]),
        ];
        let mut body = (metaclasses.len() as u16).to_le_bytes().to_vec();
        for (name, props) in metaclasses {
            let size = 2 + 1 + name.len() + 2 + 2 + props.len() * 2;
            body.extend_from_slice(&(size as u16).to_le_bytes());
            body.push(name.len() as u8);
            body.extend_from_slice(name.as_bytes());
            body.extend_from_slice(&(props.len() as u16).to_le_bytes());
            body.extend_from_slice(&2u16.to_le_bytes());
            for prop in props {
                body.extend_from_slice(&prop.to_le_bytes());
            }
        }
        block(&mut data, b"MCLD", &body);

        let sets = [
            "hexicon-vm/010006",
            "hexicon-gen/030008",
            "hexicon-io/030007",
        ];
        let mut body = (sets.len() as u16).to_le_bytes().to_vec();
        for name in sets {
            body.push(name.len() as u8);
            body.extend_from_slice(name.as_bytes());
        }
        block(&mut data, b"FNSD", &body);

        pool(&mut data, hexicon_image::POOL_CODE, &code.bytes);
        pool(&mut data, hexicon_image::POOL_CONSTANT, &consts.bytes);

        if !self.objects.is_empty() {
            let mut body = (self.objects.len() as u16).to_le_bytes().to_vec();
            body.extend_from_slice(&u16::from(META_PROGRAM_OBJECT).to_le_bytes());
            body.extend_from_slice(&0u16.to_le_bytes());
            for (id, object) in &self.objects {
                body.extend_from_slice(&id.to_le_bytes());
                body.extend_from_slice(&(object.len() as u16).to_le_bytes());
                body.extend_from_slice(object);
            }
            block(&mut data, b"OBJS", &body);
        }

        let mut symbols = self.symbols;
        symbols.push((
            "Constructor".to_owned(),
            holder(type_code::PROP, PROP_CONSTRUCT.into()),
        ));
        symbols.push((
            "exceptionMessage".to_owned(),
            holder(type_code::PROP, PROP_MESSAGE.into()),
        ));
        symbols.push(("LastProp".to_owned(), holder(type_code::PROP, 100)));
        let mut body = (symbols.len() as u16).to_le_bytes().to_vec();
        for (name, value) in &symbols {
            body.extend_from_slice(value);
            body.push(name.len() as u8);
            body.extend_from_slice(name.as_bytes());
        }
        block(&mut data, b"SYMD", &body);

        block(&mut data, b"EOF ", &[]);
        data
    }
}

/// Run an image and return what it displayed and how it ended.
fn run(image: &Image) -> (String, Result<(), String>) {
    let host = BufferHost::new();
    let mut vm = Vm::new(image, Box::new(host.clone())).expect("the image loads");
    let result = vm.run(&[]);
    let result = result.map_err(|error| vm.error_message(&error, true));
    vm.flush_output();
    (host.output(), result)
}

// ---- programs ----

#[test]
fn displays_a_string() {
    let mut consts = Consts::default();
    let hello = consts.string("Hello, world!\n");
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.say_str(hello).op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(run(&image), ("Hello, world!\n".to_owned(), Ok(())));
}

#[test]
fn loops_and_adds() {
    // local sum = 0; for (local i = 1 ; i <= 10 ; ++i) sum += i;
    let consts = Consts::default();
    let mut code = Code::default();
    let main = code.function(1, 2);
    code.op(Op::ZeroLcl1).u8(0).op(Op::OneLcl1).u8(1);
    let top = code.here();
    code.op(Op::GetLcl1)
        .u8(1)
        .op(Op::PushInt8)
        .u8(10)
        .op(Op::Jgt);
    let done = code.branch_later();
    code.op(Op::GetLcl1).u8(1).op(Op::AddToLcl).u16(0);
    code.op(Op::IncLcl).u16(1).op(Op::Jmp).branch_to(top);
    code.land(done);
    code.op(Op::GetLcl1).u8(0).say().op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(run(&image).0, "55");
}

#[test]
fn calls_a_recursive_function() {
    let consts = Consts::default();
    let mut code = Code::default();
    // factorial(n) { return n > 1 ? n * factorial(n - 1) : 1; }
    let factorial = code.function(1, 0);
    code.op(Op::GetArgN0).op(Op::Push1).op(Op::Jle);
    let base_case = code.branch_later();
    code.op(Op::GetArgN0).op(Op::GetArgN0).op(Op::Dec);
    code.op(Op::Call).u8(1).u32(factorial);
    code.op(Op::GetR0).op(Op::Mul).op(Op::RetVal);
    code.land(base_case);
    code.op(Op::Push1).op(Op::RetVal);

    let main = code.function(1, 0);
    code.op(Op::PushInt8).u8(7);
    code.op(Op::Call).u8(1).u32(factorial);
    code.op(Op::GetR0).say().op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(run(&image).0, "5040");
}

#[test]
fn concatenates_strings_and_numbers() {
    let mut consts = Consts::default();
    let text = consts.string("n = ");
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::PushStr).u32(text);
    code.op(Op::PushInt8).u8(42).op(Op::Add);
    code.op(Op::PushNil).op(Op::Add);
    code.say().op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(run(&image).0, "n = 42");
}

#[test]
fn indexes_and_compares_lists() {
    let mut consts = Consts::default();
    let list = consts.list(&[
        holder(type_code::INT, 10),
        holder(type_code::INT, 20),
        holder(type_code::INT, 30),
    ]);
    let same = consts.list(&[
        holder(type_code::INT, 10),
        holder(type_code::INT, 20),
        holder(type_code::INT, 30),
    ]);
    let yes = consts.string(" equal");
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::PushLst).u32(list).op(Op::IdxInt8).u8(2).say();
    code.op(Op::PushLst).u32(list).op(Op::PushLst).u32(same);
    code.op(Op::Jne);
    let differ = code.branch_later();
    code.say_str(yes);
    code.land(differ);
    code.op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(run(&image).0, "20 equal");
}

#[test]
fn calls_methods_and_inherits() {
    let mut consts = Consts::default();
    let name_base = consts.string("base");
    let name_derived = consts.string("derived");
    let hello = consts.string("hello from ");
    let again = consts.string(" again");
    let mut code = Code::default();

    // Base.greet() { print('hello from ' + name); }
    let base_greet = code.function(0, 0);
    code.op(Op::PushStr).u32(hello);
    code.op(Op::GetPropSelf)
        .u16(PROP_NAME)
        .op(Op::GetR0)
        .op(Op::Add);
    code.say().op(Op::RetNil);

    // Derived.greet() { inherited(); print(' again'); }
    let derived_greet = code.function(0, 0);
    code.op(Op::Inherit).u8(0).u16(PROP_GREET);
    code.say_str(again).op(Op::RetNil);

    let main = code.function(1, 0);
    code.op(Op::ObjCallProp).u8(0).u32(2).u16(PROP_GREET);
    code.op(Op::RetNil);

    let image = ImageBuilder::default()
        .object(
            1,
            program_object_data(
                &[],
                true,
                &[
                    (PROP_NAME, holder(type_code::SSTRING, name_base)),
                    (PROP_GREET, holder(type_code::CODEOFS, base_greet)),
                ],
            ),
        )
        .object(
            2,
            program_object_data(
                &[1],
                false,
                &[
                    (PROP_NAME, holder(type_code::SSTRING, name_derived)),
                    (PROP_GREET, holder(type_code::CODEOFS, derived_greet)),
                ],
            ),
        )
        .build(main, &code, &consts);
    assert_eq!(run(&image).0, "hello from derived again");
}

#[test]
fn creates_objects_and_runs_constructors() {
    let mut consts = Consts::default();
    let made = consts.string("made ");
    let mut code = Code::default();

    // Thing.construct(n) { name = n; }  Constructors return self.
    let construct = code.function(1, 0);
    code.op(Op::GetArgN0).op(Op::SetPropSelf).u16(PROP_NAME);
    code.op(Op::PushSelf).op(Op::RetVal);

    // local t = new Thing(7); print('made ' + t.name);
    let main = code.function(1, 1);
    code.op(Op::PushInt8).u8(7).op(Op::PushObj).u32(1);
    code.op(Op::New1).u8(2).u8(META_PROGRAM_OBJECT);
    code.op(Op::SetLcl1R0).u8(0);
    code.op(Op::PushStr).u32(made);
    code.op(Op::GetPropLcl1)
        .u8(0)
        .u16(PROP_NAME)
        .op(Op::GetR0)
        .op(Op::Add);
    code.say().op(Op::RetNil);

    let image = ImageBuilder::default()
        .object(
            1,
            program_object_data(
                &[],
                true,
                &[(PROP_CONSTRUCT, holder(type_code::CODEOFS, construct))],
            ),
        )
        .build(main, &code, &consts);
    assert_eq!(run(&image).0, "made 7");
}

#[test]
fn undefined_property_is_nil() {
    let mut consts = Consts::default();
    let nil = consts.string("nil");
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::ObjGetProp).u32(1).u16(PROP_GREET);
    code.op(Op::GetR0).op(Op::JNotNil);
    let not_nil = code.branch_later();
    code.say_str(nil);
    code.land(not_nil);
    code.op(Op::RetNil);

    let image = ImageBuilder::default()
        .object(1, program_object_data(&[], false, &[]))
        .build(main, &code, &consts);
    assert_eq!(run(&image).0, "nil");
}

#[test]
fn calls_string_methods_on_constants() {
    let mut consts = Consts::default();
    let text = consts.string("h\u{e9}llo");
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::PushStr).u32(text);
    code.op(Op::GetProp).u16(PROP_LENGTH);
    code.op(Op::GetR0).say().op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    // Length is in characters, not bytes.
    assert_eq!(run(&image).0, "5");
}

#[test]
fn catches_a_thrown_exception() {
    let mut consts = Consts::default();
    let caught = consts.string("caught");
    let mut code = Code::default();

    // thrower() { throw object 1; }
    let thrower = code.function(0, 0);
    code.op(Op::PushObj).u32(1).op(Op::Throw);

    // main() { try { thrower(); } catch (object 1 e) { print('caught'); } }
    let main = code.function(1, 1);
    // The protected range has to include the instruction after the call,
    // because that is where the function is when the exception arrives.
    let try_start = code.here();
    code.op(Op::Call).u8(0).u32(thrower);
    code.op(Op::RetNil);
    let try_end = code.here() - 1;
    let handler = code.here();
    code.op(Op::SetLcl1).u8(0);
    code.say_str(caught).op(Op::RetNil);
    // The exception table: one handler.
    let table = code.here();
    code.u16(1);
    code.u16((try_start - main as usize) as u16);
    code.u16((try_end - main as usize) as u16);
    code.u32(1);
    code.u16((handler - main as usize) as u16);
    let table_offset = (table - main as usize) as u16;
    code.bytes[main as usize + 6..main as usize + 8].copy_from_slice(&table_offset.to_le_bytes());

    let image = ImageBuilder::default()
        .object(1, program_object_data(&[], false, &[]))
        .build(main, &code, &consts);
    assert_eq!(run(&image), ("caught".to_owned(), Ok(())));
}

#[test]
fn reports_an_unhandled_vm_error() {
    let consts = Consts::default();
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::Push1).op(Op::Push0).op(Op::Div).op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    let (output, result) = run(&image);
    assert_eq!(output, "");
    assert_eq!(
        result,
        Err("Unhandled exception: division by zero".to_owned())
    );
}

#[test]
fn vm_errors_can_be_caught() {
    let mut consts = Consts::default();
    let caught = consts.string("caught: ");
    let mut code = Code::default();
    let main = code.function(1, 1);
    let try_start = code.here();
    code.op(Op::PushNil).op(Op::GetProp).u16(PROP_NAME);
    let try_end = code.here() - 1;
    code.op(Op::RetNil);
    let handler = code.here();
    code.op(Op::SetLcl1).u8(0);
    code.op(Op::PushStr).u32(caught);
    code.op(Op::GetPropLcl1)
        .u8(0)
        .u16(PROP_MESSAGE)
        .op(Op::GetR0)
        .op(Op::Add);
    code.say().op(Op::RetNil);
    let table = code.here();
    code.u16(1);
    code.u16((try_start - main as usize) as u16);
    code.u16((try_end - main as usize) as u16);
    // A handler with no class catches everything.
    code.u32(0);
    code.u16((handler - main as usize) as u16);
    let table_offset = (table - main as usize) as u16;
    code.bytes[main as usize + 6..main as usize + 8].copy_from_slice(&table_offset.to_le_bytes());

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(run(&image).0, "caught: nil object reference");
}

#[test]
fn wrong_argument_count_is_an_error() {
    let consts = Consts::default();
    let mut code = Code::default();
    let callee = code.function(2, 0);
    code.op(Op::RetNil);
    let main = code.function(1, 0);
    code.op(Op::Push1)
        .op(Op::Call)
        .u8(1)
        .u32(callee)
        .op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    // Which function, and what it takes; this image has no line records
    // to say where it is.
    assert_eq!(
        run(&image).1,
        Err(
            "Unhandled exception: wrong number of arguments: the function takes 2, and was given 1"
                .to_owned()
        )
    );
}

#[test]
fn runaway_recursion_overflows_the_stack() {
    let consts = Consts::default();
    let mut code = Code::default();
    let forever = code.function(0, 0);
    code.op(Op::Call).u8(0).u32(forever).op(Op::RetNil);
    let main = code.function(1, 0);
    code.op(Op::Call).u8(0).u32(forever).op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(
        run(&image).1,
        Err("Unhandled exception: stack overflow".to_owned())
    );
}

#[test]
fn builtin_functions_check_their_arguments() {
    let consts = Consts::default();
    let mut code = Code::default();
    let main = code.function(1, 0);
    // dataType() with no arguments.
    code.op(Op::Builtin1).u8(0).u8(0).u8(SET_GEN).op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(
        run(&image).1,
        Err("Unhandled exception: wrong number of arguments".to_owned())
    );
}

#[test]
fn unknown_metaclass_fails_to_load() {
    let mut meta = crate::meta::MetaTable::new();
    assert!(meta.add("hexicon-object/030005", Vec::new()).is_ok());
    let unknown = meta.add("no-such-class/030000", Vec::new());
    assert!(matches!(
        unknown,
        Err(VmError::Runtime(error)) if error.code == code::UNKNOWN_METACLASS
    ));
    let too_new = meta.add("string/999999", Vec::new());
    assert!(matches!(
        too_new,
        Err(VmError::Runtime(error)) if error.code == code::METACLASS_TOO_OLD
    ));
}

#[test]
fn identifies_itself_as_hexicon() {
    let consts = Consts::default();
    let mut code = Code::default();
    let main = code.function(1, 0);
    // print(vmId()); print(vmVersion()); print(vmBanner());
    for function in [3u8, 2, 4] {
        code.op(Op::BuiltinA).u8(0).u8(function).op(Op::GetR0).say();
        code.op(Op::PushInt8).u8(b' ').op(Op::Disc);
    }
    code.op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    let version = crate::identity::version_number();
    assert_eq!(
        run(&image).0,
        format!("hexicon{version}Hexicon VM {}", crate::identity::VERSION)
    );
    // Libraries display the version by taking the number apart.
    let dotted = format!(
        "{}.{}.{}",
        version >> 16,
        (version >> 8) & 0xff,
        version & 0xff
    );
    assert_eq!(dotted, crate::identity::VERSION);
}

// ---- objects ----

#[test]
fn free_ids_are_handed_out_from_the_top_of_a_page() {
    let mut table = ObjectTable::new();
    let string = || Object::String(std::rc::Rc::from(&[][..]));
    assert!(table.alloc_with_id(ObjId(5), string(), false));
    assert!(!table.alloc_with_id(ObjId(5), string(), false));
    assert!(!table.alloc_with_id(ObjId(0), string(), false));
    assert_eq!(table.alloc(string()), ObjId(4095));
    assert_eq!(table.alloc(string()), ObjId(4094));
    // An ID past the first page adds a page, whose IDs go first.
    assert!(table.alloc_with_id(ObjId(5000), string(), false));
    assert_eq!(table.alloc(string()), ObjId(8191));
    assert!(table.is_in_root_set(ObjId(5)));
    assert!(!table.is_in_root_set(ObjId(4095)));
}

#[test]
fn properties_keep_their_order() {
    let mut object = ProgramObject::new(Vec::new());
    for prop in [30u16, 10, 20, 4, 8, 15, 16, 23, 42] {
        object.set(prop, Value::Int(prop.into()));
    }
    object.set(10, Value::Nil);
    let order: Vec<u16> = object.props().iter().map(|entry| entry.prop).collect();
    assert_eq!(order, [30, 10, 20, 4, 8, 15, 16, 23, 42]);
    assert_eq!(object.get(10), Some(Value::Nil));
    assert_eq!(object.get(42), Some(Value::Int(42)));
    assert_eq!(object.get(99), None);
}

/// Build a table of program-defined objects from `(id, superclasses)`.
fn class_table(classes: &[(u32, &[u32])]) -> ObjectTable {
    let mut table = ObjectTable::new();
    for (id, superclasses) in classes {
        let mut object = ProgramObject::new(superclasses.iter().map(|&s| ObjId(s)).collect());
        object.set(1, Value::Int(*id as i32));
        table.alloc_with_id(ObjId(*id), Object::Program(object), false);
    }
    table
}

fn search_order(table: &ObjectTable, start: u32) -> Vec<u32> {
    let mut search = program_object::Search::new(table, ObjId(start));
    let mut order = Vec::new();
    while search.advance() {
        order.push(search.current.0);
    }
    order
}

#[test]
fn single_inheritance_searches_up_the_chain() {
    let table = class_table(&[(1, &[]), (2, &[1]), (3, &[2])]);
    assert_eq!(search_order(&table, 3), [2, 1]);
    assert!(program_object::inherits_from(&table, ObjId(3), ObjId(1)));
    assert!(!program_object::inherits_from(&table, ObjId(1), ObjId(3)));
    assert!(!program_object::inherits_from(&table, ObjId(3), ObjId(3)));
}

#[test]
fn a_shared_base_class_is_searched_last() {
    // D: B, C; B: A; C: A. The base class A comes after both B and C.
    let table = class_table(&[(1, &[]), (2, &[1]), (3, &[1]), (4, &[2, 3])]);
    assert_eq!(search_order(&table, 4), [2, 3, 1]);
    assert_eq!(
        program_object::find_prop(&table, ObjId(4), 1),
        Some((Value::Int(4), ObjId(4)))
    );
    // What B's definition overrides, from D's point of view, is C's.
    assert_eq!(
        program_object::find_inherited_prop(&table, ObjId(4), ObjId(2), 1),
        Some((Value::Int(3), ObjId(3)))
    );
    assert_eq!(
        program_object::find_inherited_prop(&table, ObjId(4), ObjId(1), 1),
        None
    );
}

#[test]
fn inheritance_order_is_breadth_first_by_declaration() {
    // E: C, D; C: A; D: B.
    let table = class_table(&[(1, &[]), (2, &[]), (3, &[1]), (4, &[2]), (5, &[3, 4])]);
    assert_eq!(search_order(&table, 5), [3, 1, 4, 2]);
}

// ---- errors ----

#[test]
fn error_messages_take_parameters() {
    let message = format_message(
        "missing named argument '%s'",
        &[ErrParam::Str("x".to_owned())],
    );
    assert_eq!(message, "missing named argument 'x'");
    let message = format_message("index %d, hex %x", &[ErrParam::Int(10), ErrParam::Int(255)]);
    assert_eq!(message, "index 10, hex ff");
    // With no parameters left, the rest is copied as it stands.
    assert_eq!(format_message("100% of %s", &[]), "100% of %s");
    assert_eq!(code::message(code::NIL_DEREF), Some("nil object reference"));
    assert_eq!(code::message(1), None);
}

// ---- the console ----

fn format(texts: &[&str]) -> String {
    let mut formatter = Formatter::new(80);
    let mut output = String::new();
    for text in texts {
        formatter.format_text(text.as_bytes());
        output.push_str(&formatter.take_output());
    }
    formatter.flush(Newline::None);
    output.push_str(&formatter.take_output());
    output
}

#[test]
fn console_breaks_lines_to_the_width_it_is_given() {
    let text = "one two three four five six seven eight nine ten eleven twelve";
    let mut formatter = Formatter::new(80);
    formatter.set_width(31);
    formatter.format_text(text.as_bytes());
    formatter.flush(Newline::None);
    let output = formatter.take_output();
    assert_eq!(
        output,
        "one two three four five six
seven eight nine ten eleven
twelve"
    );
    assert!(output.lines().all(|line| line.len() <= 30));

    // A width too small to be a real console's is not taken literally.
    formatter.set_width(0);
    formatter.format_text(
        b"
alpha beta gamma delta epsilon",
    );
    formatter.flush(Newline::None);
    assert_eq!(
        formatter.take_output(),
        "
alpha beta gamma
delta epsilon"
    );
}

#[test]
fn console_collapses_spaces_and_newlines() {
    assert_eq!(format(&["a   b\n\n\nc"]), "a b\nc");
    // Spaces at the start and end of a line are dropped.
    assert_eq!(format(&["  indented  \nnext"]), "indented\nnext");
    // Newlines before any text are dropped.
    assert_eq!(format(&["\n\nfirst"]), "first");
}

#[test]
fn console_writes_blank_lines_on_request() {
    assert_eq!(format(&["a\u{b}b"]), "a\n\nb");
    assert_eq!(format(&["a<br>b"]), "a\nb");
    assert_eq!(format(&["a<br><br>b"]), "a\n\nb");
    assert_eq!(format(&["a<p>b"]), "a\n\nb");
    assert_eq!(format(&["a<br height=2>b"]), "a\n\n\nb");
    assert_eq!(format(&["a<br height=0>b"]), "a\nb");
}

#[test]
fn console_keeps_quoted_spaces() {
    assert_eq!(format(&["a\u{15}\u{15}\u{15}b"]), "a   b");
    assert_eq!(format(&["a &nbsp; b"]), "a   b");
    // A tab goes to the next column that is one short of a multiple of 4.
    assert_eq!(format(&["x\ty"]), "x  y");
    assert_eq!(format(&["xyz\ty"]), "xyz    y");
}

#[test]
fn console_changes_case_on_request() {
    assert_eq!(format(&["\u{f}hello \u{e}WORLD"]), "Hello wORLD");
    // The request waits for the next letter.
    assert_eq!(format(&["\u{f}", "\"quoted\""]), "\"Quoted\"");
}

#[test]
fn console_translates_entities() {
    assert_eq!(format(&["&lt;b&gt; &amp; &quot;x&quot;"]), "<b> & \"x\"");
    assert_eq!(format(&["&#65;&#x42;&mdash;"]), "AB\u{2014}");
    // Not an entity: the ampersand is literal.
    assert_eq!(
        format(&["fish & chips &nosuch; &"]),
        "fish & chips &nosuch; &"
    );
}

#[test]
fn console_skips_tags_it_does_not_display() {
    assert_eq!(format(&["<b>bold</b> <i>it</i>"]), "bold it");
    assert_eq!(format(&["<a href=\"x > y\">link</a>"]), "link");
    assert_eq!(format(&["<title>Not shown</title>shown"]), "shown");
    assert_eq!(format(&["<img src='x.png' alt='a picture'>"]), "a picture");
    assert_eq!(
        format(&["He said <q>hi <q>there</q></q>"]),
        "He said \u{201c}hi \u{2018}there\u{2019}\u{201d}"
    );
    // A tag can be split across calls, though not within its name.
    assert_eq!(format(&["a<br ", "height=2>b"]), "a\n\n\nb");
    assert_eq!(format(&["a<b", "r>b"]), "ab");
}

#[test]
fn console_wraps_long_lines() {
    let words = "word ".repeat(20);
    let output = format(&[&words]);
    let lines: Vec<&str> = output.lines().collect();
    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0].len(), 79);
    assert!(lines[0].ends_with("word"));
    assert_eq!(lines[1], "word word word word");

    // A word too long for a line is cut at the width.
    let long = "x".repeat(100);
    let output = format(&[&long]);
    let lines: Vec<&str> = output.lines().collect();
    assert_eq!(lines[0].len(), 79);
    assert_eq!(lines[1].len(), 21);

    // A line can break after a hyphen.
    let hyphenated = format!("{} well-known", "a".repeat(72));
    assert_eq!(
        format(&[&hyphenated]),
        format!("{} well-\nknown", "a".repeat(72))
    );
}

#[test]
fn console_aligns_tabs() {
    assert_eq!(format(&["ab<tab id=t>cd\nx<tab to=t>y"]), "abcd\nx y");
    assert_eq!(format(&["a<tab indent=3>b"]), "a   b");
    let right = format(&["<tab align=right>end\n"]);
    assert_eq!(right, format!("{}end\n", " ".repeat(76)));
}

// ---- regular expressions ----

use crate::map::Map;
use crate::regex::{GROUP_COUNT, Pattern};

/// Match a pattern at the start of text. Returns the matched length.
fn rex_match(pattern: &str, text: &str) -> Option<usize> {
    let mut groups = [None; GROUP_COUNT];
    Pattern::compile(pattern.as_bytes()).match_at(text.as_bytes(), 0, text.len(), true, &mut groups)
}

/// Search text for a pattern. Returns the matched text.
fn rex_search<'a>(pattern: &str, text: &'a str) -> Option<&'a str> {
    let mut groups = [None; GROUP_COUNT];
    Pattern::compile(pattern.as_bytes())
        .search(text.as_bytes(), 0, true, &mut groups)
        .map(|(start, len)| &text[start..start + len])
}

#[test]
fn regex_matches_literals_and_closures() {
    assert_eq!(rex_match("abc", "abcdef"), Some(3));
    assert_eq!(rex_match("abc", "abd"), None);
    assert_eq!(rex_match("a*b", "aaab"), Some(4));
    assert_eq!(rex_match("a*", "bbb"), Some(0));
    assert_eq!(rex_match("a+", "bbb"), None);
    assert_eq!(rex_match("ab?c", "ac"), Some(2));
    assert_eq!(rex_match("x.z", "xyz"), Some(3));
    // The longest alternative wins, not the first.
    assert_eq!(rex_match("a|ab|abc", "abcd"), Some(3));
    // An empty pattern matches nothing, successfully.
    assert_eq!(rex_match("", "abc"), Some(0));
}

#[test]
fn regex_understands_classes_and_ranges() {
    assert_eq!(rex_match("<alpha>+", "hello world"), Some(5));
    assert_eq!(rex_match("%d+", "12345x"), Some(5));
    assert_eq!(rex_match("<alpha|digit>+", "abc123!"), Some(6));
    assert_eq!(rex_match("<^space>+", "two words"), Some(3));
    assert_eq!(rex_match("[a-c]+", "abcabd"), Some(5));
    assert_eq!(rex_match("[^a-c]+", "xyzabc"), Some(3));
    assert_eq!(rex_match("<lparen>x<rparen>", "(x)"), Some(3));
    assert_eq!(rex_match("%w+", "caf\u{e9} au lait"), Some(5));
    assert_eq!(rex_match("%.", "."), Some(1));
    assert_eq!(rex_match("%.", "x"), None);
}

#[test]
fn regex_honors_flags_and_intervals() {
    assert_eq!(rex_match("<nocase>hello", "HeLLo"), Some(5));
    assert_eq!(rex_match("hello", "HeLLo"), None);
    assert_eq!(rex_match("a{2,3}", "aaaa"), Some(3));
    assert_eq!(rex_match("a{2}", "a"), None);
    assert_eq!(rex_match("a{2,}", "aaaaa"), Some(5));
    // A shortest-match closure gives way to what follows it.
    assert_eq!(rex_match("a.*?c", "abcabc"), Some(3));
    assert_eq!(rex_match("a.*c", "abcabc"), Some(6));
    assert_eq!(rex_match("<min>a.*c", "abcabc"), Some(3));
}

#[test]
fn regex_records_groups() {
    let text = "key=value";
    let mut groups = [None; GROUP_COUNT];
    let pattern = Pattern::compile(b"(<alpha>+)=(<alpha>+)");
    assert_eq!(pattern.group_count, 2);
    assert_eq!(
        pattern.match_at(text.as_bytes(), 0, text.len(), true, &mut groups),
        Some(9)
    );
    assert_eq!(groups[0], Some((0, 3)));
    assert_eq!(groups[1], Some((4, 9)));
    // A group can be matched again later in the pattern.
    assert_eq!(rex_match("(a+)-%1", "aa-aa"), Some(5));
    assert_eq!(rex_match("(a+)-%1", "aa-a"), None);
    // A group that is not captured.
    assert_eq!(Pattern::compile(b"(?:ab)+(c)").group_count, 1);
}

#[test]
fn regex_anchors_and_assertions() {
    assert_eq!(rex_search("^abc", "abcabc"), Some("abc"));
    assert_eq!(rex_search("abc$", "abcab"), None);
    assert_eq!(rex_search("%<the%>", "other the end"), Some("the"));
    assert_eq!(rex_search("a(?=b)", "ac ab"), Some("a"));
    assert_eq!(rex_search("a(?!b).", "ab ac"), Some("ac"));
    assert_eq!(rex_search("(?<=x)a", "ya xa"), Some("a"));
    // A dollar sign in the middle is just a dollar sign.
    assert_eq!(rex_search("a$b", "xa$b"), Some("a$b"));
}

#[test]
fn regex_searches_forward_and_backward() {
    assert_eq!(rex_search("%d+", "abc 123 def 4567"), Some("123"));
    assert_eq!(rex_search("z", "abc"), None);
    let text = "abc 123 def 4567 ghi";
    let mut groups = [None; GROUP_COUNT];
    let pattern = Pattern::compile(b"%d+");
    assert_eq!(
        pattern.search_back(text.as_bytes(), text.len(), true, &mut groups),
        Some((12, 4))
    );
    // A backward search cannot use text past its starting point.
    assert_eq!(
        pattern.search_back(text.as_bytes(), 14, true, &mut groups),
        Some((12, 2))
    );
}

#[test]
fn regex_survives_empty_loops() {
    // Patterns that could loop forever without consuming anything.
    assert_eq!(rex_match("(a*)*b", "aab"), Some(3));
    assert_eq!(rex_match("(a*)+", "b"), Some(0));
    assert_eq!(rex_match("(|a)*", "aaa"), Some(3));
}

// ---- lookup tables ----

#[test]
fn new_map_is_empty() {
    let table = Map::new(8, 4, false);
    assert_eq!(table.bucket_count(), 8);
    assert_eq!(table.slot_count(), 4);
    assert!(table.entries().is_empty());
    assert_eq!(table.slot(0), None);
}

// ---- random numbers ----

#[test]
fn random_numbers_repeat_until_seeded() {
    let mut a = crate::rng::Isaac::new();
    let mut b = crate::rng::Isaac::new();
    let first: Vec<u32> = (0..600).map(|_| a.next()).collect();
    let second: Vec<u32> = (0..600).map(|_| b.next()).collect();
    assert_eq!(first, second);
    // The values are not all the same, and a seed changes them.
    assert!(first.iter().any(|&value| value != first[0]));
    let mut seeded = crate::rng::Isaac::seeded(&[1, 2, 3, 4]);
    let third: Vec<u32> = (0..600).map(|_| seeded.next()).collect();
    assert_ne!(first, third);
}

// ---- Decimal ----

use crate::decimal::Big;
use crate::decimal_math::{self, Cache};

fn big(text: &str) -> Big {
    Big::parse(text, Big::precision_from_str(text))
}

#[test]
fn decimal_arithmetic_keeps_the_larger_precision() {
    assert_eq!(Big::add(&big("1.5"), &big("2.25")).to_text(), "3.75");
    assert_eq!(Big::sub(&big("1.5"), &big("2.25")).to_text(), "-.75");
    assert_eq!(Big::mul(&big("1.5"), &big("2.25")).to_text(), "3.38");
    let third = Big::div(&big("1.0000"), &big("3")).expect("not dividing by zero");
    assert_eq!(third.to_text(), ".33333");
    assert!(Big::div(&big("1"), &big("0")).is_none());
}

#[test]
fn decimal_holds_what_an_integer_cannot() {
    let limit = Big::from_int(i32::MAX.into(), 10);
    let one = Big::from_int(1, 10);
    let past = Big::add(&limit, &one);
    assert_eq!(past.to_text(), "2147483648");
    assert!(past.to_i32().1, "conversion to an integer overflows");
    assert_eq!(limit.to_i32(), (i32::MAX, false));
}

#[test]
fn decimal_reads_other_radixes() {
    assert_eq!(Big::parse_radix("ff", 16).to_text(), "255");
    assert_eq!(
        Big::parse_radix("ffffffffffffffff", 16).to_text(),
        "18446744073709551615"
    );
    assert_eq!(Big::parse_radix(" -101", 2).to_text(), "-5");
    assert_eq!(Big::parse_radix("ff", 16).to_radix_text(2), "11111111");
}

#[test]
fn decimal_computes_constants_and_roots() {
    let mut cache = Cache::default();
    let pi = cache.pi(10).expect("pi").with_prec(10, true);
    assert_eq!(pi.to_text(), "3.141592654");
    let e = cache.e(10).expect("e").with_prec(10, true);
    assert_eq!(e.to_text(), "2.718281828");
    let root = decimal_math::sqrt_into(10, &big("2.000000000")).expect("root");
    assert_eq!(root.to_text(), "1.414213562");
    assert!(decimal_math::sqrt_into(10, &big("-2")).is_err());
}

#[test]
fn case_folding_expands_characters() {
    assert_eq!(crate::casefold::fold('A'), Some("a"));
    assert_eq!(crate::casefold::fold('ß'), Some("ss"));
    assert_eq!(crate::casefold::fold('a'), None);
}

// ---- hashes and byte packing ----

#[test]
fn hashes_match_the_standard_answers() {
    use crate::hash::{hex, md5, sha256};
    assert_eq!(hex(&md5(b"")), "D41D8CD98F00B204E9800998ECF8427E");
    assert_eq!(hex(&md5(b"abc")), "900150983CD24FB0D6963F7D28E17F72");
    assert_eq!(
        hex(&sha256(b"abc")),
        "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
    );
    // A message long enough to need a second block.
    let long = [b'a'; 100];
    assert_eq!(hex(&md5(&long)), "36A92CC94A9E0FA21F625F8BFB007ADF");
    assert_eq!(
        hex(&sha256(&long)),
        "2816597888E4A0D3A36B82B83316AB32680EB8F00F8CD3B904D681246D285A0E"
    );
}

#[test]
fn decimal_converts_to_and_from_binary() {
    let mut cache = Cache::default();
    let (bytes, overflow) = decimal_math::to_ieee754(&big("1.5"), 64, &mut cache).expect("double");
    assert!(!overflow);
    assert_eq!(bytes, 1.5f64.to_le_bytes());
    let (bytes, _) = decimal_math::to_ieee754(&big("-0.15625"), 32, &mut cache).expect("float");
    assert_eq!(bytes, (-0.15625f32).to_le_bytes());
    let back = decimal_math::from_ieee754(&2.5f64.to_le_bytes(), &mut cache).expect("double");
    assert_eq!(back.to_text(), "2.5");

    let (bytes, overflow) = big("-2").to_p8(true);
    assert_eq!((bytes, overflow), ((-2i64).to_le_bytes(), false));
    assert_eq!(
        Big::from_p8(u64::MAX.to_le_bytes(), false).to_text(),
        "18446744073709551615"
    );
    assert_eq!(Big::from_p8(u64::MAX.to_le_bytes(), true).to_text(), "-1");

    let (ber, overflow) = big("300").to_ber();
    assert_eq!((ber, overflow), (vec![0x82, 0x2c], false));
    assert_eq!(Big::from_ber(&[2, 0x2c]).to_text(), "300");
}

// ---- character sets ----

use crate::charset::CharMap;

#[test]
fn character_sets_go_by_many_names() {
    for name in ["latin-2", "ISO-8859-2", "iso2", "iso_8859_2"] {
        assert!(CharMap::load(name).is_some(), "{name}");
    }
    for name in ["cp1252", "windows-1252", "win1252", "1252"] {
        assert!(CharMap::load(name).is_some(), "{name}");
    }
    for name in ["utf-8", "UTF8", "utf-16le", "unicodeb", "ascii", "us", "l1"] {
        assert!(CharMap::load(name).is_some(), "{name}");
    }
    // `l2` looks like `l1`, but only that one is a name the reference knows.
    for name in ["", "nonesuch", "utf-16", "latin-0", "cp99999", "l2"] {
        assert!(CharMap::load(name).is_none(), "{name}");
    }
}

#[test]
fn character_sets_convert_text_and_bytes() {
    let latin2 = CharMap::load("latin-2").expect("latin-2");
    assert_eq!(latin2.encode("A\u{15e}\u{164}"), [0x41, 0xaa, 0xab]);
    assert_eq!(latin2.decode(&[0x41, 0xaa, 0xab]), "A\u{15e}\u{164}");

    // A set stands in for some characters it lacks, and writes a
    // question mark for the rest.
    let ascii = CharMap::load("us-ascii").expect("us-ascii");
    assert_eq!(ascii.encode("d\u{e9}j\u{e0} \u{4e2d}"), b"deja ?");
    assert_eq!(ascii.decode(&[0x41, 0xe9]), "A\u{fffd}");

    let utf16 = CharMap::load("utf-16be").expect("utf-16be");
    assert_eq!(utf16.encode("A\u{20ac}"), [0x00, 0x41, 0x20, 0xac]);
    assert_eq!(utf16.decode(&[0x00, 0x41, 0x20, 0xac, 0x00]), "A\u{20ac}");

    // UTF-8 that is cut short loses the unfinished character, and
    // anything else that is not UTF-8 becomes a question mark.
    let utf8 = CharMap::load("utf-8").expect("utf-8");
    assert_eq!(utf8.decode(&[0x41, 0xc3, 0xa9, 0xc3]), "A\u{e9}");
    assert_eq!(utf8.decode(&[0x41, 0xff, 0x42]), "A?B");
}

// ---- undo and garbage collection ----

fn vector_of(table: &ObjectTable, id: ObjId) -> Vec<Value> {
    match table.get(id) {
        Some(Object::Vector(items)) => items.clone(),
        other => panic!("not a vector: {other:?}"),
    }
}

fn push_to(table: &mut ObjectTable, id: ObjId, value: Value) {
    match table.get_mut(id) {
        Some(Object::Vector(items)) => items.push(value),
        other => panic!("not a vector: {other:?}"),
    }
}

#[test]
fn undo_returns_objects_to_the_savepoint() {
    let mut table = ObjectTable::new();
    let old = table.alloc(Object::Vector(vec![Value::Int(1)]));
    assert!(!table.undo(), "nothing to undo without a savepoint");

    table.savepoint();
    push_to(&mut table, old, Value::Int(2));
    table.savepoint();
    push_to(&mut table, old, Value::Int(3));
    push_to(&mut table, old, Value::Int(4));
    // An object made after a savepoint is not part of it.
    let new = table.alloc(Object::Vector(Vec::new()));
    push_to(&mut table, new, Value::Int(9));

    assert!(table.undo());
    assert_eq!(vector_of(&table, old), [Value::Int(1), Value::Int(2)]);
    assert_eq!(vector_of(&table, new), [Value::Int(9)]);
    assert!(table.undo());
    assert_eq!(vector_of(&table, old), [Value::Int(1)]);
    assert!(!table.undo());
}

#[test]
fn collector_frees_what_nothing_refers_to() {
    let mut table = ObjectTable::new();
    let kept = table.alloc(Object::Vector(Vec::new()));
    let inner = table.alloc(Object::String(b"inner".as_slice().into()));
    push_to(&mut table, kept, Value::Obj(inner));
    let garbage_a = table.alloc(Object::String(b"a".as_slice().into()));
    let garbage_b = table.alloc(Object::String(b"b".as_slice().into()));

    let candidates = table.collect_mark(&[Value::Obj(kept)], false);
    assert_eq!(candidates, [garbage_b, garbage_a]);
    let finalizers = table.collect_sweep(&candidates, &[]);
    assert!(finalizers.is_empty());
    assert!(table.get(kept).is_some() && table.get(inner).is_some());
    assert!(table.get(garbage_a).is_none() && table.get(garbage_b).is_none());

    // Freed IDs are handed out again, the highest first.
    assert_eq!(table.alloc(Object::Vector(Vec::new())), garbage_a);
    assert_eq!(table.alloc(Object::Vector(Vec::new())), garbage_b);
}

#[test]
fn collector_keeps_an_object_until_its_finalizer_has_run() {
    let mut table = ObjectTable::new();
    let dying = table.alloc(Object::Vector(Vec::new()));
    let held = table.alloc(Object::String(b"held".as_slice().into()));
    push_to(&mut table, dying, Value::Obj(held));

    // Unreachable, with a finalizer: it and what it refers to stay for
    // the finalizer to use.
    let candidates = table.collect_mark(&[], false);
    let queue = table.collect_sweep(&candidates, &[dying]);
    assert_eq!(queue, [dying]);
    assert!(table.get(dying).is_some() && table.get(held).is_some());

    // The next collection takes both.
    let candidates = table.collect_mark(&[], false);
    assert!(candidates.is_empty(), "nothing is finalized twice");
    assert!(table.collect_sweep(&candidates, &[]).is_empty());
    assert!(table.get(dying).is_none() && table.get(held).is_none());
}

#[test]
fn collector_spares_what_a_savepoint_can_bring_back() {
    let mut table = ObjectTable::new();
    let holder = table.alloc(Object::Vector(Vec::new()));
    let item = table.alloc(Object::String(b"item".as_slice().into()));
    push_to(&mut table, holder, Value::Obj(item));
    table.savepoint();
    match table.get_mut(holder) {
        Some(Object::Vector(items)) => items.clear(),
        _ => unreachable!(),
    }

    let candidates = table.collect_mark(&[Value::Obj(holder)], false);
    table.collect_sweep(&candidates, &[]);
    assert!(table.get(item).is_some());
    assert!(table.undo());
    assert_eq!(vector_of(&table, holder), [Value::Obj(item)]);
}

#[test]
fn collector_empties_weak_tables_of_dead_values() {
    let mut table = ObjectTable::new();
    let mut weak = Map::new(8, 8, true);
    let value = table.alloc(Object::String(b"value".as_slice().into()));
    weak.insert_for_test(Value::Int(1), Value::Obj(value));
    weak.insert_for_test(Value::Int(2), Value::Int(7));
    let weak = table.alloc(Object::Map(Box::new(weak)));

    let candidates = table.collect_mark(&[Value::Obj(weak)], false);
    table.collect_sweep(&candidates, &[]);
    assert!(table.get(value).is_none());
    match table.get(weak) {
        Some(Object::Map(weak)) => {
            assert_eq!(weak.entries(), [(Value::Int(2), Value::Int(7))]);
        }
        _ => unreachable!(),
    }
}

// ---- saving, restoring, and restarting ----

/// A VM for an image with one object, which has one property set to 1.
fn vm_with_one_object() -> Vm {
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::RetNil);
    let image = ImageBuilder::default()
        .object(
            1,
            program_object_data(&[], false, &[(PROP_GREET, holder(type_code::INT, 1))]),
        )
        .build(main, &code, &Consts::default());
    Vm::new(&image, Box::new(BufferHost::new())).expect("the image loads")
}

fn prop_of(vm: &Vm, id: u32, prop: u16) -> Option<Value> {
    vm.objects
        .program_object(ObjId(id))
        .and_then(|object| object.get(prop))
}

#[test]
fn saved_state_restores_objects() {
    let mut vm = vm_with_one_object();
    let other_prop = PROP_GREET + 1;

    // Change the image's object, and hang some new objects off it.
    let text = vm.new_str("kept");
    let vector = Value::Obj(vm.objects.alloc(Object::Vector(vec![Value::Int(5), text])));
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(2))
        .expect("set");
    vm.set_prop(ObjId(1), other_prop, vector).expect("set");
    let state = vm.save_state(Value::Nil).expect("save");

    // Change everything again, then go back.
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(3))
        .expect("set");
    vm.set_prop(ObjId(1), other_prop, Value::Nil).expect("set");
    vm.restore_state(&state).expect("restore");

    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(2)));
    let Some(Value::Obj(restored)) = prop_of(&vm, 1, other_prop) else {
        panic!("the vector is back");
    };
    match vm.objects.get(restored) {
        Some(Object::Vector(items)) => {
            assert_eq!(items[0], Value::Int(5));
            assert_eq!(&*vm.as_str(items[1]).expect("a string"), b"kept");
        }
        other => panic!("not a vector: {other:?}"),
    }
}

#[test]
fn saved_state_is_checked_before_it_is_used() {
    let mut vm = vm_with_one_object();
    let state = vm.save_state(Value::Nil).expect("save");
    let code_of = |error: VmError| match error {
        VmError::Runtime(error) => error.code,
        other => panic!("unexpected error: {other:?}"),
    };

    let error = vm
        .restore_state(b"not a saved state")
        .expect_err("rejected");
    assert_eq!(code_of(error), code::NOT_SAVED_STATE);

    let mut damaged = state.clone();
    let last = damaged.len() - 1;
    damaged[last] ^= 0xff;
    let error = vm.restore_state(&damaged).expect_err("rejected");
    assert_eq!(code_of(error), code::BAD_SAVED_STATE);

    // A state from another program has another timestamp. Changing it
    // means recomputing the checksum, as a real one would have.
    let mut foreign = state.clone();
    foreign[25] ^= 0x01;
    let crc = crate::save::checksum(&foreign[25..]);
    foreign[21..25].copy_from_slice(&crc.to_le_bytes());
    let error = vm.restore_state(&foreign).expect_err("rejected");
    assert_eq!(code_of(error), code::WRONG_SAVED_STATE);

    vm.restore_state(&state)
        .expect("the real one still restores");
}

/// A VM partway through three savepoints, with objects the program made:
/// the property is 1 at the first savepoint and 2 at the second, and
/// now 3; the vector holds 5, then 6; and at the third, another property
/// changes from one new string to another. Returns the VM, the vector, the
/// state saved at the second savepoint, and the state now.
fn vm_two_savepoints_in() -> (Vm, ObjId, Vec<u8>, Vec<u8>) {
    let mut vm = vm_with_one_object();
    let other_prop = PROP_GREET + 1;
    vm.objects.savepoint();
    let vector = vm.objects.alloc(Object::Vector(vec![Value::Int(5)]));
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(2))
        .expect("set");
    vm.set_prop(ObjId(1), other_prop, Value::Obj(vector))
        .expect("set");
    let past = vm.save_state(Value::Nil).expect("save");
    vm.objects.savepoint();
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(3))
        .expect("set");
    // A string that, once replaced, only the savepoint refers to.
    let old_text = vm.new_str("old");
    vm.set_prop(ObjId(1), PROP_GREET + 2, old_text)
        .expect("set");
    vm.objects.savepoint();
    let new_text = vm.new_str("new");
    vm.set_prop(ObjId(1), PROP_GREET + 2, new_text)
        .expect("set");
    if let Some(Object::Vector(items)) = vm.objects.get_mut(vector) {
        items[0] = Value::Int(6);
    }
    let present = vm.save_state(Value::Nil).expect("save");
    (vm, vector, past, present)
}

#[test]
fn a_visit_to_the_past_and_back_keeps_undo() {
    let (mut vm, vector, past, present) = vm_two_savepoints_in();

    // Go to the past, change something there, and come back.
    vm.restore_state_with(&past, crate::save::RestoreMode::SetUndoAside)
        .expect("go");
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(2)));
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(7))
        .expect("set");
    vm.collect_garbage(false);
    vm.restore_state_with(&present, crate::save::RestoreMode::ReturnKeepingUndo)
        .expect("return");
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(3)));

    // The string only the savepoint referred to outlasted the visit.
    assert!(vm.objects.undo());
    let Some(text) = prop_of(&vm, 1, PROP_GREET + 2) else {
        panic!("the old text is back");
    };
    assert_eq!(&*vm.as_str(text).expect("a string"), b"old");

    // The vector is back under its own ID, and UNDO goes back through
    // both savepoints as if the visit had not been made.
    assert!(matches!(vm.objects.get(vector), Some(Object::Vector(_))));
    assert!(vm.objects.undo());
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(2)));
    assert_eq!(vector_of(&vm.objects, vector), [Value::Int(5)]);
    assert!(vm.objects.undo());
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(1)));
    assert!(!vm.objects.undo());
}

#[test]
fn a_visit_forgets_undo_when_an_object_cannot_come_back_as_it_was() {
    let (mut vm, vector, past, present) = vm_two_savepoints_in();
    vm.restore_state_with(&past, crate::save::RestoreMode::SetUndoAside)
        .expect("go");

    // Something kept through the visit (transient) takes the vector's ID.
    if vm.objects.get(vector).is_none() {
        assert!(vm.objects.alloc_at(vector, Object::Vector(Vec::new())));
    }
    vm.objects.set_transient(vector);

    vm.restore_state_with(&present, crate::save::RestoreMode::ReturnKeepingUndo)
        .expect("return");
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(3)));
    assert!(!vm.objects.undo(), "the savepoints are forgotten");

    // A plain restore forgets them too, and any set aside.
    vm.objects.savepoint();
    vm.restore_state_with(&past, crate::save::RestoreMode::SetUndoAside)
        .expect("go");
    vm.restore_state(&present).expect("plain");
    assert!(vm.undo_stash.is_none());
    assert!(!vm.objects.undo());
}

#[test]
fn restart_returns_to_the_loaded_state() {
    let mut vm = vm_with_one_object();
    let other_prop = PROP_GREET + 1;
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(9))
        .expect("set");
    vm.set_prop(ObjId(1), other_prop, Value::True).expect("set");
    vm.restart_state().expect("restart");
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(1)));
    assert_eq!(prop_of(&vm, 1, other_prop), None);

    // And it can be done again.
    vm.set_prop(ObjId(1), PROP_GREET, Value::Int(7))
        .expect("set");
    vm.restart_state().expect("restart");
    assert_eq!(prop_of(&vm, 1, PROP_GREET), Some(Value::Int(1)));
}

#[test]
fn table_keys_hash_as_in_the_reference() {
    // Saved tables depend on this: the two halves of an integer
    // multiplied, and the characters of a string added up.
    let mut vm = vm_with_one_object();
    let mut table = Map::new(16, 8, false);
    table.insert_for_test(Value::Int(1), Value::True);
    let table = vm.new_table(table);
    let abc = vm.new_str("abc");
    vm.table_set(table, Value::Int(70_000), Value::Int(1))
        .expect("set");
    vm.table_set(table, abc, Value::Int(2)).expect("set");
    let Some(Object::Map(table)) = vm.objects.get(table) else {
        panic!("a table");
    };
    let (buckets, _, _) = table.save_parts();
    // 70,000 is 1 * 65,536 + 4,464, and 4,464 mod 16 is 0. "abc" adds up
    // to 294, and 294 mod 16 is 6.
    assert_eq!(buckets[0], 2, "the second slot holds 70,000");
    assert_eq!(buckets[6], 3, "the third slot holds the string");
}

// ---- the parser classes ----

use crate::dict::Dictionary;
use crate::gram::GrammarRule;
use crate::strcomp::StrComp;

/// A comparator that ignores case, accepts words cut to four characters,
/// and takes `ss` for `ß`, built from the image format.
fn test_comparator() -> StrComp {
    let mut data = Vec::new();
    for n in [4u16, 0, 1, 2] {
        data.extend_from_slice(&n.to_le_bytes());
    }
    data.extend_from_slice(&0xdfu16.to_le_bytes());
    data.push(2);
    data.extend_from_slice(&0x100u32.to_le_bytes());
    data.extend_from_slice(&0x200u32.to_le_bytes());
    for c in *b"ss" {
        data.extend_from_slice(&u16::from(c).to_le_bytes());
    }
    let (comparator, used) = StrComp::from_bytes(&data).expect("well formed");
    assert_eq!(used, data.len());
    assert_eq!(comparator.to_bytes(), data, "it writes what it read");
    comparator
}

#[test]
fn comparator_reports_how_words_match() {
    let c = test_comparator();
    let m = |value: &str, reference: &str| c.match_strings(value.as_bytes(), reference.as_bytes());
    assert_eq!(m("apple", "apple"), 1, "exactly");
    assert_eq!(m("APPLE", "apple"), 3, "ignoring case");
    assert_eq!(m("appl", "apple"), 5, "truncated");
    assert_eq!(m("app", "apple"), 0, "too short to be a truncation");
    assert_eq!(m("apples", "apple"), 0);
    assert_eq!(m("", ""), 1);
    // Equal words hash alike, as do a word and its truncation.
    assert_eq!(c.hash(b"APPLE"), c.hash(b"apple"));
    assert_eq!(c.hash(b"appl"), c.hash(b"apple"));
    assert_ne!(c.hash(b"app"), c.hash(b"apple"));
    assert_eq!(c.hash("stra\u{df}e".as_bytes()), c.hash(b"strasse"));
}

#[test]
fn dictionary_keeps_words_in_the_reference_order() {
    let hash = |text: &[u8]| text.iter().map(|&b| u32::from(b)).sum::<u32>();
    let mut d = Dictionary::new(ObjId::INVALID);
    let (noun, adjective) = (10, 11);
    let add = |d: &mut Dictionary, text: &str, object: u32, prop| {
        d.add(hash(text.as_bytes()), text.as_bytes(), ObjId(object), prop)
    };
    assert!(add(&mut d, "red", 1, adjective));
    assert!(add(&mut d, "red", 2, noun));
    assert!(add(&mut d, "red", 3, adjective));
    assert!(!add(&mut d, "red", 1, adjective), "already there");
    // An entry goes after the last with the same property, or first.
    let word = &d.candidates(hash(b"red"))[0];
    assert_eq!(
        word.entries,
        [
            (ObjId(2), noun),
            (ObjId(1), adjective),
            (ObjId(3), adjective)
        ]
    );
    // "der" has the same hash, and the newer word comes first.
    assert!(add(&mut d, "der", 4, noun));
    let texts: Vec<&[u8]> = d
        .candidates(hash(b"red"))
        .iter()
        .map(|word| word.text.as_slice())
        .collect();
    assert_eq!(texts, [b"der".as_slice(), b"red".as_slice()]);

    assert!(d.remove(hash(b"red"), b"red", ObjId(1), adjective));
    assert!(!d.remove(hash(b"red"), b"red", ObjId(1), adjective));
    d.remove_dead(&|id| id != ObjId(4));
    assert_eq!(d.words().count(), 1, "a word with no objects left is gone");
}

#[test]
fn dictionary_corrects_spelling() {
    let hash = |text: &[u8]| text.iter().map(|&b| u32::from(b)).sum::<u32>();
    let mut d = Dictionary::new(ObjId::INVALID);
    for word in ["apple", "apply", "ample", "cat"] {
        d.add(hash(word.as_bytes()), word.as_bytes(), ObjId(1), 10);
    }
    let correct = |d: &mut Dictionary, input: &str, distance| {
        let input: Vec<char> = input.chars().collect();
        let mut found = d.correct(&input, distance, None);
        found.sort();
        found
    };
    // One letter missing: an insertion, with no replacement.
    assert_eq!(
        correct(&mut d, "aple", 1),
        [("ample".to_owned(), 1, 0), ("apple".to_owned(), 1, 0)]
    );
    // Two adjacent letters swapped count as one edit.
    assert_eq!(correct(&mut d, "cta", 1), [("cat".to_owned(), 1, 0)]);
    // A word spelled right is not a correction of itself.
    assert_eq!(
        correct(&mut d, "apple", 1),
        [("ample".to_owned(), 1, 1), ("apply".to_owned(), 1, 1)]
    );
    assert!(correct(&mut d, "xyz", 1).is_empty());
}

#[test]
fn production_reads_and_writes_its_alternatives() {
    // One alternative: a literal, a part of speech, another production,
    // and the rest.
    let mut data = Vec::new();
    data.extend_from_slice(&1u16.to_le_bytes());
    data.extend_from_slice(&7i16.to_le_bytes());
    data.extend_from_slice(&0i16.to_le_bytes());
    data.extend_from_slice(&99u32.to_le_bytes());
    data.extend_from_slice(&4u16.to_le_bytes());
    data.extend_from_slice(&[0, 0, 3, 4, 0]);
    data.extend_from_slice(b"take");
    data.extend_from_slice(&[20, 0, 2, 10, 0]);
    data.extend_from_slice(&[21, 0, 1, 5, 0, 0, 0]);
    data.extend_from_slice(&[0, 0, 5]);
    let (production, used) = GrammarRule::from_bytes(&data).expect("well formed");
    assert_eq!(used, data.len());
    assert_eq!(production.alts.len(), 1);
    assert_eq!(production.alts[0].tokens.len(), 4);
    assert_eq!(production.to_bytes(), data);
    let mut references = Vec::new();
    production.references(&mut references);
    assert_eq!(references, [Value::Obj(ObjId(99)), Value::Obj(ObjId(5))]);
}

// ---- input, scripts, and logs ----

/// A VM whose program does nothing, and the host it talks to.
fn vm_with_host() -> (Vm, BufferHost) {
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::RetNil);
    let image = ImageBuilder::default().build(main, &code, &Consts::default());
    let host = BufferHost::new();
    let vm = Vm::new(&image, Box::new(host.clone())).expect("the image loads");
    (vm, host)
}

/// Call a console function with the given arguments, first one first.
fn io_call(vm: &mut Vm, index: u16, args: &[Value]) -> Value {
    for &arg in args.iter().rev() {
        vm.push(arg);
    }
    let handled = vm
        .bif_io(index, args.len() as u32)
        .expect("the function succeeds");
    assert!(handled, "function {index} is implemented");
    for _ in args {
        vm.pop();
    }
    vm.r0
}

fn text_of(vm: &Vm, value: Value) -> String {
    let text = vm.as_str(value).expect("a string");
    String::from_utf8_lossy(&text).into_owned()
}

fn items_of(vm: &Vm, value: Value) -> Vec<Value> {
    vm.as_list(value).expect("a list").to_vec()
}

const INPUT_LINE: u16 = 4;
const INPUT_KEY: u16 = 5;
const INPUT_EVENT: u16 = 6;
const INPUT_LINE_TIMEOUT: u16 = 17;

#[test]
fn input_line_reads_from_the_player() {
    let (mut vm, host) = vm_with_host();
    host.push_input("open door");
    vm.print(b"What now? ");
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "open door");
    // The prompt is written before the read, with its space.
    assert_eq!(host.output(), "What now? ");
    // No more input.
    assert_eq!(io_call(&mut vm, INPUT_LINE, &[]), Value::Nil);
    // A read with a timeout says the host has no timeouts.
    let result = io_call(&mut vm, INPUT_LINE_TIMEOUT, &[Value::Int(100)]);
    assert_eq!(items_of(&vm, result), [Value::Int(4)]);
}

#[test]
fn input_script_stands_in_for_the_player() {
    let (mut vm, host) = vm_with_host();
    host.push_input("typed");
    vm.push_input_script(b">look\r\nnot a command\n\n>wait\n", false, false);
    vm.print(b">");
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "look");
    vm.print(b">");
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "wait");
    // What the script supplied is displayed as if typed.
    assert_eq!(host.output(), ">look\n>wait\n");
    // When the script runs out, the player is asked.
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "typed");
}

#[test]
fn quiet_script_displays_nothing() {
    let (mut vm, host) = vm_with_host();
    vm.push_input_script(b">look\n", true, false);
    vm.print(b"Hidden.\n>");
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "look");
    vm.print(b"Also hidden.\n");
    // The read that finds the script used up turns the display back on.
    assert_eq!(io_call(&mut vm, INPUT_LINE, &[]), Value::Nil);
    vm.print(b"Shown.\n");
    assert_eq!(host.output(), "Shown.\n");
}

#[test]
fn event_script_supplies_keys_and_events() {
    let (mut vm, _host) = vm_with_host();
    let script = b"<eventscript>\n<key>a\n<key>[enter]\n<line>go north\n<href>link\n\
        <command>3\n<timeout>part\n<key>z\n";
    vm.push_input_script(script, false, false);
    let key = io_call(&mut vm, INPUT_KEY, &[]);
    assert_eq!(text_of(&vm, key), "a");
    let event = io_call(&mut vm, INPUT_EVENT, &[]);
    let event = items_of(&vm, event);
    assert_eq!(event[0], Value::Int(1));
    assert_eq!(text_of(&vm, event[1]), "\n");
    // A read of a line skips events of other kinds. A script cannot hold
    // a link event: the name is not one the reader knows.
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "go north");
    let event = io_call(&mut vm, INPUT_EVENT, &[]);
    assert_eq!(items_of(&vm, event), [Value::Int(0x100), Value::Int(3)]);
    let event = io_call(&mut vm, INPUT_EVENT, &[]);
    assert_eq!(items_of(&vm, event), [Value::Int(2)]);
    let key = io_call(&mut vm, INPUT_KEY, &[]);
    assert_eq!(text_of(&vm, key), "z");
    // With the script used up and no player, a key read reports the end.
    let key = io_call(&mut vm, INPUT_KEY, &[]);
    assert_eq!(text_of(&vm, key), "[eof]");
}

#[test]
fn logs_record_output_and_input() {
    const SET_LOG_FILE: u16 = 1;
    let (mut vm, host) = vm_with_host();
    let name = |vm: &mut Vm, text: &str| vm.new_str(text);

    // A transcript gets what is displayed and what the player types.
    let file = name(&mut vm, "transcript");
    assert_eq!(io_call(&mut vm, SET_LOG_FILE, &[file]), Value::True);
    host.push_input("look");
    vm.print(b"A <b>bold</b> start.\n>");
    io_call(&mut vm, INPUT_LINE, &[]);
    vm.print(b"Unfinished");
    io_call(&mut vm, SET_LOG_FILE, &[Value::Nil]);
    assert_eq!(
        host.file("transcript").as_deref(),
        Some(b"A bold start.\n>look\n".as_slice())
    );

    // A command log gets command lines only.
    let file = name(&mut vm, "commands");
    io_call(&mut vm, SET_LOG_FILE, &[file, Value::Int(2)]);
    host.push_input("north");
    host.push_input("k");
    io_call(&mut vm, INPUT_LINE, &[]);
    io_call(&mut vm, INPUT_KEY, &[]);
    io_call(&mut vm, SET_LOG_FILE, &[Value::Nil, Value::Int(2)]);
    assert_eq!(
        host.file("commands").as_deref(),
        Some(b">north\n".as_slice())
    );

    // An event log gets every event, in the form of an event script.
    let file = name(&mut vm, "events");
    io_call(&mut vm, SET_LOG_FILE, &[file, Value::Int(3)]);
    host.push_input("south");
    host.push_input(" ");
    io_call(&mut vm, INPUT_LINE, &[]);
    io_call(&mut vm, INPUT_KEY, &[]);
    io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(
        host.file("events").as_deref(),
        Some(b"<eventscript>\n<line>south\n<key>[space]\n<eof>\n".as_slice())
    );
    // The log can be played back.
    let log = host.file("events").expect("written");
    vm.push_input_script(&log, true, false);
    let line = io_call(&mut vm, INPUT_LINE, &[]);
    assert_eq!(text_of(&vm, line), "south");
    let key = io_call(&mut vm, INPUT_KEY, &[]);
    assert_eq!(text_of(&vm, key), " ");
}

#[test]
fn log_console_formats_into_a_file() {
    let (mut vm, host) = vm_with_host();
    let file = vm.new_str("notes");
    let handle = io_call(&mut vm, 30, &[file, Value::Nil, Value::Int(30)]);
    assert_eq!(handle, Value::Int(1));
    let text = vm.new_str("Text for the log console, <i>marked up</i>, long enough to wrap.\n");
    io_call(&mut vm, 32, &[handle, text]);
    io_call(&mut vm, 31, &[handle]);
    assert_eq!(
        host.file("notes").as_deref(),
        Some(b"Text for the log console,\nmarked up, long enough to\nwrap.\n".as_slice())
    );
    assert_eq!(host.output(), "", "none of it is displayed");
}

const PANE_CREATE: u16 = 19;
const PANE_SAY: u16 = 22;
const PANE_SIZE_TO_CONTENTS: u16 = 24;
const PANE_GET_INFO: u16 = 28;

/// paneCreate(nil, PaneLast, nil, PaneTypeText, PaneAlignTop, nil,
/// nil, PaneStyleBorder).
fn create_status_pane(vm: &mut Vm) -> Value {
    let args = [
        Value::Nil,
        Value::Int(2),
        Value::Nil,
        Value::Int(1),
        Value::Int(0),
        Value::Nil,
        Value::Nil,
        Value::Int(1),
    ];
    io_call(vm, PANE_CREATE, &args)
}

#[test]
fn a_plain_console_has_no_panes() {
    let (mut vm, _host) = vm_with_host();
    assert_eq!(create_status_pane(&mut vm), Value::Nil);
    assert_eq!(io_call(&mut vm, 10, &[Value::Int(33)]), Value::Int(0));
}

#[test]
fn a_pane_shows_its_own_formatted_text() {
    let (mut vm, host) = vm_with_host();
    host.enable_panes(24, 40);
    assert_eq!(io_call(&mut vm, 10, &[Value::Int(33)]), Value::Int(1));
    let handle = create_status_pane(&mut vm);
    assert_eq!(handle, Value::Int(1));
    let text = vm.new_str("Kitchen<tab align=right>Score: 5\n");
    io_call(&mut vm, PANE_SAY, &[handle, text]);
    io_call(&mut vm, PANE_SIZE_TO_CONTENTS, &[handle]);
    {
        let screen = host.screen();
        let screen = screen.as_ref().expect("a screen");
        let (id, _) = screen.panes()[0];
        // Right-aligned to the last column but one, as in the main window.
        assert_eq!(screen.view(id), [format!("Kitchen{:>32} ", "Score: 5")]);
        assert_eq!(screen.main().y, 1, "the main window starts below it");
    }
    let info = io_call(&mut vm, PANE_GET_INFO, &[handle]);
    let tab_align = 0x20;
    assert_eq!(
        items_of(&vm, info),
        [0, tab_align, 1, 40, 0, 0].map(Value::Int)
    );
    assert_eq!(host.output(), "", "nothing goes to the main window");
}

#[test]
fn pane_functions_check_their_handle() {
    let (mut vm, host) = vm_with_host();
    host.enable_panes(24, 40);
    let handle = create_status_pane(&mut vm);
    let code_of = |vm: &mut Vm, index: u16, args: &[Value]| {
        for &arg in args.iter().rev() {
            vm.push(arg);
        }
        let result = vm.bif_io(index, args.len() as u32);
        for _ in args {
            vm.pop();
        }
        match result {
            Err(VmError::Runtime(error)) => Some(error.code),
            _ => None,
        }
    };
    let bad = Some(code::BAD_VAL_BIF);
    assert_eq!(code_of(&mut vm, 21, &[Value::Int(7)]), bad, "no pane 7");
    assert_eq!(
        code_of(&mut vm, 25, &[handle, Value::Int(0), Value::Int(1)]),
        bad
    );
    // Deleting a pane that is not there is not an error, and once
    // deleted, a pane is not there.
    assert_eq!(code_of(&mut vm, 20, &[Value::Int(7)]), None);
    assert_eq!(code_of(&mut vm, 20, &[handle]), None);
    assert_eq!(code_of(&mut vm, 21, &[handle]), bad);
    // The handle is given out again.
    assert_eq!(create_status_pane(&mut vm), Value::Int(1));
}

#[test]
fn get_time_takes_a_date_apart() {
    // The host of a test has no clock and no time zone, so the time is
    // the start of 1970 in UTC, a Thursday.
    let (mut vm, _host) = vm_with_host();
    let now = vm.get_time(1).expect("a list");
    let expected = [1970, 1, 1, 5, 1, 0, 0, 0, 0].map(Value::Int);
    assert_eq!(items_of(&vm, now), expected);
    assert_eq!(vm.get_time(2).expect("a count"), Value::Int(0));
}

// ---- files ----

use crate::file::{combine, is_absolute, root_name};

#[test]
fn file_names_combine_as_paths() {
    let join = |dir, name| combine(dir, name, false);
    assert_eq!(join("a", "b"), "a/b");
    assert_eq!(join("a/", "b"), "a/b");
    assert_eq!(join("a", "/b"), "/b", "a name from the root stays");
    assert_eq!(join("a", "../b"), "b");
    assert_eq!(join("a", ".."), ".");
    assert_eq!(join("..", "b"), "../b");
    assert_eq!(join("../x", "../y"), "../y");
    assert_eq!(join("/a", "../../b"), "/../b");
    assert_eq!(join("a/b/", "./c/"), "a/b/c");
    assert_eq!(join("", "b"), "b");
    assert_eq!(join(".", "b"), "b");
    assert_eq!(join("a", ""), "a");
    // A directory listing keeps the entries as they are.
    assert_eq!(combine("dir", "..", true), "dir/..");
    assert_eq!(root_name("a/b/c.txt"), "c.txt");
    assert_eq!(root_name("plain"), "plain");
    assert!(is_absolute("/a") && is_absolute("C:/a") && !is_absolute("a/b"));
}

/// Call a method of File: on the class with no object, or on a file.
fn file_call(
    vm: &mut Vm,
    file: Option<Value>,
    index: u16,
    args: &[Value],
) -> crate::error::VmResult<Value> {
    for &arg in args.iter().rev() {
        vm.push(arg);
    }
    let mut argc = args.len() as u32;
    let id = file.map(|file| file.obj_id());
    let result = vm.file_method(id, index, Some(&mut argc));
    if result.is_err() {
        for _ in args {
            vm.pop();
        }
    }
    result.map(|value| value.expect("the method exists"))
}

const OPEN_TEXT: u16 = 1;
const OPEN_DATA: u16 = 2;
const OPEN_RAW: u16 = 3;
const CLOSE: u16 = 6;
const READ: u16 = 7;
const WRITE: u16 = 8;

#[test]
fn data_file_round_trips_values() {
    let (mut vm, host) = vm_with_host();
    let name = vm.new_str("values");
    let file = file_call(&mut vm, None, OPEN_DATA, &[name, Value::Int(2)]).expect("opens");
    let text = vm.new_str("text");
    for value in [Value::Int(7), Value::True, text, Value::Enum(3)] {
        file_call(&mut vm, Some(file), WRITE, &[value]).expect("writes");
    }
    // Nil has no form in a data file.
    let error = file_call(&mut vm, Some(file), WRITE, &[Value::Nil]).expect_err("fails");
    assert_eq!(error, VmError::new(code::BAD_TYPE_BIF));
    // The file reaches the host when it is closed.
    assert_eq!(host.file("values").as_deref(), Some(b"".as_slice()));
    file_call(&mut vm, Some(file), CLOSE, &[]).expect("closes");
    let expected: &[u8] = &[
        1, 7, 0, 0, 0, 8, 3, 6, 0, b't', b'e', b'x', b't', 0x20, 3, 0, 0, 0,
    ];
    assert_eq!(host.file("values").as_deref(), Some(expected));
    // A closed file cannot be used.
    assert!(file_call(&mut vm, Some(file), READ, &[]).is_err());

    let file = file_call(&mut vm, None, OPEN_DATA, &[name, Value::Int(1)]).expect("opens");
    let mut read = || file_call(&mut vm, Some(file), READ, &[]).expect("reads");
    assert_eq!(read(), Value::Int(7));
    assert_eq!(read(), Value::True);
    let text = read();
    assert_eq!(read(), Value::Enum(3));
    assert_eq!(read(), Value::Nil, "the end of the file");
    assert_eq!(text_of(&vm, text), "text");
    // A file opened for reading cannot be written.
    assert!(file_call(&mut vm, Some(file), WRITE, &[Value::Int(1)]).is_err());
}

#[test]
fn text_file_reads_lines_with_any_ending() {
    let (mut vm, host) = vm_with_host();
    host.set_file("lines", b"one\r\ntwo\rthree\nfour\n\rfive\r\r\ncaf\xe9");
    let name = vm.new_str("lines");
    let charset = vm.new_str("latin1");
    let file = file_call(&mut vm, None, OPEN_TEXT, &[name, Value::Int(1), charset]).expect("opens");
    let mut lines = Vec::new();
    loop {
        let line = file_call(&mut vm, Some(file), READ, &[]).expect("reads");
        if line == Value::Nil {
            break;
        }
        lines.push(text_of(&vm, line));
    }
    assert_eq!(
        lines,
        [
            "one\n",
            "two\n",
            "three\n",
            "four\n",
            "five\n",
            "\n",
            "caf\u{e9}"
        ]
    );

    // Text is written in the file's character set, with the system's
    // line ending for each newline.
    let name = vm.new_str("written");
    let file = file_call(&mut vm, None, OPEN_TEXT, &[name, Value::Int(2), charset]).expect("opens");
    let text = vm.new_str("caf\u{e9}\n");
    file_call(&mut vm, Some(file), WRITE, &[text]).expect("writes");
    file_call(&mut vm, Some(file), WRITE, &[Value::Int(42)]).expect("writes");
    file_call(&mut vm, Some(file), CLOSE, &[]).expect("closes");
    let expected: &[u8] = if cfg!(windows) {
        b"caf\xe9\r\n42"
    } else {
        b"caf\xe9\n42"
    };
    assert_eq!(host.file("written").as_deref(), Some(expected));
}

#[test]
fn unclosed_file_is_written_when_collected() {
    let (mut vm, host) = vm_with_host();
    let name = vm.new_str("left-open");
    let file = file_call(&mut vm, None, OPEN_DATA, &[name, Value::Int(2)]).expect("opens");
    file_call(&mut vm, Some(file), WRITE, &[Value::Int(1)]).expect("writes");
    assert_eq!(host.file("left-open").as_deref(), Some(b"".as_slice()));
    // Opening the file again shows what was written so far.
    let again = file_call(&mut vm, None, OPEN_RAW, &[name, Value::Int(1)]).expect("opens");
    assert_eq!(host.file("left-open").map(|data| data.len()), Some(5));
    file_call(&mut vm, Some(again), CLOSE, &[]).expect("closes");
    file_call(&mut vm, Some(file), WRITE, &[Value::Int(2)]).expect("writes");
    // Nothing refers to the file object, so the collector frees it.
    vm.r0 = Value::Nil;
    vm.collect_garbage(false);
    assert!(vm.objects.get(file.obj_id()).is_none());
    assert_eq!(host.file("left-open").map(|data| data.len()), Some(10));
}

/// A host that confines the program to a folder nothing is in.
struct LockedHost(BufferHost);

impl hexicon_host::Host for LockedHost {
    fn print(&mut self, text: &str) {
        self.0.print(text);
    }

    fn write_file(&mut self, name: &str, data: &[u8]) -> Result<(), hexicon_host::FileError> {
        self.0.write_file(name, data)
    }

    fn sandbox(&mut self, _name: &str) -> hexicon_host::Sandbox {
        hexicon_host::Sandbox::Outside
    }
}

#[test]
fn file_outside_the_sandbox_is_refused() {
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::RetNil);
    let image = ImageBuilder::default().build(main, &code, &Consts::default());
    let host = BufferHost::new();
    let mut vm = Vm::new(&image, Box::new(LockedHost(host.clone()))).expect("the image loads");
    let name = vm.new_str("secret");
    // With no exception class in the program, the refusal is a run-time
    // error.
    let error = file_call(&mut vm, None, OPEN_RAW, &[name, Value::Int(2)]).expect_err("refused");
    assert!(matches!(error, VmError::Runtime(error) if error.code == code::INTCLS_GENERAL_ERROR));
    assert!(host.file("secret").is_none());
    // Safety level 0 for writing allows it anywhere.
    vm.set_file_safety(2, 0);
    file_call(&mut vm, None, OPEN_RAW, &[name, Value::Int(2)]).expect("allowed");
    assert!(host.file("secret").is_some());
}

// ---- dates and time zones ----

use crate::date::{CalDate, UNIX_EPOCH_DAYNO};
use crate::tz::{LocalZone, ZoneCache};

#[test]
fn calendar_dates_convert_to_day_numbers_and_back() {
    assert_eq!(CalDate::new(0, 3, 1).dayno(), 0);
    assert_eq!(CalDate::new(1970, 1, 1).dayno(), UNIX_EPOCH_DAYNO);
    assert_eq!(
        CalDate::from_dayno(UNIX_EPOCH_DAYNO),
        CalDate::new(1970, 1, 1)
    );
    // The leap day, and what out-of-range months and days mean.
    assert_eq!(
        CalDate::new(2012, 3, 1).dayno() - CalDate::new(2012, 2, 28).dayno(),
        2
    );
    assert_eq!(
        CalDate::new(2012, 3, 0).dayno(),
        CalDate::new(2012, 2, 29).dayno()
    );
    assert_eq!(
        CalDate::new(2011, 13, 1).dayno(),
        CalDate::new(2012, 1, 1).dayno()
    );
    for dayno in [-800_000, -1, 0, 59, 730_000, 734_921, 3_000_000] {
        assert_eq!(CalDate::from_dayno(dayno).dayno(), dayno);
    }
    // January 1, 1970 was a Thursday.
    assert_eq!(CalDate::new(1970, 1, 1).weekday(), 4);
    assert_eq!(CalDate::new(2012, 2, 29).weekday(), 3);
}

/// What a zone's clocks were doing at noon UTC on a date: the offset
/// from UTC in hours and the abbreviation.
fn zone_at(zones: &mut ZoneCache, name: &str, y: i32, m: i32, d: i32) -> (f64, String) {
    let mut no_local = LocalZone::default;
    let zone = zones.parse_zone(name, &mut no_local).expect("a zone");
    let query = zone.query(CalDate::new(y, m, d).dayno(), 12 * 60 * 60 * 1000, false);
    (
        f64::from(query.gmtofs + query.save) / 3_600_000.0,
        query.abbr.to_string(),
    )
}

#[test]
fn zones_know_their_history_and_their_rules() {
    let mut zones = ZoneCache::default();
    let at = |zones: &mut ZoneCache, y, m, d| zone_at(zones, "America/New_York", y, m, d);
    assert_eq!(at(&mut zones, 2012, 1, 15), (-5.0, "EST".to_owned()));
    assert_eq!(at(&mut zones, 2012, 7, 15), (-4.0, "EDT".to_owned()));
    // Daylight time began on the last Sunday of April in 1967.
    assert_eq!(at(&mut zones, 1967, 4, 29).0, -5.0);
    assert_eq!(at(&mut zones, 1967, 5, 1).0, -4.0);
    // Before standard time, the zone kept New York's own mean time.
    assert_eq!(at(&mut zones, 1800, 1, 1).1, "LMT");
    // Past the end of the recorded history, the rules carry on.
    assert_eq!(at(&mut zones, 2400, 1, 15), (-5.0, "EST".to_owned()));
    assert_eq!(at(&mut zones, 2400, 7, 15), (-4.0, "EDT".to_owned()));
    // Names are found in any case, and by their other names.
    assert_eq!(zone_at(&mut zones, "us/eastern", 2012, 7, 15).0, -4.0);
    assert_eq!(zone_at(&mut zones, "Asia/Kolkata", 2012, 7, 15).0, 5.5);
}

#[test]
fn zones_are_made_from_offsets_and_descriptions() {
    let mut zones = ZoneCache::default();
    let mut no_local = LocalZone::default;
    assert_eq!(
        zone_at(&mut zones, "UTC", 2012, 1, 1),
        (0.0, "UTC".to_owned())
    );
    assert_eq!(
        zone_at(&mut zones, "+0530", 2012, 1, 1),
        (5.5, "UTC+5:30".to_owned())
    );
    assert_eq!(zone_at(&mut zones, "utc-8", 2012, 1, 1).0, -8.0);
    // A POSIX-style description, whose offset the reference VM takes as
    // east of UTC.
    assert_eq!(
        zone_at(&mut zones, "EST5", 2012, 1, 1),
        (5.0, "EST".to_owned())
    );
    let described = "PST8PDT,M3.2.0,M11.1.0";
    assert_eq!(
        zone_at(&mut zones, described, 2012, 1, 15),
        (8.0, "PST".to_owned())
    );
    assert_eq!(
        zone_at(&mut zones, described, 2012, 7, 15),
        (9.0, "PDT".to_owned())
    );
    assert!(zones.parse_zone("Nowhere/Land", &mut no_local).is_none());
    assert!(zones.parse_zone("EST5 ", &mut no_local).is_none());
    // The same name gives the same zone object.
    let first = zones
        .parse_zone("Europe/London", &mut no_local)
        .expect("a zone");
    let second = zones
        .parse_zone("europe/london", &mut no_local)
        .expect("a zone");
    assert!(std::rc::Rc::ptr_eq(&first, &second));
}

/// Make a Date from a string read in UTC.
fn date_from(vm: &mut Vm, text: &str) -> Value {
    let zone = vm.new_str("UTC");
    let text = vm.new_str(text);
    vm.push(zone);
    vm.push(text);
    let id = vm.create_date(2).expect("a date");
    vm.pop();
    vm.pop();
    Value::Obj(id)
}

/// Format a date in UTC.
fn date_format(vm: &mut Vm, date: Value, format: &str) -> String {
    let zone = vm.new_str("UTC");
    let format = vm.new_str(format);
    vm.push(zone);
    vm.push(format);
    let mut argc = 2;
    let text = vm
        .date_method(Some(date.obj_id()), 3, Some(&mut argc))
        .expect("formats")
        .expect("the method exists");
    text_of(vm, text)
}

#[test]
fn dates_parse_and_format() {
    let (mut vm, _host) = vm_with_host();
    let stamp = "%Y-%m-%d %H:%M:%S.%N";
    for (text, expected) in [
        ("2012-02-29 13:05:09.007", "2012-02-29 13:05:09.007"),
        ("2/29/2012", "2012-02-29 00:00:00.000"),
        ("February 29th, 2012 1:05 pm", "2012-02-29 13:05:00.000"),
        ("29 Feb 2012 12:30 +0530", "2012-02-29 07:00:00.000"),
        (
            "2012-07-01 12:00 America/New_York",
            "2012-07-01 16:00:00.000",
        ),
        ("@86400", "1970-01-02 00:00:00.000"),
        ("2012-W09-3", "2012-02-29 00:00:00.000"),
        ("44 BC", "-0043-01-01 00:00:00.000"),
        // Parts the text leaves out come from the reference date, which
        // for a test's host is the start of 1970.
        ("12:30", "1970-01-01 12:30:00.000"),
    ] {
        let date = date_from(&mut vm, text);
        assert_eq!(date_format(&mut vm, date, stamp), expected, "{text}");
    }
    let date = date_from(&mut vm, "2012-02-29 13:05:09");
    assert_eq!(
        date_format(&mut vm, date, "%a %A %b %B %j %t %#d/%#m %I%P %&Y %%"),
        "Wed Wednesday Feb February 060 29th 29/2 01PM MMXII %"
    );
    assert_eq!(date_format(&mut vm, date, "%c"), "Wed Feb 29 13:05:09 2012");
    assert_eq!(date_format(&mut vm, date, "%U %W %V %G"), "09 09 09 2012");
    assert_eq!(
        date_format(&mut vm, date, "%E|%s|%z|%Z"),
        "AD 2012|1330520709|UTC|+0000"
    );
}

#[test]
fn dates_do_arithmetic() {
    let (mut vm, _host) = vm_with_host();
    let stamp = "%Y-%m-%d %H:%M";
    let date = date_from(&mut vm, "2012-02-29 12:00");
    let later = vm.date_add(date.obj_id(), Value::Int(1)).expect("adds");
    assert_eq!(date_format(&mut vm, later, stamp), "2012-03-01 12:00");
    let half = vm.new_big(Big::parse("1.5", 8));
    let later = vm.date_add(date.obj_id(), half).expect("adds");
    assert_eq!(date_format(&mut vm, later, stamp), "2012-03-02 00:00");
    // The difference of two dates is a number of days.
    let difference = vm.date_sub(later.obj_id(), date).expect("subtracts");
    assert_eq!(vm.as_big(difference).expect("a number").to_text(), "1.5");
    assert_eq!(
        vm.compare(date, later).expect("compares"),
        std::cmp::Ordering::Less
    );
    assert!(vm.compare(date, Value::Int(1)).is_err());
}

// ---- stopping to wait for the host ----

#[test]
fn input_that_is_not_ready_stops_the_program() {
    let (mut vm, host) = vm_with_host();
    host.set_interactive(true);
    vm.print(b"> ");
    // A read made by the VM itself, not by a running program, has no
    // instruction to come back to, so it cannot wait.
    let error = vm.bif_io(INPUT_LINE, 0).expect_err("nothing to read yet");
    assert!(matches!(error, VmError::Unimplemented(_)));

    // A running program stops, and is asked again when it resumes.
    vm.io.resumable = true;
    vm.run_depth = 1;
    let error = vm.bif_io(INPUT_LINE, 0).expect_err("nothing to read yet");
    assert_eq!(error, VmError::Suspend);
    assert_eq!(host.output(), "> ", "the prompt is shown while it waits");
    host.push_input("north");
    assert!(vm.bif_io(INPUT_LINE, 0).expect("reads"));
    assert_eq!(text_of(&vm, vm.r0), "north");
    assert_eq!(host.output(), "> ", "and is not shown again");
}

#[test]
fn a_dialog_asks_its_question_once_however_long_the_answer_takes() {
    const INPUT_DIALOG: u16 = 7;
    let (mut vm, host) = vm_with_host();
    host.set_interactive(true);
    vm.io.resumable = true;
    vm.run_depth = 1;
    let prompt = vm.new_str("Proceed?");
    // inputDialog(icon, prompt, buttons, default, cancel), with the
    // standard Yes and No buttons.
    let args = [Value::Int(0), prompt, Value::Int(3), Value::Nil, Value::Nil];
    let ask = |vm: &mut Vm| {
        for &arg in args.iter().rev() {
            vm.push(arg);
        }
        let result = vm.bif_io(INPUT_DIALOG, args.len() as u32);
        for _ in &args {
            vm.pop();
        }
        result
    };
    assert_eq!(ask(&mut vm).expect_err("no answer yet"), VmError::Suspend);
    assert_eq!(ask(&mut vm).expect_err("no answer yet"), VmError::Suspend);
    host.push_input("n");
    assert!(ask(&mut vm).expect("answered"));
    assert_eq!(vm.r0, Value::Int(2));
    assert_eq!(host.output().matches("Proceed?").count(), 1);
}

#[test]
fn a_resource_not_in_the_image_is_a_file_beside_it() {
    let (mut vm, host) = vm_with_host();
    host.set_file("autosave.tmp", b"saved");
    assert_eq!(
        vm.resource_file(Some("autosave.tmp")),
        Some(b"saved".to_vec())
    );
    assert_eq!(vm.resource_file(Some("missing")), None);
    // Nothing outside the image's folder, however it is named.
    assert_eq!(vm.resource_file(Some("../autosave.tmp")), None);
    assert_eq!(vm.resource_file(Some("/autosave.tmp")), None);
    assert_eq!(vm.resource_file(None), None);
    // And nothing at all when the program may read no files.
    vm.set_file_safety(4, 4);
    assert_eq!(vm.resource_file(Some("autosave.tmp")), None);
}

#[test]
fn characters_are_the_kinds_the_reference_says() {
    use crate::chartype::{is_alpha, is_digit, is_lower, is_punct, is_space, is_upper, is_vspace};
    // Punctuation marks are punctuation; symbols are not.
    for c in [
        '!', '"', '#', '%', '&', '(', ')', '*', ',', '-', '.', '/', ':', ';', '?', '@',
    ] {
        assert!(is_punct(c), "{c}");
    }
    for c in ['$', '+', '<', '=', '>', '^', '`', '|', '~'] {
        assert!(!is_punct(c), "{c}");
    }
    assert!(is_punct('\u{2019}'), "a curly apostrophe");
    assert!(is_alpha('a') && is_lower('a') && !is_upper('a'));
    assert!(is_alpha('\u{c9}') && is_upper('\u{c9}'));
    assert!(is_digit('7') && !is_alpha('7'));
    assert!(is_space(' ') && is_space('\t') && !is_vspace(' '));
    assert!(is_vspace('\n') && is_vspace('\r') && !is_space('\n'));
    assert!(!is_alpha('\0') && !is_punct('\0'));
}

#[test]
fn a_run_before_the_image_is_written_is_kept_in_it() {
    // _main() { obj1.name = obj1.name + '!'; print(obj1.name); }
    let mut consts = Consts::default();
    let base = consts.string("base");
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::ObjGetProp).u32(1).u16(PROP_NAME);
    code.op(Op::GetR0);
    code.op(Op::PushStr).u32(consts.string("!"));
    code.op(Op::Add);
    code.op(Op::ObjSetProp).u32(1).u16(PROP_NAME);
    code.op(Op::ObjGetProp).u32(1).u16(PROP_NAME);
    code.op(Op::GetR0);
    code.say().op(Op::RetNil);
    let bytes = ImageBuilder::default()
        .object(
            1,
            program_object_data(&[], false, &[(PROP_NAME, holder(type_code::SSTRING, base))]),
        )
        .build_bytes(main, &code, &consts);

    // Run it once before writing: the string it makes goes into the
    // image as a constant.
    let image = Image::parse(&bytes).expect("the test image is well formed");
    let host = BufferHost::new();
    let mut vm = Vm::new(&image, Box::new(host.clone())).expect("the image loads");
    vm.run_preinit(&[])
        .expect("the run before writing succeeds");
    let rebuilt = vm.rewrite_image(&bytes).expect("the image is rewritten");
    vm.flush_output();
    assert_eq!(host.output(), "base!");

    // The image written starts from there.
    let image = Image::parse(&rebuilt.bytes).expect("the rewritten image is well formed");
    assert_eq!(run(&image), ("base!!".to_owned(), Ok(())));
}

// ---- when the collector runs, and what it keeps ----

#[test]
fn what_a_callback_made_and_dropped_is_freed_while_its_caller_runs() {
    let mut table = ObjectTable::new();
    // A built-in, at the first level, makes an object it holds, then
    // calls program code, which makes two and returns one.
    table.at_safe_point(1);
    let held = table.alloc(Object::String(b"held".as_slice().into()));
    table.enter_nested(1);
    table.at_safe_point(2);
    let dropped = table.alloc(Object::String(b"dropped".as_slice().into()));
    let returned = table.alloc(Object::String(b"returned".as_slice().into()));
    table.leave_nested(1, Value::Obj(returned));

    // A collection now, as the built-in goes on, keeps what it may be
    // holding and frees the rest.
    let candidates = table.collect_mark(&[], true);
    assert_eq!(candidates, [dropped]);

    // Once its instruction is over, none of it is protected.
    table.at_safe_point(1);
    let mut candidates = table.collect_mark(&[], false);
    candidates.sort_by_key(|id| id.0);
    let mut expected = vec![held, dropped, returned];
    expected.sort_by_key(|id| id.0);
    assert_eq!(candidates, expected);
}

#[test]
fn six_megabytes_allocated_start_a_collection() {
    crate::heap::reset();
    let mut table = ObjectTable::new();
    table.alloc(Object::String(vec![b'x'; 6 * 1024 * 1024 - 2].into()));
    assert!(!table.gc_due(), "exactly 6 MB is not more than 6 MB");
    table.alloc(Object::String(b"x".as_slice().into()));
    assert!(table.gc_due());
    table.collect_mark(&[], false);
    assert!(!table.gc_due(), "a collection starts the count again");
}

#[test]
fn a_block_that_outgrows_its_heap_cell_is_counted_twice() {
    crate::heap::reset();
    // A vector of 10 elements takes 70 bytes, in a cell of 128.
    let mut cell = crate::heap::cell_for(crate::heap::vector_bytes(10));
    assert_eq!(cell, 128);
    // Growing within the cell counts the new size once.
    crate::heap::realloc(&mut cell, crate::heap::vector_bytes(20), "vector");
    assert_eq!(crate::heap::since_gc(), 128);
    // Growing out of it counts it again, and moves to a bigger cell.
    crate::heap::realloc(&mut cell, crate::heap::vector_bytes(36), "vector");
    assert_eq!(crate::heap::since_gc(), 128 + 2 * 224);
    assert_eq!(cell, 256);
    // A block too big for any cell is counted once.
    let mut cell = crate::heap::cell_for(600);
    assert_eq!(cell, 0);
    crate::heap::realloc(&mut cell, 900, "vector");
    assert_eq!(crate::heap::since_gc(), 128 + 2 * 224 + 900);
}

#[test]
fn a_string_buffer_grows_in_steps_and_undo_keeps_its_room() {
    use crate::object::StringBuilder;
    let mut table = ObjectTable::new();
    let id = table.alloc(Object::StringBuilder(StringBuilder::new(16, 16)));
    table.savepoint();
    if let Some(Object::StringBuilder(buffer)) = table.get_mut(id) {
        buffer.chars.extend("seventeen letters".chars());
        buffer.grow();
        assert_eq!(buffer.room, 32, "grown by a step of 16");
    }
    assert!(table.undo());
    match table.get(id) {
        Some(Object::StringBuilder(buffer)) => {
            assert!(buffer.chars.is_empty());
            assert_eq!(buffer.room, 32, "the room it grew to is not given back");
        }
        other => panic!("not a string buffer: {other:?}"),
    }
}

// ---- damaged programs ----

#[test]
fn a_local_the_frame_lacks_is_an_error() {
    let consts = Consts::default();
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::GetLcl1).u8(40).say().op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(
        run(&image).1,
        Err("Unhandled exception: stack access is out of bounds".to_owned())
    );
}

#[test]
fn swapping_below_the_bottom_of_the_stack_is_an_error() {
    let consts = Consts::default();
    let mut code = Code::default();
    // The program's arguments are all the stack holds.
    let main = code.function(1, 0);
    code.op(Op::Swap2).op(Op::RetNil);

    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(
        run(&image).1,
        Err("Unhandled exception: stack access is out of bounds".to_owned())
    );
}

#[test]
fn a_list_that_holds_itself_overflows_the_stack() {
    // Two constant lists, each holding the other: comparing them, or
    // turning one into text, would go on for ever.
    let mut consts = Consts::default();
    let first = consts.bytes.len() as u32;
    let second = first + 7;
    consts.list(&[holder(crate::value::type_code::LIST, second)]);
    consts.list(&[holder(crate::value::type_code::LIST, first)]);
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::PushLst).u32(first).say().op(Op::RetNil);
    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(
        run(&image).1,
        Err("Unhandled exception: stack overflow".to_owned())
    );

    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::PushLst).u32(first).op(Op::PushLst).u32(second);
    code.op(Op::Eq).say().op(Op::RetNil);
    let image = ImageBuilder::default().build(main, &code, &consts);
    assert_eq!(
        run(&image).1,
        Err("Unhandled exception: stack overflow".to_owned())
    );
}

#[test]
fn an_object_id_far_past_the_others_is_refused() {
    let consts = Consts::default();
    let mut code = Code::default();
    let main = code.function(1, 0);
    code.op(Op::RetNil);
    let image = ImageBuilder::default()
        .object(0x4000_0000, program_object_data(&[], false, &[]))
        .build(main, &code, &consts);
    let loaded = Vm::new(&image, Box::new(BufferHost::new()));
    assert!(matches!(
        loaded,
        Err(VmError::Runtime(error)) if error.code == code::OUT_OF_MEMORY
    ));
}