aboutsummaryrefslogblamecommitdiffstats
path: root/lib/stdlib/test/erl_eval_SUITE.erl
blob: 6277b2c52e4538b5bca6b621bce1ae149eeb7da3 (plain) (tree)
1
2
3
4
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

                   
                                                        













                                                                         
                                                                    


















                              
                        
















                                                     
                                                    
                                                   



                                                      
                                  



                                     
                                         
         



                                                         
 

            




                         
                                     
           
                                    
           
 






























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                  






                                                                               





































































                                                                             
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1998-2010. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%

-module(erl_eval_SUITE).
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2]).

-export([guard_1/1, guard_2/1,
	 match_pattern/1,
	 match_bin/1,
	 string_plusplus/1,
	 pattern_expr/1,
         guard_3/1, guard_4/1,
         lc/1,
         simple_cases/1,
         unary_plus/1,
         apply_atom/1,
         otp_5269/1,
         otp_6539/1,
         otp_6543/1,
         otp_6787/1,
         otp_6977/1,
	 otp_7550/1,
         otp_8133/1,
         funs/1,
	 try_catch/1,
	 eval_expr_5/1,
	 zero_width/1]).

%%
%% Define to run outside of test server
%%
%%-define(STANDALONE,1).

-import(lists,[concat/1, sort/1]).

-export([count_down/2, count_down_fun/0, do_apply/2, 
         local_func/3, local_func_value/2]).

-ifdef(STANDALONE).
-define(config(A,B),config(A,B)).
-export([config/2]).
-define(line, noop, ).
config(priv_dir,_) ->
    ".".
-else.
-include_lib("test_server/include/test_server.hrl").
-export([init_per_testcase/2, end_per_testcase/2]).
% Default timetrap timeout (set in init_per_testcase).
-define(default_timeout, ?t:minutes(1)).
init_per_testcase(_Case, Config) ->
    ?line Dog = ?t:timetrap(?default_timeout),
    [{watchdog, Dog} | Config].
end_per_testcase(_Case, Config) ->
    Dog = ?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    ok.
-endif.

suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    [guard_1, guard_2, match_pattern, string_plusplus,
     pattern_expr, match_bin, guard_3, guard_4, lc,
     simple_cases, unary_plus, apply_atom, otp_5269,
     otp_6539, otp_6543, otp_6787, otp_6977, otp_7550,
     otp_8133, funs, try_catch, eval_expr_5, zero_width].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.

guard_1(doc) ->
    ["(OTP-2405)"];
guard_1(suite) ->
    [];
guard_1(Config) when is_list(Config) ->
    ?line {ok,Tokens ,_} =
	erl_scan:string("if a+4 == 4 -> yes; true -> no end. "),
    ?line {ok, [Expr]} = erl_parse:parse_exprs(Tokens),
    ?line no = guard_1_compiled(),
    ?line {value, no, []} = erl_eval:expr(Expr, []),
    ok.

guard_1_compiled() ->
    if a+4 == 4 -> yes; true -> no end.

guard_2(doc) ->
    ["Similar to guard_1, but type-correct"];
guard_2(suite) ->
    [];
guard_2(Config) when is_list(Config) ->
    ?line {ok,Tokens ,_} =
	erl_scan:string("if 6+4 == 4 -> yes; true -> no end. "),
    ?line {ok, [Expr]} = erl_parse:parse_exprs(Tokens),
    ?line no = guard_2_compiled(),
    ?line {value, no, []} = erl_eval:expr(Expr, []),
    ok.

guard_2_compiled() ->
    if 6+4 == 4 -> yes; true -> no end.

string_plusplus(doc) ->
    ["OTP-3069: syntactic sugar string ++ ..."];
string_plusplus(suite) ->
    [];
string_plusplus(Config) when is_list(Config) ->
    ?line check(fun() -> case "abc" of "ab" ++ L -> L end end,
		"case \"abc\" of \"ab\" ++ L -> L end. ",
		"c"),
    ?line check(fun() -> case "abcde" of "ab" ++ "cd" ++ L -> L end end,
		"case \"abcde\" of \"ab\" ++ \"cd\" ++ L -> L end. ",
		"e"),
    ?line check(fun() -> case "abc" of [97, 98] ++ L -> L end end,
		"case \"abc\" of [97, 98] ++ L -> L end. ",
		"c"),
    ok.

match_pattern(doc) ->
    ["OTP-2983: match operator in pattern"];
match_pattern(suite) ->
    [];
match_pattern(Config) when is_list(Config) ->
    ?line check(fun() -> case {a, b} of {a, _X}=Y -> {x,Y} end end,
		"case {a, b} of {a, X}=Y -> {x,Y} end. ",
		{x, {a, b}}),
    ?line check(fun() -> case {a, b} of Y={a, _X} -> {x,Y} end end,
		"case {a, b} of Y={a, X} -> {x,Y} end. ",
		{x, {a, b}}),
    ?line check(fun() -> case {a, b} of Y={a, _X}=Z -> {Z,Y} end end,
		"case {a, b} of Y={a, X}=Z -> {Z,Y} end. ",
		{{a, b}, {a, b}}),
    ?line check(fun() -> A = 4, B = 28, <<13:(A+(X=B))>>, X end,
                "begin A = 4, B = 28, <<13:(A+(X=B))>>, X end.",
                28),
    ok.

match_bin(doc) ->
    ["binary match problems"];
match_bin(suite) ->
    [];
match_bin(Config) when is_list(Config) ->
    ?line check(fun() -> <<"abc">> = <<"abc">> end,
		"<<\"abc\">> = <<\"abc\">>. ",
		<<"abc">>),
    ?line check(fun() ->
			<<Size,B:Size/binary,Rest/binary>> = <<2,"AB","CD">>,
			{Size,B,Rest}
		end,
		"begin <<Size,B:Size/binary,Rest/binary>> = <<2,\"AB\",\"CD\">>, "
		"{Size,B,Rest} end. ",
		{2,<<"AB">>,<<"CD">>}),
    ok.

pattern_expr(doc) ->
    ["OTP-3144: compile-time expressions in pattern"];
pattern_expr(suite) ->
    [];
pattern_expr(Config) when is_list(Config) ->
    ?line check(fun() -> case 4 of 2+2 -> ok end end,
		"case 4 of 2+2 -> ok end. ",
		ok),
    ?line check(fun() -> case 2 of +2 -> ok end end,
		"case 2 of +2 -> ok end. ",
		ok),
    ok.

guard_3(doc) ->
    ["OTP-4518."];
guard_3(suite) ->
    [];
guard_3(Config) when is_list(Config) ->
    ?line check(fun() -> if false -> false; true -> true end end,
                "if false -> false; true -> true end.",
                true),
    ?line check(fun() -> if <<"hej">> == <<"hopp">> -> true; 
                            true -> false end end,
                "begin if <<\"hej\">> == <<\"hopp\">> -> true; 
                          true -> false end end.",
                false),
    ?line check(fun() -> if <<"hej">> == <<"hej">> -> true; 
                            true -> false end end,
                "begin if <<\"hej\">> == <<\"hej\">> -> true; 
                          true -> false end end.",
                true),
    ok.

guard_4(doc) ->
    ["OTP-4885."];
guard_4(suite) ->
    [];
guard_4(Config) when is_list(Config) ->
    ?line check(fun() -> if {erlang,'+'}(3,a) -> true ; true -> false end end,
                "if {erlang,'+'}(3,a) -> true ; true -> false end.",
                false),
    ?line check(fun() -> if {erlang,is_integer}(3) -> true ; true -> false end
                end,
                "if {erlang,is_integer}(3) -> true ; true -> false end.",
                true),
    ?line check(fun() -> [X || X <- [1,2,3], erlang:is_integer(X)] end,
                "[X || X <- [1,2,3], erlang:is_integer(X)].",
                [1,2,3]),
    ?line check(fun() -> if is_atom(is_integer(a)) -> true ; true -> false end
                end,
                "if is_atom(is_integer(a)) -> true ; true -> false end.",
                true),
    ?line check(fun() -> if {erlang,is_atom}({erlang,is_integer}(a)) -> true;
                            true -> false end end,
                "if {erlang,is_atom}({erlang,is_integer}(a)) -> true; "
                "true -> false end.",
                true),
    ?line check(fun() -> if is_atom(3+a) -> true ; true -> false end end,
                "if is_atom(3+a) -> true ; true -> false end.",
                false),
    ?line check(fun() -> if erlang:is_atom(3+a) -> true ; true -> false end 
                end,
                "if erlang:is_atom(3+a) -> true ; true -> false end.",
                false),
    ok.


