File: Common.pm

package info (click to toggle)
io-stringy 2.111-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 320 kB
  • sloc: perl: 1,427; makefile: 5
file content (365 lines) | stat: -rw-r--r-- 8,355 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
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.
# 1
#
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().
# 1
#
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().
# 4
#
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().
# 4
#
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).
# 2
#
sub test_seek {
    my ($self, $GH) = @_;
    local($_);

    $M = "SEEK/SET: seek(2,SET) + read(5) returns 'diner'";
    $GH->seek(2,0);
    $GH->read($BUF,5);
    $T->ok_eq($BUF, 'diner', 
	      $M);

    $M = "SEEK/END: seek(-6,END) + read(3) returns 'too'";
    $GH->seek(-6,2);
    $GH->read($BUF,3);
    $T->ok_eq($BUF, 'too', 
	      $M);

    $M = "SEEK/CUR: seek(-7,CUR) + read(7) returns 'one too'"; 
    $GH->seek(-7,1);
    $GH->read($BUF,7);
    $T->ok_eq($BUF, 'one too',
	      $M);
}

#------------------------------
# test_tie PARAMHASH
#------------------------------
# Test tiehandle getline() interface.
# 4
#
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,
	   TieClass => $tieclass,
	   TieArgs => \@tieargs);

    $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);

#    $M = "TIE/TELL: telling data";
#    my $tell_oo  = tied(*OUT)->tell;
#    my $tell_tie = tell OUT;
#    $T->ok(($tell_oo == $tell_tie), $M,
#	   Want => $tell_oo,
#	   Gotl => $tell_tie);

}

#------------------------------
# test_recordsep
#------------------------------
# Try $/ tests.
#
#    3 x undef
#    3 x empty
#    2 x custom
#   11 x newline
#
sub test_recordsep_count {
    my ($self, $seps) = @_;
    my $count = 0;
    $count += 3 if ($seps =~ /undef/) ;
    $count += 3 if ($seps =~ /empty/) ;
    $count += 2 if ($seps =~ /custom/) ;
    $count += 11 if ($seps =~ /newline/); 
    $count;
}
sub test_recordsep {
    my ($self, $seps, $opener) = @_;
    my $GH;
    my @lines = ("par 1, line 1\n",
		 "par 1, line 2\n",
		 "\n",
		 "\n",
		 "\n",
		 "\n",
		 "par 2, line 1\n",
		 "\n",
		 "par 3, line 1\n",
		 "par 3, line 2\n",
		 "par 3, line 3");
    my $all = join('', @lines);

    ### Slurp everything:
    if ($seps =~ /undef/) {
	$GH = &$opener(\@lines);
        local $/ = undef;
        $T->ok_eq($GH->getline, $all,
                  "RECORDSEP undef: getline slurps everything");
    }

    ### Read a little, slurp the rest:
    if ($seps =~ /undef/) {
	$GH = &$opener(\@lines);
        $T->ok_eq($GH->getline, $lines[0],
		  "RECORDSEP undef: get first line");
        local $/ = undef;
        $T->ok_eq($GH->getline, join('', @lines[1..$#lines]),
		  "RECORDSEP undef: slurp the rest");
    }

    ### Read paragraph by paragraph:
    if ($seps =~ /empty/) {
	$GH = &$opener(\@lines);
        local $/ = "";
        $T->ok_eq($GH->getline, join('', @lines[0..2]),
                  "RECORDSEP empty: first par");
        $T->ok_eq($GH->getline, join('', @lines[6..7]),
                  "RECORDSEP empty: second par");
        $T->ok_eq($GH->getline, join('', @lines[8..10]),
                  "RECORDSEP empty: third par");
    }

    ### Read record by record:
    if ($seps =~ /custom/) {
	$GH = &$opener(\@lines);
        local $/ = "1,";
        $T->ok_eq($GH->getline, "par 1,",
                  "RECORDSEP custom: first rec");
        $T->ok_eq($GH->getline, " line 1\npar 1,",
                  "RECORDSEP custom: second rec");
    }

    ### Read line by line:
    if ($seps =~ /newline/) {
	$GH = &$opener(\@lines);
        local $/ = "\n";
	for my $i (0..10) {
	    $T->ok_eq($GH->getline, $lines[$i],
		      "RECORDSEP newline: rec $i");
	}
    }

}

#------------------------------
1;