1
2# This is a test script of IPC::LDT,
3# using file handles to check the
4# switching between ASCII and object mode.
5# (This is just a combination of ascii.t and data.t.)
6
7
8# load modules
9use IPC::LDT;
10use FileHandle;
11use Data::Dumper;
12
13# display number of test
14print "1..12\n";
15
16# build temporary filename
17my $file="/tmp/.$$.ipc.ldt.tmp";
18
19# init the data to transfer
20my $msg="This is a simple\nmultiline check message.";
21my @msg=('This message', "contains\nof", 'several parts.');
22my $scalar=50;
23my @array=(3, 7, 15);
24my %hash=(a=>'A', z=>'Z');
25my $ref=\$IPC::LDT::VERSION;
26
27# write message
28{
29 # open file
30 open(O, ">$file") or die "[Fatal] Could not open $file for writing.\n";
31
32 # build LDT object
33 my $ldt=new IPC::LDT(handle=>*O) or die "[Fatal] Could not build LDT object.\n";
34
35 # send ASCII messages
36 $ldt->send($msg);
37 $ldt->send(@msg);
38
39 # switch to object mode
40 $ldt->setObjectMode;
41
42 # send data
43 $ldt->send($scalar, \@array, \%hash, $ref);
44
45 # switch to ASCII mode
46 $ldt->setAsciiMode;
47
48 # send ASCII messages again
49 $ldt->send($msg);
50 $ldt->send(@msg);
51
52 # switch to object mode
53 $ldt->setObjectMode;
54
55 # send data again
56 $ldt->send($scalar, \@array, \%hash, $ref);
57
58 # close the temporary file
59 close(O);
60}
61
62
63# read message
64{
65 # open file
66 open(I, $file) or die "[Fatal] Could not open $file for reading.\n";
67
68 # build LDT object
69 my $ldt=new IPC::LDT(handle=>*I) or die "[Fatal] Could not build LDT object.\n";
70
71 # read the messages
72 my $read1=$ldt->receive;
73 my $read2=$ldt->receive;
74
75 # switch to object mode
76 $ldt->setObjectMode;
77
78 # read data
79 my @data1=$ldt->receive;
80
81 # switch to ASCII mode
82 $ldt->setAsciiMode;
83
84 # read the messages again
85 my $read3=$ldt->receive;
86 my $read4=$ldt->receive;
87
88 # switch to object mode
89 $ldt->setObjectMode;
90
91 # read data again
92 my @data2=$ldt->receive;
93
94 # perform the ASCII checks
95 print $read1 eq $msg ? 'ok' : 'not ok', "\n";
96 print $read2 eq join('', @msg) ? 'ok' : 'not ok', "\n";
97
98 print $read3 eq $msg ? 'ok' : 'not ok', "\n";
99 print $read4 eq join('', @msg) ? 'ok' : 'not ok', "\n";
100
101 # perform the data checks
102 print $data1[0]==$scalar ? 'ok' : 'not ok', "\n";
103 print Dumper(@{$data1[1]}) eq Dumper(@array) ? 'ok' : 'not ok', "\n";
104 print Dumper(%{$data1[2]}) eq Dumper(%hash)  ? 'ok' : 'not ok', "\n";
105 print ${$data1[3]} eq $$ref ? 'ok' : 'not ok', "\n";
106
107 print $data2[0]==$scalar ? 'ok' : 'not ok', "\n";
108 print Dumper(@{$data2[1]}) eq Dumper(@array) ? 'ok' : 'not ok', "\n";
109 print Dumper(%{$data2[2]}) eq Dumper(%hash)  ? 'ok' : 'not ok', "\n";
110 print ${$data2[3]} eq $$ref ? 'ok' : 'not ok', "\n";
111
112 # close the temporary file
113 close(I);
114}
115
116# clean up
117unlink $file;
118