unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 16612965bd1896973edb5396c72976c7cb9b3680 44960 bytes (raw)
name: test/src/comp-tests.el 	 # note: path name is non-authoritative(*)

   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
 
;;; comp-tests.el --- unit tests for src/comp.c      -*- lexical-binding: t; -*-

;; Copyright (C) 2019-2021 Free Software Foundation, Inc.

;; Author: Andrea Corallo <akrl@sdf.org>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Unit tests for src/comp.c.

;;; Code:

(require 'ert)
(require 'ert-x)
(eval-when-compile
  (require 'cl-lib)
  (require 'comp)
  (defconst comp-test-src (ert-resource-file "comp-test-funcs.el"))
  (defconst comp-test-dyn-src (ert-resource-file "comp-test-funcs-dyn.el"))
  (defconst comp-test-pure-src (ert-resource-file "comp-test-pure.el"))
  (defconst comp-test-45603-src (ert-resource-file "comp-test-45603.el")))
(eval-and-compile
  (require 'comp-cstr)          ;in eval-and-compile for its defstruct
  ;; Load the test code here so the compiler can check the function
  ;; names used in this file.
  (load comp-test-src nil t)
  (load comp-test-dyn-src nil t)
  (load comp-test-pure-src nil t)
  (load comp-test-45603-src nil t))

(when (native-comp-available-p)
  (message "Compiling tests...")
  (load (native-compile comp-test-src))
  (load (native-compile comp-test-dyn-src)))

(defmacro comp-deftest (name args &rest docstring-and-body)
  "Define a test for the native compiler tagging it as :nativecomp."
  (declare (indent defun)
           (doc-string 3))
  `(ert-deftest ,(intern (concat "comp-tests-" (symbol-name name))) ,args
     :tags '(:nativecomp)
     ,@docstring-and-body))

\f

(ert-deftest comp-tests-bootstrap ()
  "Compile the compiler and load it to compile it-self.
Check that the resulting binaries do not differ."
  :tags '(:expensive-test :nativecomp)
  (ert-with-temp-file comp1-src
    :suffix "-comp-stage1.el"
    (ert-with-temp-file comp2-src
      :suffix "-comp-stage2.el"
      (let* ((byte+native-compile t)     ; FIXME HACK
             (comp-src (expand-file-name "../../../lisp/emacs-lisp/comp.el"
                                     (ert-resource-directory)))
             ;; Can't use debug symbols.
             (native-comp-debug 0))
        (copy-file comp-src comp1-src t)
        (copy-file comp-src comp2-src t)
        (let ((load-no-native t))
          (load (concat comp-src "c") nil nil t t))
        (should-not (subr-native-elisp-p (symbol-function #'native-compile)))
        (message "Compiling stage1...")
        (let* ((t0 (current-time))
               (comp1-eln (native-compile comp1-src)))
          (message "Done in %d secs" (float-time (time-since t0)))
          (load comp1-eln nil nil t t)
          (should (subr-native-elisp-p (symbol-function 'native-compile)))
          (message "Compiling stage2...")
          (let ((t0 (current-time))
                (comp2-eln (native-compile comp2-src)))
            (message "Done in %d secs" (float-time (time-since t0)))
            (message "Comparing %s %s" comp1-eln comp2-eln)
            (should (= (call-process "cmp" nil nil nil comp1-eln comp2-eln) 0))))))))

(comp-deftest provide ()
  "Testing top level provide."
  (should (featurep 'comp-test-funcs)))

(comp-deftest varref ()
  "Testing varref."
  (should (= (comp-tests-varref-f) 3)))

(comp-deftest list ()
  "Testing cons car cdr."
  (should (equal (comp-tests-list-f) '(1 2 3)))
  (should (equal (comp-tests-list2-f 1 2 3) '(1 2 3)))
  (should (= (comp-tests-car-f '(1 . 2)) 1))
  (should (null (comp-tests-car-f nil)))
  (should-error (comp-tests-car-f 3)
                :type 'wrong-type-argument)
  (should (= (comp-tests-cdr-f '(1 . 2)) 2))
  (should (null (comp-tests-cdr-f nil)))
  (should-error (comp-tests-cdr-f 3)
                :type 'wrong-type-argument)
  (should (= (comp-tests-car-safe-f '(1 . 2)) 1))
  (should (null (comp-tests-car-safe-f 'a)))
  (should (= (comp-tests-cdr-safe-f '(1 . 2)) 2))
  (should (null (comp-tests-cdr-safe-f 'a))))

(comp-deftest comp-tests-cons-car-cdr ()
  "Testing cons car cdr."
  (should (= (comp-tests-cons-car-f) 1))
  (should (= (comp-tests-cons-cdr-f 3) 3)))

(comp-deftest varset ()
  "Testing varset."
  (comp-tests-varset0-f)
  (should (= comp-tests-var1 55))

  (should (= (comp-tests-varset1-f) 4))
  (should (= comp-tests-var1 66)))

(comp-deftest length ()
  "Testing length."
  (should (= (comp-tests-length-f) 3)))

(comp-deftest aref-aset ()
  "Testing aref and aset."
  (should (= (comp-tests-aref-aset-f) 100)))

(comp-deftest symbol-value ()
  "Testing aref and aset."
  (should (= (comp-tests-symbol-value-f) 3)))

(comp-deftest concat ()
  "Testing concatX opcodes."
  (should (string= (comp-tests-concat-f "bar") "abcdabcabfoobar")))

(comp-deftest ffuncall ()
  "Test calling conventions."

  ;; (defun comp-tests-ffuncall-caller-f ()
  ;;   (comp-tests-ffuncall-callee-f 1 2 3))

  ;; (should (equal (comp-tests-ffuncall-caller-f) '(1 2 3)))

  ;; ;; After it gets compiled
  ;; (native-compile #'comp-tests-ffuncall-callee-f)
  ;; (should (equal (comp-tests-ffuncall-caller-f) '(1 2 3)))

  ;; ;; Recompiling the caller once with callee already compiled
  ;; (defun comp-tests-ffuncall-caller-f ()
  ;;   (comp-tests-ffuncall-callee-f 1 2 3))
  ;; (should (equal (comp-tests-ffuncall-caller-f) '(1 2 3)))

  (should (equal (comp-tests-ffuncall-callee-optional-f 1 2 3 4)
                 '(1 2 3 4)))
  (should (equal (comp-tests-ffuncall-callee-optional-f 1 2 3)
                 '(1 2 3 nil)))
  (should (equal (comp-tests-ffuncall-callee-optional-f 1 2)
                 '(1 2 nil nil)))

  (should (equal (comp-tests-ffuncall-callee-rest-f 1 2)
                 '(1 2 nil)))
  (should (equal (comp-tests-ffuncall-callee-rest-f 1 2 3)
                 '(1 2 (3))))
  (should (equal (comp-tests-ffuncall-callee-rest-f 1 2 3 4)
                 '(1 2 (3 4))))

  (should (equal (comp-tests-ffuncall-callee-more8-f 1 2 3 4 5 6 7 8 9 10)
                 '(1 2 3 4 5 6 7 8 9 10)))

  (should (equal (comp-tests-ffuncall-callee-more8-rest-f 1 2 3 4 5 6 7 8 9 10 11)
                 '(1 2 3 4 5 6 7 8 9 (10 11))))

  (should (equal (comp-tests-ffuncall-native-f) [nil]))

  (should (equal (comp-tests-ffuncall-native-rest-f) [1 2 3]))

  (should (equal (comp-tests-ffuncall-apply-many-f '(1 2 3))
                 '(1 2 3)))

  (should (= (comp-tests-ffuncall-lambda-f 1) 2)))

(comp-deftest jump-table ()
  "Testing jump tables"
  (should (eq (comp-tests-jump-table-1-f 'x) 'a))
  (should (eq (comp-tests-jump-table-1-f 'y) 'b))
  (should (eq (comp-tests-jump-table-1-f 'xxx) 'c))

  ;; Jump table not with eq as test
  (should (eq (comp-tests-jump-table-2-f "aaa") 'a))
  (should (eq (comp-tests-jump-table-2-f "bbb") 'b)))

(comp-deftest conditionals ()
  "Testing conditionals."
  (should (= (comp-tests-conditionals-1-f t) 1))
  (should (= (comp-tests-conditionals-1-f nil) 2))
  (should (= (comp-tests-conditionals-2-f t) 1340))
  (should (eq (comp-tests-conditionals-2-f nil) nil)))

(comp-deftest fixnum ()
  "Testing some fixnum inline operation."
  (should (= (comp-tests-fixnum-1-minus-f 10) 9))
  (should (= (comp-tests-fixnum-1-minus-f most-negative-fixnum)
             (1- most-negative-fixnum)))
  (should-error (comp-tests-fixnum-1-minus-f 'a)
                :type 'wrong-type-argument)
  (should (= (comp-tests-fixnum-1-plus-f 10) 11))
  (should (= (comp-tests-fixnum-1-plus-f most-positive-fixnum)
             (1+ most-positive-fixnum)))
  (should-error (comp-tests-fixnum-1-plus-f 'a)
                :type 'wrong-type-argument)
  (should (= (comp-tests-fixnum-minus-f 10) -10))
  (should (= (comp-tests-fixnum-minus-f most-negative-fixnum)
             (- most-negative-fixnum)))
  (should-error (comp-tests-fixnum-minus-f 'a)
                :type 'wrong-type-argument))

(comp-deftest type-hints ()
  "Just test compiler hints are transparent in this case."
  ;; FIXME we should really check they are also effective.
  (should (= (comp-tests-hint-fixnum-f 3) 4))
  (should (= (comp-tests-hint-cons-f (cons 1 2)) 1)))

(comp-deftest arith-comp ()
  "Testing arithmetic comparisons."
  (should (eq (comp-tests-eqlsign-f 4 3) nil))
  (should (eq (comp-tests-eqlsign-f 3 3) t))
  (should (eq (comp-tests-eqlsign-f 2 3) nil))
  (should (eq (comp-tests-gtr-f 4 3) t))
  (should (eq (comp-tests-gtr-f 3 3) nil))
  (should (eq (comp-tests-gtr-f 2 3) nil))
  (should (eq (comp-tests-lss-f 4 3) nil))
  (should (eq (comp-tests-lss-f 3 3) nil))
  (should (eq (comp-tests-lss-f 2 3) t))
  (should (eq (comp-tests-les-f 4 3) nil))
  (should (eq (comp-tests-les-f 3 3) t))
  (should (eq (comp-tests-les-f 2 3) t))
  (should (eq (comp-tests-geq-f 4 3) t))
  (should (eq (comp-tests-geq-f 3 3) t))
  (should (eq (comp-tests-geq-f 2 3) nil)))

(comp-deftest setcarcdr ()
  "Testing setcar setcdr."
  (should (equal (comp-tests-setcar-f '(10 . 10) 3) '(3 . 10)))
  (should (equal (comp-tests-setcdr-f '(10 . 10) 3) '(10 . 3)))
  (should-error (comp-tests-setcar-f 3 10)
                :type 'wrong-type-argument)
  (should-error (comp-tests-setcdr-f 3 10)
                :type 'wrong-type-argument))

(comp-deftest bubble-sort ()
  "Run bubble sort."
  (let* ((list1 (mapcar #'random (make-list 1000 most-positive-fixnum)))
         (list2 (copy-sequence list1)))
    (should (equal (comp-bubble-sort-f list1)
                   (sort list2 #'<)))))

(comp-deftest apply ()
  "Test some inlined list functions."
  (should (eq (comp-tests-consp-f '(1)) t))
  (should (eq (comp-tests-consp-f 1) nil))
  (let ((x (cons 1 2)))
    (should (= (comp-tests-setcar2-f x) 3))
    (should (equal x '(3 . 2)))))

(comp-deftest num-inline ()
  "Test some inlined number functions."
  (should (eq (comp-tests-integerp-f 1) t))
  (should (eq (comp-tests-integerp-f '(1)) nil))
  (should (eq (comp-tests-integerp-f 3.5) nil))
  (should (eq (comp-tests-integerp-f (1+ most-negative-fixnum)) t))

  (should (eq (comp-tests-numberp-f 1) t))
  (should (eq (comp-tests-numberp-f 'a) nil))
  (should (eq (comp-tests-numberp-f 3.5) t)))

(comp-deftest stack ()
  "Test some stack operation."
  (should (= (comp-tests-discardn-f 10) 2))
  (should (string= (with-temp-buffer
                      (comp-tests-insertn-f "a" "b" "c" "d")
                      (buffer-string))
                   "abcd")))

(comp-deftest non-locals ()
  "Test non locals."
  (should (string= (comp-tests-condition-case-0-f)
                   "arith-error Arithmetic error catched"))
  (should (string= (comp-tests-condition-case-1-f)
                   "error Foo catched"))
  (should (= (comp-tests-catch-f
              (lambda () (throw 'foo 3)))
             3))
  (should (= (catch 'foo
               (comp-tests-throw-f 3)))))

(comp-deftest gc ()
  "Try to do some longer computation to let the GC kick in."
  (dotimes (_ 100000)
    (comp-tests-cons-cdr-f 3))
  (should (= (comp-tests-cons-cdr-f 3) 3)))

(comp-deftest buffer ()
  (should (string= (comp-tests-buff0-f) "foo")))

(comp-deftest lambda-return ()
  (let ((f (comp-tests-lambda-return-f)))
    (should (subr-native-elisp-p f))
    (should (= (funcall f 3) 4))))

(comp-deftest recursive ()
  (should (= (comp-tests-fib-f 10) 55)))

(comp-deftest macro ()
  "Just check we can define macros"
  (should (macrop (symbol-function 'comp-tests-macro-m))))

(comp-deftest string-trim ()
  (should (string= (comp-tests-string-trim-f "dsaf ") "dsaf")))

(comp-deftest trampoline-removal ()
  ;; This tests that we can call primitives with no dedicated bytecode.
  ;; At speed >= 2 the trampoline will not be used.
  (should (hash-table-p (comp-tests-trampoline-removal-f))))

(comp-deftest signal ()
  (should (equal (condition-case err
                     (comp-tests-signal-f)
                   (t err))
                 '(foo . t))))

(comp-deftest func-call-removal ()
  ;; See `comp-propagate-insn' `comp-function-call-remove'.
  (should (= (comp-tests-func-call-removal-f) 1)))

(comp-deftest doc ()
  (should (string= (documentation #'comp-tests-doc-f)
                   "A nice docstring."))
  ;; Check a preloaded function, we can't use `comp-tests-doc-f' now
  ;; as this is loaded manually with no .elc.
  (should (string-match "\\.*.elc\\'" (symbol-file #'error))))

(comp-deftest interactive-form ()
  (should (equal (interactive-form #'comp-test-interactive-form0-f)
                 '(interactive "D")))
  (should (equal (interactive-form #'comp-test-interactive-form1-f)
                 '(interactive '(1 2))))
  (should (equal (interactive-form #'comp-test-interactive-form2-f)
                 '(interactive nil)))
  (should (cl-every #'commandp '(comp-test-interactive-form0-f
                                 comp-test-interactive-form1-f
                                 comp-test-interactive-form2-f)))
  (should-not (commandp #'comp-tests-doc-f)))

(declare-function comp-tests-free-fun-f nil)

(comp-deftest free-fun ()
  "Check we are able to compile a single function."
  (eval '(defun comp-tests-free-fun-f ()
           "Some doc."
           (interactive)
           3)
        t)
  (native-compile #'comp-tests-free-fun-f)

  (should (subr-native-elisp-p (symbol-function #'comp-tests-free-fun-f)))
  (should (= (comp-tests-free-fun-f) 3))
  (should (string= (documentation #'comp-tests-free-fun-f)
                   "Some doc."))
  (should (commandp #'comp-tests-free-fun-f))
  (should (equal (interactive-form #'comp-tests-free-fun-f)
                 '(interactive))))

(declare-function comp-tests/free\fun-f nil)

(comp-deftest free-fun-silly-name ()
  "Check we are able to compile a single function."
  (eval '(defun comp-tests/free\fun-f ()) t)
  (native-compile #'comp-tests/free\fun-f)
  (should (subr-native-elisp-p (symbol-function #'comp-tests/free\fun-f))))

(comp-deftest bug-40187 ()
  "Check function name shadowing.
https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-03/msg00914.html."
  (should (eq (comp-test-40187-1-f) 'foo))
  (should (eq (comp-test-40187-2-f) 'bar)))

(comp-deftest speed--1 ()
  "Check that at speed -1 we do not native compile."
  (should (= (comp-test-speed--1-f) 3))
  (should-not (subr-native-elisp-p (symbol-function #'comp-test-speed--1-f))))

(comp-deftest bug-42360 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-07/msg00418.html>."
  (should (string= (comp-test-42360-f "Nel mezzo del " 18 0 32 "yyy" nil)
                   "Nel mezzo del     yyy")))

(comp-deftest bug-44968 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-11/msg02357.html>"
  (comp-test-44968-f "/tmp/test/foo" "/tmp"))

(comp-deftest bug-45342 ()
  "Preserve multibyte immediate strings.
<https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-12/msg01771.html>"
  (should (string= " ➊" (comp-test-45342-f 1))))

(comp-deftest assume-double-neg ()
  "In fwprop assumptions (not (not (member x))) /= (member x)."
  (should-not (comp-test-assume-double-neg-f "bar" "foo")))

(comp-deftest assume-in-loop-1 ()
  "Broken call args assumptions lead to infinite loop."
  (should (equal (comp-test-assume-in-loop-1-f "cd") '("cd"))))

(comp-deftest bug-45376-1 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-12/msg01883.html>"
  (should (equal (comp-test-45376-1-f) '(1 0))))

(comp-deftest bug-45376-2 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-12/msg01883.html>"
  (should (equal (comp-test-45376-2-f) '(0 2 1 0 1 0 1 0 0 0 0 0))))

(defvar comp-test-primitive-advice)
(comp-deftest primitive-advice ()
  "Test effectiveness of primitive advising."
  (let (comp-test-primitive-advice
        (f (lambda (&rest args)
             (setq comp-test-primitive-advice args))))
    (advice-add #'+ :before f)
    (unwind-protect
        (progn
          (should (= (comp-test-primitive-advice-f 3 4) 7))
          (should (equal comp-test-primitive-advice '(3 4))))
      (advice-remove #'+ f))))

(defvar comp-test-primitive-redefine-args)
(comp-deftest primitive-redefine ()
  "Test effectiveness of primitive redefinition."
  (cl-letf ((comp-test-primitive-redefine-args nil)
            ((symbol-function #'-)
             (lambda (&rest args)
	       (setq comp-test-primitive-redefine-args args)
               'xxx)))
    (should (eq (comp-test-primitive-redefine-f 10 2) 'xxx))
    (should (equal comp-test-primitive-redefine-args '(10 2)))))

(comp-deftest compile-forms ()
  "Verify lambda form native compilation."
  (should-error (native-compile '(+ 1 foo)))
  (let ((lexical-binding t)
        (f (native-compile '(lambda (x) (1+ x)))))
    (should (subr-native-elisp-p f))
    (should (= (funcall f 2) 3)))
  (let* ((lexical-binding nil)
         (f (native-compile '(lambda (x) (1+ x)))))
    (should (subr-native-elisp-p f))
    (should (= (funcall f 2) 3))))

(comp-deftest comp-test-defsubst ()
  ;; Bug#42664, Bug#43280, Bug#44209.
  (should-not (subr-native-elisp-p (symbol-function #'comp-test-defsubst-f))))

(comp-deftest primitive-redefine-compile-44221 ()
  "Test the compiler still works while primitives are redefined (bug#44221)."
  (cl-letf (((symbol-function #'delete-region)
             (lambda (_ _))))
    (should (subr-native-elisp-p
             (native-compile
              '(lambda ()
                 (delete-region (point-min) (point-max))))))))

(comp-deftest and-3 ()
  (should (= (comp-test-and-3-f t) 2))
  (should (null (comp-test-and-3-f '(1 2)))))

(comp-deftest copy-insn ()
  (should (equal (comp-test-copy-insn-f '(1 2 3 (4 5 6)))
                 '(1 2 3 (4 5 6))))
  (should (null (comp-test-copy-insn-f nil))))

(comp-deftest cond-rw-1 ()
  "Check cond-rw does not break target blocks with multiple predecessor."
  (should (null (comp-test-cond-rw-1-2-f))))

(comp-deftest not-cons-1 ()
  (should-not (comp-test-not-cons-f nil)))

(comp-deftest 45576-1 ()
  "Functionp satisfies also symbols.
<https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-01/msg00029.html>."
  (should (eq (comp-test-45576-f) 'eval)))

(comp-deftest 45635-1 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-01/msg00158.html>."
  (should (string= (comp-test-45635-f :height 180 :family "PragmataPro Liga")
                   "PragmataPro Liga")))

(comp-deftest 45603-1 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-12/msg01994.html>"
  (load (native-compile comp-test-45603-src))
  (should (fboundp #'comp-test-45603--file-local-name)))

(comp-deftest 46670-1 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-02/msg01413.html>"
  (should (string= (comp-test-46670-2-f "foo") "foo"))
  (should (equal (subr-type (symbol-function #'comp-test-46670-2-f))
                 '(function (t) t))))

(comp-deftest 46824-1 ()
  "<https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-02/msg01949.html>"
  (should (equal (comp-test-46824-1-f) nil)))

(comp-deftest comp-test-47868-1 ()
  "Verify string hash consing strategy.

<https://lists.gnu.org/archive/html/bug-gnu-emacs/2021-04/msg00921.html>"
  (should-not (equal-including-properties (comp-test-47868-1-f)
                                          (comp-test-47868-2-f)))
  (should (eq (comp-test-47868-1-f) (comp-test-47868-3-f)))
  (should (eq (comp-test-47868-2-f) (comp-test-47868-4-f))))

\f
;;;;;;;;;;;;;;;;;;;;;
;; Tromey's tests. ;;
;;;;;;;;;;;;;;;;;;;;;

(comp-deftest consp ()
  (should-not (comp-test-consp 23))
  (should-not (comp-test-consp nil))
  (should (comp-test-consp '(1 . 2))))

(comp-deftest listp ()
  (should-not (comp-test-listp 23))
  (should (comp-test-listp nil))
  (should (comp-test-listp '(1 . 2))))

(comp-deftest stringp ()
  (should-not (comp-test-stringp 23))
  (should-not (comp-test-stringp nil))
  (should (comp-test-stringp "hi")))

(comp-deftest symbolp ()
  (should-not (comp-test-symbolp 23))
  (should-not (comp-test-symbolp "hi"))
  (should (comp-test-symbolp 'whatever)))

(comp-deftest integerp ()
  (should (comp-test-integerp 23))
  (should-not (comp-test-integerp 57.5))
  (should-not (comp-test-integerp "hi"))
  (should-not (comp-test-integerp 'whatever)))

(comp-deftest numberp ()
  (should (comp-test-numberp 23))
  (should (comp-test-numberp 57.5))
  (should-not (comp-test-numberp "hi"))
  (should-not (comp-test-numberp 'whatever)))

(comp-deftest add1 ()
  (should (eq (comp-test-add1 23) 24))
  (should (eq (comp-test-add1 -17) -16))
  (should (eql (comp-test-add1 1.0) 2.0))
  (should-error (comp-test-add1 nil)
                :type 'wrong-type-argument))

(comp-deftest sub1 ()
  (should (eq (comp-test-sub1 23) 22))
  (should (eq (comp-test-sub1 -17) -18))
  (should (eql (comp-test-sub1 1.0) 0.0))
  (should-error (comp-test-sub1 nil)
                :type 'wrong-type-argument))

(comp-deftest negate ()
  (should (eq (comp-test-negate 23) -23))
  (should (eq (comp-test-negate -17) 17))
  (should (eql (comp-test-negate 1.0) -1.0))
  (should-error (comp-test-negate nil)
                :type 'wrong-type-argument))

(comp-deftest not ()
  (should (eq (comp-test-not 23) nil))
  (should (eq (comp-test-not nil) t))
  (should (eq (comp-test-not t) nil)))

(comp-deftest bobp-and-eobp ()
  (with-temp-buffer
    (should (comp-test-bobp))
    (should (comp-test-eobp))
    (insert "hi")
    (goto-char (point-min))
    (should (eq (comp-test-point-min) (point-min)))
    (should (eq (comp-test-point) (point-min)))
    (should (comp-test-bobp))
    (should-not (comp-test-eobp))
    (goto-char (point-max))
    (should (eq (comp-test-point-max) (point-max)))
    (should (eq (comp-test-point) (point-max)))
    (should-not (comp-test-bobp))
    (should (comp-test-eobp))))

(comp-deftest car-cdr ()
  (let ((pair '(1 . b)))
    (should (eq (comp-test-car pair) 1))
    (should (eq (comp-test-car nil) nil))
    (should-error (comp-test-car 23)
                  :type 'wrong-type-argument)
    (should (eq (comp-test-cdr pair) 'b))
    (should (eq (comp-test-cdr nil) nil))
    (should-error (comp-test-cdr 23)
                  :type 'wrong-type-argument)))

(comp-deftest car-cdr-safe ()
  (let ((pair '(1 . b)))
    (should (eq (comp-test-car-safe pair) 1))
    (should (eq (comp-test-car-safe nil) nil))
    (should (eq (comp-test-car-safe 23) nil))
    (should (eq (comp-test-cdr-safe pair) 'b))
    (should (eq (comp-test-cdr-safe nil) nil))
    (should (eq (comp-test-cdr-safe 23) nil))))

(comp-deftest eq ()
  (should (comp-test-eq 'a 'a))
  (should (comp-test-eq 5 5))
  (should-not (comp-test-eq 'a 'b)))

(comp-deftest if ()
  (should (eq (comp-test-if 'a 'b) 'a))
  (should (eq (comp-test-if 0 23) 0))
  (should (eq (comp-test-if nil 'b) 'b)))

(comp-deftest and ()
  (should (eq (comp-test-and 'a 'b) 'b))
  (should (eq (comp-test-and 0 23) 23))
  (should (eq (comp-test-and nil 'b) nil)))

(comp-deftest or ()
  (should (eq (comp-test-or 'a 'b) 'a))
  (should (eq (comp-test-or 0 23) 0))
  (should (eq (comp-test-or nil 'b) 'b)))

(comp-deftest save-excursion ()
  (with-temp-buffer
    (comp-test-save-excursion)
    (should (eq (point) (point-min)))
    (should (eq (comp-test-current-buffer) (current-buffer)))))

(comp-deftest > ()
  (should (eq (comp-test-> 0 23) nil))
  (should (eq (comp-test-> 23 0) t)))

(comp-deftest catch ()
  (should (eq (comp-test-catch 0 1 2 3 4) nil))
  (should (eq (comp-test-catch 20 21 22 23 24 25 26 27 28) 24)))

(comp-deftest memq ()
  (should (equal (comp-test-memq 0 '(5 4 3 2 1 0)) '(0)))
  (should (eq (comp-test-memq 72 '(5 4 3 2 1 0)) nil)))

(comp-deftest listN ()
  (should (equal (comp-test-listN 57)
                 '(57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57))))

(comp-deftest concatN ()
  (should (equal (comp-test-concatN "x") "xxxxxx")))

(comp-deftest opt-rest ()
  (should (equal (comp-test-opt-rest 1) '(1 nil nil)))
  (should (equal (comp-test-opt-rest 1 2) '(1 2 nil)))
  (should (equal (comp-test-opt-rest 1 2 3) '(1 2 (3))))
  (should (equal (comp-test-opt-rest 1 2 56 57 58)
                 '(1 2 (56 57 58)))))

(comp-deftest opt ()
  (should (equal (comp-test-opt 23) '(23)))
  (should (equal (comp-test-opt 23 24) '(23 . 24)))
  (should-error (comp-test-opt)
                :type 'wrong-number-of-arguments)
  (should-error (comp-test-opt nil 24 97)
                :type 'wrong-number-of-arguments))

(comp-deftest unwind-protect ()
  (comp-test-unwind-protect 'ignore)
  (should (eq comp-test-up-val 999))
  (condition-case nil
      (comp-test-unwind-protect (lambda () (error "HI")))
    (error
     nil))
  (should (eq comp-test-up-val 999)))

\f
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests for dynamic scope. ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(comp-deftest dynamic-ffuncall ()
  "Test calling convention for dynamic binding."

  (should (equal (comp-tests-ffuncall-callee-dyn-f 1 2)
                 '(1 2)))

  (should (equal (comp-tests-ffuncall-callee-opt-dyn-f 1 2 3 4)
                 '(1 2 3 4)))
  (should (equal (comp-tests-ffuncall-callee-opt-dyn-f 1 2 3)
                 '(1 2 3 nil)))
  (should (equal (comp-tests-ffuncall-callee-opt-dyn-f 1 2)
                 '(1 2 nil nil)))

  (should (equal (comp-tests-ffuncall-callee-rest-dyn-f 1 2)
                 '(1 2 nil)))
  (should (equal (comp-tests-ffuncall-callee-rest-dyn-f 1 2 3)
                 '(1 2 (3))))
  (should (equal (comp-tests-ffuncall-callee-rest-dyn-f 1 2 3 4)
                 '(1 2 (3 4))))

  (should (equal (comp-tests-ffuncall-callee-opt-rest-dyn-f 1 2)
                 '(1 2 nil nil)))
  (should (equal (comp-tests-ffuncall-callee-opt-rest-dyn-f 1 2 3)
                 '(1 2 3 nil)))
  (should (equal (comp-tests-ffuncall-callee-opt-rest-dyn-f 1 2 3 4)
                 '(1 2 3 (4)))))

(comp-deftest dynamic-arity ()
  "Test func-arity on dynamic scope functions."
  (should (equal '(2 . 2)
                 (func-arity #'comp-tests-ffuncall-callee-dyn-f)))
  (should (equal '(2 . 4)
                 (func-arity #'comp-tests-ffuncall-callee-opt-dyn-f)))
  (should (equal '(2 . many)
                 (func-arity #'comp-tests-ffuncall-callee-rest-dyn-f)))
  (should (equal '(2 . many)
                 (func-arity #'comp-tests-ffuncall-callee-opt-rest-dyn-f))))

(comp-deftest dynamic-help-arglist ()
  "Test `help-function-arglist' works on lisp/d (bug#42572)."
  (should (equal (help-function-arglist
                  (symbol-function #'comp-tests-ffuncall-callee-opt-rest-dyn-f)
                  t)
                 '(a b &optional c &rest d))))

(comp-deftest cl-macro-exp ()
  "Verify CL macro expansion (bug#42088)."
  (should (equal (comp-tests-cl-macro-exp-f) '(a b))))

(comp-deftest cl-uninterned-arg-parse-f ()
  "Verify the parsing of a lambda list with uninterned symbols (bug#42120)."
  (should (equal (comp-tests-cl-uninterned-arg-parse-f 1 2)
                 '(1 2))))

\f
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Middle-end specific tests. ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun comp-tests-mentioned-p-1 (x insn)
  (cl-loop for y in insn
           when (cond
                 ((consp y) (comp-tests-mentioned-p x y))
                 ((and (comp-mvar-p y) (comp-cstr-imm-vld-p y))
                  (equal (comp-cstr-imm y) x))
                 (t (equal x y)))
             return t))

(defun comp-tests-mentioned-p (x insn)
  "Check if X is actively mentioned in INSN."
  (unless (eq (car-safe insn)
              'comment)
    (comp-tests-mentioned-p-1 x insn)))

(defun comp-tests-map-checker (func-name checker)
  "Apply CHECKER to each insn of FUNC-NAME.
Return a list of results."
  (cl-loop
    with func-c-name = (comp-c-func-name (or func-name 'anonymous-lambda) "F" t)
    with f = (gethash func-c-name (comp-ctxt-funcs-h comp-ctxt))
    for bb being each hash-value of (comp-func-blocks f)
    nconc
    (cl-loop
     for insn in (comp-block-insns bb)
     collect (funcall checker insn))))

(defun comp-tests-tco-checker (_)
  "Check that inside `comp-tests-tco-f' we have no recursion."
  (should
   (cl-notany
    #'identity
    (comp-tests-map-checker
     'comp-tests-tco-f
     (lambda (insn)
       (or (comp-tests-mentioned-p 'comp-tests-tco-f insn)
           (comp-tests-mentioned-p (comp-c-func-name 'comp-tests-tco-f "F" t)
                                   insn)))))))

(declare-function comp-tests-tco-f nil)

(comp-deftest tco ()
  "Check for tail recursion elimination."
  (let ((native-comp-speed 3)
        ;; Disable ipa-pure otherwise `comp-tests-tco-f' gets
        ;; optimized-out.
        (comp-disabled-passes '(comp-ipa-pure))
        (comp-post-pass-hooks '((comp-tco comp-tests-tco-checker)
                                (comp-final comp-tests-tco-checker))))
    (eval '(defun comp-tests-tco-f (a b count)
             (if (= count 0)
                 b
               (comp-tests-tco-f (+ a b) a (- count 1))))
          t)
    (native-compile #'comp-tests-tco-f)
    (should (subr-native-elisp-p (symbol-function #'comp-tests-tco-f)))
    (should (= (comp-tests-tco-f 1 0 10) 55))))

(defun comp-tests-fw-prop-checker-1 (_)
  "Check that inside `comp-tests-fw-prop-f' `concat' and `length' are folded."
  (should
   (cl-notany
    #'identity
    (comp-tests-map-checker
     'comp-tests-fw-prop-1-f
     (lambda (insn)
       (or (comp-tests-mentioned-p 'concat insn)
           (comp-tests-mentioned-p 'length insn)))))))

(declare-function comp-tests-fw-prop-1-f nil)

(comp-deftest fw-prop-1 ()
  "Some tests for forward propagation."
  (let ((native-comp-speed 2)
        (comp-post-pass-hooks '((comp-final comp-tests-fw-prop-checker-1))))
    (eval '(defun comp-tests-fw-prop-1-f ()
             (let* ((a "xxx")
	            (b "yyy")
	            (c (concat a b))) ; <= has to optimize
               (length c))) ; <= has to optimize
          t)
    (native-compile #'comp-tests-fw-prop-1-f)
    (should (subr-native-elisp-p (symbol-function #'comp-tests-fw-prop-1-f)))
    (should (= (comp-tests-fw-prop-1-f) 6))))

(defun comp-tests-check-ret-type-spec (func-form ret-type)
  (let ((lexical-binding t)
        (native-comp-speed 2)
        (f-name (cl-second func-form)))
    (eval func-form t)
    (native-compile f-name)
    (should (equal (cl-third (subr-type (symbol-function f-name)))
                   ret-type))))

(cl-eval-when (compile eval load)
  (defconst comp-tests-type-spec-tests
    `(
      ;; 1
      ((defun comp-tests-ret-type-spec-f (x)
         x)
       t)

      ;; 2
      ((defun comp-tests-ret-type-spec-f ()
         1)
       (integer 1 1))

      ;; 3
      ((defun comp-tests-ret-type-spec-f (x)
         (if x 1 3))
       (or (integer 1 1) (integer 3 3)))

      ;; 4
      ((defun comp-tests-ret-type-spec-f (x)
         (let (y)
           (if x
               (setf y 1)
             (setf y 2))
           y))
       (integer 1 2))

      ;; 5
      ((defun comp-tests-ret-type-spec-f (x)
         (let (y)
           (if x
               (setf y 1)
             (setf y 3))
           y))
       (or (integer 1 1) (integer 3 3)))

      ;; 6
      ((defun comp-tests-ret-type-spec-f (x)
         (if x
             (list x)
           3))
       (or cons (integer 3 3)))

      ;; 7
      ((defun comp-tests-ret-type-spec-f (x)
         (if x
             'foo
           3))
       (or (member foo) (integer 3 3)))

      ;; 8
      ((defun comp-tests-ret-type-spec-f (x)
         (if (eq x 3)
             x
           'foo))
       (or (member foo) (integer 3 3)))

      ;; 9
      ((defun comp-tests-ret-type-spec-f (x)
         (if (eq 3 x)
             x
           'foo))
       (or (member foo) (integer 3 3)))

      ;; 10
      ((defun comp-tests-ret-type-spec-f (x)
         (if (eql x 3)
             x
           'foo))
       (or (member foo) (integer 3 3)))

      ;; 11
      ((defun comp-tests-ret-type-spec-f (x)
         (if (eql 3 x)
             x
           'foo))
       (or (member foo) (integer 3 3)))

      ;; 12
      ((defun comp-tests-ret-type-spec-f (x)
         (if (eql x 3)
             'foo
           x))
       (not (integer 3 3)))

      ;; 13
      ((defun comp-tests-ret-type-spec-f (x y)
         (if (= x y)
             x
           'foo))
       (or (member foo) marker number))

      ;; 14
      ((defun comp-tests-ret-type-spec-f (x)
         (comp-hint-fixnum x))
       (integer ,most-negative-fixnum ,most-positive-fixnum))

      ;; 15
      ((defun comp-tests-ret-type-spec-f (x)
         (comp-hint-cons x))
       cons)

      ;; 16
      ((defun comp-tests-ret-type-spec-f (x)
         (let (y)
           (when x
             (setf y 4))
           y))
       (or null (integer 4 4)))

      ;; 17
      ((defun comp-tests-ret-type-spec-f ()
         (let (x
               (y 3))
           (setf x y)
           y))
       (integer 3 3))

      ;; 18
      ((defun comp-tests-ret-type-spec-f (x)
         (let ((y 3))
           (when x
             (setf y x))
           y))
       t)

      ;; 19
      ((defun comp-tests-ret-type-spec-f (x y)
         (eq x y))
       boolean)

      ;; 20
      ((defun comp-tests-ret-type-spec-f (x)
         (when x
           'foo))
       (or (member foo) null))

      ;; 21
      ((defun comp-tests-ret-type-spec-f (x)
         (unless x
           'foo))
       (or (member foo) null))

      ;; 22
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (> x 3)
	   x))
       (or null float (integer 4 *)))

      ;; 23
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (>= x 3)
	   x))
       (or null float (integer 3 *)))

      ;; 24
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (< x 3)
	   x))
       (or null float (integer * 2)))

      ;; 25
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (<= x 3)
	   x))
       (or null float (integer * 3)))

      ;; 26
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (> 3 x)
	   x))
       (or null float (integer * 2)))

      ;; 27
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (>= 3 x)
	   x))
       (or null float (integer * 3)))

      ;; 28
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (< 3 x)
	   x))
       (or null float (integer 4 *)))

      ;; 29
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (<= 3 x)
	   x))
       (or null float (integer 3 *)))

      ;; 30
      ((defun comp-tests-ret-type-spec-f (x)
         (let ((y 3))
	   (when (> x y)
	     x)))
       (or null float (integer 4 *)))

      ;; 31
      ((defun comp-tests-ret-type-spec-f (x)
         (let ((y 3))
	   (when (> y x)
	     x)))
       (or null float (integer * 2)))

      ;; 32
      ((defun comp-tests-ret-type-spec-f (x)
         (when (and (> x 3)
		    (< x 10))
	   x))
       (or null float (integer 4 9)))

      ;; 33
      ((defun comp-tests-ret-type-spec-f (x)
         (when (or (> x 3)
                   (< x 10))
	   x))
       (or null float integer))

      ;; 34
      ((defun comp-tests-ret-type-spec-f (x)
         (when (or (< x 3)
                   (> x 10))
	   x))
       (or null float (integer * 2) (integer 11 *)))

      ;; 35 No float range support.
      ((defun comp-tests-ret-type-spec-f (x)
	 (when (> x 1.0)
	   x))
       (or null marker number))

      ;; 36
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (> x 3)
                    (> y 2))
           (+ x y)))
       (or null float (integer 7 *)))

      ;; 37
      ;; SBCL: (OR REAL NULL)
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (<= x 3)
                    (<= y 2))
           (+ x y)))
       (or null float (integer * 5)))

      ;; 38
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (< 1 x 5)
	            (< 1 y 5))
           (+ x y)))
       (or null float (integer 4 8)))

      ;; 39
      ;; SBCL gives: (OR REAL NULL)
      ((defun comp-tests-ret-type-spec-f (x y)
	 (when (and (<= 1 x 10)
		    (<= 2 y 3))
	   (+ x y)))
       (or null float (integer 3 13)))

      ;; 40
      ;; SBCL: (OR REAL NULL)
      ((defun comp-tests-ret-type-spec-f (x y)
	 (when (and (<= 1 x 10)
		    (<= 2 y 3))
	   (- x y)))
       (or null float (integer -2 8)))

      ;; 41
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (<= 1 x)
                    (<= 2 y 3))
           (- x y)))
       (or null float (integer -2 *)))

      ;; 42
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (<= 1 x 10)
                    (<= 2 y))
           (- x y)))
       (or null float (integer * 8)))

      ;; 43
      ((defun comp-tests-ret-type-spec-f (x y)
	 (when (and (<= x 10)
		    (<= 2 y))
	   (- x y)))
       (or null float (integer * 8)))

      ;; 44
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (<= x 10)
                    (<= y 3))
           (- x y)))
       (or null float integer))

      ;; 45
      ((defun comp-tests-ret-type-spec-f (x y)
         (when (and (<= 2 x)
                    (<= 3 y))
           (- x y)))
       (or null float integer))

      ;; 46
      ;; SBCL: (OR (RATIONAL (6) (30)) (SINGLE-FLOAT 6.0 30.0)
      ;;           (DOUBLE-FLOAT 6.0d0 30.0d0) NULL)
      ((defun comp-tests-ret-type-spec-f (x y z i j k)
         (when (and (< 1 x 5)
	            (< 1 y 5)
	            (< 1 z 5)
	            (< 1 i 5)
	            (< 1 j 5)
	            (< 1 k 5))
           (+ x y z i j k)))
       (or null float (integer 12 24)))

      ;; 47
      ((defun comp-tests-ret-type-spec-f (x)
         (when (<= 1 x 5)
           (1+ x)))
       (or null float (integer 2 6)))

      ;;48
      ((defun comp-tests-ret-type-spec-f (x)
         (when (<= 1 x 5)
           (1- x)))
       (or null float (integer 0 4)))

      ;; 49
      ((defun comp-tests-ret-type-spec-f ()
         (error "Foo"))
       nil)

      ;; 50
      ((defun comp-tests-ret-type-spec-f (x)
         (if (stringp x)
	     x
           'bar))
       (or (member bar) string))

      ;; 51
      ((defun comp-tests-ret-type-spec-f (x)
         (if (stringp x)
             'bar
           x))
       (not string))

      ;; 52
      ((defun comp-tests-ret-type-spec-f (x)
         (if (integerp x)
             x
           'bar))
       (or (member bar) integer))

      ;; 53
      ((defun comp-tests-ret-type-spec-f (x)
         (when (integerp x)
           x))
       (or null integer))

      ;; 54
      ((defun comp-tests-ret-type-spec-f (x)
         (unless (symbolp x)
           x))
       t)

      ;; 55
      ((defun comp-tests-ret-type-spec-f (x)
         (unless (integerp x)
           x))
       (not integer))

      ;; 56
      ((defun comp-tests-ret-type-spec-f (x)
         (cl-ecase x
           (1 (message "one"))
           (5 (message "five")))
         x)
       t
       ;; FIXME improve `comp-cond-cstrs-target-mvar' to cross block
       ;; boundary if necessary as this should return:
       ;; (or (integer 1 1) (integer 5 5))
       )

      ;; 57
      ((defun comp-tests-ret-type-spec-f (x)
         (unless (or (eq x 'foo)
	             (eql x 3))
           (error "Not foo or 3"))
         x)
       (or (member foo) (integer 3 3)))

      ;;58
      ((defun comp-tests-ret-type-spec-f (x y)
         (if (and (natnump x)
                  (natnump y)
                  (<= x y))
             x
           (error "")))
       (integer 0 *))

      ;; 59
      ((defun comp-tests-ret-type-spec-f (x y)
         (if (and (>= x 3)
                  (<= y 10)
                  (<= x y))
             x
           (error "")))
       (or float (integer 3 10)))

      ;; 60
      ((defun comp-tests-ret-type-spec-f (x y)
		    (if (and (<= x 10)
			     (>= y 3)
			     (>= x y))
             x
           (error "")))
       (or float (integer 3 10)))

      ;; 61
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (= x 1.0)
             x
           (error "")))
       (or (member 1.0) (integer 1 1)))

      ;; 62
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (= x 1.0)
             x
           (error "")))
       (or (member 1.0) (integer 1 1)))

      ;; 63
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (= x 1.1)
             x
           (error "")))
       (member 1.1))

      ;; 64
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (= x 1)
             x
           (error "")))
       (or (member 1.0) (integer 1 1)))

      ;; 65
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (= x 1)
             x
           (error "")))
       (or (member 1.0) (integer 1 1)))

      ;; 66
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (eql x 0.0)
	     x
	   (error "")))
       float)

      ;; 67
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (equal x '(1 2 3))
	     x
	   (error "")))
       cons)

      ;; 68
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (and (floatp x)
	          (= x 1))
             x
           (error "")))
       ;; Conservative (see cstr relax in `comp-cstr-=').
       (or (member 1.0) (integer 1 1)))

      ;; 69
      ((defun comp-tests-ret-type-spec-f (x)
	 (if (and (integer x)
	          (= x 1))
             x
           (error "")))
       ;; Conservative (see cstr relax in `comp-cstr-=').
       (or (member 1.0) (integer 1 1)))

      ;; 70
      ((defun comp-tests-ret-type-spec-f (x y)
	 (if (and (floatp x)
	          (integerp y)
	          (= x y))
             x
           (error "")))
       (or float integer))

      ;; 71
      ((defun comp-tests-ret-type-spec-f (x)
         (if (= x 0.0)
             x
           (error "")))
       (or (member -0.0 0.0) (integer 0 0)))

      ;; 72
      ((defun comp-tests-ret-type-spec-f (x)
         (unless (= x 0.0)
           (error ""))
         (unless (eql x -0.0)
           (error ""))
         x)
       float)

      ;; 73
      ((defun comp-tests-ret-type-spec-f (x)
         (when (eql x 1.0)
	   (error ""))
         x)
       t)))

  (defun comp-tests-define-type-spec-test (number x)
    `(comp-deftest ,(intern (format "ret-type-spec-%d" number)) ()
                   ,(format "Type specifier test number %d." number)
                   (let ((comp-ctxt (make-comp-cstr-ctxt)))
                     (comp-tests-check-ret-type-spec ',(car x) ',(cadr x))))))

(defmacro comp-tests-define-type-spec-tests ()
  "Define all type specifier tests."
  `(progn
     ,@(cl-loop
        for test in comp-tests-type-spec-tests
        for n from 1
        collect (comp-tests-define-type-spec-test n test))))

(comp-tests-define-type-spec-tests)

(defun comp-tests-pure-checker-1 (_)
  "Check that inside `comp-tests-pure-caller-f' `comp-tests-pure-callee-f' is
folded."
  (should
   (cl-notany
    #'identity
    (comp-tests-map-checker
     'comp-tests-pure-caller-f
     (lambda (insn)
       (or (comp-tests-mentioned-p 'comp-tests-pure-callee-f insn)
           (comp-tests-mentioned-p (comp-c-func-name
                                    'comp-tests-pure-callee-f "F" t)
                                   insn)))))))

(defun comp-tests-pure-checker-2 (_)
  "Check that `comp-tests-pure-fibn-f' is folded."
  (should
   (cl-notany
    #'identity
    (comp-tests-map-checker
     'comp-tests-pure-fibn-entry-f
     (lambda (insn)
       (or (comp-tests-mentioned-p 'comp-tests-pure-fibn-f insn)
           (comp-tests-mentioned-p (comp-c-func-name 'comp-tests-pure-fibn-f "F" t)
                                   insn)))))))

(comp-deftest pure ()
  "Some tests for pure functions optimization."
  (let ((native-comp-speed 3)
        (comp-post-pass-hooks '((comp-final comp-tests-pure-checker-1
                                            comp-tests-pure-checker-2))))
    (load (native-compile comp-test-pure-src))

    (should (subr-native-elisp-p (symbol-function #'comp-tests-pure-caller-f)))
    (should (= (comp-tests-pure-caller-f) 4))

    (should (subr-native-elisp-p (symbol-function #'comp-tests-pure-fibn-entry-f)))
    (should (= (comp-tests-pure-fibn-entry-f) 6765))))

(defvar comp-tests-cond-rw-checked-function nil
  "Function to be checked.")
(defun comp-tests-cond-rw-checker-val (_)
  "Check we manage to propagate the correct return value."
  (should
   (cl-some
    #'identity
    (comp-tests-map-checker
     comp-tests-cond-rw-checked-function
     (lambda (insn)
       (pcase insn
         (`(return ,mvar)
          (and (comp-cstr-imm-vld-p mvar)
               (eql (comp-cstr-imm mvar) 123)))))))))

(defvar comp-tests-cond-rw-expected-type nil
  "Type to expect in `comp-tests-cond-rw-checker-type'.")
(defun comp-tests-cond-rw-checker-type (_)
  "Check we manage to propagate the correct return type."
  (should
   (cl-some
    #'identity
    (comp-tests-map-checker
     comp-tests-cond-rw-checked-function
     (lambda (insn)
       (pcase insn
         (`(return ,mvar)
          (equal (comp-mvar-typeset mvar)
                 comp-tests-cond-rw-expected-type))))))))

;;; comp-tests.el ends here

debug log:

solving 16612965bd ...
found 16612965bd in https://yhetil.org/emacs-bugs/3377181.1637849415@pental/
found 025bc2058e in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 025bc2058ec170a9f9787a99a17d80cb1158a7a9	test/src/comp-tests.el

applying [1/1] https://yhetil.org/emacs-bugs/3377181.1637849415@pental/
diff --git a/test/src/comp-tests.el b/test/src/comp-tests.el
index 025bc2058e..16612965bd 100644

Checking patch test/src/comp-tests.el...
Applied patch test/src/comp-tests.el cleanly.

index at:
100644 16612965bd1896973edb5396c72976c7cb9b3680	test/src/comp-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).