lc(doc) ->
    ["OTP-4518."];
lc(suite) ->
    [];
lc(Config) when is_list(Config) ->
    ?line check(fun() -> X = 32, [X || X <- [1,2,3]] end,
                "begin X = 32, [X || X <- [1,2,3]] end.",
                [1,2,3]),
    ?line check(fun() -> X = 32, 
                         [X || <<X:X>> <- [<<1:32>>,<<2:32>>,<<3:8>>]] end,
    %% "binsize variable"          ^
                "begin X = 32, 
                 [X || <<X:X>> <- [<<1:32>>,<<2:32>>,<<3:8>>]] end.",
                [1,2]),
    ?line check(fun() -> Y = 13,[X || {X,Y} <- [{1,2}]] end,
                "begin Y = 13,[X || {X,Y} <- [{1,2}]] end.",
                [1]),
    ?line error_check("begin [A || X <- [{1,2}], 1 == A] end.",
                      {unbound_var,'A'}),
    ?line error_check("begin X = 32, 
                        [{Y,W} || X <- [1,2,32,Y=4], Z <- [1,2,W=3]] end.",
                      {unbound_var,'Y'}),
    ?line error_check("begin X = 32,<<A:B>> = <<100:X>> end.",
                      {unbound_var,'B'}),
    ?line check(fun() -> [X || X <- [1,2,3,4], not (X < 2)] end,
                "begin [X || X <- [1,2,3,4], not (X < 2)] end.",
                [2,3,4]),
    ?line check(fun() -> [X || X <- [true,false], X] end,
                "[X || X <- [true,false], X].", [true]),
    ok.

simple_cases(doc) ->
    ["Simple cases, just to cover some code."];
simple_cases(suite) ->
    [];
