#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('newline-before-endstream');

my $n_tests = 12;

# From issue 133, http://verapdf.org/software/ is an open source
# package that can verify PDF/A compliance. This could potentially be
# useful for manual or automated verification that qpdf doesn't break
# PDF/A compliance should that ever be desired.

foreach my $d (
    ['--qdf', 'qdf', 'qdf'],
    ['--newline-before-endstream', 'newline', 'nl'],
    ['--qdf --newline-before-endstream', 'newline and qdf', 'nl-qdf'],
    ['--object-streams=generate --newline-before-endstream',
     'newline and object streams', 'nl-objstm'],
    )
{
    my ($flags, $description, $suffix) = @$d;
    $td->runtest("newline before endstream: $description",
                 {$td->COMMAND => "qpdf --static-id --stream-data=preserve" .
                      " $flags streams-with-newlines.pdf a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check output ($description)",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "newline-before-endstream-$suffix.pdf"});
    if ($flags =~ /qdf/)
    {
        $td->runtest("fix-qdf",
                     {$td->COMMAND => "fix-qdf a.pdf"},
                     {$td->FILE => "a.pdf", $td->EXIT_STATUS => 0});
    }
}

$td->runtest("newline before endstream (C)",
             {$td->COMMAND =>
                  "qpdf-ctest 22 streams-with-newlines.pdf '' a.pdf"},
             {$td->STRING => "C test 22 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "newline-before-endstream-nl.pdf"});

cleanup();
$td->report($n_tests);
