3 use Test::More tests => 252;
4 # for SEEK_SET etc, Fcntl doesn't provide these in 5.005_03
7 BEGIN { use_ok(Imager => ':all') };
9 -d "testout" or mkdir "testout";
13 Imager->open_log(log => "testout/t07iolayer.log");
16 # start by testing io buffer
18 my $data="P2\n2 2\n255\n 255 0\n0 255\n";
19 my $IO = Imager::io_new_buffer($data);
20 my $im = Imager::i_readpnm_wiol($IO, -1);
22 ok($im, "read from data io");
24 open(FH, ">testout/t07.ppm") or die $!;
27 my $IO2 = Imager::io_new_fd( $fd );
28 Imager::i_writeppm_wiol($im, $IO2);
32 open(FH, "<testimg/penguin-base.ppm");
36 my $IO3 = Imager::IO->new_buffer($data);
38 $im = Imager::i_readpnm_wiol($IO3, -1);
40 ok($im, "read from buffer, for compare");
43 open(FH, "<testimg/penguin-base.ppm") or die $!;
46 my $IO4 = Imager::IO->new_fd( $fd );
47 my $im2 = Imager::i_readpnm_wiol($IO4, -1);
51 ok($im2, "read from file, for compare");
53 is(i_img_diff($im, $im2), 0, "compare images");
56 my $IO5 = Imager::io_new_bufchain();
57 Imager::i_writeppm_wiol($im, $IO5);
58 my $data2 = Imager::io_slurp($IO5);
61 ok($data2, "check we got data from bufchain");
63 my $IO6 = Imager::io_new_buffer($data2);
64 my $im3 = Imager::i_readpnm_wiol($IO6, -1);
66 is(Imager::i_img_diff($im, $im3), 0, "read from buffer");
71 my ($size, $maxread) = @_;
72 my $out = substr($work, $pos, $maxread);
77 my ($size, $maxread) = @_;
78 my $out = substr($work, $pos, $maxread);
82 my $IO7 = Imager::IO->new_cb(undef, \&io_reader, undef, undef);
83 ok($IO7, "making readcb object");
84 my $im4 = Imager::i_readpnm_wiol($IO7, -1);
85 ok($im4, "read from cb");
86 ok(Imager::i_img_diff($im, $im4) == 0, "read from cb image match");
89 $IO7 = Imager::io_new_cb(undef, \&io_reader2, undef, undef);
90 ok($IO7, "making short readcb object");
91 my $im5 = Imager::i_readpnm_wiol($IO7, -1);
92 ok($im4, "read from cb2");
93 is(Imager::i_img_diff($im, $im5), 0, "read from cb2 image match");
97 substr($work, $pos, $pos+length $what) = $what;
108 my $IO8 = Imager::io_new_cb(\&io_writer, undef, undef, \&io_close);
109 ok($IO8, "making writecb object");
112 ok(Imager::i_writeppm_wiol($im, $IO8), "write to cb");
113 # I originally compared this to $data, but that doesn't include the
115 is($work, $data2, "write image match");
116 ok($did_close, "did close");
118 # with a short buffer, no closer
119 my $IO9 = Imager::io_new_cb(\&io_writer, undef, undef, undef, 1);
120 ok($IO9, "making short writecb object");
123 ok(Imager::i_writeppm_wiol($im, $IO9), "write to short cb");
124 is($work, $data2, "short write image match");
127 my $buf_data = "Test data";
128 my $io9 = Imager::io_new_buffer($buf_data);
129 is(ref $io9, "Imager::IO", "check class");
131 is($io9->raw_read($work, 4), 4, "read 4 from buffer object");
132 is($work, "Test", "check data read");
133 is($io9->raw_read($work, 5), 5, "read the rest");
134 is($work, " data", "check data read");
135 is($io9->raw_seek(5, SEEK_SET), 5, "seek");
136 is($io9->raw_read($work, 5), 4, "short read");
137 is($work, "data", "check data read");
138 is($io9->raw_seek(-1, SEEK_CUR), 8, "seek relative");
139 is($io9->raw_seek(-5, SEEK_END), 4, "seek relative to end");
140 is($io9->raw_seek(-10, SEEK_CUR), -1, "seek failure");
144 my $io = Imager::IO->new_bufchain();
145 is(ref $io, "Imager::IO", "check class");
146 is($io->raw_write("testdata"), 8, "check write");
147 is($io->raw_seek(-8, SEEK_CUR), 0, "seek relative");
149 is($io->raw_read($work, 8), 8, "check read");
150 is($work, "testdata", "check data read");
151 is($io->raw_seek(-3, SEEK_END), 5, "seek end relative");
152 is($io->raw_read($work, 5), 3, "short read");
153 is($work, "ata", "check read data");
154 is($io->raw_seek(4, SEEK_SET), 4, "absolute seek to write some");
155 is($io->raw_write("testdata"), 8, "write");
156 is($io->raw_seek(0, SEEK_CUR), 12, "check size");
160 my $data = Imager::io_slurp($io);
161 is($data, "testtestdata", "check we have the right data");
164 { # callback failure checks
165 my $fail_io = Imager::io_new_cb(\&fail_write, \&fail_read, \&fail_seek, undef, 1);
168 my $read_result = $fail_io->raw_read($buffer, 10);
169 is($read_result, undef, "read failure undef in scalar context");
170 my @read_result = $fail_io->raw_read($buffer, 10);
171 is(@read_result, 0, "empty list in list context");
172 $read_result = $fail_io->raw_read2(10);
173 is($read_result, undef, "raw_read2 failure (scalar)");
174 @read_result = $fail_io->raw_read2(10);
175 is(@read_result, 0, "raw_read2 failure (list)");
177 my $write_result = $fail_io->raw_write("test");
178 is($write_result, -1, "failed write");
180 my $seek_result = $fail_io->raw_seek(-1, SEEK_SET);
181 is($seek_result, -1, "failed seek");
184 { # callback success checks
185 my $good_io = Imager::io_new_cb(\&good_write, \&good_read, \&good_seek, undef, 1);
188 my $read_result = $good_io->raw_read($buffer, 10);
189 is($read_result, 8, "read success (scalar)");
190 is($buffer, "testdata", "check data");
191 my @read_result = $good_io->raw_read($buffer, 10);
192 is_deeply(\@read_result, [ 8 ], "read success (list)");
193 is($buffer, "testdata", "check data");
194 $read_result = $good_io->raw_read2(10);
195 is($read_result, "testdata", "read2 success (scalar)");
196 @read_result = $good_io->raw_read2(10);
197 is_deeply(\@read_result, [ "testdata" ], "read2 success (list)");
201 my $eof_io = Imager::io_new_cb(undef, \&eof_read, undef, undef, 1);
203 my $read_result = $eof_io->raw_read($buffer, 10);
204 is($read_result, 0, "read eof (scalar)");
205 is($buffer, '', "check data");
206 my @read_result = $eof_io->raw_read($buffer, 10);
207 is_deeply(\@read_result, [ 0 ], "read eof (list)");
208 is($buffer, '', "check data");
212 my $none_io = Imager::io_new_cb(undef, undef, undef, undef, 0);
213 is($none_io->raw_write("test"), -1, "write with no writecb should fail");
215 is($none_io->raw_read($buffer, 10), undef, "read with no readcb should fail");
216 is($none_io->raw_seek(0, SEEK_SET), -1, "seek with no seekcb should fail");
220 { # make sure we croak when trying to write a string with characters over 0xff
221 # the write callback shouldn't get called
222 skip("no native UTF8 support in this version of perl", 2)
224 my $io = Imager::io_new_cb(\&good_write, undef, undef, 1);
225 my $data = chr(0x100);
226 is(ord $data, 0x100, "make sure we got what we expected");
229 $io->raw_write($data);
231 ok($@, "should have croaked")
235 { # 0.52 left some debug code in a path that wasn't tested, make sure
236 # that path is tested
237 # http://rt.cpan.org/Ticket/Display.html?id=20705
238 my $io = Imager::io_new_cb
241 print "# write $_[0]\n";
245 print "# read $_[0], $_[1]\n";
248 sub { print "# seek\n"; 0 },
249 sub { print "# close\n"; 1 });
251 is($io->raw_read($buffer, 10), 10, "read 10");
252 is($buffer, "xxxxxxxxxx", "read value");
253 ok($io->raw_write("foo"), "write");
254 is($io->raw_close, 0, "close");
258 { # fd_seek write failure
260 or skip("No /dev/full", 3);
261 open my $fh, "> /dev/full"
262 or skip("Can't open /dev/full: $!", 3);
263 my $io = Imager::io_new_fd(fileno($fh));
264 ok($io, "make fd io for /dev/full");
265 Imager::i_clear_error();
266 is($io->raw_write("test"), -1, "fail to write");
267 my $msg = Imager->_error_as_msg;
268 like($msg, qr/^write\(\) failure: /, "check error message");
271 # /dev/full succeeds on seek on Linux
277 { # fd_seek seek failure
278 my $seekfail = "testout/t07seekfail.dat";
279 open my $fh, "> $seekfail"
280 or skip("Can't open $seekfail: $!", 3);
281 my $io = Imager::io_new_fd(fileno($fh));
282 ok($io, "make fd io for $seekfail");
284 Imager::i_clear_error();
285 is($io->raw_seek(-1, SEEK_SET), -1, "shouldn't be able to seek to -1");
286 my $msg = Imager->_error_as_msg;
287 like($msg, qr/^lseek\(\) failure: /, "check error message");
296 { # fd_seek read failure
297 open my $fh, "> testout/t07writeonly.txt"
298 or skip("Can't open testout/t07writeonly.txt: $!", 3);
299 my $io = Imager::io_new_fd(fileno($fh));
300 ok($io, "make fd io for write-only");
302 Imager::i_clear_error();
304 is($io->raw_read($buf, 10), undef,
305 "file open for write shouldn't be readable");
306 my $msg = Imager->_error_as_msg;
307 like($msg, qr/^read\(\) failure: /, "check error message");
315 open my $fh, "> testout/t07readeof.txt"
316 or skip("Can't open testout/t07readeof.txt: $!", 5);
320 open my $fhr, "< testout/t07readeof.txt",
321 or skip("Can't open testout/t07readeof.txt: $!", 5);
322 my $io = Imager::io_new_fd(fileno($fhr));
323 ok($io, "make fd io for read eof");
325 Imager::i_clear_error();
327 is($io->raw_read($buf, 10), 4,
328 "10 byte read on 4 byte file should return 4");
329 my $msg = Imager->_error_as_msg;
330 is($msg, "", "should be no error message")
331 or print STDERR "# read(4) message is: $msg\n";
333 Imager::i_clear_error();
335 is($io->raw_read($buf, 10), 0,
336 "10 byte read at end of 4 byte file should return 0 (eof)");
338 $msg = Imager->_error_as_msg;
339 is($msg, "", "should be no error message")
340 or print STDERR "# read(4), eof message is: $msg\n";
346 my $data="P2\n2 2\n255\n 255 0\n0 255\n";
347 my $io = Imager::io_new_buffer($data);
351 is($c, ord "P", "getc");
352 my $peekc = $io->peekc();
354 is($peekc, ord "2", "peekc");
356 my $peekn = $io->peekn(2);
357 is($peekn, "2\n", "peekn");
360 is($c, ord "2", "getc after peekc/peekn");
362 is($io->seek(0, SEEK_SET), "0", "seek");
363 is($io->getc, ord "P", "check we got back to the start");
366 { # test closecb result is propagated
367 my $success_cb = sub { 1 };
368 my $failure_cb = sub { 0 };
371 my $io = Imager::io_new_cb(undef, $success_cb, undef, $success_cb);
372 is($io->close(), 0, "test successful close");
375 my $io = Imager::io_new_cb(undef, $success_cb, undef, $failure_cb);
376 is($io->close(), -1, "test failed close");
380 { # buffered coverage/function tests
381 # some data to play with
382 my $base = pack "C*", map rand(26) + ord("a"), 0 .. 20_001;
384 { # buffered accessors
385 my $io = Imager::io_new_buffer($base);
386 ok($io->set_buffered(0), "set unbuffered");
387 ok(!$io->is_buffered, "verify unbuffered");
388 ok($io->set_buffered(1), "set buffered");
389 ok($io->is_buffered, "verify buffered");
392 { # initial i_io_read(), buffered
399 my $req_size = $size;
401 if ($pos + $size > length $work) {
402 $size = length($work) - $pos;
405 my $result = substr($work, $pos, $size);
407 $ops .= "R$req_size>$size;";
409 print "# read $req_size>$size\n";
416 substr($work, $pos, length($data), $data);
421 my $io = Imager::io_new_cb(undef, $read, undef, undef);
423 is($io->read($buf, 1000), 1000, "read initial 1000");
424 is($buf, substr($base, 0, 1000), "check data read");
425 is($ops, "R8192>8192;", "check read op happened to buffer size");
428 is($io->read($buf, 1001), 1001, "read another 1001");
429 is($buf, substr($base, 1000, 1001), "check data read");
430 is($ops, "R8192>8192;", "should be no further reads");
433 is($io->read($buf, 40_000), length($base) - 2001,
434 "read the rest in one chunk");
435 is($buf, substr($base, 2001), "check the data read");
436 my $buffer_left = 8192 - 2001;
437 my $after_buffer = length($base) - 8192;
438 is($ops, "R8192>8192;R".(40_000 - $buffer_left).">$after_buffer;R21999>0;",
439 "check we tried to read the remainder");
442 # read after write errors
443 my $io = Imager::io_new_cb($write, $read, undef, undef);
444 is($io->write("test"), 4, "write 4 bytes, io in write mode");
445 is($io->read2(10), undef, "read should fail");
446 is($io->peekn(10), undef, "peekn should fail");
447 is($io->getc(), -1, "getc should fail");
448 is($io->peekc(), -1, "peekc should fail");
453 my $io = Imager::io_new_buffer($base);
454 print "# buffer fill check\n";
455 ok($io, "make memory io");
457 is($io->read($buf, 4096), 4096, "read 4k");
458 is($buf, substr($base, 0, 4096), "check data is correct");
462 is($io->peekn(5120), substr($base, 4096, 5120),
463 "peekn() 5120, which should exceed the buffer, and only read the left overs");
467 my $io = Imager::io_new_buffer($base);
468 is($io->peekn(10), substr($base, 0, 10),
469 "make sure initial peekn() is sane");
470 is($io->read2(10), substr($base, 0, 10),
471 "and that reading 10 gets the expected data");
475 my $io = Imager::io_new_buffer($base);
476 is($io->peekn(10_000), substr($base, 0, 8192),
477 "peekn() larger than buffer should return buffer-size bytes");
480 { # small peekn then large peekn with a small I/O back end
481 # this might happen when reading from a socket
488 my $req_size = $size;
489 # do small reads, to trigger a possible bug
494 if ($pos + $size > length $work) {
495 $size = length($work) - $pos;
498 my $result = substr($work, $pos, $size);
500 $ops .= "R$req_size>$size;";
502 print "# read $req_size>$size\n";
506 my $io = Imager::io_new_cb(undef, $reader, undef, undef);
507 ok($io, "small reader io");
508 is($io->peekn(25), substr($base, 0, 25), "peek 25");
509 is($ops, "R8192>10;R8182>10;R8172>10;",
510 "check we got the raw calls expected");
511 is($io->peekn(65), substr($base, 0, 65), "peek 65");
512 is($ops, "R8192>10;R8182>10;R8172>10;R8162>10;R8152>10;R8142>10;R8132>10;",
513 "check we got the raw calls expected");
515 for my $buffered (1, 0) { # peekn followed by errors
516 my $buffered_desc = $buffered ? "buffered" : "unbuffered";
522 my $req_size = $size;
523 if ($pos + $size > length $base) {
524 $size = length($base) - $pos;
526 # error instead of eof
528 print "# read $req_size>error\n";
531 my $result = substr($base, $pos, $size);
534 print "# read $req_size>$size\n";
538 my $io = Imager::io_new_cb(undef, $reader, undef, undef);
539 ok($io, "make $buffered_desc cb with error after 6 bytes");
540 is($io->peekn(5), "abcde",
541 "peekn until just before error ($buffered_desc)");
542 is($io->peekn(6), "abcdef", "peekn until error ($buffered_desc)");
543 is($io->peekn(7), "abcdef", "peekn past error ($buffered_desc)");
545 "should be no error indicator, since data buffered ($buffered_desc)");
547 "should be no eof indicator, since data buffered ($buffered_desc)");
550 is($io->read2(6), "abcdef", "consume the buffer ($buffered_desc)");
551 is($io->peekn(10), undef,
552 "peekn should get an error indicator ($buffered_desc)");
553 ok($io->error, "should be an error state ($buffered_desc)");
554 ok(!$io->eof, "but not eof ($buffered_desc)");
556 { # peekn on an empty file
557 my $io = Imager::io_new_buffer("");
558 is($io->peekn(10), "", "peekn on empty source");
559 ok($io->eof, "should be in eof state");
560 ok(!$io->error, "but not error");
562 { # peekn on error source
563 my $io = Imager::io_new_cb(undef, sub { return; }, undef, undef);
564 is($io->peekn(10), undef, "peekn on empty source");
565 ok($io->error, "should be in error state");
566 ok(!$io->eof, "but not eof");
568 { # peekn on short source
569 my $io = Imager::io_new_buffer("abcdef");
570 is($io->peekn(4), "abcd", "peekn 4 on 6 byte source");
571 is($io->peekn(10), "abcdef", "followed by peekn 10 on 6 byte source");
572 is($io->peekn(10), "abcdef", "and again, now eof is set");
575 Imager::i_clear_error();
576 my $io = Imager::io_new_buffer("abcdef");
577 is($io->peekn(0), undef, "peekn 0 on 6 byte source");
578 my $msg = Imager->_error_as_msg;
579 is($msg, "peekn size must be positive");
581 { # getc through a whole file (buffered)
582 my $io = Imager::io_new_buffer($base);
584 while ((my $c = $io->getc()) != -1) {
587 is($out, $base, "getc should return the file byte by byte (buffered)");
588 is($io->getc, -1, "another getc after eof should fail too");
589 ok($io->eof, "should be marked eof");
590 ok(!$io->error, "shouldn't be marked in error");
592 { # getc through a whole file (unbuffered)
593 my $io = Imager::io_new_buffer($base);
594 $io->set_buffered(0);
596 while ((my $c = $io->getc()) != -1) {
599 is($out, $base, "getc should return the file byte by byte (unbuffered)");
600 is($io->getc, -1, "another getc after eof should fail too");
601 ok($io->eof, "should be marked eof");
602 ok(!$io->error, "shouldn't be marked in error");
604 { # buffered getc with an error
605 my $io = Imager::io_new_cb(undef, sub { return; }, undef, undef);
606 is($io->getc, -1, "buffered getc error");
607 ok($io->error, "io marked in error");
608 ok(!$io->eof, "but not eof");
610 { # unbuffered getc with an error
611 my $io = Imager::io_new_cb(undef, sub { return; }, undef, undef);
612 $io->set_buffered(0);
613 is($io->getc, -1, "unbuffered getc error");
614 ok($io->error, "io marked in error");
615 ok(!$io->eof, "but not eof");
617 { # initial peekc - buffered
618 my $io = Imager::io_new_buffer($base);
620 is($c, ord($base), "buffered peekc matches");
621 is($io->peekc, $c, "duplicate peekc matchess");
623 { # initial peekc - unbuffered
624 my $io = Imager::io_new_buffer($base);
625 $io->set_buffered(0);
627 is($c, ord($base), "unbuffered peekc matches");
628 is($io->peekc, $c, "duplicate peekc matchess");
630 { # initial peekc eof - buffered
631 my $io = Imager::io_new_cb(undef, sub { "" }, undef, undef);
633 is($c, -1, "buffered eof peekc is -1");
634 is($io->peekc, $c, "duplicate matches");
635 ok($io->eof, "io marked eof");
636 ok(!$io->error, "but not error");
638 { # initial peekc eof - unbuffered
639 my $io = Imager::io_new_cb(undef, sub { "" }, undef, undef);
640 $io->set_buffered(0);
642 is($c, -1, "buffered eof peekc is -1");
643 is($io->peekc, $c, "duplicate matches");
644 ok($io->eof, "io marked eof");
645 ok(!$io->error, "but not error");
647 { # initial peekc error - buffered
648 my $io = Imager::io_new_cb(undef, sub { return; }, undef, undef);
650 is($c, -1, "buffered error peekc is -1");
651 is($io->peekc, $c, "duplicate matches");
652 ok($io->error, "io marked error");
653 ok(!$io->eof, "but not eof");
655 { # initial peekc error - unbuffered
656 my $io = Imager::io_new_cb(undef, sub { return; }, undef, undef);
657 $io->set_buffered(0);
659 is($c, -1, "unbuffered error peekc is -1");
660 is($io->peekc, $c, "duplicate matches");
661 ok($io->error, "io marked error");
662 ok(!$io->eof, "but not eof");
665 my $io = Imager::io_new_bufchain();
666 is($io->putc(ord "A"), ord "A", "initial putc buffered");
667 is($io->close, 0, "close it");
668 is(Imager::io_slurp($io), "A", "check it was written");
670 { # initial putc - unbuffered
671 my $io = Imager::io_new_bufchain();
672 $io->set_buffered(0);
673 is($io->putc(ord "A"), ord "A", "initial putc unbuffered");
674 is($io->close, 0, "close it");
675 is(Imager::io_slurp($io), "A", "check it was written");
677 { # putc unbuffered with error
678 my $io = Imager::io_new_cb(undef, undef, undef, undef);
679 $io->set_buffered(0);
680 is($io->putc(ord "A"), -1, "initial putc unbuffered error");
681 ok($io->error, "io in error");
682 is($io->putc(ord "B"), -1, "still in error");
684 { # writes while in read state
685 my $io = Imager::io_new_cb(sub { 1 }, sub { return "AA" }, undef, undef);
686 is($io->getc, ord "A", "read to setup read buffer");
687 is($io->putc(ord "B"), -1, "putc should fail");
688 is($io->write("test"), -1, "write should fail");
690 { # buffered putc error handling
691 # tests the check for error state in the buffered putc code
692 my $io = Imager::io_new_cb(undef, undef, undef, undef);
694 ok(!$io->flush, "flush should fail");
695 ok($io->error, "should be in error state");
696 is($io->putc(ord "B"), -1, "check for error");
698 { # buffered putc flush error handling
699 # test handling of flush failure and of the error state resulting
701 my $io = Imager::io_new_cb(undef, undef, undef, undef);
703 while (++$i < 100_000 && $io->putc(ord "A") == ord "A") {
704 # until we have to flush and fail doing do
706 is($i, 8193, "should have failed on 8193rd byte");
707 ok($io->error, "should be in error state");
708 is($io->putc(ord "B"), -1, "next putc should fail");
710 { # buffered write flush error handling
711 # test handling of flush failure and of the error state resulting
713 my $io = Imager::io_new_cb(undef, undef, undef, undef);
715 while (++$i < 100_000 && $io->write("A") == 1) {
716 # until we have to flush and fail doing do
718 is($i, 8193, "should have failed on 8193rd byte");
719 ok($io->error, "should be in error state");
720 is($io->write("B"), -1, "next write should fail");
722 { # buffered read error
723 my $io = Imager::io_new_cb(undef, undef, undef, undef);
724 is($io->read2(10), undef, "initial read returning error");
725 ok($io->error, "should be in error state");
727 { # unbuffered read error
728 my $io = Imager::io_new_cb(undef, undef, undef, undef);
729 $io->set_buffered(0);
730 is($io->read2(10), undef, "initial read returning error");
731 ok($io->error, "should be in error state");
733 { # unbuffered write error
735 my $io = Imager::io_new_cb(sub { return $count++; }, undef, undef, undef);
736 $io->set_buffered(0);
737 is($io->write("A"), -1, "unbuffered write failure");
738 ok($io->error, "should be in error state");
739 is($io->write("BC"), -1, "should still fail");
741 { # buffered write + large write
742 my $io = Imager::io_new_bufchain();
743 is($io->write(substr($base, 0, 4096)), 4096,
744 "should be buffered");
745 is($io->write(substr($base, 4096)), length($base) - 4096,
746 "large write, should fill buffer and fall back to direct write");
747 is($io->close, 0, "close it");
748 is(Imager::io_slurp($io), $base, "make sure the data is correct");
750 { # initial large write with failure
751 # tests error handling for the case where we bypass the buffer
752 # when the write is too large to fit
753 my $io = Imager::io_new_cb(undef, undef, undef, undef);
754 ok($io->flush, "flush with nothing buffered should succeed");
755 is($io->write($base), -1, "large write failure");
756 ok($io->error, "should be in error state");
757 is($io->close, -1, "should fail to close");
759 { # write that causes a flush then fills the buffer a bit
760 my $io = Imager::io_new_bufchain();
761 is($io->write(substr($base, 0, 6000)), 6000, "fill the buffer a bit");
762 is($io->write(substr($base, 6000, 4000)), 4000,
763 "cause it to flush and then fill some more");
764 is($io->write(substr($base, 10000)), length($base)-10000,
765 "write out the rest of our test data");
766 is($io->close, 0, "close the stream");
767 is(Imager::io_slurp($io), $base, "make sure the data is right");
769 { # failure on flush on close
770 my $io = Imager::io_new_cb(undef, undef, undef, undef);
771 is($io->putc(ord "A"), ord "A", "something in the buffer");
772 ok(!$io->error, "should be no error yet");
773 is($io->close, -1, "close should failure due to flush error");
776 my $io = Imager::io_new_cb(undef, undef, undef, undef);
777 is($io->seek(0, SEEK_SET), -1, "seek failure");
779 { # read a little and seek
780 my $io = Imager::io_new_buffer($base);
781 is($io->getc, ord $base, "read one");
782 is($io->getc, ord substr($base, 1, 1), "read another");
783 is($io->seek(-1, SEEK_CUR), 1, "seek relative back to origin+1");
784 is($io->getc, ord substr($base, 1, 1), "read another again");
786 { # seek with failing flush
787 my $io = Imager::io_new_cb(undef, undef, undef, undef);
788 is($io->putc(ord "A"), ord "A", "write one");
789 ok(!$io->error, "not in error mode (yet)");
790 is($io->seek(0, SEEK_SET), -1, "seek failure due to flush");
791 ok($io->error, "in error mode");
794 my $data = "test1\ntest2\ntest3";
795 my $io = Imager::io_new_buffer($data);
796 is($io->gets(6), "test1\n", "gets(6)");
797 is($io->gets(5), "test2", "gets(5) (short for the line)");
798 is($io->gets(10), "\n", "gets(10) the rest of the line (the newline)");
799 is($io->gets(), "test3", "gets(default) unterminated line");
802 my $data = "test1\ntest2\ntest3";
803 my $io = Imager::io_new_buffer($data);
804 is($io->gets(6, ord("1")), "test1", "gets(6) (line terminator 1)");
805 is($io->gets(6, ord("2")), "\ntest2", "gets(6) (line terminator 2)");
806 is($io->gets(6, ord("3")), "\ntest3", "gets(6) (line terminator 3)");
807 is($io->getc, -1, "should be eof");
811 { # based on discussion on IRC, user was attempting to write a TIFF
812 # image file with only a write callback, but TIFF requires seek and
813 # read callbacks when writing.
814 # https://rt.cpan.org/Ticket/Display.html?id=76782
815 my $cb = Imager::io_new_cb(undef, undef, undef, undef);
817 Imager::i_clear_error();
819 is($cb->read($data, 10), undef, "default read callback should fail");
820 is(Imager->_error_as_msg(), "read callback called but no readcb supplied",
821 "check error message");
824 Imager::i_clear_error();
825 is($cb->raw_write("abc"), -1, "default write callback should fail");
826 is(Imager->_error_as_msg(), "write callback called but no writecb supplied",
827 "check error message");
830 Imager::i_clear_error();
831 is($cb->seek(0, 0), -1, "default seek callback should fail");
832 is(Imager->_error_as_msg(), "seek callback called but no seekcb supplied",
833 "check error message");
839 unless ($ENV{IMAGER_KEEP_FILES}) {
840 unlink "testout/t07.ppm", "testout/t07iolayer.log";
852 my $data = "testdata";
853 length $data <= $max_len or substr($data, $max_len) = '';
855 print "# good_read ($max_len) => $data\n";