File: test.ml

package info (click to toggle)
ppx-bin-prot 0.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 396 kB
  • sloc: ml: 5,351; makefile: 15
file content (1962 lines) | stat: -rw-r--r-- 45,051 bytes parent folder | download | duplicates (2)
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
open Core
open Poly

module Shape = struct
  include Bin_prot.Shape

  let annotate s xs = annotate (Uuid.of_string s) xs
  let basetype s x = basetype (Uuid.of_string s) x
end

module Canonical = struct
  include Shape.Canonical
  include Shape.Canonical.Create

  let annotate x t = annotate (Shape.Uuid.of_string x) t
  let basetype s xs = basetype (Shape.Uuid.of_string s) xs
  let base0 s = basetype s []
  let base1 s t1 = basetype s [ t1 ]
  let unit = base0 "unit"
  let bool = base0 "bool"
  let string = base0 "string"
  let char = base0 "char"
  let int = base0 "int"
  let float = base0 "float"
  let option = base1 "option"
  let list = base1 "list"
  let array = base1 "array"
  let dvariant x = define (variant x)
  let poly_variant = poly_variant (Shape.Location.of_string "somewhere")
end

let expect_raise f =
  assert (
    try
      f ();
      false
    with
    | _ -> true)
;;

let eval_to_digest (exp : Shape.t) : string =
  (* Test result from `quick' [eval_to_digest] always matches the `slow' sequence:
     [eval] ; [to_digest]. *)
  let res_quick = Shape.eval_to_digest exp in
  let res_slow = Canonical.to_digest (Shape.eval exp) in
  [%test_result: Shape.Digest.t] res_quick ~expect:res_slow;
  Shape.Digest.to_hex res_quick
;;

let ensure_all_different exps =
  let alist = List.map exps ~f:(fun e -> eval_to_digest e, e) in
  let ht = String.Map.of_alist_multi alist in
  List.iter (Map.to_alist ht) ~f:(fun (h, exps) ->
    match exps with
    | [] -> assert false
    | [ _ ] -> ()
    | e1 :: _ :: _ ->
      failwithf
        "shapes are not all different: %s -> %s"
        (Canonical.to_string_hum (Shape.eval e1))
        h
        ())
;;

let ensure_all_same exps =
  let alist = List.map exps ~f:(fun e -> eval_to_digest e, e) in
  let m = String.Map.of_alist_multi alist in
  match Map.to_alist m with
  | [] -> ()
  | [ _ ] -> ()
  | _ :: _ :: _ as xs ->
    failwithf
      "shapes are not all the same: %s"
      (String.concat
         ~sep:", "
         (List.map xs ~f:(fun (h, exps) ->
            let e1 =
              match exps with
              | [] -> assert false
              | e :: _ -> e
            in
            sprintf "%s -> %s" (Canonical.to_string_hum (Shape.eval e1)) h)))
      ()
;;

let ensure_shape exp ~expect =
  (* Test that a Shape.t [exp], evaluates to the expected
     Canonical.t [expect] *)
  [%test_result: Canonical.t] (Shape.eval exp) ~expect
;;

let ( = ) x y = eval_to_digest x = eval_to_digest y
let ( != ) x y = not (x = y)

(* meta-test that we are using sensible bindings for [=] and [!=] *)
let%test _ = [%bin_shape: int] = [%bin_shape: int]
let%test _ = not ([%bin_shape: int] != [%bin_shape: int])

(* int/string have distinct hashes; but self-equal *)
let%test _ = [%bin_shape: int] = [%bin_shape: int]
let%test _ = [%bin_shape: string] = [%bin_shape: string]
let%test _ = [%bin_shape: int] != [%bin_shape: string]

(* existence of some predefined bin_shape_<t> functions *)
let%test _ = [%bin_shape: int] = bin_shape_int
let%test _ = [%bin_shape: string] = bin_shape_string

(* different types must have different hashes *)
let%test_unit _ =
  ensure_all_different
    [ [%bin_shape: int]
    ; [%bin_shape: string]
    ; [%bin_shape: float]
    ; [%bin_shape: int * int]
    ; [%bin_shape: int * string]
    ; [%bin_shape: string * int]
    ; [%bin_shape: string * string]
    ; [%bin_shape: int * int * int]
    ; [%bin_shape: (int * int) * int]
    ; [%bin_shape: int * (int * int)]
    ]
;;

(* deriving for simple type_declaration *)
type my_int = int [@@deriving bin_shape]

let%test _ = [%bin_shape: my_int] = [%bin_shape: int]
let%test _ = [%bin_shape: my_int] = bin_shape_my_int

(* deriving in structures/signatures *)
module My_int : sig
  type t = int [@@deriving bin_shape]
end = struct
  type t = int [@@deriving bin_shape]
end

(* Tvars in signatures.. *)
module Signatures = struct
  module type P1_sig = sig
    type 'a t = 'a * int [@@deriving bin_shape]
  end

  module P1 : P1_sig = struct
    type 'a t = 'a * int [@@deriving bin_shape]
  end

  module P2 : sig
    type ('a, 'b) t = 'a * int * 'b [@@deriving bin_shape]
  end = struct
    type ('a, 'b) t = 'a * int * 'b [@@deriving bin_shape]
  end
end

let%test _ = [%bin_shape: My_int.t] = [%bin_shape: int]
let%test _ = [%bin_shape: My_int.t] = My_int.bin_shape_t

(* tuples *)
type ipair = int * int [@@deriving bin_shape]

let%test _ = [%bin_shape: ipair] = bin_shape_ipair
let%test _ = [%bin_shape: ipair] = [%bin_shape: int * int]

type ipair2 = int * my_int [@@deriving bin_shape]

let%test _ = [%bin_shape: ipair2] = [%bin_shape: ipair]

(* type variables and applications *)
type 'a pair = 'a * 'a [@@deriving bin_shape]
type 'a i_pair = int * 'a [@@deriving bin_shape]
type 'a pair_i = 'a * int [@@deriving bin_shape]
type ('a, 'b) gpair = 'a * 'b [@@deriving bin_shape]
type ('a, 'b) gpairR = 'b * 'a [@@deriving bin_shape]

(* alias type definitions are expanded *)
let%test_unit _ =
  ensure_shape [%bin_shape: int pair] ~expect:Canonical.(create (tuple [ int; int ]))
;;

