aboutsummaryrefslogblamecommitdiffstats
path: root/lib/erl_interface/test/erl_eterm_SUITE.erl
blob: 860d080acf659c10f6a00b8cc2068a749e7f8bc0 (plain) (tree)
1
2
3
4
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


                   
                                                        

















                                                                         
                                                    











                                                                           


                                                                    






































                                                                   

                                                
         












                                                             



            





                         
                                     
           

                                    
           
 




































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                            
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-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_eterm_SUITE).

-include_lib("test_server/include/test_server.hrl").
-include("erl_eterm_SUITE_data/eterm_test_cases.hrl").

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The tests are organised as follows:
%%%
%%% 1. Basic tests (encoding, decoding, memory allocation).
%%% 2. Constructing terms (the erl_mk_xxx() functions and erl_copy_term()).
%%% 3. Extracting & info functions (erl_hd(), erl_length() etc).
%%% 4. I/O list functions.
%%% 5. Miscellanous functions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2, 
	 build_terms/1, round_trip_conversion/1,
	 decode_terms/1, decode_float/1,
	 t_erl_mk_int/1, t_erl_mk_list/1,
	 basic_copy/1,
	 t_erl_cons/1,
	 t_erl_mk_atom/1,
	 t_erl_mk_binary/1,
	 t_erl_mk_empty_list/1,
	 t_erl_mk_float/1,
	 t_erl_mk_pid/1,
	 t_erl_mk_xpid/1,
	 t_erl_mk_port/1,
	 t_erl_mk_xport/1,
	 t_erl_mk_ref/1,
	 t_erl_mk_long_ref/1,
	 t_erl_mk_string/1,
	 t_erl_mk_estring/1,
	 t_erl_mk_tuple/1,
	 t_erl_mk_uint/1,
	 t_erl_mk_var/1,
	 t_erl_size/1,
	 t_erl_var_content/1,
	 t_erl_element/1,
	 t_erl_length/1, t_erl_hd/1, t_erl_tl/1,
	 type_checks/1, extractor_macros/1,
	 t_erl_iolist_length/1, t_erl_iolist_to_binary/1,
	 t_erl_iolist_to_string/1,
	 erl_print_term/1, print_string/1,
	 t_erl_free_compound/1,
	 high_chaparal/1,
	 broken_data/1,
	 cnode_1/1]).

-export([start_cnode/1]).

-import(runner, [get_term/1]).

%% This test suite controls the running of the C language functions
%% in eterm_test.c and print_term.c.

suite() -> [{suite_callbacks,[ts_install_scb]}].

all() -> 
    [build_terms, round_trip_conversion, decode_terms,
     decode_float, t_erl_mk_int, t_erl_mk_list, basic_copy,
     t_erl_mk_atom, t_erl_mk_binary, t_erl_mk_empty_list,
     t_erl_mk_float, t_erl_mk_pid, t_erl_mk_xpid,
     t_erl_mk_port, t_erl_mk_xport, t_erl_mk_ref,
     t_erl_mk_long_ref, t_erl_mk_string, t_erl_mk_estring,
     t_erl_mk_tuple, t_erl_mk_uint, t_erl_mk_var, t_erl_size,
     t_erl_var_content, t_erl_element, t_erl_cons,
     t_erl_length, t_erl_hd, t_erl_tl, type_checks,
     extractor_macros, t_erl_iolist_length,
     t_erl_iolist_to_binary, t_erl_iolist_to_string,
     erl_print_term, print_string, t_erl_free_compound,
     high_chaparal, broken_data, cnode_1].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%%	1.   B a s i c    t e s t s
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% This test asks the C function to construct all data types in
%% a list and verifies that the result is as expected.

build_terms(suite) -> [];
build_terms(Config) when is_list(Config) ->
    ?line P = runner:start(?build_terms),
    ?line {term, Term} = get_term(P),
    ?line io:format("Received: ~p", [Term]),
    ?line [ARefLN, ARef, APortLN, APort, APidLN, APid,
	   {element1, 42, 767}, "A string",
	   1, -1, 0, 3.0, ABin, 'I am an atom'] = Term,
    ?line "A binary" = binary_to_list(ABin),
    ?line case ARef of
	      R when is_reference(R), node(R) == kalle@localhost -> ok
	  end,
    ?line case ARefLN of
	      R1 when is_reference(R1), node(R1) == abcdefghijabcdefghij@localhost -> ok
	  end,
    ?line case APort of
	      Port when is_port(Port), node(Port) == kalle@localhost -> ok
	  end,
    ?line case APortLN of
	      Port1 when is_port(Port1), node(Port1) == abcdefghijabcdefghij@localhost -> ok
	  end,
    ?line case APid of
	      Pid when is_pid(Pid), node(Pid) == kalle@localhost -> ok
	  end,
    ?line case APidLN of
	      Pid1 when is_pid(Pid1), node(Pid1) == abcdefghijabcdefghij@localhost -> ok
	  end,

    ?line runner:recv_eot(P),
    ok.