simple_cases(Config) when is_list(Config) ->
    ?line check(fun() -> A = $C end, "A = $C.", $C),
    %% ?line check(fun() -> A = 3.14 end, "A = 3.14.", 3.14),
    ?line check(fun() -> self() ! a, A = receive a -> true end end,
                "begin self() ! a, A = receive a -> true end end.",
                true),
    ?line check(fun() -> c:flush(), self() ! a, self() ! b, self() ! c,
                         receive b -> b end, 
                         {messages, [a,c]} =
                             erlang:process_info(self(), messages),
                         c:flush() end,
                "begin c:flush(), self() ! a, self() ! b, self() ! c,"
                "receive b -> b end,"
                "{messages, [a,c]} ="
                "     erlang:process_info(self(), messages), c:flush() end.",
                ok),
    ?line check(fun() -> self() ! a, A = receive a -> true 
                                         after 0 -> false end end,
                "begin self() ! a, A = receive a -> true"
                "                      after 0 -> false end end.",
                true),
    ?line check(fun() -> c:flush(), self() ! a, self() ! b, self() ! c,
                         receive b -> b after 0 -> true end, 
                         {messages, [a,c]} =
                             erlang:process_info(self(), messages),
                         c:flush() end,
                "begin c:flush(), self() ! a, self() ! b, self() ! c,"
                "receive b -> b after 0 -> true end,"
                "{messages, [a,c]} ="
                "     erlang:process_info(self(), messages), c:flush() end.",
                ok),
    ?line check(fun() -> receive _ -> true after 10 -> false end end,
                "receive _ -> true after 10 -> false end.",
                false),
    ?line check(fun() -> F = fun(A) -> A end, true = 3 == F(3) end,
                "begin F = fun(A) -> A end, true = 3 == F(3) end.",
                true),
    ?line check(fun() -> F = fun(A) -> A end, true = 3 == apply(F, [3]) end,
                "begin F = fun(A) -> A end, true = 3 == apply(F,[3]) end.",
                true),
    ?line check(fun() -> catch throw(a) end, "catch throw(a).", a),
    ?line check(fun() -> catch a end, "catch a.", a),
    ?line check(fun() -> 4 == 3 end, "4 == 3.", false),
    ?line check(fun() -> not true end, "not true.", false),
    ?line check(fun() -> -3 end, "-3.", -3),

    ?line error_check("3.0 = 4.0.", {badmatch,4.0}),
    ?line check(fun() -> <<(3.0+2.0):32/float>> = <<5.0:32/float>> end,
                "<<(3.0+2.0):32/float>> = <<5.0:32/float>>.",
                <<5.0:32/float>>),

    ?line check(fun() -> false andalso kludd end, "false andalso kludd.",
                false),
    ?line check(fun() -> true andalso true end, "true andalso true.",
                true),
    ?line check(fun() -> true andalso false end, "true andalso false.",
                false),
    ?line check(fun() -> true andalso kludd end, "true andalso kludd.",
                kludd),
    ?line error_check("kladd andalso kludd.", {badarg,kladd}),

    ?line check(fun() -> if false andalso kludd -> a; true -> b end end,
                "if false andalso kludd -> a; true -> b end.",
                b),
    ?line check(fun() -> if true andalso true -> a; true -> b end end, 
                "if true andalso true -> a; true -> b end.",
                a),
    ?line check(fun() -> if true andalso false -> a; true -> b end end, 
                "if true andalso false -> a; true -> b end.",
                b),

    ?line check(fun() -> true orelse kludd end, 
                "true orelse kludd.", true),
    ?line check(fun() -> false orelse false end, 
                "false orelse false.", false),
    ?line check(fun() -> false orelse true end, 
                "false orelse true.", true),
    ?line check(fun() -> false orelse kludd end, 
                "false orelse kludd.", kludd),
    ?line error_check("kladd orelse kludd.", {badarg,kladd}),
    ?line error_check("[X || X <- [1,2,3], begin 1 end].",{bad_filter,1}),
    ?line error_check("[X || X <- a].",{bad_generator,a}),

    ?line check(fun() -> if true orelse kludd -> a; true -> b end end, 
                "if true orelse kludd -> a; true -> b end.", a),
    ?line check(fun() -> if false orelse false -> a; true -> b end end, 
                "if false orelse false -> a; true -> b end.", b),
    ?line check(fun() -> if false orelse true -> a; true -> b end end, 
                "if false orelse true -> a; true -> b end.", a),

    ?line check(fun() -> [X || X <- [1,2,3], X+2] end,
                "[X || X <- [1,2,3], X+2].", []),

    ?line check(fun() -> [X || X <- [1,2,3], [X] == [X || X <- [2]]] end,
                "[X || X <- [1,2,3], [X] == [X || X <- [2]]].",
                [2]),
    ?line check(fun() -> F = fun(1) -> ett; (2) -> zwei end, 
                         ett = F(1), zwei = F(2) end,
                "begin F = fun(1) -> ett; (2) -> zwei end, 
                         ett = F(1), zwei = F(2) end.",
                zwei),
    ?line check(fun() -> F = fun(X) when X == 1 -> ett; 
                                (X) when X == 2 -> zwei end, 
                         ett = F(1), zwei = F(2) end,
                "begin F = fun(X) when X == 1 -> ett; 
                              (X) when X == 2 -> zwei end, 
                         ett = F(1), zwei = F(2) end.",
                zwei),
    ?line error_check("begin F = fun(1) -> ett end, zwei = F(2) end.",
                      function_clause),
    ?line check(fun() -> if length([1]) == 1 -> yes; 
                            true -> no end end,
                "if length([1]) == 1 -> yes; 
                            true -> no end.",
                yes),
    ?line check(fun() -> if is_integer(3) -> true; true -> false end end,
                "if is_integer(3) -> true; true -> false end.", true),
    ?line check(fun() -> if integer(3) -> true; true -> false end end,
                "if integer(3) -> true; true -> false end.", true),
    ?line check(fun() -> if is_float(3) -> true; true -> false end end,
                "if is_float(3) -> true; true -> false end.", false),
    ?line check(fun() -> if float(3) -> true; true -> false end end,
                "if float(3) -> true; true -> false end.", false),
    ?line check(fun() -> if is_number(3) -> true; true -> false end end,
                "if is_number(3) -> true; true -> false end.", true),
    ?line check(fun() -> if number(3) -> true; true -> false end end,
                "if number(3) -> true; true -> false end.", true),
    ?line check(fun() -> if is_atom(a) -> true; true -> false end end,
                "if is_atom(a) -> true; true -> false end.", true),
    ?line check(fun() -> if atom(a) -> true; true -> false end end,
                "if atom(a) -> true; true -> false end.", true),
    ?line check(fun() -> if is_list([]) -> true; true -> false end end,
                "if is_list([]) -> true; true -> false end.", true),
    ?line check(fun() -> if list([]) -> true; true -> false end end,
                "if list([]) -> true; true -> false end.", true),
    ?line check(fun() -> if is_tuple({}) -> true; true -> false end end,
                "if is_tuple({}) -> true; true -> false end.", true),
    ?line check(fun() -> if tuple({}) -> true; true -> false end end,
                "if tuple({}) -> true; true -> false end.", true),
    ?line check(fun() -> if is_pid(self()) -> true; true -> false end end,
                "if is_pid(self()) -> true; true -> false end.", true),
    ?line check(fun() -> if pid(self()) -> true; true -> false end end,
                "if pid(self()) -> true; true -> false end.", true),
    ?line check(fun() -> R = make_ref(), if is_reference(R) -> true; 
                                            true -> false end end,
                "begin R = make_ref(), if is_reference(R) -> true;"
                "true -> false end end.", true),
    ?line check(fun() -> R = make_ref(), if reference(R) -> true; 
                                            true -> false end end,
                "begin R = make_ref(), if reference(R) -> true;"
                "true -> false end end.", true),
    ?line check(fun() -> if is_port(a) -> true; true -> false end end,
                "if is_port(a) -> true; true -> false end.", false),
    ?line check(fun() -> if port(a) -> true; true -> false end end,
                "if port(a) -> true; true -> false end.", false),
    ?line check(fun() -> if is_function(a) -> true; true -> false end end,
                "if is_function(a) -> true; true -> false end.", false),
    ?line check(fun() -> if function(a) -> true; true -> false end end,
                "if function(a) -> true; true -> false end.", false),
    ?line check(fun() -> if is_binary(<<>>) -> true; true -> false end end,
                "if is_binary(<<>>) -> true; true -> false end.", true),
    ?line check(fun() -> if binary(<<>>) -> true; true -> false end end,
                "if binary(<<>>) -> true; true -> false end.", true),
    ?line check(fun() -> if is_integer(a) == true -> yes; 
                            true -> no end end,
                "if is_integer(a) == true -> yes; 
                            true -> no end.",
                no),
    ?line check(fun() -> if [] -> true; true -> false end end,
                "if [] -> true; true -> false end.", false),
    ?line error_check("if lists:member(1,[1]) -> true; true -> false end.",
                      illegal_guard_expr),
    ?line error_check("if false -> true end.", if_clause),
    ?line check(fun() -> if a+b -> true; true -> false end end,
                "if a + b -> true; true -> false end.", false),
    ?line check(fun() -> if + b -> true; true -> false end end,
                "if + b -> true; true -> false end.", false),
    ?line error_check("case foo of bar -> true end.", {case_clause,foo}),
    ?line error_check("case 4 of 2+a -> true; _ -> false end.", 
                      illegal_pattern),
    ?line error_check("case 4 of +a -> true; _ -> false end.", 
                      illegal_pattern),
    ?line check(fun() -> case a of 
                             X when X == b -> one;
                             X when X == a -> two
                         end end,
                "begin case a of 
                             X when X == b -> one;
                             X when X == a -> two
                         end end.", two),
    ?line error_check("3 = 4.", {badmatch,4}),
    ?line error_check("a = 3.", {badmatch,3}),
    %% ?line error_check("3.1 = 2.7.",{badmatch,2.7}),
    ?line error_check("$c = 4.", {badmatch,4}),
    ?line check(fun() -> $c = $c end, "$c = $c.", $c),
    ?line check(fun() -> _ = bar end, "_ = bar.", bar),
    ?line check(fun() -> A = 14, A = 14 end, 
                "begin A = 14, A = 14 end.", 14),
    ?line error_check("begin A = 14, A = 16 end.", {badmatch,16}),
    ?line error_check("\"hej\" = \"san\".", {badmatch,"san"}),
    ?line check(fun() -> "hej" = "hej" end,
                "\"hej\" = \"hej\".", "hej"),
    ?line error_check("[] = [a].", {badmatch,[a]}),
    ?line check(fun() -> [] = [] end, "[] = [].", []),
    ?line error_check("[a] = [].", {badmatch,[]}),
    ?line error_check("{a,b} = 34.", {badmatch,34}),
    ?line check(fun() -> <<X:7>> = <<8:7>>, X end,
		"begin <<X:7>> = <<8:7>>, X end.", 8),
    ?line error_check("<<34:32>> = \"hej\".", {badmatch,"hej"}),
    ?line check(fun() -> trunc((1 * 3 div 3 + 4 - 3) / 1) rem 2 end,
                "begin trunc((1 * 3 div 3 + 4 - 3) / 1) rem 2 end.", 0),
    ?line check(fun() -> (2#101 band 2#10101) bor (2#110 bxor 2#010) end,
                "(2#101 band 2#10101) bor (2#110 bxor 2#010).", 5),
    ?line check(fun() -> (2#1 bsl 4) + (2#10000 bsr 3) end,
                "(2#1 bsl 4) + (2#10000 bsr 3).", 18),
    ?line check(fun() -> ((1<3) and ((1 =:= 2) or (1 =/= 2))) xor (1=<2) end,
                "((1<3) and ((1 =:= 2) or (1 =/= 2))) xor (1=<2).", false),
    ?line check(fun() -> (a /= b) or (2 > 4) or (3 >= 3) end,
                "(a /= b) or (2 > 4) or (3 >= 3).", true),
    ?line check(fun() -> "hej" ++ "san" =/= "hejsan" -- "san" end,
                "\"hej\" ++ \"san\" =/= \"hejsan\" -- \"san\".", true),
    ?line check(fun() -> (bnot 1) < -0 end, "(bnot (+1)) < -0.", true),
    ok.

unary_plus(doc) ->
    ["OTP-4929. Unary plus rejects non-numbers."];
unary_plus(suite) ->
    [];
unary_plus(Config) when is_list(Config) ->
    ?line check(fun() -> F = fun(X) -> + X end, 
                         true = -1 == F(-1) end,
                "begin F = fun(X) -> + X end," 
                "      true = -1 == F(-1) end.", true, ['F'], none, none),
    ?line error_check("+a.", badarith),
    ok.

apply_atom(doc) ->
    ["OTP-5064. Can no longer apply atoms."];
apply_atom(suite) ->
    [];
apply_atom(Config) when is_list(Config) ->
    ?line error_check("[X || X <- [[1],[2]], 
                             begin L = length, L(X) =:= 1 end].", 
                      {badfun,length}),
    ok.

otp_5269(doc) ->
    ["OTP-5269. Bugs in the bit syntax."];
otp_5269(suite) ->
    [];
otp_5269(Config) when is_list(Config) ->
    ?line check(fun() -> L = 8,
                         F = fun(<<A:L,B:A>>) -> B end,
                         F(<<16:8, 7:16>>)
                end,
                "begin 
                   L = 8, F = fun(<<A:L,B:A>>) -> B end, F(<<16:8, 7:16>>)
                 end.",
                7),
    ?line check(fun() -> L = 8,
                         F = fun(<<L:L,B:L>>) -> B end,
                         F(<<16:8, 7:16>>)
                end,
                "begin 
                   L = 8, F = fun(<<L:L,B:L>>) -> B end, F(<<16:8, 7:16>>)
                 end.",
                7),
    ?line check(fun() -> L = 8, <<A:L,B:A>> = <<16:8, 7:16>>, B end,
                "begin L = 8, <<A:L,B:A>> = <<16:8, 7:16>>, B end.",
                7),
    ?line error_check("begin L = 8, <<L:L,B:L>> = <<16:8, 7:16>> end.",
                      {badmatch,<<16:8,7:16>>}),
    
    ?line error_check("begin <<L:16,L:L>> = <<16:16,8:16>>, L end.",
                      {badmatch, <<16:16,8:16>>}),
    ?line check(fun() -> U = 8, (fun(<<U:U>>) -> U end)(<<32:8>>) end,
                "begin U = 8, (fun(<<U:U>>) -> U end)(<<32:8>>) end.",
                32),
    ?line check(fun() -> U = 8, [U || <<U:U>> <- [<<32:8>>]] end,
                "begin U = 8, [U || <<U:U>> <- [<<32:8>>]] end.",
                [32]),
    ?line error_check("(fun({3,<<A:32,A:32>>}) -> a end)
                          ({3,<<17:32,19:32>>}).",
                      function_clause),
    ?line check(fun() -> [X || <<A:8,
                                 B:A>> <- [<<16:8,19:16>>],
                               <<X:8>> <- [<<B:8>>]] end,
                "[X || <<A:8,
                                 B:A>> <- [<<16:8,19:16>>],
                               <<X:8>> <- [<<B:8>>]].",
                [19]),
    ok.

otp_6539(doc) ->
    ["OTP-6539. try/catch bugs."];
otp_6539(suite) ->
    [];
otp_6539(Config) when is_list(Config) ->
    ?line check(fun() -> 
                        F = fun(A,B) -> 
                                    try A+B 
                                    catch _:_ -> dontthinkso 
                                    end 
                            end,
                        lists:zipwith(F, [1,2], [2,3])
                end,
                "begin 
                     F = fun(A,B) -> 
                                 try A+B 
                                 catch _:_ -> dontthinkso 
                                 end 
                         end,
                     lists:zipwith(F, [1,2], [2,3])
                 end.",
                [3, 5]),
    ok.

otp_6543(doc) ->
    ["OTP-6543. bitlevel binaries."];
otp_6543(suite) ->
    [];
otp_6543(Config) when is_list(Config) ->
    ?line check(fun() ->
                        << <<X>> || <<X>> <- [1,2,3] >>
                end,
                "<< <<X>> || <<X>> <- [1,2,3] >>.",
                <<>>),
    ?line check(fun() -> 
                        << <<X>> || X <- [1,2,3] >>
                end,
                "<< <<X>> || X <- [1,2,3] >>.",
                <<1,2,3>>),
    ?line check(fun() -> 
                        << <<X:8>> || <<X:2>> <= <<"hej">> >>
                end,
                "<< <<X:8>> || <<X:2>> <= <<\"hej\">> >>.",
                <<1,2,2,0,1,2,1,1,1,2,2,2>>),
    ?line check(fun() ->
                        << <<X:8>> || 
                            <<65,X:4>> <= <<65,7:4,65,3:4,66,8:4>> >>
                end,
                "<< <<X:8>> || 
                            <<65,X:4>> <= <<65,7:4,65,3:4,66,8:4>> >>.",
                <<7,3>>),
    ?line check(fun() -> <<34:18/big>> end,
                "<<34:18/big>>.",
                <<0,8,2:2>>),
    ?line check(fun() -> <<34:18/big-unit:2>> end,
                "<<34:18/big-unit:2>>.",
                <<0,0,0,2,2:4>>),
    ?line check(fun() -> <<34:18/little>> end,
                "<<34:18/little>>.",
                <<34,0,0:2>>),
    ?line case eval_string("<<34:18/native>>.") of
              <<0,8,2:2>> -> ok;
              <<34,0,0:2>> -> ok
          end,
    ?line check(fun() -> <<34:18/big-signed>> end,
                "<<34:18/big-signed>>.",
                <<0,8,2:2>>),
    ?line check(fun() -> <<34:18/little-signed>> end,
                "<<34:18/little-signed>>.",
                <<34,0,0:2>>),
    ?line case eval_string("<<34:18/native-signed>>.") of
              <<0,8,2:2>> -> ok;
              <<34,0,0:2>> -> ok
          end,
    ?line check(fun() -> <<34:18/big-unsigned>> end,
                "<<34:18/big-unsigned>>.",
                <<0,8,2:2>>),
    ?line check(fun() -> <<34:18/little-unsigned>> end,
                "<<34:18/little-unsigned>>.",
                <<34,0,0:2>>),
    ?line case eval_string("<<34:18/native-unsigned>>.") of
              <<0,8,2:2>> -> ok;
              <<34,0,0:2>> -> ok
          end,
    ?line check(fun() -> <<3.14:32/float-big>> end,
                "<<3.14:32/float-big>>.",
                <<64,72,245,195>>),
    ?line check(fun() -> <<3.14:32/float-little>> end,
                "<<3.14:32/float-little>>.",
                <<195,245,72,64>>),
    ?line case eval_string("<<3.14:32/float-native>>.") of
              <<64,72,245,195>> -> ok;
              <<195,245,72,64>> -> ok
          end,
    ?line error_check("<<(<<17,3:2>>)/binary>>.", badarg),
    ?line check(fun() -> <<(<<17,3:2>>)/bitstring>> end,
                "<<(<<17,3:2>>)/bitstring>>.",
                <<17,3:2>>),
    ?line check(fun() -> <<(<<17,3:2>>):10/bitstring>> end,
                "<<(<<17,3:2>>):10/bitstring>>.",
                <<17,3:2>>),
    ?line check(fun() -> <<<<344:17>>/binary-unit:17>> end,
		"<<<<344:17>>/binary-unit:17>>.",
		<<344:17>>),

    ?line check(fun() -> <<X:18/big>> = <<34:18/big>>, X end,
                "begin <<X:18/big>> = <<34:18/big>>, X end.",
                34),
    ?line check(fun() -> <<X:18/big-unit:2>> = <<34:18/big-unit:2>>, X end,
                "begin <<X:18/big-unit:2>> = <<34:18/big-unit:2>>, X end.",
                34),
    ?line check(fun() -> <<X:18/little>> = <<34:18/little>>, X end,
                "begin <<X:18/little>> = <<34:18/little>>, X end.",
                34),
    ?line check(fun() -> <<X:18/native>> = <<34:18/native>>, X end,
                "begin <<X:18/native>> = <<34:18/native>>, X end.",
                34),
    ?line check(fun() -> <<X:18/big-signed>> = <<34:18/big-signed>>, X end,
                "begin <<X:18/big-signed>> = <<34:18/big-signed>>, X end.",
                34),
    ?line check(fun() -> <<X:18/little-signed>> = <<34:18/little-signed>>, 
                         X end,
                "begin <<X:18/little-signed>> = <<34:18/little-signed>>, 
                       X end.",
                34),
    ?line check(fun() -> <<X:18/native-signed>> = <<34:18/native-signed>>, 
                         X end,
                "begin <<X:18/native-signed>> = <<34:18/native-signed>>, 
                       X end.",
                34),
    ?line check(fun() -> <<X:18/big-unsigned>> = <<34:18/big-unsigned>>, 
                         X end,
                "begin <<X:18/big-unsigned>> = <<34:18/big-unsigned>>, 
                       X end.",
                34),
    ?line check(fun() -> 
                        <<X:18/little-unsigned>> = <<34:18/little-unsigned>>, 
                        X end,
                "begin <<X:18/little-unsigned>> = <<34:18/little-unsigned>>, 
                       X end.",
                34),
    ?line check(fun() -> 
                        <<X:18/native-unsigned>> = <<34:18/native-unsigned>>, 
                        X end,
                "begin <<X:18/native-unsigned>> = <<34:18/native-unsigned>>, 
                       X end.",
                34),
    ?line check(fun() -> <<X:32/float-big>> = <<2.0:32/float-big>>, X end,
                "begin <<X:32/float-big>> = <<2.0:32/float-big>>, 
                        X end.",
                2.0),
    ?line check(fun() -> <<X:32/float-little>> = <<2.0:32/float-little>>, 
                         X end,
                "begin <<X:32/float-little>> = <<2.0:32/float-little>>, 
                        X end.",
                2.0),
    ?line check(fun() -> <<X:32/float-native>> = <<2.0:32/float-native>>, 
                         X end,
                "begin <<X:32/float-native>> = <<2.0:32/float-native>>, 
                        X end.",
                2.0),

    ?line check(
            fun() -> 
                    [X || <<"hej",X:8>> <= <<"hej",8,"san",9,"hej",17,"hej">>]
            end,
            "[X || <<\"hej\",X:8>> <= 
                        <<\"hej\",8,\"san\",9,\"hej\",17,\"hej\">>].",
            [8,17]),
    ?line check(
            fun() ->
                    L = 8, << <<B:32>> || <<L:L,B:L>> <= <<16:8, 7:16>> >>
            end,
            "begin L = 8, << <<B:32>> || <<L:L,B:L>> <= <<16:8, 7:16>> >> 
             end.",
            <<0,0,0,7>>),
    %% Test the Value part of a binary segment. 
    %% "Old" bugs have been fixed (partial_eval is called on Value).
    ?line check(fun() -> [ 3 || <<17/float>> <= <<17.0/float>>] end,
                "[ 3 || <<17/float>> <= <<17.0/float>>].",
                [3]),
    ?line check(fun() -> [ 3 || <<17/float>> <- [<<17.0/float>>]] end,
                "[ 3 || <<17/float>> <- [<<17.0/float>>]].",
                [3]),
    ?line check(fun() -> [ X || <<17/float,X:3>> <= <<17.0/float,2:3>>] end,
                "[ X || <<17/float,X:3>> <= <<17.0/float,2:3>>].",
                [2]),
    ?line check(fun() -> 
                 [ foo || <<(1 bsl 1023)/float>> <= <<(1 bsl 1023)/float>>]
                end,
                "[ foo || <<(1 bsl 1023)/float>> <= <<(1 bsl 1023)/float>>].",
                [foo]),
    ?line check(fun() -> 
                 [ foo || <<(1 bsl 1023)/float>> <- [<<(1 bsl 1023)/float>>]]
                end,
               "[ foo || <<(1 bsl 1023)/float>> <- [<<(1 bsl 1023)/float>>]].",
                [foo]),
    ?line error_check("[ foo || <<(1 bsl 1024)/float>> <- 
                            [<<(1 bsl 1024)/float>>]].",
                      badarg),
    ?line check(fun() -> 
                 [ foo || <<(1 bsl 1024)/float>> <- [<<(1 bsl 1023)/float>>]]
                end,
                "[ foo || <<(1 bsl 1024)/float>> <- 
                            [<<(1 bsl 1023)/float>>]].",
                []),
    ?line check(fun() -> 
                 [ foo || <<(1 bsl 1024)/float>> <= <<(1 bsl 1023)/float>>]
                end,
                "[ foo || <<(1 bsl 1024)/float>> <= 
                            <<(1 bsl 1023)/float>>].",
                []),
    ?line check(fun() ->
                        L = 8, 
                        [{L,B} || <<L:L,B:L/float>> <= <<32:8,7:32/float>>]
                end,
                "begin L = 8, 
                       [{L,B} || <<L:L,B:L/float>> <= <<32:8,7:32/float>>]
                 end.",
                [{32,7.0}]),
    ?line check(fun() ->
                        L = 8, 
                        [{L,B} || <<L:L,B:L/float>> <- [<<32:8,7:32/float>>]]
                end,
                "begin L = 8, 
                       [{L,B} || <<L:L,B:L/float>> <- [<<32:8,7:32/float>>]]
                 end.",
                [{32,7.0}]),
    ?line check(fun() ->
                        [foo || <<"s">> <= <<"st">>]
                end,
                "[foo || <<\"s\">> <= <<\"st\">>].",
                [foo]),
    ?line check(fun() -> <<_:32>> = <<17:32>> end,
                "<<_:32>> = <<17:32>>.",
                <<17:32>>),
    ?line check(fun() -> [foo || <<_:32>> <= <<17:32,20:32>>] end,
                "[foo || <<_:32>> <= <<17:32,20:32>>].",
                [foo,foo]),

    ?line check(fun() -> << <<X:32>> || X <- [1,2,3], X > 1 >> end,
                "<< <<X:32>> || X <- [1,2,3], X > 1 >>.",
                <<0,0,0,2,0,0,0,3>>),
    ?line error_check("[X || <<X>> <= [a,b]].",{bad_generator,[a,b]}),
    ok.