let%test_unit _ = ensure_all_different [ [%bin_shape: int]; [%bin_shape: int * int] ]

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: int * int]
    ; [%bin_shape: int pair]
    ; [%bin_shape: int i_pair]
    ; [%bin_shape: int pair_i]
    ; [%bin_shape: (int, int) gpair]
    ; [%bin_shape: (int, int) gpairR]
    ]
;;

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: int * string]
    ; [%bin_shape: string i_pair]
    ; [%bin_shape: (int, string) gpair]
    ; [%bin_shape: (string, int) gpairR]
    ]
;;

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: string * int]
    ; [%bin_shape: string pair_i]
    ; [%bin_shape: (string, int) gpair]
    ; [%bin_shape: (int, string) gpairR]
    ]
;;

let%test_unit _ =
  ensure_all_same [ [%bin_shape: int * (int * int)]; [%bin_shape: (int, int pair) gpair] ]
;;

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: (int * int) * (int * int)]
    ; [%bin_shape: (int pair, int pair) gpair]
    ; [%bin_shape: int pair pair]
    ]
;;

let%test_unit _ =
  ensure_shape
    [%bin_shape: int pair pair]
    ~expect:Canonical.(create (tuple [ tuple [ int; int ]; tuple [ int; int ] ]))
;;

(* records *)

type r1 =
  { i : int
  ; s : string
  }
[@@deriving bin_shape]

type 'a id = 'a [@@deriving bin_shape]
type r1_copy1 = r1 [@@deriving bin_shape]
type r1_copy2 = r1 id [@@deriving bin_shape]

type r1_redef =
  { i : int
  ; s : string
  }
[@@deriving bin_shape]

type 'a abstracted_r1 =
  { i : 'a
  ; s : string
  }
[@@deriving bin_shape]

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: r1]
    ; [%bin_shape: r1_copy1]
    ; [%bin_shape: r1_copy2]
    ; [%bin_shape: r1_redef]
    ]
;;

let%test_unit _ = ensure_all_same [ [%bin_shape: r1]; [%bin_shape: int abstracted_r1] ]

let%test_unit _ =
  ensure_shape
    [%bin_shape: r1]
    ~expect:Canonical.(create (record [ "i", int; "s", string ]))
;;

type r2 =
  { i : int
  ; s : int
  }
[@@deriving bin_shape]

type r3 =
  { i : int * int
  ; s : string
  }
[@@deriving bin_shape]

type r4 =
  { ii : int
  ; s : string
  }
[@@deriving bin_shape]

type r5 =
  { i : int
  ; ss : string
  }
[@@deriving bin_shape]

type r6 =
  { i : int
  ; s : string
  ; x : int
  }
[@@deriving bin_shape]

type r7 =
  { s : string
  ; i : int
  }
[@@deriving bin_shape]

let%test_unit _ =
  ensure_all_different
    [ [%bin_shape: int * string]
    ; [%bin_shape: string * int]
    ; [%bin_shape: r1]
    ; [%bin_shape: string abstracted_r1]
    ; [%bin_shape: r2]
    ; [%bin_shape: r3]
    ; [%bin_shape: r4]
    ; [%bin_shape: r5]
    ; [%bin_shape: r6]
    ; [%bin_shape: r7]
    ]
;;

module What_about_this_one = struct
  type r1 = { i : int } [@@deriving bin_shape]
  type r2 = { i : int } [@@deriving bin_shape]
  type r1_r1 = r1 * r1 [@@deriving bin_shape]
  type r1_r2 = r1 * r2 [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: r1_r1]; [%bin_shape: r1_r2] ]

  (* for what it's worth, these do actually satisfy the same signature: *)
  module type S = sig
    type r1 = { i : int }
    type r2 = { i : int }
    type r1_r2 = r1 * r2
  end

  module S1 : S = struct
    type nonrec r1 = r1 = { i : int }
    type nonrec r2 = r2 = { i : int }
    type r1_r2 = r1 * r2
  end

  module S2 : S = struct
    type nonrec r1 = r1 = { i : int }
    type r2 = r1 = { i : int }
    type r1_r2 = r1 * r2
  end
end

module Variants = struct
  type v1 =
    | E1
    | E2
  [@@deriving bin_shape]

  type v2 =
    | E1
    | E2 of int
  [@@deriving bin_shape]

  type v3 =
    | XE1
    | E2 of int
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different [ [%bin_shape: v1]; [%bin_shape: v2]; [%bin_shape: v3] ]
  ;;

  type v2_copy1 =
    | E1
    | E2 of int
  [@@deriving bin_shape]

  type v2_copy2 =
    | E1
    | E2 of my_int
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_same
      [ [%bin_shape: v2]
      ; [%bin_shape: v2_copy1]
      ; [%bin_shape: v2_copy2]
      ; [%bin_shape: v2_copy2 id]
      ]
  ;;

  type a1 =
    | A1
    | A2
  [@@deriving bin_shape]

  type a2 =
    | A2
    | A1
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: a1]; [%bin_shape: a2] ]

  type b1 = A of int * int [@@deriving bin_shape]
  type b2 = A of (int * int) [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: b1]; [%bin_shape: b2] ]
end

module Empty_types = struct
  type t1 = Nothing.t [@@deriving bin_shape]
  type t2 [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: t1]; [%bin_shape: t2] ]
end

module Recursive_types = struct
  type nat =
    | Zero
    | Succ of nat
  [@@deriving bin_shape]

  type nat1 =
    | Zero
    | Succ of nat
  [@@deriving bin_shape]

  type nat2 =
    | Zero
    | Succ of nat2
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different [ [%bin_shape: int]; [%bin_shape: nat]; [%bin_shape: nat1] ]
  ;;

  let%test_unit _ = ensure_all_same [ [%bin_shape: nat]; [%bin_shape: nat2] ]
end

module Mutually_recursive_types = struct
  type t1 =
    | TT of t1
    | TU of u1
    | TB

  and u1 =
    | UT of t1
    | UU of u1
    | UB
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: t1]; [%bin_shape: u1] ]

  type u2 =
    | UT of t2
    | UU of u2
    | UB

  (* swap order: t,u *)
  and t2 =
    | TT of t2
    | TU of u2
    | TB
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: t1]; [%bin_shape: t2] ]
  let%test_unit _ = ensure_all_same [ [%bin_shape: u1]; [%bin_shape: u2] ]

  type t3 =
    | TT of u3
    | TU of t3
    | TB

  (* swap types w.r.t. t1 *)
  and u3 =
    | UT of t3
    | UU of u3
    | UB
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different
      [ [%bin_shape: t1]; [%bin_shape: t3]; [%bin_shape: u1]; [%bin_shape: u3] ]
  ;;
