1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
package Common;
#--------------------
#
# GLOBALS...
#
#--------------------
use vars qw(@DATA_SA
@DATA_LA
$DATA_S
@ADATA_SA
$ADATA_S
$FDATA_S
@FDATA_LA
);
#------------------------------
# Data...
# ...as a scalar-array:
@DATA_SA = (
"A diner while ",
"dining at Crewe\n",
"Found a rather large ",
"mouse in his stew\n Said the waiter, \"Don't shout,\n",
" And ",
"wave it about..."
);
# ...as a string:
$DATA_S = join '', @DATA_SA;
# ...as a line-array:
@DATA_LA = lines($DATA_S);
# Additional data...
# ...as a scalar-array:
@ADATA_SA = (
"\nor the rest",
" will be wanting one ",
"too.\"\n",
);
# ...as a string:
$ADATA_S = join '', @ADATA_SA;
# Full data...
# ...as a string:
$FDATA_S = $DATA_S . $ADATA_S;
# ...as a line-array:
@FDATA_LA = lines($FDATA_S);
# Tester:
my $T;
# Scratch...
my $BUF = ''; # buffer
my $M; # message
#------------------------------
# lines STR
#------------------------------
sub lines {
my $s = shift;
split /^/, $s;
}
#------------------------------
# test_init PARAMHASH
#------------------------------
# Init common tests.
#
sub test_init {
my ($self, %p) = @_;
$T = $p{TBone};
}
#------------------------------
# test_print HANDLE, TEST
#------------------------------
# Test printing to handle.
#
sub test_print {
my ($self, $GH, $all) = @_;
local($_);
# Append with print:
$M = "PRINT: able to print to $GH";
$GH->print($ADATA_SA[0]);
$GH->print(@ADATA_SA[1..2]);
$T->ok(1, $M);
}
#------------------------------
# test_getc HANDLE
#------------------------------
# Test getc().
#
sub test_getc {
my ($self, $GH) = @_;
local($_);
my @c;
$M = "GETC: seek(0,0) and getc()";
$GH->seek(0,0);
for (0..2) { $c[$_] = $GH->getc };
$T->ok((($c[0] eq 'A') &&
($c[1] eq ' ') &&
($c[2] eq 'd')), $M);
}
#------------------------------
# test_getline HANDLE
#------------------------------
# Test getline() and getlines().
#
sub test_getline {
my ($self, $GH) = @_;
local($_);
$M = "GETLINE/SEEK3: seek(3,START) and getline() gets part of 1st line";
$GH->seek(3,0);
my $got = $GH->getline;
my $want = "iner while dining at Crewe\n";
$T->ok(($got eq $want), $M,
GH => $GH,
Got => $got,
Want => $want);
$M = "GETLINE/NEXT: next getline() gets subsequent line";
$_ = $GH->getline;
$T->ok(($_ eq "Found a rather large mouse in his stew\n"), $M,
Got => $_);
$M = "GETLINE/EOF: repeated getline() finds end of stream";
my $last;
for (1..6) { $last = $GH->getline }
$T->ok(!$last, $M,
Last => (defined($last) ? $last : 'undef'));
$M = "GETLINE/GETLINES: seek(0,0) and getlines() slurps in string";
$GH->seek(0,0);
my @got = $GH->getlines;
my $gots = join '', @got;
$T->ok(($gots eq $FDATA_S), $M,
GotAll => $gots,
WantAll => $FDATA_S,
Got => \@got);
}
#------------------------------
# test_read HANDLE
#------------------------------
# Test read().
#
sub test_read {
my ($self, $GH) = @_;
local($_);
$M = "READ/FIRST10: reading first 10 bytes with seek(0,START) + read(10)";
$GH->seek(0,0);
$GH->read($BUF,10);
$T->ok(($BUF eq "A diner wh"), $M);
$M = "READ/NEXT10: reading next 10 bytes with read(10)";
$GH->read($BUF,10);
$T->ok(($BUF eq "ile dining"), $M);
$M = "READ/TELL20: tell() the current location as 20";
$T->ok(($GH->tell == 20), $M);
$M = "READ/SLURP: seek(0,START) + read(1000) reads in whole handle";
$GH->seek(0,0);
$GH->read($BUF,1000);
$T->ok(($BUF eq $FDATA_S), $M);
}
#------------------------------
# test_seek HANDLE
#------------------------------
# Test seeks other than (0,0).
#
sub test_seek {
my ($self, $GH) = @_;
local($_);
$M = "SEEK/END: seek(-6,END) + read(3) returns 'too'";
$GH->seek(-6,2);
$GH->read($BUF,3);
$T->ok(($BUF eq 'too'), $M);
$M = "SEEK/CUR: seek(-3,CUR) + read(3) returns 'too' again";
$GH->seek(-3,1);
$GH->read($BUF,3);
$T->ok(($BUF eq 'too'), $M);
}
#------------------------------
# test_tie PARAMHASH
#------------------------------
# Test tiehandle getline() interface.
#
sub test_tie {
my ($self, %p) = @_;
my ($tieclass, @tieargs) = @{$p{TieArgs}};
local($_);
my @lines;
my $i;
my $nmatched;
$M = "TIE/TIE: able to tie";
tie(*OUT, $tieclass, @tieargs);
$T->ok(1, $M);
$M = "TIE/PRINT: printing data";
print OUT @DATA_SA;
print OUT $ADATA_SA[0];
print OUT @ADATA_SA[1..2];
$T->ok(1, $M);
$M = "TIE/GETLINE: seek(0,0) and scalar <> get expected lines";
tied(*OUT)->seek(0,0); # rewind
@lines = (); push @lines, $_ while <OUT>; # get lines one at a time
$nmatched = 0; # total up matches...
for (0..$#lines) { ++$nmatched if ($lines[$_] eq $FDATA_LA[$_]) };
$T->ok(($nmatched == int(@FDATA_LA)), $M,
Want => \@FDATA_LA,
Gotl => \@lines,
Lines=> "0..$#lines",
Match=> $nmatched,
FDatl=> int(@FDATA_LA),
FData=> \@FDATA_LA);
$M = "TIE/GETLINES: seek(0,0) and array <> slurps in lines";
tied(*OUT)->seek(0,0); # rewind
@lines = <OUT>; # get lines all at once
$nmatched = 0; # total up matches...
for (0..$#lines) { ++$nmatched if ($lines[$_] eq $FDATA_LA[$_]) };
$T->ok(($nmatched == int(@FDATA_LA)), $M,
Want => \@FDATA_LA,
Gotl => \@lines,
Lines=> "0..$#lines",
Match=> $nmatched);
}
#------------------------------
1;
|