%% This test is run entirely in C code.

round_trip_conversion(suite) -> [];
round_trip_conversion(Config) when is_list(Config) ->
    ?line runner:test(?round_trip_conversion),
    ok.

%% This test sends a list of all data types to the C code function,
%% which decodes it and verifies it.

decode_terms(suite) -> [];
decode_terms(Config) when is_list(Config) ->
    ?line Dummy1 = list_to_atom(filename:join(?config(priv_dir, Config),
					      dummy_file1)),
    ?line Dummy2 = list_to_atom(filename:join(?config(priv_dir, Config),
					      dummy_file2)),
    ?line Port1 = open_port(Dummy1, [out]),
    ?line Port2 = open_port(Dummy2, [out]),
    ?line ABinary = list_to_binary("A binary"),
    ?line Terms = [make_ref(), make_ref(),
		   Port1, Port2,
		   self(), self(),
		   {element1, 42, 767}, "A string",
		   1, -1, 0, 3.0, ABinary, 'I am an atom'],

    ?line P = runner:start(?decode_terms),
    ?line runner:send_term(P, Terms),
    ?line runner:recv_eot(P),

    ok.

%% Decodes the floating point number 3.1415.

decode_float(suite) -> [];
decode_float(Config) when is_list(Config) ->
    ?line P = runner:start(?decode_float),
    ?line runner:send_term(P, 3.1415),
    ?line runner:recv_eot(P),
    ok.

%% Tests the erl_free_compound() function.

t_erl_free_compound(suite) -> [];
t_erl_free_compound(Config) when is_list(Config) ->
    ?line runner:test(?t_erl_free_compound),
    ok.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%%	2.   C o n s t r u c t i n g   t e r m s
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% This tests the erl_mk_list() function.

t_erl_mk_list(suite) -> [];
t_erl_mk_list(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_list),

    ?line {term, []} = get_term(P),
    ?line {term, [abc]} = get_term(P),
    ?line {term, [abcdef, 42]} = get_term(P),
    ?line {term, [0.0, 23, [], 3.1415]} = get_term(P),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_int() function.