end

module Blowup1 = struct
  (* No blowup in code here, but the values constructed do increase in size exponentially *)
  type t0 = int [@@deriving bin_shape]
  type t1 = t0 * t0 [@@deriving bin_shape]
  type t2 = t1 * t1 [@@deriving bin_shape]
  type t3 = t2 * t2 [@@deriving bin_shape]
  type t4 = t3 * t3 [@@deriving bin_shape]
end

module Blowup2 = struct
  (* Exponential blowup in code size here *)
  type t0 = int
  and t1 = t0 * t0
  and t2 = t1 * t1
  and t3 = t2 * t2
  and t4 = t3 * t3 [@@deriving bin_shape]
end

module Blowup3 = struct
  (* Reverse the order of the decs...*)
  (* Still have exponential blowup in code size here *)
  type t4 = t3 * t3
  and t3 = t2 * t2
  and t2 = t1 * t1
  and t1 = t0 * t0
  and t0 = int [@@deriving bin_shape]
end

module Blowup4 = struct
  type q1 = unit [@@deriving bin_shape]
  type q2 = q1 * q1 [@@deriving bin_shape]
  type q3 = q2 * q2 [@@deriving bin_shape]
  type q4 = q3 * q3 [@@deriving bin_shape]
  type q5 = q4 * q4 [@@deriving bin_shape]
  type q6 = q5 * q5 [@@deriving bin_shape]
  type q7 = q6 * q6 [@@deriving bin_shape]
  type q8 = q7 * q7 [@@deriving bin_shape]
  type q9 = q8 * q8 [@@deriving bin_shape]
  type q10 = q9 * q9 [@@deriving bin_shape]
  type q11 = q10 * q10 [@@deriving bin_shape]
  type q12 = q11 * q11 [@@deriving bin_shape]
  type q13 = q12 * q12 [@@deriving bin_shape]
  type q14 = q13 * q13 [@@deriving bin_shape]
  type q15 = q14 * q14 [@@deriving bin_shape]
end

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: Blowup1.t4]; [%bin_shape: Blowup2.t4]; [%bin_shape: Blowup3.t4] ]
;;

module Tricky_mutual_recursion = struct
  module A = struct
    type t1 = A of t1
    and t2 = B of t1 * t2 [@@deriving bin_shape]
  end

  module B = struct
    type t0 = t1
    and t1 = A of t0
    and t2 = B of t1 * t2 [@@deriving bin_shape]
  end

  let%test_unit _ = ensure_all_same [ [%bin_shape: A.t2]; [%bin_shape: B.t2] ]
end

(* examples which make use of predefined list *)
module List_example = struct
  let%test_unit _ =
    ensure_all_different
      [ [%bin_shape: int]
      ; [%bin_shape: int list]
      ; [%bin_shape: float list]
      ; [%bin_shape: int list list]
      ]
  ;;

  type 'a copy_predef_list = 'a list [@@deriving bin_shape]
  type my_int_predef_list = int list [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_same
      [ [%bin_shape: int list]
      ; [%bin_shape: int copy_predef_list]
      ; [%bin_shape: my_int_predef_list]
      ]
  ;;
end

(* examples which make use of mylist *)
module My_list = struct
  type 'a mylist =
    | Nil
    | Cons of 'a * 'a mylist
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different [ [%bin_shape: int list]; [%bin_shape: int mylist] ]
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: int mylist]
      ~expect:
        Canonical.(
          create
            (apply
               (dvariant [ "Nil", []; "Cons", [ var 0; recurse 0 [ var 0 ] ] ])
               [ int ]))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: int mylist mylist]
      ~expect:
        Canonical.(
          create
            (let mylist =
               dvariant [ "Nil", []; "Cons", [ var 0; recurse 0 [ var 0 ] ] ]
             in
             apply mylist [ apply mylist [ int ] ]))
  ;;
end

(* Support for predefined types... *)
let%test_unit _ =
  ensure_all_different
    [ [%bin_shape: unit]
    ; [%bin_shape: bool]
    ; [%bin_shape: string]
    ; [%bin_shape: char]
    ; [%bin_shape: float]
    ; [%bin_shape: int32]
    ; [%bin_shape: int64]
    ]
;;

let%test_unit _ = ensure_shape [%bin_shape: unit] ~expect:Canonical.(create unit)
let%test_unit _ = ensure_shape [%bin_shape: bool] ~expect:Canonical.(create bool)

let%test_unit _ =
  ensure_shape
    [%bin_shape: char option array]
    ~expect:Canonical.(create (array (option char)))
;;

(* Support for predefined type constructors... *)
let%test_unit _ =
  ensure_all_different
    [ [%bin_shape: unit]
    ; [%bin_shape: unit ref]
    ; [%bin_shape: unit option]
    ; [%bin_shape: unit list]
    ; [%bin_shape: unit array]
    ]
;;

(* being lazy does not change the shape *)
let%test_unit _ = ensure_all_same [ [%bin_shape: unit]; [%bin_shape: unit lazy_t] ]