otp_6787(doc) ->
    ["OTP-6787. bitlevel binaries."];
otp_6787(suite) ->
    [];
otp_6787(Config) when is_list(Config) ->
    ?line check(
            fun() -> <<16:(1024*1024)>> = <<16:(1024*1024)>> end,
            "<<16:(1024*1024)>> = <<16:(1024*1024)>>.",
            <<16:1048576>>),
    ok.

otp_6977(doc) ->
    ["OTP-6977. ++ bug."];
otp_6977(suite) ->
    [];
otp_6977(Config) when is_list(Config) ->
    ?line check(
            fun() -> (fun([$X] ++ _) -> ok end)("X") end,
            "(fun([$X] ++ _) -> ok end)(\"X\").",
            ok),
    ok.

otp_7550(doc) ->
    ["OTP-7550. Support for UTF-8, UTF-16, UTF-32."];
otp_7550(Config) when is_list(Config) ->

    %% UTF-8.
    ?line check(
	    fun() -> <<65>> = <<65/utf8>> end,
	    "<<65>> = <<65/utf8>>.",
	    <<65>>),
    ?line check(
	    fun() -> <<350/utf8>> = <<197,158>> end,
	    "<<350/utf8>> = <<197,158>>.",
	    <<197,158>>),
    ?line check(
	    fun() -> <<$b,$j,$\303,$\266,$r,$n>> = <<"bj\366rn"/utf8>> end,
	    "<<$b,$j,$\303,$\266,$r,$n>> = <<\"bj\366rn\"/utf8>>.",
	    <<$b,$j,$\303,$\266,$r,$n>>),

    %% UTF-16.
    ?line check(
	    fun() -> <<0,65>> = <<65/utf16>> end,
	    "<<0,65>> = <<65/utf16>>.",
	    <<0,65>>),
    ?line check(
	    fun() -> <<16#D8,16#08,16#DF,16#45>> = <<16#12345/utf16>> end,
	    "<<16#D8,16#08,16#DF,16#45>> = <<16#12345/utf16>>.",
	    <<16#D8,16#08,16#DF,16#45>>),
    ?line check(
	    fun() -> <<16#08,16#D8,16#45,16#DF>> = <<16#12345/little-utf16>> end,
	    "<<16#08,16#D8,16#45,16#DF>> = <<16#12345/little-utf16>>.",
	    <<16#08,16#D8,16#45,16#DF>>),

    ?line check(
	    fun() -> <<350/utf16>> = <<1,94>> end,
	    "<<350/utf16>> = <<1,94>>.",
	    <<1,94>>),
    ?line check(
	    fun() -> <<350/little-utf16>> = <<94,1>> end,
	    "<<350/little-utf16>> = <<94,1>>.",
	    <<94,1>>),
    ?line check(
	    fun() -> <<16#12345/utf16>> = <<16#D8,16#08,16#DF,16#45>> end,
	    "<<16#12345/utf16>> = <<16#D8,16#08,16#DF,16#45>>.",
	    <<16#D8,16#08,16#DF,16#45>>),
    ?line check(
	    fun() -> <<16#12345/little-utf16>> = <<16#08,16#D8,16#45,16#DF>> end,
	    "<<16#12345/little-utf16>> = <<16#08,16#D8,16#45,16#DF>>.",
	    <<16#08,16#D8,16#45,16#DF>>),

    %% UTF-32.
    ?line check(
	    fun() -> <<16#12345/utf32>> = <<16#0,16#01,16#23,16#45>> end,
	    "<<16#12345/utf32>> = <<16#0,16#01,16#23,16#45>>.",
	    <<16#0,16#01,16#23,16#45>>),
    ?line check(
	    fun() -> <<16#0,16#01,16#23,16#45>> = <<16#12345/utf32>> end,
	    "<<16#0,16#01,16#23,16#45>> = <<16#12345/utf32>>.",
	    <<16#0,16#01,16#23,16#45>>),
    ?line check(
	    fun() -> <<16#12345/little-utf32>> = <<16#45,16#23,16#01,16#00>> end,
	    "<<16#12345/little-utf32>> = <<16#45,16#23,16#01,16#00>>.",
	    <<16#45,16#23,16#01,16#00>>),
    ?line check(
	    fun() -> <<16#12345/little-utf32>> end,
	    "<<16#12345/little-utf32>>.",
	    <<16#45,16#23,16#01,16#00>>),

    %% Mixed.
    ?line check(
	    fun() -> <<16#41,16#12345/utf32,16#0391:16,16#2E:8>> end,
	    "<<16#41,16#12345/utf32,16#0391:16,16#2E:8>>.",
	    <<16#41,16#00,16#01,16#23,16#45,16#03,16#91,16#2E>>),
    ok.


otp_8133(doc) ->
    ["OTP-8133. Bit comprehension bug."];
otp_8133(suite) ->
    [];
otp_8133(Config) when is_list(Config) ->
    ?line check(
            fun() ->
                  E = fun(N) -> 
                              if 
                                  is_integer(N) -> <<N/integer>>; 
                                  true -> throw(foo) 
                              end 
                      end,
                  try << << (E(V))/binary >> || V <- [1,2,3,a] >> 
                  catch foo -> ok
                  end
            end,
            "begin
                 E = fun(N) -> 
                            if is_integer(N) -> <<N/integer>>; 
                               true -> throw(foo) 
                            end 
                     end,
                 try << << (E(V))/binary >> || V <- [1,2,3,a] >> 
                 catch foo -> ok
                 end
             end.",
            ok),
    ?line check(
            fun() ->
                  E = fun(N) -> 
                              if 
                                  is_integer(N) -> <<N/integer>>; 
                                  true -> erlang:error(foo) 
                              end 
                      end,
                  try << << (E(V))/binary >> || V <- [1,2,3,a] >> 
                  catch error:foo -> ok
                  end
            end,
            "begin
                 E = fun(N) -> 
                            if is_integer(N) -> <<N/integer>>; 
                               true -> erlang:error(foo) 
                            end 
                     end,
                 try << << (E(V))/binary >> || V <- [1,2,3,a] >> 
                 catch error:foo -> ok
                 end
             end.",
            ok),
    ok.

funs(doc) ->
    ["Simple cases, just to cover some code."];
funs(suite) ->
    [];
funs(Config) when is_list(Config) ->
    do_funs(none, none),
    do_funs(lfh(), none),
    do_funs(lfh(), efh()),

    ?line error_check("nix:foo().", {access_not_allowed,nix}, lfh(), efh()),
    ?line error_check("bar().", undef, none, none),

    ?line check(fun() -> F1 = fun(F,N) -> ?MODULE:count_down(F, N) end,
                         F1(F1, 1000) end,
                "begin F1 = fun(F,N) -> count_down(F, N) end,"
                "F1(F1,1000) end.",
		0, ['F1'], lfh(), none),

    ?line check(fun() -> F1 = fun(F,N) -> ?MODULE:count_down(F, N) end,
                         F1(F1, 1000) end,
                "begin F1 = fun(F,N) -> count_down(F, N) end,"
                "F1(F1,1000) end.",
		0, ['F1'], lfh_value(), none),

    ?line check(fun() -> F1 = fun(F,N) -> ?MODULE:count_down(F, N) end,
                         F1(F1, 1000) end,
                "begin F1 = fun(F,N) -> count_down(F, N) end,"
                "F1(F1,1000) end.",
		0, ['F1'], lfh_value_extra(), none),

    ?line check(fun() -> F1 = fun(F,N) -> ?MODULE:count_down(F, N) end,
                         F1(F1, 1000) end,
                "begin F1 = fun(F,N) -> count_down(F, N) end,"
                "F1(F1,1000) end.",
		0, ['F1'], {?MODULE,local_func_value}, none),
    %% This is not documented, and only for backward compatibility (good!).
    B0 = erl_eval:new_bindings(),
    ?line check(fun() -> is_function(?MODULE:count_down_fun()) end,
                "begin is_function(count_down_fun()) end.",
                true, [], {?MODULE,local_func,[B0]},none),

    EF = fun({timer,sleep}, As) when length(As) == 1 -> exit({got_it,sleep});
            ({M,F}, As) -> apply(M, F, As)
         end,
    EFH = {value, EF},
    ?line error_check("apply(timer, sleep, [1]).", got_it, none, EFH),
    ?line error_check("begin F = fun(T) -> timer:sleep(T) end,F(1) end.",
                      got_it, none, EFH),
    ?line error_check("fun c/1.", undef),
    ?line error_check("fun a:b/0().", undef),

    MaxArgs = 20,
    ?line [true] = 
        lists:usort([run_many_args(SAs) || SAs <- many_args(MaxArgs)]),
    ?line {'EXIT',{{argument_limit,_},_}} = 
        (catch run_many_args(many_args1(MaxArgs+1))),
    ok.

run_many_args({S, As}) ->
    apply(eval_string(S), As) =:= As.

many_args(N) ->
    [many_args1(I) || I <- lists:seq(1, N)].

many_args1(N) ->
    F = fun(L, P) -> 
                tl(lists:flatten([","++P++integer_to_list(E) || E <- L]))
        end,
    L = lists:seq(1, N),
    T = F(L, "V"),
    S = lists:flatten(io_lib:format("fun(~s) -> [~s] end.", [T, T])),
    {S, L}.

do_funs(LFH, EFH) ->
    %% LFH is not really used by these examples...

    %% These tests do not prove that tail recursive functions really
    %% work (that the process does not grow); one should also run them
    %% manually with 1000 replaced by 1000000.

    M = atom_to_list(?MODULE),
    ?line check(fun() -> F1 = fun(F,N) -> ?MODULE:count_down(F, N) end,
                         F1(F1, 1000) end,
                concat(["begin F1 = fun(F,N) -> ", M, 
                        ":count_down(F, N) end, F1(F1,1000) end."]),
		0, ['F1'], LFH, EFH),
    ?line check(fun() -> F1 = fun(F,N) -> apply(?MODULE,count_down,[F,N]) 
                              end, F1(F1, 1000) end,
                concat(["begin F1 = fun(F,N) -> apply(", M, 
                        ",count_down,[F, N]) end, F1(F1,1000) end."]),
		0, ['F1'], LFH, EFH),
    ?line check(fun() -> F1 = fun(F,N) -> {?MODULE,count_down}(F,N)
                              end, F1(F1, 1000) end,
                concat(["begin F1 = fun(F,N) -> {", M, 
                        ",count_down}(F, N) end, F1(F1,1000) end."]),
		0, ['F1'], LFH, EFH),
    ?line check(fun() -> F = fun(F,N) when N > 0 -> apply(F,[F,N-1]); 
                                (_F,0) -> ok end, 
                         F(F, 1000)
                end,
                "begin F = fun(F,N) when N > 0 -> apply(F,[F,N-1]);"
                             "(_F,0) -> ok end,"
                       "F(F, 1000) end.",
                ok, ['F'], LFH, EFH),
    ?line check(fun() -> F = fun(F,N) when N > 0 -> 
                                     apply(erlang,apply,[F,[F,N-1]]);
                                (_F,0) -> ok end, 
                         F(F, 1000)
                end,
                "begin F = fun(F,N) when N > 0 ->"
                                   "apply(erlang,apply,[F,[F,N-1]]);"
                             "(_F,0) -> ok end,"
                       "F(F, 1000) end.",
                ok, ['F'], LFH, EFH),
    ?line check(fun() -> F = count_down_fun(), 
                         SF = fun(SF, F1, N) -> F(SF, F1, N) end,
                         SF(SF, F, 1000) end,
                concat(["begin F = ", M, ":count_down_fun(),"
                        "SF = fun(SF, F1, N) -> F(SF, F1, N) end,"
                        "SF(SF, F, 1000) end."]),
                ok, ['F','SF'], LFH, EFH),


    ?line check(fun() -> F = fun(X) -> A = 1+X, {X,A} end, 
                         true = {2,3} == F(2) end,
                "begin F = fun(X) -> A = 1+X, {X,A} end, 
                       true = {2,3} == F(2) end.", true, ['F'], LFH, EFH),
    ?line check(fun() -> F = fun(X) -> {erlang,'+'}(X,2) end, 
                         true = 3 == F(1) end,
                "begin F = fun(X) -> {erlang,'+'}(X,2) end," 
                "      true = 3 == F(1) end.", true, ['F'],
               LFH, EFH),
    ?line check(fun() -> F = fun(X) -> byte_size(X) end,
                         ?MODULE:do_apply(F,<<"hej">>) end, 
                concat(["begin F = fun(X) -> size(X) end,",
                        M,":do_apply(F,<<\"hej\">>) end."]),
                3, ['F'], LFH, EFH),

    ?line check(fun() -> F1 = fun(X, Z) -> {X,Z} end,
                         Z = 5,
                         F2 = fun(X, Y) -> F1(Z,{X,Y}) end,
                         F3 = fun(X, Y) -> {a,F1(Z,{X,Y})} end,
                         {5,{x,y}} = F2(x,y), 
                         {a,{5,{y,x}}} = F3(y,x), 
                         {5,{5,y}} = F2(Z,y), 
                         true = {5,{x,5}} == F2(x,Z) end,
                "begin F1 = fun(X, Z) -> {X,Z} end,
                       Z = 5,
                       F2 = fun(X, Y) -> F1(Z,{X,Y}) end,
                       F3 = fun(X, Y) -> {a,F1(Z,{X,Y})} end,
                       {5,{x,y}} = F2(x,y), 
                       {a,{5,{y,x}}} = F3(y,x), 
                       {5,{5,y}} = F2(Z,y), 
                       true = {5,{x,5}} == F2(x,Z) end.",
                true, ['F1','Z','F2','F3'], LFH, EFH),
    ?line check(fun() -> F = fun(X) -> byte_size(X) end,
                         F2 = fun(Y) -> F(Y) end,
                         ?MODULE:do_apply(F2,<<"hej">>) end, 
                concat(["begin F = fun(X) -> size(X) end,",
                        "F2 = fun(Y) -> F(Y) end,",
                        M,":do_apply(F2,<<\"hej\">>) end."]),
                3, ['F','F2'], LFH, EFH),
    ?line check(fun() -> Z = 5, F = fun(X) -> {Z,X} end,
                         F2 = fun(Z) -> F(Z) end, F2(3) end,
                "begin Z = 5, F = fun(X) -> {Z,X} end,
                       F2 = fun(Z) -> F(Z) end, F2(3) end.",
                {5,3},['F','F2','Z'], LFH, EFH),
    ?line check(fun() -> F = fun(Z) -> Z end,
                         F2 = fun(X) -> F(X), Z = {X,X}, Z end,
                         {1,1} = F2(1), Z = 7, Z end,
                "begin F = fun(Z) -> Z end,
                       F2 = fun(X) -> F(X), Z = {X,X}, Z end,
                       {1,1} = F2(1), Z = 7, Z end.", 7, ['F','F2','Z'], 
                LFH, EFH),
    ?line check(fun() -> F = fun(F, N) -> [?MODULE:count_down(F,N) || X <-[1]]
                             end, F(F,2) end,
                concat(["begin F = fun(F, N) -> [", M, 
                       ":count_down(F,N) || X <-[1]] end, F(F,2) end."]),
                [[[0]]], ['F'], LFH, EFH),

    %% Tests for a bug found by the Dialyzer - used to crash.
    ?line check(fun() -> Pmod = erl_eval_helper:new(42), Pmod:add(5) end,
		"begin Pmod = erl_eval_helper:new(42), Pmod:add(5) end.",
		47,
		['Pmod'], LFH, EFH),
    ?line check(fun() -> Pmod = erl_eval_helper:new(42), B = Pmod:add(7), B end,
		"begin Pmod = erl_eval_helper:new(42), B = Pmod:add(7), B end.",
		49,
		['B','Pmod'], LFH, EFH),

    ok.

count_down(F, N) when N > 0 ->
    F(F, N-1);
count_down(_F, N) ->
    N.

count_down_fun() ->
    fun(SF,F,N) when N > 0 -> SF(SF,F,N-1);
       (_SF,_F,_N) -> ok
    end.

do_apply(F, V) ->
    F(V).

lfh() ->
    {eval, fun(F, As, Bs) -> local_func(F, As, Bs) end}.

local_func(F, As0, Bs0) when is_atom(F) ->
    {As,Bs} = erl_eval:expr_list(As0, Bs0, {eval,lfh()}),
    case erlang:function_exported(?MODULE, F, length(As)) of
	true ->
	    {value,apply(?MODULE, F, As),Bs};
	false ->
	    {value,apply(shell_default, F, As),Bs}
    end.

lfh_value_extra() ->
    %% Not documented.
    {value, fun(F, As) -> local_func_value(F, As) end, []}.

lfh_value() ->
    {value, fun(F, As) -> local_func_value(F, As) end}.

local_func_value(F, As) when is_atom(F) ->
    case erlang:function_exported(?MODULE, F, length(As)) of
	true ->
	    apply(?MODULE, F, As);
	false ->
	    apply(shell_default, F, As)
    end.

efh() ->
    {value, fun(F, As) -> external_func(F, As) end}.

external_func({M,_}, _As) when M == nix ->
    exit({{access_not_allowed,M},[mfa]});
external_func(F, As) when is_function(F) ->
    apply(F, As);
external_func({M,F}, As) ->
    apply(M, F, As).



try_catch(doc) ->
    ["Test try-of-catch-after-end statement"];
try_catch(suite) -> 
    [];
try_catch(Config) when is_list(Config) ->
    %% Match in of with catch
    ?line check(fun() -> try 1 of 1 -> 2 catch _:_ -> 3 end end,
		"try 1 of 1 -> 2 catch _:_ -> 3 end.", 2),
    ?line check(fun() -> try 1 of 1 -> 2; 3 -> 4 catch _:_ -> 5 end end,
		"try 1 of 1 -> 2; 3 -> 4 catch _:_ -> 5 end.", 2),
    ?line check(fun() -> try 3 of 1 -> 2; 3 -> 4 catch _:_ -> 5 end end,
		"try 3 of 1 -> 2; 3 -> 4 catch _:_ -> 5 end.", 4),
    %% Just after
    ?line check(fun () -> X = try 1 after put(try_catch, 2) end,
			  {X,get(try_catch)} end,
		"begin X = try 1 after put(try_catch, 2) end, "
		"{X,get(try_catch)} end.", {1,2}),
    %% Match in of with after
    ?line check(fun() -> X = try 1 of 1 -> 2 after put(try_catch, 3) end,
			 {X,get(try_catch)} end,
		"begin X = try 1 of 1 -> 2 after put(try_catch, 3) end, "
		"{X,get(try_catch)} end.", {2,3}),
    ?line check(fun() -> X = try 1 of 1 -> 2; 3 -> 4 
			     after put(try_catch, 5) end,
			 {X,get(try_catch)} end,
		"begin X = try 1 of 1 -> 2; 3 -> 4 "
		"          after put(try_catch, 5) end, "
		"      {X,get(try_catch)} end.", {2,5}),
    ?line check(fun() -> X = try 3 of 1 -> 2; 3 -> 4 
			     after put(try_catch, 5) end,
			 {X,get(try_catch)} end,
		"begin X = try 3 of 1 -> 2; 3 -> 4 "
		"          after put(try_catch, 5) end, "
		"      {X,get(try_catch)} end.", {4,5}),
    %% Nomatch in of
    ?line error_check("try 1 of 2 -> 3 catch _:_ -> 4 end.",
		      {try_clause,1}),
    %% Nomatch in of with after
    ?line check(fun () -> {'EXIT',{{try_clause,1},_}} = 
			      begin catch try 1 of 2 -> 3
				          after put(try_catch, 4) end end,
			  get(try_catch) end,
		"begin {'EXIT',{{try_clause,1},_}} = "
		"          begin catch try 1 of 2 -> 3 "
		"                      after put(try_catch, 4) end end, "
		"      get(try_catch) end. ", 4),
    %% Exception in try
    ?line check(fun () -> try 1=2 catch error:{badmatch,2} -> 3 end end,
		"try 1=2 catch error:{badmatch,2} -> 3 end.", 3),
    ?line check(fun () -> try 1=2 of 3 -> 4 
			  catch error:{badmatch,2} -> 5 end end,
		"try 1=2 of 3 -> 4 "
		"catch error:{badmatch,2} -> 5 end.", 5),
    %% Exception in try with after
    ?line check(fun () -> X = try 1=2 
			      catch error:{badmatch,2} -> 3
			      after put(try_catch, 4) end,
			  {X,get(try_catch)} end,
		"begin X = try 1=2 "
		"          catch error:{badmatch,2} -> 3 "
		"          after put(try_catch, 4) end, "
		"      {X,get(try_catch)} end. ", {3,4}),
    ?line check(fun () -> X = try 1=2 of 3 -> 4
			      catch error:{badmatch,2} -> 5
			      after put(try_catch, 6) end,
			  {X,get(try_catch)} end,
		"begin X = try 1=2 of 3 -> 4"
		"          catch error:{badmatch,2} -> 5 "
		"          after put(try_catch, 6) end, "
		"      {X,get(try_catch)} end. ", {5,6}),
    %% Uncaught exception
    ?line error_check("try 1=2 catch error:undefined -> 3 end. ",
		      {badmatch,2}),
    ?line error_check("try 1=2 of 3 -> 4 catch error:undefined -> 5 end. ",
		      {badmatch,2}),
    %% Uncaught exception with after
    ?line check(fun () -> {'EXIT',{{badmatch,2},_}} =
			      begin catch try 1=2 
					  after put(try_catch, 3) end end,
			  get(try_catch) end,
		"begin {'EXIT',{{badmatch,2},_}} = "
		"          begin catch try 1=2 "
		"                      after put(try_catch, 3) end end, "
		"      get(try_catch) end. ", 3),
    ?line check(fun () -> {'EXIT',{{badmatch,2},_}} =
			      begin catch try 1=2 of 3 -> 4
					  after put(try_catch, 5) end end,
			  get(try_catch) end,
		"begin {'EXIT',{{badmatch,2},_}} = "
		"          begin catch try 1=2 of 3 -> 4"
		"                      after put(try_catch, 5) end end, "
		"      get(try_catch) end. ", 5),
    ?line check(fun () -> {'EXIT',{{badmatch,2},_}} =
			      begin catch try 1=2 catch error:undefined -> 3
					  after put(try_catch, 4) end end,
			  get(try_catch) end,
		"begin {'EXIT',{{badmatch,2},_}} = "
		"          begin catch try 1=2 catch error:undefined -> 3 "
		"                      after put(try_catch, 4) end end, "
		"      get(try_catch) end. ", 4),
    ?line check(fun () -> {'EXIT',{{badmatch,2},_}} =
			      begin catch try 1=2 of 3 -> 4
					  catch error:undefined -> 5
					  after put(try_catch, 6) end end,
			  get(try_catch) end,
		"begin {'EXIT',{{badmatch,2},_}} = "
		"          begin catch try 1=2 of 3 -> 4 "
		"                      catch error:undefined -> 5 "
		"                      after put(try_catch, 6) end end, "
		"      get(try_catch) end. ", 6),
    ok.


eval_expr_5(doc) ->
    ["(OTP-7933)"];
eval_expr_5(suite) ->
    [];
eval_expr_5(Config) when is_list(Config) ->
    ?line {ok,Tokens ,_} =
	erl_scan:string("if a+4 == 4 -> yes; true -> no end. "),
    ?line {ok, [Expr]} = erl_parse:parse_exprs(Tokens),
    ?line {value, no, []} = erl_eval:expr(Expr, [], none, none, none),
    ?line no = erl_eval:expr(Expr, [], none, none, value),
    try
	erl_eval:expr(Expr, [], none, none, 4711),
	?line function_clause = should_never_reach_here
    catch
	error:function_clause ->
	    ok
    end.

zero_width(Config) when is_list(Config) ->
    ?line check(fun() ->
			{'EXIT',{badarg,_}} = (catch <<not_a_number:0>>),
			ok
		end, "begin {'EXIT',{badarg,_}} = (catch <<not_a_number:0>>), "
		"ok end.", ok),
    ok.

%% Check the string in different contexts: as is; in fun; from compiled code.
check(F, String, Result) ->
    check1(F, String, Result),
    FunString = concat(["fun() -> ", no_final_dot(String), " end(). "]),
    check1(F, FunString, Result),
    CompileString = concat(["hd(lists:map(fun(_) -> ", no_final_dot(String), 
                            " end, [foo])). "]),
    check1(F, CompileString, Result).

check1(F, String, Result) ->
    Result = F(),
    case catch parse_and_run(String) of
        {value, Result, _} ->
            ok;
        Other ->
            test_server:fail({eval, Other, Result})
    end.

check(F, String, Result, BoundVars, LFH, EFH) ->
    Result = F(),
    case catch parse_and_run(String, LFH, EFH) of
        {value, Result, Bs} ->
            %% We just assume that Bs is an orddict...
            Keys = orddict:fetch_keys(Bs),
            case sort(BoundVars) == Keys of
                true -> 
                    ok;
                false -> 
                    test_server:fail({check, BoundVars, Keys})
            end,
            ok;
        Other ->
            test_server:fail({check, Other, Result})
    end.

error_check(String, Result) ->
    case catch parse_and_run(String) of
        {'EXIT', {Result,_}} ->
            ok;
        Other ->
            test_server:fail({eval, Other, Result})
    end.

error_check(String, Result, LFH, EFH) ->
    case catch parse_and_run(String, LFH, EFH) of
        {'EXIT', {Result,_}} ->
            ok;
        Other ->
            test_server:fail({eval, Other, Result})
    end.

eval_string(String) ->
    {value, Result, _} = parse_and_run(String),
    Result.

parse_and_run(String) ->
    {ok,Tokens,_} = erl_scan:string(String),
    {ok, [Expr]} = erl_parse:parse_exprs(Tokens),
    erl_eval:expr(Expr, []).

parse_and_run(String, LFH, EFH) ->
    {ok,Tokens,_} = erl_scan:string(String),
    {ok, [Expr]} = erl_parse:parse_exprs(Tokens),
    erl_eval:expr(Expr, [], LFH, EFH).

no_final_dot(S) ->
    case lists:reverse(S) of
        " ." ++ R -> lists:reverse(R);
        "." ++ R -> lists:reverse(R);
        _ -> S
    end.