summaryrefslogtreecommitdiffstats
path: root/libtests
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2018-06-22 18:15:22 +0200
committerJay Berkenbilt <ejb@ql.org>2018-06-22 18:52:45 +0200
commit4ccc8b1a44109a913c87f31029c9c17db30ecc43 (patch)
treefb2259c3507896c252037aca9b14e2d93acb2259 /libtests
parent32ddcec91e77b9f6a52480c3393ab17cbc105880 (diff)
downloadqpdf-4ccc8b1a44109a913c87f31029c9c17db30ecc43.tar.zst
Add ClosedFileInputSource
ClosedFileInputSource is an input source that keeps the file closed when not reading it.
Diffstat (limited to 'libtests')
-rw-r--r--libtests/build.mk1
-rw-r--r--libtests/closed_file_input_source.cc62
-rw-r--r--libtests/qtest/closedfile.test16
-rw-r--r--libtests/qtest/closedfile/input5
-rw-r--r--libtests/qtest/closedfile/output3
5 files changed, 87 insertions, 0 deletions
diff --git a/libtests/build.mk b/libtests/build.mk
index 0b895225..e8f20270 100644
--- a/libtests/build.mk
+++ b/libtests/build.mk
@@ -3,6 +3,7 @@ BINS_libtests = \
ascii85 \
bits \
buffer \
+ closed_file_input_source \
concatenate \
dct_compress \
dct_uncompress \
diff --git a/libtests/closed_file_input_source.cc b/libtests/closed_file_input_source.cc
new file mode 100644
index 00000000..9fd3eec7
--- /dev/null
+++ b/libtests/closed_file_input_source.cc
@@ -0,0 +1,62 @@
+#include <qpdf/ClosedFileInputSource.hh>
+#include <qpdf/FileInputSource.hh>
+
+#include <stdio.h>
+#include <string.h>
+#include <iostream>
+#include <stdlib.h>
+
+void check(std::string const& what, bool result)
+{
+ if (! result)
+ {
+ std::cout << "FAIL: " << what << std::endl;
+ }
+}
+
+void do_tests(InputSource* is)
+{
+ check("get name", "input" == is->getName());
+ check("initial tell", 0 == is->tell());
+ is->seek(11, SEEK_SET);
+ check("tell after SEEK_SET", 11 == is->tell());
+ check("read offset 11", "Offset 11" == is->readLine(100));
+ check("last offset after read 11", 11 == is->getLastOffset());
+ check("tell after read", 21 == is->tell());
+ is->findAndSkipNextEOL();
+ check("tell after findAndSkipNextEOL", 522 == is->tell());
+ is->unreadCh('Q');
+ char b[1];
+ b[0] = '\0';
+ check("read unread character", 1 == is->read(b, 1));
+ check("last offset after read unread", 521 == is->getLastOffset());
+ check("got character", 'Q' == b[0]);
+ is->seek(0, SEEK_END);
+ check("tell at end", 556 == is->tell());
+ is->seek(-25, SEEK_END);
+ check("tell before end", 531 == is->tell());
+ check("last offset unchanged after seek", 521 == is->getLastOffset());
+ is->seek(-9, SEEK_CUR);
+ check("tell after SEEK_CUR", 522 == is->tell());
+ check("read offset 522", "9 before" == is->readLine(100));
+ check("last offset after read", 522 == is->getLastOffset());
+ is->rewind();
+ check("last offset unchanged after rewind", 522 == is->getLastOffset());
+ check("tell after rewind", 0 == is->tell());
+ check("read offset at beginning", "!00000000?" == is->readLine(100));
+ check("last offset after read 0", 0 == is->getLastOffset());
+}
+
+int main()
+{
+ // This test is designed to work with a specified input file.
+ std::cout << "testing with ClosedFileInputSource\n";
+ ClosedFileInputSource cf("input");
+ do_tests(&cf);
+ std::cout << "testing with FileInputSource\n";
+ FileInputSource f;
+ f.setFilename("input");
+ do_tests(&f);
+ std::cout << "all assertions passed" << std::endl;
+ return 0;
+}
diff --git a/libtests/qtest/closedfile.test b/libtests/qtest/closedfile.test
new file mode 100644
index 00000000..864f9c12
--- /dev/null
+++ b/libtests/qtest/closedfile.test
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl
+require 5.008;
+use warnings;
+use strict;
+
+chdir("closedfile") or die "chdir testdir failed: $!\n";
+
+require TestDriver;
+
+my $td = new TestDriver('closed_file_input_source');
+
+$td->runtest("closed file input source",
+ {$td->COMMAND => "closed_file_input_source"},
+ {$td->FILE => "output", $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
+$td->report(1);
diff --git a/libtests/qtest/closedfile/input b/libtests/qtest/closedfile/input
new file mode 100644
index 00000000..25dbd5c2
--- /dev/null
+++ b/libtests/qtest/closedfile/input
@@ -0,0 +1,5 @@
+!00000000?
+Offset 11
+wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
+9 before
+This is 25 from the end.
diff --git a/libtests/qtest/closedfile/output b/libtests/qtest/closedfile/output
new file mode 100644
index 00000000..7a5554af
--- /dev/null
+++ b/libtests/qtest/closedfile/output
@@ -0,0 +1,3 @@
+testing with ClosedFileInputSource
+testing with FileInputSource
+all assertions passed