(* examples which make use of predefined array *)
module Array = struct
  let%test_unit _ =
    ensure_all_different
      [ [%bin_shape: int]
      ; [%bin_shape: int array]
      ; [%bin_shape: float array]
      ; [%bin_shape: int array array]
      ]
  ;;

  type 'a copy_predef_array = 'a array [@@deriving bin_shape]
  type my_int_predef_array = int array [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_same
      [ [%bin_shape: int array]
      ; [%bin_shape: int copy_predef_array]
      ; [%bin_shape: my_int_predef_array]
      ]
  ;;
end

module Non_regular = struct
  (* Types [a] and [b] make no `base' use of their polymorphic variable ['a] *)
  type 'a a = A of 'a a [@@deriving bin_shape]

  (* But we regard applications to different types as having different shapes *)
  let%test_unit _ = ensure_all_different [ [%bin_shape: int a]; [%bin_shape: float a] ]

  (* This is because of our handling of recursion which is general enough to handle non
     regular recursion *)

  let%test_unit _ =
    ensure_shape
      [%bin_shape: int a]
      ~expect:
        Canonical.(create (apply (dvariant [ "A", [ recurse 0 [ var 0 ] ] ]) [ int ]))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: float a]
      ~expect:
        Canonical.(create (apply (dvariant [ "A", [ recurse 0 [ var 0 ] ] ]) [ float ]))
  ;;

  (* And here is an example of non-regular recursion *)
  type 'a b = B of 'a list b [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: int b]; [%bin_shape: float b] ]

  let%test_unit _ =
    ensure_shape
      [%bin_shape: int b]
      ~expect:
        Canonical.(
          create (apply (dvariant [ "B", [ recurse 0 [ list (var 0) ] ] ]) [ int ]))
  ;;
end

module Polymorphism_and_recursion = struct
  (* example from Valentin *)

  type 'a t = A of 'a t list [@@deriving bin_shape]
  type u1 = int t [@@deriving bin_shape]
  type u2 = A of u2 list [@@deriving bin_shape]

  let%test_unit _ =
    ensure_shape
      [%bin_shape: u1]
      ~expect:
        Canonical.(
          create (apply (dvariant [ "A", [ list (recurse 0 [ var 0 ]) ] ]) [ int ]))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: u2]
      ~expect:Canonical.(create (apply (dvariant [ "A", [ list (recurse 0 []) ] ]) []))
  ;;

  (* So these types are not equivalent. Which is a shame. *)
  let%test_unit _ = ensure_all_different [ [%bin_shape: u1]; [%bin_shape: u2] ]
end

module Polymorphic_variants = struct
  type v0 = [ `E1 ] [@@deriving bin_shape]

  type v1 =
    [ `E1
    | `E2
    ]
  [@@deriving bin_shape]

  type v2 =
    [ `E1
    | `E2 of int
    ]
  [@@deriving bin_shape]

  type v3 =
    [ `XE1
    | `E2 of int
    ]
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different
      [ [%bin_shape: v0]
      ; [%bin_shape: v1]
      ; [%bin_shape: v2]
      ; [%bin_shape: v3]
      ; [%bin_shape: Variants.v1]
      ]
  ;;

  type v4 =
    [ `E1
    | `E2
    ]
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: v1]; [%bin_shape: v4] ]

  (* Polymorphic_variants allow reordering (unlike normal variants) *)
  type v5 =
    [ `E2
    | `E1
    ]
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: v1]; [%bin_shape: v5] (* reordered *) ]

  let%test_unit _ =
    ensure_shape
      [%bin_shape: v1]
      ~expect:Canonical.(create (poly_variant [ "E1", None; "E2", None ]))
  ;;

  (* Support for [Rinherit] *)

  type v6 = [ | v1 ] [@@deriving bin_shape]

  type v7 =
    [ v0
    | `E2
    ]
  [@@deriving bin_shape]

  type v8 =
    [ v1
    | `E2
    ]
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_shape
      [%bin_shape: v6]
      ~expect:Canonical.(create (poly_variant [ "E1", None; "E2", None ]))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: v7]
      ~expect:Canonical.(create (poly_variant [ "E1", None; "E2", None ]))
  ;;

  let%test_unit _ =
    ensure_all_same
      [ [%bin_shape: v1]; [%bin_shape: v6]; [%bin_shape: v7]; [%bin_shape: v8] ]
  ;;

  type q =
    [ `a
    | `b
    | `c
    ]
  [@@deriving bin_shape]

  type q1 =
    [ `a
    | `b
    ]
  [@@deriving bin_shape]

  type q2 =
    [ `b
    | `c
    ]
  [@@deriving bin_shape]

  type q3 =
    [ q1
    | q2
    ]
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: q]; [%bin_shape: q3] ]
end

module Parameters_and_orders = struct
  type a1 = A of a3
  and a2 = B of a1
  and a3 = C of a2 [@@deriving bin_shape]

  type b3 = C of b2
  and b2 = B of b1
  and b1 = A of b3 [@@deriving bin_shape]

  type ('a, 'b) c1 = 'a * 'b [@@deriving bin_shape]
  type ('b, 'a) c2 = 'a * 'b [@@deriving bin_shape]
  type t1 = (b1, b2) c1 [@@deriving bin_shape]
  type t2 = (b2, b1) c2 [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: t1]; [%bin_shape: t2] ]
end

module Recursive_polymorphic_variant = struct
  type t1 =
    [ `A
    | `B of t1
    ]
  [@@deriving bin_shape]

  let%test_unit _ =
    let (_ : Shape.Digest.t) = Shape.eval_to_digest bin_shape_t1 in
    ()
  ;;
end

module Inheriting_from_recursive_polymorphic_variant = struct
  let%test_unit _ =
    expect_raise (fun () ->
      let module M = struct
        type t1 =
          [ `A
          | `B of t1
          ]
        [@@deriving bin_shape]

        type t2 =
          [ t1
          | `C
          ]
        [@@deriving bin_shape]
      end
      in
      let _ = Shape.eval [%bin_shape: M.t2] in
      ())
  ;;
end