t_erl_mk_int(suite) -> [];
t_erl_mk_int(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_int),
    
    ?line {term, 0} = get_term(P),
    ?line {term, 127} = get_term(P),
    ?line {term, 128} = get_term(P),
    ?line {term, 255} = get_term(P),
    ?line {term, 256} = get_term(P),

    ?line {term, 16#FFFF} = get_term(P),
    ?line {term, 16#10000} = get_term(P),

    ?line {term, 16#07FFFFFF} = get_term(P),
    ?line {term, 16#0FFFFFFF} = get_term(P),
    ?line {term, 16#1FFFFFFF} = get_term(P),
    ?line {term, 16#3FFFFFFF} = get_term(P),
    ?line {term, 16#7FFFFFFF} = get_term(P),

    ?line {term, 16#08000000} = get_term(P),
    ?line {term, 16#10000000} = get_term(P),
    ?line {term, 16#20000000} = get_term(P),
    ?line {term, 16#40000000} = get_term(P),


    ?line {term, -16#07FFFFFF} = get_term(P),
    ?line {term, -16#0FFFFFFF} = get_term(P),
    ?line {term, -16#1FFFFFFF} = get_term(P),
    ?line {term, -16#3FFFFFFF} = get_term(P),
    ?line {term, -16#7FFFFFFF} = get_term(P),

    ?line {term, -16#08000000} = get_term(P),
    ?line {term, -16#10000000} = get_term(P),
    ?line {term, -16#20000000} = get_term(P),
    ?line {term, -16#40000000} = get_term(P),

    ?line {term, -16#08000001} = get_term(P),
    ?line {term, -16#10000001} = get_term(P),
    ?line {term, -16#20000001} = get_term(P),
    ?line {term, -16#40000001} = get_term(P),

    ?line {term, -16#08000002} = get_term(P),
    ?line {term, -16#10000002} = get_term(P),
    ?line {term, -16#20000002} = get_term(P),
    ?line {term, -16#40000002} = get_term(P),

    ?line {term, -1999999999} = get_term(P),
    ?line {term, -2000000000} = get_term(P),
    ?line {term, -2000000001} = get_term(P),

    ?line runner:recv_eot(P),
    ok.


%% Basic test of erl_copy_term().

basic_copy(suite) -> [];
basic_copy(Config) when is_list(Config) ->
    ?line runner:test(?basic_copy),
    ok.


%% This tests the erl_mk_tuple() function.

t_erl_mk_tuple(suite) -> [];
t_erl_mk_tuple(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_tuple),

    ?line {term, {madonna, 21, 'mad donna', 12}} = get_term(P),
    ?line {term, {'Madonna',21,{children,{"Isabella",2}},
		  {'home page',"http://www.madonna.com/"}}} = get_term(P),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_atom() function.

t_erl_mk_atom(suite) -> [];
t_erl_mk_atom(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_atom),

    ?line {term, madonna} = (get_term(P)),
    ?line {term, 'Madonna'} = (get_term(P)),
    ?line {term, 'mad donna'} = (get_term(P)),
    ?line {term, '_madonna_'} = (get_term(P)),
    ?line {term, '/home/madonna/tour_plan'} = (get_term(P)),
    ?line {term, 'http://www.madonna.com/tour_plan'} = (get_term(P)),
    ?line {term, '\'madonna\''} = (get_term(P)),
    ?line {term, '\"madonna\"'} = (get_term(P)),
    ?line {term, '\\madonna\\'} = (get_term(P)),
    ?line {term, '{madonna,21,\'mad donna\',12}'} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_binary() function.

t_erl_mk_binary(suite) -> [];
t_erl_mk_binary(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_binary),

    ?line {term, Bin} = (get_term(P)),
    ?line "{madonna,21,'mad donna',1234.567.890, !#$%&/()=?+-@, \" \\}" = 
	binary_to_list(Bin),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_empty_list() function.

t_erl_mk_empty_list(suite) -> [];
t_erl_mk_empty_list(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_empty_list),

    ?line {term, []} = get_term(P),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_float() function.

t_erl_mk_float(suite) -> [];
t_erl_mk_float(Config) when is_list(Config) ->
    case os:type() of 
	vxworks ->
	    {skipped, "Floating point numbers never compare equal on PPC"};
	_ ->
	    ?line P = runner:start(?t_erl_mk_float),
	    ?line {term, {3.1415, 1.999999, 2.000000, 2.000001, 
			  2.000002, 12345.67890}} = 
		get_term(P),
	    ?line runner:recv_eot(P),
	    ok
    end.


%% This tests the erl_mk_pid() function.

t_erl_mk_pid(suite) -> [];
t_erl_mk_pid(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_pid),

    ?line {term, A_pid} = (get_term(P)),
    ?line {pid, kalle@localhost, 3, 2} = nc2vinfo(A_pid),

    ?line runner:recv_eot(P),
    ok.

t_erl_mk_xpid(suite) -> [];
t_erl_mk_xpid(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_xpid),

    ?line {term, A_pid} = (get_term(P)),
    ?line {pid, kalle@localhost, 32767, 8191} = nc2vinfo(A_pid),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_port() function.

t_erl_mk_port(suite) -> [];
t_erl_mk_port(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_port),

    ?line {term, A_port} = (get_term(P)),
    ?line {port, kalle@localhost, 4} = nc2vinfo(A_port),

    ?line runner:recv_eot(P),
    ok.

t_erl_mk_xport(suite) -> [];
t_erl_mk_xport(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_xport),

    ?line {term, A_port} = (get_term(P)),
    ?line {port, kalle@localhost, 268435455} = nc2vinfo(A_port),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_ref() function.

t_erl_mk_ref(suite) -> [];
t_erl_mk_ref(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_ref),

    ?line {term, A_ref} = (get_term(P)),
    ?line {ref, kalle@localhost, _Length, [6]} = nc2vinfo(A_ref),

    ?line runner:recv_eot(P),
    ok.

t_erl_mk_long_ref(suite) -> [];
t_erl_mk_long_ref(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_long_ref),

    ?line {term, A_ref} = (get_term(P)),
    ?line {ref, kalle@localhost, _Length, [4294967295,4294967295,262143]}
	= nc2vinfo(A_ref),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_string() function.

t_erl_mk_string(suite) -> [];
t_erl_mk_string(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_string),

    ?line {term, "madonna"} = (get_term(P)),
    ?line {term, "Madonna"} = (get_term(P)),
    ?line {term, "mad donna"} = (get_term(P)),
    ?line {term, "_madonna_"} = (get_term(P)),
    ?line {term, "/home/madonna/tour_plan"} = (get_term(P)),
    ?line {term, "http://www.madonna.com/tour_plan"} = (get_term(P)),
    ?line {term, "\'madonna\'"} = (get_term(P)),
    ?line {term, "\"madonna\""} = (get_term(P)),
    ?line {term, "\\madonna\\"} = (get_term(P)),
    ?line {term, "{madonna,21,'mad donna',12}"} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_estring() function.

t_erl_mk_estring(suite) -> [];
t_erl_mk_estring(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_estring),

    ?line {term, "madonna"} = (get_term(P)),
    ?line {term, "Madonna"} = (get_term(P)),
    ?line {term, "mad donna"} = (get_term(P)),
    ?line {term, "_madonna_"} = (get_term(P)),
    ?line {term, "/home/madonna/tour_plan"} = (get_term(P)),
    ?line {term, "http://www.madonna.com/tour_plan"} = (get_term(P)),
    ?line {term, "\'madonna\'"} = (get_term(P)),
    ?line {term, "\"madonna\""} = (get_term(P)),
    ?line {term, "\\madonna\\"} = (get_term(P)),
    ?line {term, "{madonna,21,'mad donna',12}"} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_uint() function.

t_erl_mk_uint(suite) -> [];
t_erl_mk_uint(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_uint),

    ?line {term, 54321} = (get_term(P)),
    ?line {term, 2147483647} = (get_term(P)),
    ?line {term, 2147483648} = (get_term(P)),
    ?line {term, 2147483649} = (get_term(P)),
    ?line {term, 2147483650} = (get_term(P)),
    ?line {term, 4294967295} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_mk_var() function.

t_erl_mk_var(suite) -> [];
t_erl_mk_var(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_mk_var),

    ?line {term, 1} = (get_term(P)),
    ?line {term, 0} = (get_term(P)),
    ?line {term, 1} = (get_term(P)),
    ?line {term, 0} = (get_term(P)),
    ?line {term, 1} = (get_term(P)),
    ?line {term, 0} = (get_term(P)),
    ?line {term, 1} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_cons() function.

t_erl_cons(suite) -> [];
t_erl_cons(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_cons),

    ?line {term, [madonna, 21]} = get_term(P),

    ?line runner:recv_eot(P),
    ok.





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%%	3.   E x t r a c t i n g  &   i n f o    f u n c t i o n s
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Tests the erl_length() function.

t_erl_length(suite) -> [];
t_erl_length(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_length),

    ?line 0 = erl_length(P, []),
    ?line 1 = erl_length(P, [a]),
    ?line 2 = erl_length(P, [a, b]),
    ?line 3 = erl_length(P, [a, b, c]),

    ?line 4 = erl_length(P, [a, [x, y], c, []]),

    ?line -1 = erl_length(P, [a|b]),
    ?line -1 = erl_length(P, a),

    ?line runner:finish(P),
    ok.

%% Invokes the erl_length() function.

erl_length(Port, List) ->
    call_erl_function(Port, List).

%% Tests the erl_hd() function.

t_erl_hd(suite) -> [];
t_erl_hd(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_hd),
    
    ?line 'NULL' = erl_hd(P, 42),
    ?line 'NULL' = erl_hd(P, abc),
    ?line 'NULL' = erl_hd(P, []),

    ?line [] = erl_hd(P, [[], a]),
    ?line a = erl_hd(P, [a]),
    ?line a = erl_hd(P, [a, b]),
    ?line a = erl_hd(P, [a, b, c]),
    ?line a = erl_hd(P, [a|b]),

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

%% Invokes the erl_hd() function.

erl_hd(Port, List) ->
    call_erl_function(Port, List).

%% Tests the erl_tail() function.

t_erl_tl(suite) -> [];
t_erl_tl(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_tl),

    ?line 'NULL' = erl_tl(P, 42),
    ?line 'NULL' = erl_tl(P, abc),
    ?line 'NULL' = erl_tl(P, []),

    ?line [] = erl_tl(P, [a]),
    ?line [b] = erl_tl(P, [a, b]),
    ?line [b, c] = erl_tl(P, [a, b, c]),

    ?line b = erl_tl(P, [a|b]),

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

%% Invokes the erl_tail() function in erl_interface.

erl_tl(Port, List) ->
    call_erl_function(Port, List).

%% Tests the type checking macros (done in the C program).

type_checks(suite) -> [];
type_checks(Config) when is_list(Config) ->
    ?line runner:test(?type_checks),
    ok.

%% Tests the extractor macros (done in the C program).

extractor_macros(suite) -> [];
extractor_macros(Config) when is_list(Config) ->
    ?line runner:test(?extractor_macros),
    ok.


%% This tests the erl_size() function.

t_erl_size(suite) -> [];
t_erl_size(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_size),

    ?line {term, 0} = (get_term(P)),
    ?line {term, 4} = (get_term(P)),

    ?line {term, 0} = (get_term(P)),
    ?line {term, 27} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_var_content() function.

t_erl_var_content(suite) -> [];
t_erl_var_content(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_var_content),

    ?line {term, 17} = (get_term(P)),
    ?line {term, "http://www.madonna.com"} = (get_term(P)),
    ?line {term, 2} = (get_term(P)),
    ?line {term, "http://www.madonna.com"} = (get_term(P)),
    ?line {term, 2} = (get_term(P)),

    ?line runner:recv_eot(P),
    ok.


%% This tests the erl_element() function.

t_erl_element(suite) -> [];
t_erl_element(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_element),

    ?line {term, madonna} = get_term(P),
    ?line {term, 21} = get_term(P),
    ?line {term, 'mad donna'} = get_term(P),
    ?line {term, 12} = get_term(P),

    ?line {term, 'Madonna'} = get_term(P),
    ?line {term, 21} = get_term(P),
    ?line {term, {children,{"Isabella",2}}} = get_term(P),
    ?line {term, {'home page',"http://www.madonna.com/"}} = get_term(P),

    ?line runner:recv_eot(P),
    ok.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%%	4.   I / O   l i s t   f u n c t i o n s
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Tests the erl_iolist_length() function.

t_erl_iolist_length(suite) -> [];
t_erl_iolist_length(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_iolist_length),

    %% Flat lists.

    ?line 0 = erl_iolist_length(P, []),
    ?line 1 = erl_iolist_length(P, [10]),
    ?line 2 = erl_iolist_length(P, [10, 20]),
    ?line 3 = erl_iolist_length(P, [10, 20, 30]),
    ?line 256 = erl_iolist_length(P, lists:seq(0, 255)),

    %% Deep lists.

    ?line 0 = erl_iolist_length(P, [[]]),
    ?line 1 = erl_iolist_length(P, [[], 42]),
    ?line 1 = erl_iolist_length(P, [42, []]),
    ?line 2 = erl_iolist_length(P, [42, [], 45]),

    ?line 3 = erl_iolist_length(P, [42, [90], 45]),
    ?line 3 = erl_iolist_length(P, [[42, [90]], 45]),
    ?line 3 = erl_iolist_length(P, [[42, [90]], 45]),

    %% List with binaries.

    ?line 0 = erl_iolist_length(P, [list_to_binary([])]),
    ?line 0 = erl_iolist_length(P, [[], list_to_binary([])]),
    ?line 1 = erl_iolist_length(P, [[1], list_to_binary([])]),
    ?line 1 = erl_iolist_length(P, [[], list_to_binary([2])]),
    ?line 2 = erl_iolist_length(P, [[42], list_to_binary([2])]),
    ?line 4 = erl_iolist_length(P, [[42], list_to_binary([2, 3, 4])]),

    %% Binaries as tail.

    ?line 0 = erl_iolist_length(P, [[]| list_to_binary([])]),
    ?line 1 = erl_iolist_length(P, [[1]| list_to_binary([])]),
    ?line 1 = erl_iolist_length(P, [[]| list_to_binary([2])]),
    ?line 2 = erl_iolist_length(P, [[42]| list_to_binary([2])]),

    %% Binaries only.

    ?line 0 = erl_iolist_length(P, list_to_binary("")),
    ?line 1 = erl_iolist_length(P, list_to_binary([1])),
    ?line 2 = erl_iolist_length(P, list_to_binary([1, 2])),

    %% Illegal cases.

    ?line -1 = erl_iolist_length(P, [42|43]),
    ?line -1 = erl_iolist_length(P, a),

    ?line -1 = erl_iolist_length(P, [a]),
    ?line -1 = erl_iolist_length(P, [256]),
    ?line -1 = erl_iolist_length(P, [257]),
    ?line -1 = erl_iolist_length(P, [-1]),
    ?line -1 = erl_iolist_length(P, [-2]),
    ?line -1 = erl_iolist_length(P, [-127]),
    ?line -1 = erl_iolist_length(P, [-128]),

    ?line runner:finish(P),
    ok.

%% Invokes the erl_iolist_length() function.

erl_iolist_length(Port, List) ->
    call_erl_function(Port, List).

%% Tests the erl_iolist_to_binary() function.

t_erl_iolist_to_binary(suite) -> [];
t_erl_iolist_to_binary(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_iolist_to_binary),

    %% Flat lists.

    ?line [] = iolist_to_list(P, []),
    ?line [10] = iolist_to_list(P, [10]),
    ?line [10, 20] = iolist_to_list(P, [10, 20]),
    ?line [10, 20, 30] = iolist_to_list(P, [10, 20, 30]),
    ?line AllBytes = lists:seq(0, 255),
    ?line AllBytes = iolist_to_list(P, AllBytes),

    %% Deep lists.

    ?line [] = iolist_to_list(P, [[]]),
    ?line [42] = iolist_to_list(P, [[], 42]),
    ?line [42] = iolist_to_list(P, [42, []]),
    ?line [42, 45] = iolist_to_list(P, [42, [], 45]),

    ?line [42, 90, 45] = iolist_to_list(P, [42, [90], 45]),
    ?line [42, 90, 45] = iolist_to_list(P, [[42, [90]], 45]),
    ?line [42, 90, 45] = iolist_to_list(P, [[42, [90]], 45]),

    %% List with binaries.

    ?line [] = iolist_to_list(P, [list_to_binary([])]),
    ?line [] = iolist_to_list(P, [[], list_to_binary([])]),
    ?line [1] = iolist_to_list(P, [[1], list_to_binary([])]),
    ?line [2] = iolist_to_list(P, [[], list_to_binary([2])]),
    ?line [42, 2] = iolist_to_list(P, [[42], list_to_binary([2])]),
    ?line [42, 2, 3, 4] = iolist_to_list(P, [[42], list_to_binary([2, 3, 4])]),

    %% Binaries as tail.

    ?line [] = iolist_to_list(P, [[]| list_to_binary([])]),
    ?line [1] = iolist_to_list(P, [[1]| list_to_binary([])]),
    ?line [2] = iolist_to_list(P, [[]| list_to_binary([2])]),
    ?line [42, 2] = iolist_to_list(P, [[42]| list_to_binary([2])]),

    %% Binaries only.

    ?line [] = iolist_to_list(P, list_to_binary("")),
    ?line [1] = iolist_to_list(P, list_to_binary([1])),
    ?line [1, 2] = iolist_to_list(P, list_to_binary([1, 2])),

    %% Illegal cases.

    ?line 'NULL' = iolist_to_list(P, [42|43]),
    ?line 'NULL' = iolist_to_list(P, a),

    ?line 'NULL' = iolist_to_list(P, [a]),
    ?line 'NULL' = iolist_to_list(P, [256]),
    ?line 'NULL' = iolist_to_list(P, [257]),
    ?line 'NULL' = iolist_to_list(P, [-1]),
    ?line 'NULL' = iolist_to_list(P, [-2]),
    ?line 'NULL' = iolist_to_list(P, [-127]),
    ?line 'NULL' = iolist_to_list(P, [-128]),

    ?line runner:finish(P),
    ok.

iolist_to_list(Port, Term) ->
    case call_erl_function(Port, Term) of
	'NULL' ->
	    'NULL';
	Bin when is_binary(Bin) ->
	    binary_to_list(Bin)
    end.

%% Tests the erl_iolist_to_string() function.

t_erl_iolist_to_string(suite) -> [];
t_erl_iolist_to_string(Config) when is_list(Config) ->
    ?line P = runner:start(?t_erl_iolist_to_string),

    %% Flat lists.

    ?line [0] = iolist_to_string(P, []),
    ?line [10, 0] = iolist_to_string(P, [10]),
    ?line [10, 20, 0] = iolist_to_string(P, [10, 20]),
    ?line [10, 20, 30, 0] = iolist_to_string(P, [10, 20, 30]),
    ?line AllBytes = lists:seq(1, 255)++[0],
    ?line AllBytes = iolist_to_string(P, lists:seq(1, 255)),

    %% Deep lists.

    ?line [0] = iolist_to_string(P, [[]]),
    ?line [42, 0] = iolist_to_string(P, [[], 42]),
    ?line [42, 0] = iolist_to_string(P, [42, []]),
    ?line [42, 45, 0] = iolist_to_string(P, [42, [], 45]),

    ?line [42, 90, 45, 0] = iolist_to_string(P, [42, [90], 45]),
    ?line [42, 90, 45, 0] = iolist_to_string(P, [[42, [90]], 45]),
    ?line [42, 90, 45, 0] = iolist_to_string(P, [[42, [90]], 45]),

    %% List with binaries.

    ?line [0] = iolist_to_string(P, [list_to_binary([])]),
    ?line [0] = iolist_to_string(P, [[], list_to_binary([])]),
    ?line [1, 0] = iolist_to_string(P, [[1], list_to_binary([])]),
    ?line [2, 0] = iolist_to_string(P, [[], list_to_binary([2])]),
    ?line [42, 2, 0] = iolist_to_string(P, [[42], list_to_binary([2])]),
    ?line [42, 2, 3, 4, 0] = iolist_to_string(P, [[42],
						  list_to_binary([2, 3, 4])]),

    %% Binaries as tail.

    ?line [0] = iolist_to_string(P, [[]| list_to_binary([])]),
    ?line [1, 0] = iolist_to_string(P, [[1]| list_to_binary([])]),
    ?line [2, 0] = iolist_to_string(P, [[]| list_to_binary([2])]),
    ?line [42, 2, 0] = iolist_to_string(P, [[42]| list_to_binary([2])]),

    %% Binaries only.

    ?line [0] = iolist_to_string(P, list_to_binary("")),
    ?line [1, 0] = iolist_to_string(P, list_to_binary([1])),
    ?line [1, 2, 0] = iolist_to_string(P, list_to_binary([1, 2])),

    %% Illegal cases.

    ?line 'NULL' = iolist_to_string(P, [0]),
    ?line 'NULL' = iolist_to_string(P, [65, 0, 66]),
    ?line 'NULL' = iolist_to_string(P, [65, 66, 67, 0]),

    ?line 'NULL' = iolist_to_string(P, [42|43]),
    ?line 'NULL' = iolist_to_string(P, a),

    ?line 'NULL' = iolist_to_string(P, [a]),
    ?line 'NULL' = iolist_to_string(P, [256]),
    ?line 'NULL' = iolist_to_string(P, [257]),
    ?line 'NULL' = iolist_to_string(P, [-1]),
    ?line 'NULL' = iolist_to_string(P, [-2]),
    ?line 'NULL' = iolist_to_string(P, [-127]),
    ?line 'NULL' = iolist_to_string(P, [-128]),

    ?line runner:finish(P),
    ok.

%% Invokes the erl_iolist_to_string() function.

iolist_to_string(Port, Term) ->
    runner:send_term(Port, Term),
    case get_term(Port) of
	{bytes, Result} -> Result;
	'NULL'       -> 'NULL'
    end.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%%	5.   M i s c e l l a n o u s   T e s t s
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

erl_print_term(suite) -> [];
erl_print_term(doc) -> "Tests the erl_print_term() function";
erl_print_term(Config) when is_list(Config) ->
    ?line PrintTerm = print_term(Config),
    ?line P = open_port({spawn, PrintTerm}, [stream]),

    %% Lists.

    ?line print(P, "[]", []),
    ?line print(P, "[a]", [a]),
    ?line print(P, "[[a]]", [[a]]),
    ?line print(P, "[[]]", [[]]),
    ?line print(P, "[a,b,c]", [a,b,c]),
    ?line print(P, "[a,b|c]", [a,b|c]),
    ?line print(P, "[a,[],c]", [a,[],c]),
    ?line print(P, "[a,[1000,1],c]", [a,[1000,1],c]),

    %% Tuples.

    ?line print(P, "{}", {}),
    ?line print(P, "{ok}", {ok}),
    ?line print(P, "{1,2,3}", {1, 2, 3}),

    %% Pids.

    ?line {_X, Y, Z} = split_pid(self()),
    ?line PidString = lists:flatten(io_lib:format("<~s.~w.~w>",
						  [node(), Y, Z])),
    ?line print(P, PidString, self()),

    ?line unlink(P),
    ?line exit(P, die),
    ok.

split_pid(Pid) when is_pid(Pid) ->
    split_pid(pid_to_list(Pid), 0, []).

split_pid([$<|Rest], Cur, Result) ->
    split_pid(Rest, Cur, Result);
split_pid([Digit|Rest], Cur, Result) when $0 =< Digit, Digit =< $9 ->
    split_pid(Rest, 10*Cur+Digit-$0, Result);
split_pid([$.|Rest], Cur, Result) ->
    split_pid(Rest, 0, Result++[Cur]);
split_pid([$>], Cur, Result) ->
    list_to_tuple(Result++[Cur]).

print_string(suite) -> [];
print_string(doc) -> "Test printing a string with erl_print_term()";
print_string(Config) when is_list(Config) ->
    ?line PrintTerm = print_term(Config),
    ?line P = open_port({spawn, PrintTerm}, [stream]),

    %% Strings.

    ?line print(P, "\"ABC\"", "ABC"),
    ?line {11, "\"\\tABC\\r\\n\""} = print(P, "\tABC\r\n"),

    %% Not strings.

    ?line print(P, "[65,66,67,0]", "ABC\000"),

    ?line unlink(P),
    ?line exit(P, die),
    ok.

print(Port, TermString, Term) ->
    Length = length(TermString),
    {Length, TermString} = print(Port, Term).

%% This function uses the erl_print_term() function in erl_interface
%% to print a term.
%% Returns: {NumChars, Chars}

print(Port, Term) ->
    Bin = term_to_binary(Term),
    Size = size(Bin),
    Port ! {self(), {command, [Size div 256, Size rem 256, Bin]}},
    collect_line(Port, []).

collect_line(Port, Result) ->
    receive
	{Port, {data, Data}} ->
	    case lists:reverse(Data) of
		[$\n|Rest] ->
		    collect_line1(Rest++Result, []);
		Chars ->
		    collect_line(Port, Chars++Result)
	    end
    after test_server:seconds(5) ->
	    test_server:fail("No response from C program")
    end.

collect_line1([$\r|Rest], Result) ->
    {list_to_integer(Result), lists:reverse(Rest)};
collect_line1([C|Rest], Result) ->
    collect_line1(Rest, [C|Result]).

%% Test case submitted by Per Lundgren, ERV.

high_chaparal(suite) -> [];
high_chaparal(Config) when is_list(Config) ->
    ?line P = runner:start(?high_chaparal),
    ?line {term, [hello, world]} = get_term(P),
    ?line runner:recv_eot(P),
    ok.

%% OTP-7448
broken_data(suite) -> [];
broken_data(Config) when is_list(Config) ->
    ?line P = runner:start(?broken_data),
    ?line runner:recv_eot(P),
    ok.

%% This calls a C function with one parameter and returns the result.

call_erl_function(Port, Term) ->
    runner:send_term(Port, Term),
    case get_term(Port) of
	{term, Result} -> Result;
	'NULL'       -> 'NULL'
    end.

print_term(Config) when is_list(Config) ->
    filename:join(?config(data_dir, Config), "print_term").



%%% We receive a ref from the cnode, and expect it to be a long ref.
%%% We also send a ref we created ourselves, and expect to get it
%%% back, without having been mutated into short form. We must take
%%% care then to check the actual returned ref, and not the original
%%% one, which is equal to it.
cnode_1(suite) -> [];
cnode_1(doc) -> "Tests involving cnode: sends a long ref from a cnode to us";
cnode_1(Config) when is_list(Config) ->
    ?line Cnode = filename:join(?config(data_dir, Config), "cnode"),
    ?line register(mip, self()),
    ?line spawn_link(?MODULE, start_cnode, [Cnode]),
    ?line Ref1 = get_ref(),
    io:format("Ref1 ~p~n", [Ref1]),
    ?line check_ref(Ref1),
    ?line Ref2 = make_ref(),
    ?line receive
	      Pid -> Pid
	  end,
    ?line Fun1 = fun(X) -> {Pid, X} end,	% sneak in a fun test here
    %?line Fun1 = {wait_with_funs, new_dist_format},
    ?line Term = {Ref2, Fun1, {1,2,3,4,5,6,7,8,9,10}},
    %% A term which will overflow the original buffer used in 'cnode'.
    ?line Pid ! Term,
    ?line receive
	      Term2 ->
		  io:format("received ~p~n", [Term2]),
		  case Term2 of
		      Term ->
			  {Ref22,_,_} = Term2,
			  ?line check_ref(Ref22);
		      X ->
		      test_server:fail({receive1,X})
		  end
	  after 5000 ->
		  test_server:fail(receive1)
	  end,
    ?line receive
	      Pid ->
		  ok;
	      Y ->
		  test_server:fail({receive1,Y})
	  after 5000 ->
		  test_server:fail(receive2)
	  end,
    ?line io:format("ref = ~p~n", [Ref1]),
    ?line check_ref(Ref1),
    ok.

check_ref(Ref) ->
    case bin_ext_type(Ref) of
	101 ->
	    test_server:fail(oldref);
	114 ->
	    ok;
	Type ->
	    test_server:fail({type, Type})
    end.

bin_ext_type(T) ->
    [131, Type | _] = binary_to_list(term_to_binary(T)),
    Type.

get_ref() ->
    receive
	X when is_reference(X) ->
	    X
    after 5000 ->
	    test_server:fail({cnode, timeout})
    end.

start_cnode(Cnode) ->
    open_port({spawn, Cnode ++ " " ++ atom_to_list(erlang:get_cookie())}, []),
    rec_cnode().

rec_cnode() ->
    receive
	X ->
	    io:format("from cnode: ~p~n", [X]),
	    rec_cnode()
    end.

nc2vinfo(Pid) when is_pid(Pid) ->
    ?line [_NodeStr, NumberStr, SerialStr]
	= string:tokens(pid_to_list(Pid), "<.>"),
    ?line Number = list_to_integer(NumberStr),
    ?line Serial = list_to_integer(SerialStr),
    ?line {pid, node(Pid), Number, Serial};
nc2vinfo(Port) when is_port(Port) ->
    ?line ["#Port", _NodeStr, NumberStr]
	= string:tokens(erlang:port_to_list(Port), "<.>"),
    ?line Number = list_to_integer(NumberStr),
    ?line {port, node(Port), Number};    
nc2vinfo(Ref) when is_reference(Ref) ->
    ?line ["#Ref", _NodeStr | NumStrList]
	= string:tokens(erlang:ref_to_list(Ref), "<.>"),
    ?line {Len, RevNumList} = lists:foldl(fun ("0", {N, []}) ->
						  {N+1, []};
					      (IStr, {N, Is}) ->
						  {N+1,
						   [list_to_integer(IStr)|Is]}
					  end,
					  {0, []},
					  NumStrList),
    ?line {ref, node(Ref), Len, lists:reverse(RevNumList)};
nc2vinfo(Other) ->
    ?line {badarg, Other}.