module Inheriting_from_recursive_polymorphic_variant2 = struct
  let%test_unit _ =
    expect_raise (fun () ->
      let module M = struct
        type t1 =
          [ `A
          | `B of t1
          ]
        [@@deriving bin_shape]

        type t2 =
          [ `Q
          | `P of t2
          | t1
          ]
        [@@deriving bin_shape]

        type t3 =
          [ t2
          | `C
          ]
        [@@deriving bin_shape]
      end
      in
      let _ = Shape.eval [%bin_shape: M.t3] in
      ())
  ;;
end

module Unrolling_bad_0 = struct
  let%test_unit _ =
    let module _ = struct
      type t1 =
        [ `A
        | `B of t1
        ]
      [@@deriving bin_shape]

      type t2 =
        [ `A
        | `B of [ `A | `B of t2 ]
        ]
      [@@deriving bin_shape]

      let () =
        ensure_shape
          [%bin_shape: t1]
          ~expect:
            Canonical.(
              create
                (apply (define (poly_variant [ "A", None; "B", Some (recurse 0 []) ])) []))
      ;;

      let () =
        ensure_shape
          [%bin_shape: t2]
          ~expect:
            Canonical.(
              create
                (apply
                   (define
                      (poly_variant
                         [ "A", None
                         ; ( "B"
                           , Some (poly_variant [ "A", None; "B", Some (recurse 0 []) ]) )
                         ]))
                   []))
      ;;

      let () = ensure_all_different [ [%bin_shape: t1]; [%bin_shape: t2] ]
    end
    in
    ()
  ;;
end

let%test_unit _ =
  let module _ = struct
    type t = [ `A of t ] [@@deriving bin_shape]
    type u = [ `A of u ] [@@deriving bin_shape]

    let () = ensure_all_same [ [%bin_shape: t]; [%bin_shape: u] ]
  end
  in
  ()
;;

let%test_unit _ =
  let module _ = struct
    type t = [ `A of t ] [@@deriving bin_shape]
    type u = [ `A of t ] [@@deriving bin_shape] (* like good, except: [of u] -> [of t] *)

    let () = ensure_all_different [ [%bin_shape: t]; [%bin_shape: u] ]
  end
  in
  ()
;;

module Unrolling_bad_2 = struct
  type 'a named =
    [ `A
    | `B of 'a
    ]
  [@@deriving bin_shape]

  type t1 = t1 named [@@deriving bin_shape]
  type t2 = t2 named named [@@deriving bin_shape]

  (* We get different shapes for t1 and t2, because we dont regard a type as equivalent to
     an unrolled version of itself. *)

  let%test_unit _ =
    ensure_shape
      [%bin_shape: t1]
      ~expect:
        Canonical.(
          create
            (apply (define (poly_variant [ "A", None; "B", Some (recurse 0 []) ])) []))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: t2]
      ~expect:
        Canonical.(
          create
            (apply
               (define
                  (poly_variant
                     [ "A", None
                     ; "B", Some (poly_variant [ "A", None; "B", Some (recurse 0 []) ])
                     ]))
               []))
  ;;

  let%test_unit _ = ensure_all_different [ [%bin_shape: t1]; [%bin_shape: t2] ]
end

module Tricky1 = struct
  type 'a inner =
    | Tight of 'a inner
    | Loose of 'a
  [@@deriving bin_shape]

  type outer =
    | Z
    | S of outer inner
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: int]; [%bin_shape: outer] ]
end

module Tricky_mutual = struct
  type 'a inner =
    | Tight of 'a inner
    | Loose of 'a

  and outer =
    | Z
    | S of outer inner
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: int]; [%bin_shape: outer] ]
end

module Cyclic_app = struct
  type ('a, 'b) t =
    | A of ('b, 'a) t
    | B
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_shape
      [%bin_shape: (int, string) t]
      ~expect:
        Canonical.(
          create
            (apply
               (dvariant [ "A", [ recurse 0 [ var 1; var 0 ] ]; "B", [] ])
               [ int; string ]))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: (string, int) t]
      ~expect:
        Canonical.(
          create
            (apply
               (dvariant [ "A", [ recurse 0 [ var 1; var 0 ] ]; "B", [] ])
               [ string; int ]))
  ;;

  (* So these types are not equivalent. *)
  let%test_unit _ =
    ensure_all_different [ [%bin_shape: (int, string) t]; [%bin_shape: (string, int) t] ]
  ;;
end

module Non_regular_recursion = struct
  type 'a mylist =
    | Nil
    | Cons of 'a * 'a mylist
  [@@deriving bin_shape]

  type 'a mylist2 =
    | Nil
    | Cons of 'a * string mylist2
  [@@deriving bin_shape]

  type 'a mylist3 =
    | Nil
    | Cons of 'a * float mylist3
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different
      [ [%bin_shape: int mylist]; [%bin_shape: int mylist2]; [%bin_shape: int mylist3] ]
  ;;
end

module Dub = struct
  type 'a dub = 'a * 'a [@@deriving bin_shape]

  type t0 =
    | B
    | Knot of t0
  [@@deriving bin_shape]

  type t1 =
    | B
    | Knot of t1 dub
  [@@deriving bin_shape]

  type t2 =
    | B
    | Knot of t2 dub dub
  [@@deriving bin_shape]

  type t3 =
    | B
    | Knot of t3 dub dub dub
  [@@deriving bin_shape]

  type t4 =
    | B
    | Knot of t4 dub dub dub dub
  [@@deriving bin_shape]

  let r0 = Canonical.(recurse 0 [])

  let%test_unit _ =
    ensure_shape
      [%bin_shape: t2]
      ~expect:
        Canonical.(
          create
            (apply
               (dvariant
                  [ "B", []; "Knot", [ tuple [ tuple [ r0; r0 ]; tuple [ r0; r0 ] ] ] ])
               []))
  ;;

  let%test_unit _ =
    ensure_shape
      [%bin_shape: t3]
      ~expect:
        Canonical.(
          create
            (apply
               (dvariant
                  [ "B", []
                  ; ( "Knot"
                    , [ tuple
                          [ tuple [ tuple [ r0; r0 ]; tuple [ r0; r0 ] ]
                          ; tuple [ tuple [ r0; r0 ]; tuple [ r0; r0 ] ]
                          ]
                      ] )
                  ])
               []))
  ;;

  (* Testing for specific digest values - fails if the digest scheme is changed *)

  let%test_unit _ =
    [%test_result: string]
      (eval_to_digest [%bin_shape: t3])
      ~expect:"e502ebd5fc3a340527fe4aa39b68bee5"
  ;;

  let%test_unit _ =
    [%test_result: string]
      (eval_to_digest [%bin_shape: t4])
      ~expect:"a66734cfcaea50f37e04cd706cf9c7d0"
  ;;

  (* Use [bin_digest..] function/extension *)
  let%test_unit _ =
    [%test_result: string] [%bin_digest: t4] ~expect:"a66734cfcaea50f37e04cd706cf9c7d0"
  ;;

  module By_hand = struct
    module type Unary_with_shape = sig
      type 'a t [@@deriving bin_shape]
    end

    let add_dub (module M : Unary_with_shape) : (module Unary_with_shape) =
      (module struct
        type 'a t = 'a M.t dub [@@deriving bin_shape]
      end)
    ;;

    let dubs n : (module Unary_with_shape) =
      Fn.apply_n_times
        ~n
        add_dub
        (module struct
          type 'a t = 'a [@@deriving bin_shape]
        end)
    ;;

    let gen_t' n =
      let module Dubs = (val dubs n) in
      let module M = struct
        type t =
          | B
          | Knot of t Dubs.t
        [@@deriving bin_shape]

        let _f () = Knot (assert false)
        let _f () = B
      end
      in
      M.bin_shape_t
    ;;

    (* [gen_t n] constructs a type-expression for t<n>, which if tricky to
       express directly in OCaml. But possible. See [gen_t'] above. *)
    let gen_t n =
      assert (n >= 0);
      let open Shape in
      let name = Tid.of_string "any_name_will_do" in
      let _group =
        group
          (Location.of_string "group")
          [ ( name
            , []
            , variant
                [ "B", []
                ; "Knot", [ Fn.apply_n_times ~n bin_shape_dub (rec_app name []) ]
                ] )
          ]
      in
      top_app _group name []
    ;;

    let%test_unit _ = ensure_all_same [ [%bin_shape: t4]; gen_t 4; gen_t' 4 ]
  end

  let test_eval_time n allowed =
    let exp = By_hand.gen_t n in
    let before = Time_float.now () in
    let _res = Shape.eval_to_digest exp in
    let after = Time_float.now () in
    [%test_pred: Time_float.Span.t] (fun x -> x < allowed) (Time_float.diff after before)
  ;;

  let%test_unit _ = test_eval_time 15 (sec 1.)

  (* demonstate that we have no exponential blowup *)
  let%test_unit _ = test_eval_time 10000 (sec 1.)
end

(* Captures all kinds of shape *)
module Complex_type = struct
  type t0 =
    | A
    | B of t0
  [@@deriving bin_shape]

  let bin_shape_t0 = Shape.annotate "t0" bin_shape_t0

  type 'a t1 =
    | Z
    | Q of 'a t1
  [@@deriving bin_shape]

  type base_types =
    { a : unit
    ; b : bool
    ; c : string
    ; d : char
    ; f : float
    ; g : int32
    ; h : int64
    ; i : int64 ref
    ; j : int64 Lazy.t
    ; k : int64 option
    ; l : int64 list
    ; m : int64 array
    ; o : Bigstring.Stable.V1.t
    }
  [@@deriving bin_shape]

  type 'a all_stuff =
    [ `Nullary
    | `Variant of t0
    | `Record of base_types
    | `Big_tuple of int64 * string * unit * bool
    | `List of int64 list
    | `Application of int64 t1
    | `Var of 'a
    ]
  [@@deriving bin_shape]

  let%test_unit _ =
    [%test_result: string]
      (eval_to_digest [%bin_shape: bool all_stuff])
      ~expect:"23cb21f19c45331a6a0227af4eeb55f5"
  ;;
end

module Too_aggressive_memoization_1 = struct
  type t = A of t [@@deriving bin_shape]
  type t1 = A of t1 [@@deriving bin_shape]

  module A = struct
    type u = A of t [@@deriving bin_shape]

    type s =
      | T of t
      | U of u
    [@@deriving bin_shape]
  end

  module B = struct
    type u = A of t [@@deriving bin_shape]

    type s =
      | T of t1
      | U of u
    [@@deriving bin_shape]
  end

  (* This test fails if we do memoization too aggressively. *)
  let%test_unit _ = ensure_all_same [ [%bin_shape: A.s]; [%bin_shape: B.s] ]
end

module Too_aggressive_memoization_2 = struct
  type t = A of t [@@deriving bin_shape]
  type t1 = A of t [@@deriving bin_shape]

  type u1 = X of v1
  and v1 = A of u1 [@@deriving bin_shape]

  type u2 = X of t [@@deriving bin_shape]

  let%test_unit _ = ensure_all_different [ [%bin_shape: u1]; [%bin_shape: u2] ]

  module A = struct
    type s =
      | T of t
      | U of u1
    [@@deriving bin_shape]
  end

  module B = struct
    type s =
      | T of t
      | U of u2
    [@@deriving bin_shape]
  end

  (* This test fails if we do memoization too aggressively. *)
  let%test_unit _ = ensure_all_different [ [%bin_shape: A.s]; [%bin_shape: B.s] ]
end

module Example_poly_variants = struct
  type a =
    [ `qaz
    | `bar
    ]
  [@@deriving bin_shape]

  type b =
    [ `foo
    | a
    ]
  [@@deriving bin_shape]

  let exp = [%bin_shape: b]

  let expect : Canonical.t =
    let open Canonical in
    create (poly_variant [ "bar", None; "foo", None; "qaz", None ])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Example_direct_recusion = struct
  type 'a mylist =
    | Nil
    | Cons of 'a * 'a mylist
  [@@deriving bin_shape]

  let exp = [%bin_shape: int mylist]

  let expect : Canonical.t =
    let open Canonical in
    create
      (apply (dvariant [ "Nil", []; "Cons", [ var 0; recurse 0 [ var 0 ] ] ]) [ int ])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Example_block_of_3 = struct
  type 'a mylist =
    | Nil
    | Cons of 'a * 'a mylist

  and 'a and_string = 'a * string
  and t = (int * float) mylist and_string [@@deriving bin_shape]

  let exp = [%bin_shape: t]

  let expect : Canonical.t =
    let open Canonical in
    let def = dvariant [ "Nil", []; "Cons", [ var 0; recurse 0 [ var 0 ] ] ] in
    create (tuple [ apply def [ tuple [ int; float ] ]; string ])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Example_inner_outer1 = struct
  (* .. within the same mut-block *)
  type 'a inner =
    | Tight of 'a inner
    | Loose of 'a

  and outer =
    | Z of outer
    | S of outer inner
  [@@deriving bin_shape]

  let exp = [%bin_shape: outer]

  let expect : Canonical.t =
    let open Canonical in
    let inner = dvariant [ "Tight", [ recurse 1 [ var 0 ] ]; "Loose", [ var 0 ] ] in
    let outer =
      dvariant [ "Z", [ recurse 0 [] ]; "S", [ apply inner [ recurse 0 [] ] ] ]
    in
    create (apply outer [])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Example_inner_outer2 = struct
  (* .. within  sequence of mut-blocks *)
  type 'a inner =
    | Tight of 'a inner
    | Loose of 'a
  [@@deriving bin_shape]

  type outer =
    | Z of outer
    | S of outer inner
  [@@deriving bin_shape]

  let exp = [%bin_shape: outer]

  let expect : Canonical.t =
    let open Canonical in
    let inner = dvariant [ "Tight", [ recurse 1 [ var 0 ] ]; "Loose", [ var 0 ] ] in
    let outer =
      dvariant [ "Z", [ recurse 0 [] ]; "S", [ apply inner [ recurse 0 [] ] ] ]
    in
    create (apply outer [])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

let%test_unit _ =
  ensure_all_same
    [ [%bin_shape: Example_inner_outer1.outer]; [%bin_shape: Example_inner_outer2.outer] ]
;;

module Example_mut_recursion_with_extra_aliases = struct
  type t =
    | A of t1
    | B of u

  and t1 = t

  and u =
    | C of t1
    | D of u
  [@@deriving bin_shape]

  let exp = [%bin_shape: t1]

  let expect : Canonical.t =
    let open Canonical in
    let u = dvariant [ "C", [ recurse 0 [] ]; "D", [ recurse 1 [] ] ] in
    let t = dvariant [ "A", [ recurse 0 [] ]; "B", [ apply u [] ] ] in
    create (apply t [])
  ;;

  let%test_unit _ = ensure_shape exp ~expect

  module Tiny_variation = struct
    type t =
      | A of t
      | B of u

    (* changed [t1] -> [t] ...*)
    and t1 = t

    and u =
      | C of t1
      | D of u
    [@@deriving bin_shape]

    let exp_variation = [%bin_shape: t1]

    (* happily the shape is unchanged *)
    let%test_unit _ = ensure_shape exp_variation ~expect
  end

  let%test_unit _ =
    ensure_all_same
      [ [%bin_shape: t]
      ; [%bin_shape: t1]
      ; [%bin_shape: Tiny_variation.t]
      ; [%bin_shape: Tiny_variation.t1]
      ]
  ;;
end

module Tricky = struct
  type 'b u =
    | Uu of 'b u
    | Ub of 'b
  [@@deriving bin_shape]

  type 'a t =
    | Tt of 'a t
    | Ta of 'a
    | Tu of 'a u
  [@@deriving bin_shape]

  type knot =
    | Base
    | Knot of knot t
  [@@deriving bin_shape]

  let exp = [%bin_shape: knot]

  let expect : Canonical.t =
    let open Canonical in
    let u = dvariant [ "Uu", [ recurse 2 [ var 0 ] ]; "Ub", [ var 0 ] ] in
    let t =
      dvariant
        [ "Tt", [ recurse 1 [ var 0 ] ]; "Ta", [ var 0 ]; "Tu", [ apply u [ var 0 ] ] ]
    in
    let knot = dvariant [ "Base", []; "Knot", [ apply t [ recurse 0 [] ] ] ] in
    create (apply knot [])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Test_tid_clash = struct
  module Other = struct
    type 'a t =
      | Tt of 'a t
      | Ta of 'a
    [@@deriving bin_shape]
  end

  type t =
    | Base
    | Knot of t Other.t
  [@@deriving bin_shape]

  let exp = [%bin_shape: t]

  let expect : Canonical.t =
    let open Canonical in
    let other = dvariant [ "Tt", [ recurse 1 [ var 0 ] ]; "Ta", [ var 0 ] ] in
    let knot = dvariant [ "Base", []; "Knot", [ apply other [ recurse 0 [] ] ] ] in
    create (apply knot [])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Test_nested_type_application = struct
  type 'a pair = 'a * 'a [@@deriving bin_shape]
  type t = int pair pair [@@deriving bin_shape]

  let exp = [%bin_shape: int pair pair]
  let expect = Canonical.(create (tuple [ tuple [ int; int ]; tuple [ int; int ] ]))
  let%test_unit _ = ensure_shape exp ~expect
end

module Test_recursive_nested_type_application = struct
  type 'a mylist =
    | Nil
    | Cons of 'a * 'a mylist
  [@@deriving bin_shape]

  type t = int mylist mylist [@@deriving bin_shape]

  let exp = [%bin_shape: int mylist mylist]

  let expect =
    let open Canonical in
    let mylist = dvariant [ "Nil", []; "Cons", [ var 0; recurse 0 [ var 0 ] ] ] in
    create (apply mylist [ apply mylist [ int ] ])
  ;;

  let%test_unit _ = ensure_shape exp ~expect
end

module Examples_where_shape_exps_are_constructed_by_hand = struct
  module Example_mut_recursion = struct
    type t =
      | A of t
      | B of u

    and u =
      | C of t
      | D of u
    [@@deriving bin_shape]

    let exp = [%bin_shape: t]

    let expect : Canonical.t =
      let open Canonical in
      let u = dvariant [ "C", [ recurse 0 [] ]; "D", [ recurse 1 [] ] ] in
      let t = dvariant [ "A", [ recurse 0 [] ]; "B", [ apply u [] ] ] in
      create (apply t [])
    ;;

    module By_hand = struct
      let exp =
        let open Shape in
        let t = Tid.of_string "t" in
        let u = Tid.of_string "u" in
        let _group =
          group
            (Location.of_string "group")
            [ t, [], variant [ "A", [ rec_app t [] ]; "B", [ rec_app u [] ] ]
            ; u, [], variant [ "C", [ rec_app t [] ]; "D", [ rec_app u [] ] ]
            ]
        in
        top_app _group t []
      ;;

      let%test_unit _ = ensure_shape exp ~expect
    end

    let%test_unit _ = ensure_shape exp ~expect
  end

  module Example_bad = struct
    type 'a t =
      | A of int
      | B of string t
    [@@deriving bin_shape]

    type 'a s =
      | A of 'a
      | B of string s
    [@@deriving bin_shape]

    let%test_unit _ = ensure_all_different [ [%bin_shape: int t]; [%bin_shape: int s] ]
  end

  module Example_annotation = struct
    let exp =
      let open Shape in
      let foo = Tid.of_string "foo" in
      let my_int = Tid.of_string "my_int" in
      let _group =
        group
          (Location.of_string "group")
          [ foo, [], annotate "blah" (rec_app my_int []); my_int, [], bin_shape_int ]
      in
      top_app _group foo []
    ;;

    let expect : Canonical.t =
      let open Canonical in
      create (annotate "blah" int)
    ;;

    let%test_unit _ = ensure_shape exp ~expect
  end

  module Example_definition_order_1 = struct
    type t1 = A [@@deriving bin_shape]
    type t2 = B [@@deriving bin_shape]
    type t = t1 * t2 [@@deriving bin_shape]

    let exp = [%bin_shape: t]

    let expect : Canonical.t =
      let open Canonical in
      let t1 = variant [ "A", [] ] in
      let t2 = variant [ "B", [] ] in
      create (tuple [ t1; t2 ])
    ;;

    let%test_unit _ = ensure_shape exp ~expect
  end
end

module Example_definition_order_2 = struct
  type t1 = A [@@deriving bin_shape]
  type t2 = B [@@deriving bin_shape]
  type ('a, 'b) tup = 'b * 'a [@@deriving bin_shape]
  type x1 = t1 * t2 [@@deriving bin_shape]
  type x2 = (t2, t1) tup [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: x1]; [%bin_shape: x2] ]
end

module Example_definition_order_3 = struct
  type t1 = A [@@deriving bin_shape]
  type t2 = B [@@deriving bin_shape]

  type x1 =
    [ `t1 of t1
    | `t2 of t2
    ]
  [@@deriving bin_shape]

  type x2 =
    [ `t2 of t2
    | `t1 of t1
    ]
  [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: x1]; [%bin_shape: x2] ]
end

module Example_definition_order_4 = struct
  type t1 = A of t1 [@@deriving bin_shape]
  type t2 = B of t2 [@@deriving bin_shape]
  type ('a, 'b) tup = 'b * 'a [@@deriving bin_shape]
  type x1 = t1 * t2 [@@deriving bin_shape]
  type x2 = (t2, t1) tup [@@deriving bin_shape]

  let%test_unit _ = ensure_all_same [ [%bin_shape: x1]; [%bin_shape: x2] ]
end

module Annotations = struct
  module D1 = struct
    type dollars = float [@@deriving bin_io]

    let bin_shape_dollars = Shape.annotate "dollars" bin_shape_dollars

    type dollars2 = float [@@deriving bin_io]

    let bin_shape_dollars2 = Shape.annotate "dollars2" bin_shape_dollars2

    let%test_unit _ =
      ensure_all_different
        [ [%bin_shape: float]; [%bin_shape: dollars]; [%bin_shape: dollars2] ]
    ;;

    type dollars_copy = float [@@deriving bin_io]

    (* Annotations are not generative. A different type with the same annotation has the
       same shape. *)
    let bin_shape_dollars_copy = Shape.annotate "dollars" bin_shape_dollars_copy

    let%test_unit _ =
      ensure_all_same [ [%bin_shape: dollars]; [%bin_shape: dollars_copy] ]
    ;;
  end

  module D2 = struct
    (* Annotations are different from the original un-annotated type.  And different from
       records and new base types. *)
    module Orig = struct
      type t = int [@@deriving bin_io]
    end

    module Record = struct
      type t = { qaz : int } [@@deriving bin_io]
    end

    module Annotation = struct
      type t = int [@@deriving bin_io]

      let bin_shape_t = Shape.annotate "qaz" bin_shape_t
    end

    module New_base = struct
      type t = int [@@deriving bin_io]

      let bin_shape_t = Shape.basetype "qaz" [ bin_shape_t ]
    end

    let%test_unit _ =
      ensure_all_different
        [ [%bin_shape: Orig.t]
        ; [%bin_shape: Record.t]
        ; [%bin_shape: Annotation.t]
        ; [%bin_shape: New_base.t]
        ]
    ;;
  end
end

module Annotation_Syntax = struct
  module Dollars = struct
    module Without_syntax = struct
      type t = float [@@deriving bin_shape]

      let bin_shape_t = Shape.annotate "dollars" bin_shape_t
    end

    module With_syntax = struct
      type t = float [@@deriving bin_shape ~annotate:"dollars"]
    end

    let%test_unit _ =
      ensure_all_same [ [%bin_shape: Without_syntax.t]; [%bin_shape: With_syntax.t] ]
    ;;

    module Bin_io_with_annotated_shape = struct
      type t = float [@@deriving bin_io ~annotate:"dollars"]

      let%test_unit _ =
        ensure_all_same [ [%bin_shape: Without_syntax.t]; [%bin_shape: t] ]
      ;;
    end

    module Bin_io_with_annotated_shape_broken = struct
      type t = float [@@deriving bin_shape ~annotate:"dollars", bin_io]

      let%test_unit _ =
        ensure_all_different [ (* BUG *) [%bin_shape: Without_syntax.t]; [%bin_shape: t] ]
      ;;
    end
  end
end

module Test_nonrec = struct
  module A = struct
    type t = int [@@deriving bin_shape]
  end

  module B = struct
    open A

    type nonrec t = t [@@deriving bin_shape]
  end

  let%test_unit _ = ensure_all_same [ [%bin_shape: A.t]; [%bin_shape: B.t] ]
end

module Basetype_syntax = struct
  module Kind0 = struct
    module Orig = struct
      type t = int [@@deriving bin_shape]
    end

    type shapeless = Orig.t

    module Without_syntax = struct
      type t = shapeless

      let bin_shape_t = Shape.basetype "my-base" []
    end

    let%test_unit _ =
      ensure_all_different [ [%bin_shape: Orig.t]; [%bin_shape: Without_syntax.t] ]
    ;;

    module With_syntax = struct
      type t = shapeless [@@deriving bin_shape ~basetype:"my-base"]
    end

    let%test_unit _ =
      ensure_all_same [ [%bin_shape: Without_syntax.t]; [%bin_shape: With_syntax.t] ]
    ;;
  end

  module Kind1 = struct
    module Orig = struct
      type 'a t = 'a list [@@deriving bin_shape]
    end

    type 'a shapeless = 'a Orig.t

    module Without_syntax = struct
      type 'a t = 'a shapeless

      let bin_shape_t a = Shape.basetype "my-base1" [ a ]
    end

    let%test_unit _ =
      ensure_all_different
        [ [%bin_shape: int Orig.t]; [%bin_shape: int Without_syntax.t] ]
    ;;

    module With_syntax = struct
      type 'a t = 'a shapeless [@@deriving bin_shape ~basetype:"my-base1"]
    end

    let%test_unit _ =
      ensure_all_same
        [ [%bin_shape: int Without_syntax.t]; [%bin_shape: int With_syntax.t] ]
    ;;
  end
end

module Inline_records = struct
  type r1 =
    { i : int
    ; s : string
    }
  [@@deriving bin_shape]

  type t = A of r1 [@@deriving bin_shape]

  type t_using_inline_record =
    | A of
        { i : int
        ; s : string
        }
  [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_different [ [%bin_shape: t_using_inline_record]; [%bin_shape: r1] ]
  ;;

  let%test_unit _ =
    ensure_all_same [ [%bin_shape: t]; [%bin_shape: t_using_inline_record] ]
  ;;
end

module Wildcard : sig
  type _ abstract [@@deriving bin_shape]
  type _ concrete [@@deriving bin_shape]
end = struct
  type _ abstract [@@deriving bin_shape]
  type 'a concrete = 'a list [@@deriving bin_shape]

  let%test_unit _ =
    ensure_all_same [ [%bin_shape: int abstract]; [%bin_shape: string abstract] ]
  ;;

  let%test_unit _ =
    ensure_all_different [ [%bin_shape: int concrete]; [%bin_shape: string concrete] ]
  ;;
end