diff options
Diffstat (limited to 'libtests')
32 files changed, 953 insertions, 1059 deletions
diff --git a/libtests/aes.cc b/libtests/aes.cc index 71348744..3ad491cc 100644 --- a/libtests/aes.cc +++ b/libtests/aes.cc @@ -1,14 +1,15 @@ #include <qpdf/Pl_AES_PDF.hh> #include <qpdf/Pl_StdioFile.hh> -#include <qpdf/QUtil.hh> #include <qpdf/QIntC.hh> +#include <qpdf/QUtil.hh> -#include <stdio.h> -#include <string.h> #include <iostream> +#include <stdio.h> #include <stdlib.h> +#include <string.h> -static void usage() +static void +usage() { std::cerr << "Usage: aes options hex-key infile outfile" << std::endl << " -cbc -- disable CBC mode" << std::endl @@ -16,13 +17,15 @@ static void usage() << " -encrypt -- encrypt" << std::endl << " -decrypt -- decrypt CBC mode" << std::endl << " -zero-iv -- use zero initialization vector" << std::endl - << " -static-iv -- use static initialization vector" << std::endl + << " -static-iv -- use static initialization vector" + << std::endl << " -no-padding -- disable padding" << std::endl << "Options must precede key and file names." << std::endl; exit(2); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { bool encrypt = true; bool cbc_mode = true; @@ -33,58 +36,36 @@ int main(int argc, char* argv[]) bool static_iv = false; bool disable_padding = false; - for (int i = 1; i < argc; ++i) - { + for (int i = 1; i < argc; ++i) { char* arg = argv[i]; - if ((arg[0] == '-') || (arg[0] == '+')) - { - if (strcmp(arg, "-cbc") == 0) - { + if ((arg[0] == '-') || (arg[0] == '+')) { + if (strcmp(arg, "-cbc") == 0) { cbc_mode = false; - } - else if (strcmp(arg, "+cbc") == 0) - { + } else if (strcmp(arg, "+cbc") == 0) { cbc_mode = true; - } - else if (strcmp(arg, "-decrypt") == 0) - { + } else if (strcmp(arg, "-decrypt") == 0) { encrypt = false; - } - else if (strcmp(arg, "-encrypt") == 0) - { + } else if (strcmp(arg, "-encrypt") == 0) { encrypt = true; - } - else if (strcmp(arg, "-zero-iv") == 0) - { + } else if (strcmp(arg, "-zero-iv") == 0) { zero_iv = true; - } - else if (strcmp(arg, "-static-iv") == 0) - { + } else if (strcmp(arg, "-static-iv") == 0) { static_iv = true; - } - else if (strcmp(arg, "-no-padding") == 0) - { + } else if (strcmp(arg, "-no-padding") == 0) { disable_padding = true; - } - else - { + } else { usage(); } - } - else if (argc == i + 3) - { + } else if (argc == i + 3) { hexkey = argv[i]; - infilename = argv[i+1]; - outfilename = argv[i+2]; + infilename = argv[i + 1]; + outfilename = argv[i + 2]; break; - } - else - { + } else { usage(); } } - if (outfilename == 0) - { + if (outfilename == 0) { usage(); } @@ -94,50 +75,40 @@ int main(int argc, char* argv[]) FILE* infile = QUtil::safe_fopen(infilename, "rb"); FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); unsigned char* key = new unsigned char[keylen]; - for (unsigned int i = 0; i < strlen(hexkey); i += 2) - { + for (unsigned int i = 0; i < strlen(hexkey); i += 2) { char t[3]; t[0] = hexkey[i]; t[1] = hexkey[i + 1]; t[2] = '\0'; long val = strtol(t, 0, 16); - key[i/2] = static_cast<unsigned char>(val); + key[i / 2] = static_cast<unsigned char>(val); } Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); Pl_AES_PDF* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key, keylen); - delete [] key; + delete[] key; key = 0; - if (! cbc_mode) - { + if (!cbc_mode) { aes->disableCBC(); } - if (zero_iv) - { + if (zero_iv) { aes->useZeroIV(); - } - else if (static_iv) - { + } else if (static_iv) { aes->useStaticIV(); } - if (disable_padding) - { + if (disable_padding) { aes->disablePadding(); } // 16 < buffer size, buffer_size is not a multiple of 8 for testing unsigned char buf[83]; bool done = false; - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), infile); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { aes->write(buf, len); } } diff --git a/libtests/arg_parser.cc b/libtests/arg_parser.cc index 4b10709e..8b4d556a 100644 --- a/libtests/arg_parser.cc +++ b/libtests/arg_parser.cc @@ -1,8 +1,8 @@ #include <qpdf/QPDFArgParser.hh> -#include <qpdf/QUtil.hh> #include <qpdf/QPDFUsage.hh> -#include <iostream> +#include <qpdf/QUtil.hh> #include <cstring> +#include <iostream> #ifdef NDEBUG // We need assert even in a release build for test code. @@ -59,39 +59,39 @@ ArgParser::initOptions() char const* choices[] = {"pig", "boar", "sow", 0}; ap.addChoices("oink", p(&ArgParser::handleOink), true, choices); ap.selectHelpOptionTable(); - ap.addBare("version", [this](){ output("3.14159"); }); + ap.addBare("version", [this]() { output("3.14159"); }); ap.selectMainOptionTable(); ap.addBare("quack", b(&ArgParser::startQuack)); ap.registerOptionTable("quack", b(&ArgParser::endQuack)); ap.addPositional(p(&ArgParser::getQuack)); ap.addFinalCheck(b(&ArgParser::finalChecks)); ap.selectMainOptionTable(); - ap.addBare("baaa", [this](){ this->ap.selectOptionTable("baaa"); }); + ap.addBare("baaa", [this]() { this->ap.selectOptionTable("baaa"); }); ap.registerOptionTable("baaa", nullptr); - ap.addBare("ewe", [this](){ output("you"); }); - ap.addBare("ram", [this](){ output("ram"); }); + ap.addBare("ewe", [this]() { output("you"); }); + ap.addBare("ram", [this]() { output("ram"); }); ap.selectMainOptionTable(); - ap.addBare("sheep", [this](){ this->ap.selectOptionTable("sheep"); }); + ap.addBare("sheep", [this]() { this->ap.selectOptionTable("sheep"); }); ap.registerOptionTable("sheep", nullptr); ap.addHelpFooter("For more help, read the manual.\n"); ap.addHelpTopic( - "quack", "Quack Options", + "quack", + "Quack Options", "Just put stuff after quack to get a count at the end.\n"); ap.addHelpTopic( - "baaa", "Baaa Options", + "baaa", + "Baaa Options", "Ewe can do sheepish things.\n" "For example, ewe can add more ram to your computer.\n"); - ap.addOptionHelp("--ewe", "baaa", - "just for ewe", "You are not a ewe.\n"); + ap.addOptionHelp("--ewe", "baaa", "just for ewe", "You are not a ewe.\n"); ap.addOptionHelp("--ram", "baaa", "curly horns", ""); } void ArgParser::output(std::string const& msg) { - if (! this->ap.isCompleting()) - { + if (!this->ap.isCompleting()) { std::cout << msg << std::endl; } } @@ -130,10 +130,8 @@ void ArgParser::startQuack() { this->ap.selectOptionTable("quack"); - if (this->ap.isCompleting()) - { - if (this->ap.isCompleting() && (this->ap.argsLeft() == 0)) - { + if (this->ap.isCompleting()) { + if (this->ap.isCompleting() && (this->ap.argsLeft() == 0)) { this->ap.insertCompletion("something"); this->ap.insertCompletion("anything"); } @@ -145,8 +143,7 @@ void ArgParser::getQuack(std::string const& p) { ++this->quacks; - if (this->ap.isCompleting() && (this->ap.argsLeft() == 0)) - { + if (this->ap.isCompleting() && (this->ap.argsLeft() == 0)) { this->ap.insertCompletion( std::string("thing-") + QUtil::int_to_string(this->quacks)); return; @@ -170,74 +167,56 @@ void ArgParser::test_exceptions() { auto err = [](char const* msg, std::function<void()> fn) { - try - { + try { fn(); assert(msg == nullptr); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << msg << ": " << e.what() << std::endl; } }; err("duplicate handler", [this]() { ap.selectMainOptionTable(); - ap.addBare("potato", [](){}); + ap.addBare("potato", []() {}); }); err("duplicate handler", [this]() { ap.selectOptionTable("baaa"); - ap.addBare("ram", [](){}); - }); - err("duplicate table", [this]() { - ap.registerOptionTable("baaa", nullptr); - }); - err("unknown table", [this]() { - ap.selectOptionTable("aardvark"); - }); - err("add existing help topic", [this]() { - ap.addHelpTopic("baaa", "potato", "salad"); - }); - err("add reserved help topic", [this]() { - ap.addHelpTopic("all", "potato", "salad"); - }); - err("add to unknown topic", [this]() { - ap.addOptionHelp("--new", "oops", "potato", "salad"); - }); - err("bad option for help", [this]() { - ap.addOptionHelp("nodash", "baaa", "potato", "salad"); - }); - err("bad topic for help", [this]() { - ap.addHelpTopic("--dashes", "potato", "salad"); - }); - err("duplicate option help", [this]() { - ap.addOptionHelp("--ewe", "baaa", "potato", "salad"); + ap.addBare("ram", []() {}); }); + err("duplicate table", + [this]() { ap.registerOptionTable("baaa", nullptr); }); + err("unknown table", [this]() { ap.selectOptionTable("aardvark"); }); + err("add existing help topic", + [this]() { ap.addHelpTopic("baaa", "potato", "salad"); }); + err("add reserved help topic", + [this]() { ap.addHelpTopic("all", "potato", "salad"); }); + err("add to unknown topic", + [this]() { ap.addOptionHelp("--new", "oops", "potato", "salad"); }); + err("bad option for help", + [this]() { ap.addOptionHelp("nodash", "baaa", "potato", "salad"); }); + err("bad topic for help", + [this]() { ap.addHelpTopic("--dashes", "potato", "salad"); }); + err("duplicate option help", + [this]() { ap.addOptionHelp("--ewe", "baaa", "potato", "salad"); }); err("invalid choice handler to unknown", [this]() { - ap.addInvalidChoiceHandler( - "elephant", [](std::string const&){}); + ap.addInvalidChoiceHandler("elephant", [](std::string const&) {}); }); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { ArgParser ap(argc, argv); - if ((argc == 2) && (strcmp(argv[1], "exceptions") == 0)) - { + if ((argc == 2) && (strcmp(argv[1], "exceptions") == 0)) { ap.test_exceptions(); return 0; } - try - { + try { ap.parseArgs(); - } - catch (QPDFUsage& e) - { + } catch (QPDFUsage& e) { std::cerr << "usage: " << e.what() << std::endl; exit(2); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << "exception: " << e.what() << std::endl; exit(3); } diff --git a/libtests/ascii85.cc b/libtests/ascii85.cc index 91f3b7f5..2acc56ba 100644 --- a/libtests/ascii85.cc +++ b/libtests/ascii85.cc @@ -4,31 +4,25 @@ #include <iostream> #include <stdlib.h> -int main() +int +main() { Pl_StdioFile out("stdout", stdout); Pl_ASCII85Decoder decode("decode", &out); - try - { + try { unsigned char buf[10000]; bool done = false; - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), stdin); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { decode.write(buf, len); } } decode.finish(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << e.what() << std::endl; exit(2); } diff --git a/libtests/bits.cc b/libtests/bits.cc index 2c3dc349..07870eb7 100644 --- a/libtests/bits.cc +++ b/libtests/bits.cc @@ -1,8 +1,8 @@ #include <qpdf/BitStream.hh> #include <qpdf/BitWriter.hh> #include <qpdf/Pl_Buffer.hh> -#include <qpdf/QUtil.hh> #include <qpdf/QIntC.hh> +#include <qpdf/QUtil.hh> #include <iostream> #include <stdio.h> #include <stdlib.h> @@ -14,8 +14,7 @@ #include <qpdf/bits_functions.hh> static void -print_values(long long byte_offset, size_t bit_offset, - size_t bits_available) +print_values(long long byte_offset, size_t bit_offset, size_t bits_available) { std::cout << "byte offset = " << byte_offset << ", " << "bit offset = " << bit_offset << ", " @@ -23,9 +22,12 @@ print_values(long long byte_offset, size_t bit_offset, } static void -test_read_bits(unsigned char const* buf, - unsigned char const*& p, size_t& bit_offset, - size_t& bits_available, size_t bits_wanted) +test_read_bits( + unsigned char const* buf, + unsigned char const*& p, + size_t& bit_offset, + size_t& bits_available, + size_t bits_wanted) { unsigned long result = QIntC::to_ulong(read_bits(p, bit_offset, bits_available, bits_wanted)); @@ -36,8 +38,12 @@ test_read_bits(unsigned char const* buf, } static void -test_write_bits(unsigned char& ch, size_t& bit_offset, unsigned long val, - size_t bits, Pl_Buffer* bp) +test_write_bits( + unsigned char& ch, + size_t& bit_offset, + unsigned long val, + size_t bits, + Pl_Buffer* bp) { write_bits(ch, bit_offset, val, bits, bp); std::cout << "ch = " << QUtil::uint_to_string_base(ch, 16, 2) @@ -51,8 +57,7 @@ print_buffer(Pl_Buffer* bp) Buffer* b = bp->getBuffer(); unsigned char const* p = b->getBuffer(); size_t l = b->getSize(); - for (unsigned long i = 0; i < l; ++i) - { + for (unsigned long i = 0; i < l; ++i) { std::cout << QUtil::uint_to_string_base(p[i], 16, 2) << ((i == l - 1) ? "\n" : " "); } @@ -69,8 +74,7 @@ test() // Read tests static unsigned char const buf[] = { - 0xF5, 0x15, 0x65, 0x79, 0x12, 0x89, 0x75, 0x4B - }; + 0xF5, 0x15, 0x65, 0x79, 0x12, 0x89, 0x75, 0x4B}; unsigned char const* p = buf; size_t bit_offset = 7; @@ -89,12 +93,9 @@ test() test_read_bits(buf, p, bit_offset, bits_available, 0); test_read_bits(buf, p, bit_offset, bits_available, 25); - try - { + try { test_read_bits(buf, p, bit_offset, bits_available, 4); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << "exception: " << e.what() << std::endl; print_values(p - buf, bit_offset, bits_available); } @@ -179,14 +180,12 @@ test() delete bp; } -int main() +int +main() { - try - { + try { test(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << "unexpected exception: " << e.what() << std::endl; exit(2); } diff --git a/libtests/buffer.cc b/libtests/buffer.cc index 5dfe55f8..a24c0f99 100644 --- a/libtests/buffer.cc +++ b/libtests/buffer.cc @@ -2,10 +2,10 @@ #include <qpdf/Pl_Count.hh> #include <qpdf/Pl_Discard.hh> #include <qpdf/QUtil.hh> -#include <stdlib.h> -#include <stdexcept> -#include <iostream> #include <cstring> +#include <iostream> +#include <stdexcept> +#include <stdlib.h> #ifdef NDEBUG // We need assert even in a release build for test code. @@ -13,12 +13,14 @@ #endif #include <cassert> -static unsigned char* uc(char const* s) +static unsigned char* +uc(char const* s) { return QUtil::unsigned_char_pointer(s); } -int main() +int +main() { { // Test that buffers can be copied by value. @@ -39,8 +41,7 @@ int main() assert(bc2p[1] == 'W'); } - try - { + try { Pl_Discard discard; Pl_Count count("count", &discard); Pl_Buffer bp1("bp1", &count); @@ -68,12 +69,9 @@ int main() Pl_Buffer bp2("bp2"); bp2.write(uc("moo"), 3); bp2.write(uc("quack"), 6); - try - { + try { delete bp2.getBuffer(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << e.what() << std::endl; } bp2.finish(); @@ -84,9 +82,7 @@ int main() unsigned char lbuf[10]; Buffer b1(lbuf, 10); - if (! ((b1.getBuffer() == lbuf) && - (b1.getSize() == 10))) - { + if (!((b1.getBuffer() == lbuf) && (b1.getSize() == 10))) { throw std::logic_error("hand-created buffer is not as expected"); } @@ -110,13 +106,10 @@ int main() bp4.write(uc("asdf"), 4); unsigned char* mbuf; size_t len; - try - { + try { bp4.getMallocBuffer(&mbuf, &len); assert(false); - } - catch (std::logic_error& e) - { + } catch (std::logic_error& e) { std::cout << "malloc buffer logic error: " << e.what() << std::endl; } bp4.finish(); @@ -129,9 +122,7 @@ int main() bp4.getMallocBuffer(&mbuf, &len); assert(mbuf == nullptr); assert(len == 0); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << "unexpected exception: " << e.what() << std::endl; exit(2); } diff --git a/libtests/closed_file_input_source.cc b/libtests/closed_file_input_source.cc index f3e4f2ac..ff5354fb 100644 --- a/libtests/closed_file_input_source.cc +++ b/libtests/closed_file_input_source.cc @@ -1,20 +1,21 @@ #include <qpdf/ClosedFileInputSource.hh> #include <qpdf/FileInputSource.hh> -#include <stdio.h> -#include <string.h> #include <iostream> +#include <stdio.h> #include <stdlib.h> +#include <string.h> -void check(std::string const& what, bool result) +void +check(std::string const& what, bool result) { - if (! result) - { + if (!result) { std::cout << "FAIL: " << what << std::endl; } } -void do_tests(InputSource* is) +void +do_tests(InputSource* is) { check("get name", "input" == is->getName()); check("initial tell", 0 == is->tell()); @@ -59,7 +60,8 @@ void do_tests(InputSource* is) check("last offset after read 0", 0 == is->getLastOffset()); } -int main() +int +main() { // This test is designed to work with a specified input file. std::cout << "testing with ClosedFileInputSource\n"; diff --git a/libtests/concatenate.cc b/libtests/concatenate.cc index 84fee2bb..10902adf 100644 --- a/libtests/concatenate.cc +++ b/libtests/concatenate.cc @@ -1,6 +1,6 @@ +#include <qpdf/Pl_Buffer.hh> #include <qpdf/Pl_Concatenate.hh> #include <qpdf/Pl_Flate.hh> -#include <qpdf/Pl_Buffer.hh> #include <qpdf/QUtil.hh> #include <iostream> @@ -10,13 +10,15 @@ #endif #include <cassert> -static void pipeStringAndFinish(Pipeline* p, std::string const& str) +static void +pipeStringAndFinish(Pipeline* p, std::string const& str) { p->write(QUtil::unsigned_char_pointer(str), str.length()); p->finish(); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { Pl_Buffer b1("compressed"); Pl_Flate deflate("compress", &b1, Pl_Flate::a_deflate); @@ -31,14 +33,11 @@ int main(int argc, char* argv[]) inflate.write(b1_buf->getBuffer(), b1_buf->getSize()); inflate.finish(); auto b2_buf = b2.getBufferSharedPointer(); - std::string result(reinterpret_cast<char*>(b2_buf->getBuffer()), - b2_buf->getSize()); - if (result == "-one--two-") - { + std::string result( + reinterpret_cast<char*>(b2_buf->getBuffer()), b2_buf->getSize()); + if (result == "-one--two-") { std::cout << "concatenate test passed" << std::endl; - } - else - { + } else { std::cout << "concatenate test failed: " << result << std::endl; } return 0; diff --git a/libtests/cxx11.cc b/libtests/cxx11.cc index 5ad3e6ed..fbf3cc38 100644 --- a/libtests/cxx11.cc +++ b/libtests/cxx11.cc @@ -1,13 +1,13 @@ -#include <iostream> -#include <cstring> +#include <cstdint> #include <cstdlib> +#include <cstring> #include <functional> -#include <type_traits> -#include <cstdint> -#include <vector> +#include <iostream> #include <map> #include <memory> #include <regex> +#include <type_traits> +#include <vector> #ifdef NDEBUG // We need assert even in a release build for test code. @@ -18,29 +18,31 @@ // Functional programming // Function that returns a callable in the form of a lambda -std::function<int (int)> make_adder(int x) +std::function<int(int)> +make_adder(int x) { - return ([=](int a) -> int{ return x + a; }); + return ([=](int a) -> int { return x + a; }); } -void do_functional() +void +do_functional() { // Lambda with no capture - auto simple_lambda = [](int a){ return a + 3; }; + auto simple_lambda = [](int a) { return a + 3; }; assert(simple_lambda(5) == 8); // Capture by value int x = 5; - auto by_value = [x](int a){ return a + x; }; + auto by_value = [x](int a) { return a + x; }; assert(by_value(1) == 6); x = 7; // change not seen by lambda assert(by_value(1) == 6); // Also >> at end of template - assert((std::is_convertible<decltype(by_value), - std::function<int(int)>>::value)); + assert((std::is_convertible<decltype(by_value), std::function<int(int)>>:: + value)); // Capture by reference - auto by_reference = [&x](int a){ return a + x; }; + auto by_reference = [&x](int a) { return a + x; }; assert(by_reference(1) == 8); x = 8; // change seen my lambda assert(by_reference(1) == 9); @@ -49,29 +51,29 @@ void do_functional() auto add3 = make_adder(3); assert(add3(5) == 8); - auto make_addr_lambda = [](int a) { - return [a](int b) { return a + b; }; - }; + auto make_addr_lambda = [](int a) { return [a](int b) { return a + b; }; }; assert(make_addr_lambda(6)(8) == 14); // nullptr and {} are empty functions std::function<void()> f1 = {}; - assert(! f1); + assert(!f1); std::function<void()> f2 = nullptr; - assert(! f2); + assert(!f2); } // Integer types, type traits template <typename T> -void check_size(size_t size, bool is_signed) +void +check_size(size_t size, bool is_signed) { assert(sizeof(T) == size); assert(std::is_signed<T>::value == is_signed); } -void do_inttypes() +void +do_inttypes() { // static_assert is a compile-time check static_assert(1 == sizeof(int8_t), "int8_t check"); @@ -99,10 +101,12 @@ class A { } // Constructor delegation - A() : A(def_value) + A() : + A(def_value) { } - int getX() const + int + getX() const { return x; } @@ -111,66 +115,71 @@ class A int x; }; -void do_iteration() +void +do_iteration() { // Initializers, foreach syntax, auto for iterators - std::vector<int> v = { 1, 2, 3, 4 }; + std::vector<int> v = {1, 2, 3, 4}; assert(v.size() == 4); int sum = 0; - for (auto& i: v) - { + for (auto& i : v) { sum += i; } assert(10 == sum); - for (auto i = v.begin(); i != v.end(); ++i) - { + for (auto i = v.begin(); i != v.end(); ++i) { sum += *i; } assert(20 == sum); - std::vector<A> v2 = { A(), A(3) }; + std::vector<A> v2 = {A(), A(3)}; assert(5 == v2.at(0).getX()); assert(3 == v2.at(1).getX()); } // Variadic template -template<class A1> -void variadic1(A1 const& a1) +template <class A1> +void +variadic1(A1 const& a1) { assert(a1 == 12); } -template<class A1, class A2> -void variadic1(A1 const& a1, A2 const& a2) +template <class A1, class A2> +void +variadic1(A1 const& a1, A2 const& a2) { assert(a1 == a2); } -template<class ...Args> -void variadic(Args... args) +template <class... Args> +void +variadic(Args... args) { variadic1(args...); } -template<class A> -bool pairwise_equal(A const& a, A const& b) +template <class A> +bool +pairwise_equal(A const& a, A const& b) { return (a == b); } -template<class T, class ...Rest> -bool pairwise_equal(T const& a, T const& b, Rest... rest) +template <class T, class... Rest> +bool +pairwise_equal(T const& a, T const& b, Rest... rest) { return pairwise_equal(a, b) && pairwise_equal(rest...); } -void do_variadic() +void +do_variadic() { variadic(15, 15); variadic(12); assert(pairwise_equal(5, 5, 2.0, 2.0, std::string("a"), std::string("a"))); - assert(! pairwise_equal(5, 5, 2.0, 3.0)); + assert(!pairwise_equal(5, 5, 2.0, 3.0)); } // deleted, default @@ -182,10 +191,12 @@ class B x(x) { } - B() : B(5) + B() : + B(5) { } - int getX() const + int + getX() const { return x; } @@ -198,12 +209,13 @@ class B int x; }; -void do_default_deleted() +void +do_default_deleted() { B b1; assert(5 == b1.getX()); assert(std::is_copy_constructible<A>::value); - assert(! std::is_copy_constructible<B>::value); + assert(!std::is_copy_constructible<B>::value); } // smart pointers @@ -220,38 +232,40 @@ class C { decr(id); } - C(C const& rhs) : C(rhs.id) + C(C const& rhs) : + C(rhs.id) { } - C& operator=(C const& rhs) + C& + operator=(C const& rhs) { - if (&rhs != this) - { + if (&rhs != this) { decr(id); id = rhs.id; incr(id); } return *this; } - static void check(size_t size, int v, int count) + static void + check(size_t size, int v, int count) { assert(m.size() == size); auto p = m.find(v); - if (p != m.end()) - { + if (p != m.end()) { assert(p->second == count); } } private: - void incr(int i) + void + incr(int i) { ++m[i]; } - void decr(int i) + void + decr(int i) { - if (--m[i] == 0) - { + if (--m[i] == 0) { m.erase(i); } } @@ -262,29 +276,30 @@ class C std::map<int, int> C::m; -std::shared_ptr<C> make_c(int id) +std::shared_ptr<C> +make_c(int id) { return std::make_shared<C>(id); } -std::shared_ptr<C> make_c_array(std::vector<int> const& is) +std::shared_ptr<C> +make_c_array(std::vector<int> const& is) { auto p = std::shared_ptr<C>(new C[is.size()], std::default_delete<C[]>()); C* pp = p.get(); - for (size_t i = 0; i < is.size(); ++i) - { + for (size_t i = 0; i < is.size(); ++i) { pp[i] = C(is.at(i)); } return p; } -void do_smart_pointers() +void +do_smart_pointers() { auto p1 = make_c(1); C::check(1, 1, 1); auto p2 = make_c_array({2, 3, 4, 5}); - for (auto i: {1, 2, 3, 4, 5}) - { + for (auto i : {1, 2, 3, 4, 5}) { C::check(5, i, 1); } { @@ -306,7 +321,8 @@ void do_smart_pointers() // Regular expressions -void do_regex() +void +do_regex() { // Basic match/search. Match matches whole string; search searches // within string. Use std::smatch for matching std::string and @@ -320,9 +336,9 @@ void do_regex() std::string str4("234"); std::string str5("234 five 678"); std::smatch match1; - assert(! std::regex_match(str1, match1, expr1)); - assert(! std::regex_match(str2, match1, expr1)); - assert(! std::regex_match(str3, match1, expr1)); + assert(!std::regex_match(str1, match1, expr1)); + assert(!std::regex_match(str2, match1, expr1)); + assert(!std::regex_match(str3, match1, expr1)); assert(std::regex_match(str4, match1, expr1)); assert(match1[0].first == match1[1].first); assert(match1[0].second == match1[1].second); @@ -347,8 +363,7 @@ void do_regex() std::sregex_iterator m1(str6.begin(), str6.end(), expr3); std::sregex_iterator m2; s.clear(); - for (std::sregex_iterator iter = m1; iter != m2; ++iter) - { + for (std::sregex_iterator iter = m1; iter != m2; ++iter) { std::smatch const& match2 = *iter; s += match2[0].str() + "|"; } @@ -360,24 +375,25 @@ void do_regex() std::cregex_iterator m3(str7, str7 + std::strlen(str7), expr4); assert("asdf" == (*m3)[0].str()); assert((*m3)[1].matched); - assert(! (*m3)[2].matched); + assert(!(*m3)[2].matched); ++m3; assert("qwer" == (*m3)[0].str()); - assert(! (*m3)[1].matched); + assert(!(*m3)[1].matched); assert((*m3)[2].matched); } -static long operator ""_x(char const* v) +static long operator""_x(char const* v) { return strtol(v, nullptr, 16); } -static std::string operator ""_y(char const* v, size_t len) +static std::string operator""_y(char const* v, size_t len) { return "y" + std::string(v, len) + "y"; } -void do_user_defined_literals() +void +do_user_defined_literals() { assert(10_x == 16); // operator""_x("10") assert("abc"_y == "yabcy"); // operator""_y("abc", 3) @@ -390,7 +406,8 @@ void do_user_defined_literals() // assert(R"x(a)"bc)x"_y == "ya)\"bcy"); } -int main() +int +main() { do_functional(); do_inttypes(); diff --git a/libtests/dct_compress.cc b/libtests/dct_compress.cc index 2a583ffd..446d4d0a 100644 --- a/libtests/dct_compress.cc +++ b/libtests/dct_compress.cc @@ -2,12 +2,13 @@ #include <qpdf/Pl_StdioFile.hh> #include <qpdf/QUtil.hh> -#include <stdio.h> -#include <string.h> #include <iostream> +#include <stdio.h> #include <stdlib.h> +#include <string.h> -static void usage() +static void +usage() { std::cerr << "Usage: dct_compress infile outfile width height" << " {rgb|cmyk|gray}" << std::endl; @@ -28,15 +29,16 @@ class Callback: public Pl_DCT::CompressConfig bool called; }; -void Callback::apply(jpeg_compress_struct*) +void +Callback::apply(jpeg_compress_struct*) { this->called = true; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if (argc != 6) - { + if (argc != 6) { usage(); } @@ -46,23 +48,22 @@ int main(int argc, char* argv[]) JDIMENSION height = QUtil::string_to_uint(argv[4]); char* colorspace = argv[5]; J_COLOR_SPACE cs = - ((strcmp(colorspace, "rgb") == 0) ? JCS_RGB : - (strcmp(colorspace, "cmyk") == 0) ? JCS_CMYK : - (strcmp(colorspace, "gray") == 0) ? JCS_GRAYSCALE : - JCS_UNKNOWN); + ((strcmp(colorspace, "rgb") == 0) ? JCS_RGB + : (strcmp(colorspace, "cmyk") == 0) ? JCS_CMYK + : (strcmp(colorspace, "gray") == 0) ? JCS_GRAYSCALE + : JCS_UNKNOWN); int components = 0; - switch (cs) - { - case JCS_RGB: + switch (cs) { + case JCS_RGB: components = 3; break; - case JCS_CMYK: + case JCS_CMYK: components = 4; break; - case JCS_GRAYSCALE: + case JCS_GRAYSCALE: components = 1; break; - default: + default: usage(); break; } @@ -74,21 +75,16 @@ int main(int argc, char* argv[]) bool done = false; Callback callback; Pl_DCT dct("dct", &out, width, height, components, cs, &callback); - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), infile); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { dct.write(buf, len); } } dct.finish(); - if (! callback.called) - { + if (!callback.called) { std::cout << "Callback was not called" << std::endl; } fclose(infile); diff --git a/libtests/dct_uncompress.cc b/libtests/dct_uncompress.cc index dd366852..1ab22e97 100644 --- a/libtests/dct_uncompress.cc +++ b/libtests/dct_uncompress.cc @@ -2,17 +2,16 @@ #include <qpdf/Pl_StdioFile.hh> #include <qpdf/QUtil.hh> -#include <stdio.h> -#include <string.h> #include <iostream> +#include <stdio.h> #include <stdlib.h> +#include <string.h> -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if (argc != 3) - { - std::cerr << "Usage: dct_uncompress infile outfile" - << std::endl; + if (argc != 3) { + std::cerr << "Usage: dct_uncompress infile outfile" << std::endl; exit(2); } @@ -25,15 +24,11 @@ int main(int argc, char* argv[]) unsigned char buf[100]; bool done = false; Pl_DCT dct("dct", &out); - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), infile); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { dct.write(buf, len); } } diff --git a/libtests/flate.cc b/libtests/flate.cc index 058b956e..77fb440e 100644 --- a/libtests/flate.cc +++ b/libtests/flate.cc @@ -1,15 +1,16 @@ +#include <qpdf/Pl_Count.hh> #include <qpdf/Pl_Flate.hh> #include <qpdf/Pl_StdioFile.hh> -#include <qpdf/Pl_Count.hh> #include <qpdf/QUtil.hh> -#include <iostream> #include <errno.h> -#include <string.h> +#include <iostream> #include <stdlib.h> +#include <string.h> -void run(char const* filename) +void +run(char const* filename) { std::string n1 = std::string(filename) + ".1"; std::string n2 = std::string(filename) + ".2"; @@ -38,8 +39,7 @@ void run(char const* filename) FILE* in1 = QUtil::safe_fopen(filename, "rb"); unsigned char buf[1024]; size_t len; - while ((len = fread(buf, 1, sizeof(buf), in1)) > 0) - { + while ((len = fread(buf, 1, sizeof(buf), in1)) > 0) { // Write to the compression pipeline def1->write(buf, len); @@ -57,7 +57,6 @@ void run(char const* filename) std::cout << "bytes written to o3: " << count3->getCount() << std::endl; - delete def3; delete inf3; delete count3; @@ -66,8 +65,7 @@ void run(char const* filename) // Now read the compressed data and write to the output uncompress pipeline FILE* in2 = QUtil::safe_fopen(n1.c_str(), "rb"); - while ((len = fread(buf, 1, sizeof(buf), in2)) > 0) - { + while ((len = fread(buf, 1, sizeof(buf), in2)) > 0) { inf2->write(buf, len); } fclose(in2); @@ -83,21 +81,18 @@ void run(char const* filename) std::cout << "done" << std::endl; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if (argc != 2) - { + if (argc != 2) { std::cerr << "Usage: pipeline filename" << std::endl; exit(2); } char* filename = argv[1]; - try - { + try { run(filename); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << e.what() << std::endl; } return 0; diff --git a/libtests/hex.cc b/libtests/hex.cc index 0480c7ea..9e708204 100644 --- a/libtests/hex.cc +++ b/libtests/hex.cc @@ -4,33 +4,27 @@ #include <iostream> #include <stdlib.h> -int main() +int +main() { Pl_StdioFile out("stdout", stdout); Pl_ASCIIHexDecoder decode("decode", &out); - try - { + try { unsigned char buf[10000]; bool done = false; - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), stdin); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { decode.write(buf, len); } } decode.finish(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << e.what() << std::endl; - exit(2); + exit(2); } return 0; diff --git a/libtests/input_source.cc b/libtests/input_source.cc index 9e72bbf6..cb62daea 100644 --- a/libtests/input_source.cc +++ b/libtests/input_source.cc @@ -1,9 +1,9 @@ +#include <qpdf/Buffer.hh> #include <qpdf/BufferInputSource.hh> #include <qpdf/PointerHolder.hh> -#include <qpdf/Buffer.hh> #include <qpdf/QPDFTokenizer.hh> -#include <iostream> #include <cstring> +#include <iostream> static PointerHolder<Buffer> get_buffer() @@ -11,8 +11,7 @@ get_buffer() size_t size = 3172; PointerHolder<Buffer> b(new Buffer(size)); unsigned char* p = b->getBuffer(); - for (size_t i = 0; i < size; ++i) - { + for (size_t i = 0; i < size; ++i) { p[i] = static_cast<unsigned char>(i & 0xff); } return b; @@ -41,22 +40,22 @@ Finder::check() { QPDFTokenizer tokenizer; QPDFTokenizer::Token t = tokenizer.readToken(is, "finder", true); - if (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "potato")) - { + if (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "potato")) { t = tokenizer.readToken(is, "finder", true); return (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, after)); } return false; } -void check(char const* description, bool expected, bool actual) +void +check(char const* description, bool expected, bool actual) { - std::cout << description << ": " - << ((actual == expected) ? "PASS" : "FAIL") + std::cout << description << ": " << ((actual == expected) ? "PASS" : "FAIL") << std::endl; } -int main() +int +main() { PointerHolder<Buffer> b1 = get_buffer(); unsigned char* b = b1->getBuffer(); @@ -68,42 +67,46 @@ int main() auto is = PointerHolder<InputSource>( new BufferInputSource("test buffer input source", b1.get())); Finder f1(is, "salad"); - check("find potato salad", true, - is->findFirst("potato", 0, 0, f1)); - check("barely find potato salad", true, - is->findFirst("potato", 1100, 945, f1)); - check("barely find potato salad", true, - is->findFirst("potato", 2000, 45, f1)); - check("potato salad is too late", false, - is->findFirst("potato", 1100, 944, f1)); - check("potato salad is too late", false, - is->findFirst("potato", 2000, 44, f1)); - check("potato salad not found", false, - is->findFirst("potato", 2045, 0, f1)); - check("potato salad not found", false, - is->findFirst("potato", 0, 1, f1)); + check("find potato salad", true, is->findFirst("potato", 0, 0, f1)); + check( + "barely find potato salad", + true, + is->findFirst("potato", 1100, 945, f1)); + check( + "barely find potato salad", + true, + is->findFirst("potato", 2000, 45, f1)); + check( + "potato salad is too late", + false, + is->findFirst("potato", 1100, 944, f1)); + check( + "potato salad is too late", + false, + is->findFirst("potato", 2000, 44, f1)); + check( + "potato salad not found", false, is->findFirst("potato", 2045, 0, f1)); + check("potato salad not found", false, is->findFirst("potato", 0, 1, f1)); // Put one more right at EOF memcpy(b + b1->getSize() - 12, "potato salad", 12); - check("potato salad at EOF", true, - is->findFirst("potato", 3000, 0, f1)); + check("potato salad at EOF", true, is->findFirst("potato", 3000, 0, f1)); is->findFirst("potato", 0, 0, f1); - check("findFirst found first", true, - is->tell() == 2056); - check("findLast found potato salad", true, - is->findLast("potato", 0, 0, f1)); - check("findLast found at EOF", true, - is->tell() == 3172); + check("findFirst found first", true, is->tell() == 2056); + check( + "findLast found potato salad", true, is->findLast("potato", 0, 0, f1)); + check("findLast found at EOF", true, is->tell() == 3172); // Make check() bump into EOF memcpy(b + b1->getSize() - 6, "potato", 6); - check("potato but not salad salad at EOF", false, - is->findFirst("potato", 3000, 0, f1)); - check("findLast found potato salad", true, - is->findLast("potato", 0, 0, f1)); - check("findLast found first one", true, - is->tell() == 2056); + check( + "potato but not salad salad at EOF", + false, + is->findFirst("potato", 3000, 0, f1)); + check( + "findLast found potato salad", true, is->findLast("potato", 0, 0, f1)); + check("findLast found first one", true, is->tell() == 2056); return 0; } diff --git a/libtests/json.cc b/libtests/json.cc index a8c72a3b..17a5d574 100644 --- a/libtests/json.cc +++ b/libtests/json.cc @@ -8,32 +8,34 @@ #endif #include <cassert> -static void check(JSON const& j, std::string const& exp) +static void +check(JSON const& j, std::string const& exp) { - if (exp != j.unparse()) - { + if (exp != j.unparse()) { std::cout << "Got " << j.unparse() << "; wanted " << exp << "\n"; } } -static void test_main() +static void +test_main() { JSON jstr = JSON::makeString( "<1>\xcf\x80<2>\xf0\x9f\xa5\x94\\\"<3>\x03\t\b\r\n<4>"); - check(jstr, - "\"<1>\xcf\x80<2>\xf0\x9f\xa5\x94\\\\\\\"<3>" - "\\u0003\\t\\b\\r\\n<4>\""); + check( + jstr, + "\"<1>\xcf\x80<2>\xf0\x9f\xa5\x94\\\\\\\"<3>" + "\\u0003\\t\\b\\r\\n<4>\""); JSON jnull = JSON::makeNull(); check(jnull, "null"); assert(jnull.isNull()); std::string value; - assert(! jnull.getNumber(value)); + assert(!jnull.getNumber(value)); JSON jarr = JSON::makeArray(); check(jarr, "[]"); JSON jstr2 = JSON::makeString("a\tb"); assert(jstr2.getString(value)); assert(value == "a\tb"); - assert(! jstr2.getNumber(value)); + assert(!jstr2.getNumber(value)); /* cSpell: ignore jbool xavalue dvalue xdvalue */ JSON jint = JSON::makeInt(16059); JSON jdouble = JSON::makeReal(3.14159); @@ -44,24 +46,24 @@ static void test_main() assert(jbool1.getBool(bvalue)); assert(bvalue); assert(jbool2.getBool(bvalue)); - assert(! bvalue); + assert(!bvalue); jarr.addArrayElement(jstr2); jarr.addArrayElement(jnull); jarr.addArrayElement(jint); jarr.addArrayElement(jdouble); jarr.addArrayElement(jexp); - check(jarr, - "[\n" - " \"a\\tb\",\n" - " null,\n" - " 16059,\n" - " 3.14159,\n" - " 2.1e5\n" - "]"); + check( + jarr, + "[\n" + " \"a\\tb\",\n" + " null,\n" + " 16059,\n" + " 3.14159,\n" + " 2.1e5\n" + "]"); std::vector<std::string> avalue; - assert(jarr.forEachArrayItem([&avalue](JSON j) { - avalue.push_back(j.unparse()); - })); + assert(jarr.forEachArrayItem( + [&avalue](JSON j) { avalue.push_back(j.unparse()); })); std::vector<std::string> xavalue = { "\"a\\tb\"", "null", @@ -79,37 +81,36 @@ static void test_main() jmap.addDictionaryMember("no", JSON::makeBool(true)); jmap.addDictionaryMember("empty_dict", JSON::makeDictionary()); jmap.addDictionaryMember("empty_list", JSON::makeArray()); - jmap.addDictionaryMember("single", JSON::makeArray()). - addArrayElement(JSON::makeInt(12)); - check(jmap, - "{\n" - " \"a\": [\n" - " \"a\\tb\",\n" - " null,\n" - " 16059,\n" - " 3.14159,\n" - " 2.1e5\n" - " ],\n" - " \"b\": \"a\\tb\",\n" - " \"c\\r\\nd\": null,\n" - " \"empty_dict\": {},\n" - " \"empty_list\": [],\n" - " \"no\": true,\n" - " \"single\": [\n" - " 12\n" - " ],\n" - " \"yes\": false\n" - "}"); + jmap.addDictionaryMember("single", JSON::makeArray()) + .addArrayElement(JSON::makeInt(12)); + check( + jmap, + "{\n" + " \"a\": [\n" + " \"a\\tb\",\n" + " null,\n" + " 16059,\n" + " 3.14159,\n" + " 2.1e5\n" + " ],\n" + " \"b\": \"a\\tb\",\n" + " \"c\\r\\nd\": null,\n" + " \"empty_dict\": {},\n" + " \"empty_list\": [],\n" + " \"no\": true,\n" + " \"single\": [\n" + " 12\n" + " ],\n" + " \"yes\": false\n" + "}"); check(QPDFObjectHandle::newReal("0.12").getJSON(), "0.12"); check(QPDFObjectHandle::newReal(".34").getJSON(), "0.34"); check(QPDFObjectHandle::newReal("-0.56").getJSON(), "-0.56"); check(QPDFObjectHandle::newReal("-.78").getJSON(), "-0.78"); JSON jmap2 = JSON::parse(R"({"a": 1, "b": "two", "c": [true]})"); std::map<std::string, std::string> dvalue; - assert(jmap2.forEachDictItem([&dvalue] - (std::string const& k, JSON j) { - dvalue[k] = j.unparse(); - })); + assert(jmap2.forEachDictItem( + [&dvalue](std::string const& k, JSON j) { dvalue[k] = j.unparse(); })); std::map<std::string, std::string> xdvalue = { {"a", "1"}, {"b", "\"two\""}, @@ -118,21 +119,27 @@ static void test_main() assert(dvalue == xdvalue); } -static void check_schema(JSON& obj, JSON& schema, unsigned long flags, - bool exp, std::string const& description) +static void +check_schema( + JSON& obj, + JSON& schema, + unsigned long flags, + bool exp, + std::string const& description) { std::list<std::string> errors; std::cout << "--- " << description << std::endl; assert(exp == obj.checkSchema(schema, flags, errors)); for (std::list<std::string>::iterator iter = errors.begin(); - iter != errors.end(); ++iter) - { + iter != errors.end(); + ++iter) { std::cout << *iter << std::endl; } std::cout << "---" << std::endl; } -static void test_schema() +static void +test_schema() { /* cSpell: ignore ptional ebra */ JSON schema = JSON::parse(R"( @@ -249,7 +256,8 @@ static void test_schema() check_schema(good, schema, JSON::f_optional, true, "pass"); } -int main() +int +main() { test_main(); test_schema(); diff --git a/libtests/json_handler.cc b/libtests/json_handler.cc index 6e894e11..567cde86 100644 --- a/libtests/json_handler.cc +++ b/libtests/json_handler.cc @@ -1,6 +1,6 @@ #include <qpdf/JSONHandler.hh> -#include <qpdf/QUtil.hh> #include <qpdf/QPDFUsage.hh> +#include <qpdf/QUtil.hh> #include <iostream> #ifdef NDEBUG @@ -9,39 +9,46 @@ #endif #include <cassert> -static void print_null(std::string const& path) +static void +print_null(std::string const& path) { std::cout << path << ": null" << std::endl; } -static void print_string(std::string const& path, std::string const& value) +static void +print_string(std::string const& path, std::string const& value) { std::cout << path << ": string: " << value << std::endl; } -static void print_number(std::string const& path, std::string const& value) +static void +print_number(std::string const& path, std::string const& value) { std::cout << path << ": number: " << value << std::endl; } -static void print_bool(std::string const& path, bool value) +static void +print_bool(std::string const& path, bool value) { std::cout << path << ": bool: " << (value ? "true" : "false") << std::endl; } -static void print_json(std::string const& path, JSON value) +static void +print_json(std::string const& path, JSON value) { std::cout << path << ": json: " << value.unparse() << std::endl; } -static JSONHandler::void_handler_t make_print_message(std::string msg) +static JSONHandler::void_handler_t +make_print_message(std::string msg) { return [msg](std::string const& path) { std::cout << path << ": json: " << msg << std::endl; }; } -static void test_scalar() +static void +test_scalar() { std::cout << "-- scalar --" << std::endl; JSONHandler h; @@ -50,11 +57,11 @@ static void test_scalar() h.handle(".", j); } -static std::shared_ptr<JSONHandler> make_all_handler() +static std::shared_ptr<JSONHandler> +make_all_handler() { auto h = std::make_shared<JSONHandler>(); - h->addDictHandlers( - print_json, make_print_message("dict end")); + h->addDictHandlers(print_json, make_print_message("dict end")); auto h1 = std::make_shared<JSONHandler>(); h1->addStringHandler(print_string); h->addDictKeyHandler("one", h1); @@ -75,16 +82,12 @@ static std::shared_ptr<JSONHandler> make_all_handler() h5->addNullHandler(print_null); auto h5s = std::make_shared<JSONHandler>(); h->addDictKeyHandler("five", h5s); - h5s->addArrayHandlers( - print_json, make_print_message("array end"), - h5); + h5s->addArrayHandlers(print_json, make_print_message("array end"), h5); auto h6 = std::make_shared<JSONHandler>(); - h6->addDictHandlers( - print_json, make_print_message("dict end")); + h6->addDictHandlers(print_json, make_print_message("dict end")); auto h6a = std::make_shared<JSONHandler>(); h6->addDictKeyHandler("a", h6a); - h6a->addDictHandlers( - print_json, make_print_message("dict end")); + h6a->addDictHandlers(print_json, make_print_message("dict end")); auto h6ab = std::make_shared<JSONHandler>(); h6a->addDictKeyHandler("b", h6ab); auto h6ax = std::make_shared<JSONHandler>(); @@ -96,7 +99,8 @@ static std::shared_ptr<JSONHandler> make_all_handler() return h; } -static void test_all() +static void +test_all() { std::cout << "-- all --" << std::endl; auto h = make_all_handler(); @@ -113,32 +117,29 @@ static void test_all() h->handle(".", j); } -static void test_errors() +static void +test_errors() { std::cout << "-- errors --" << std::endl; auto h = make_all_handler(); auto t = [h](std::string const& msg, std::function<void()> fn) { - try - { + try { fn(); assert(false); - } - catch (QPDFUsage& e) - { + } catch (QPDFUsage& e) { std::cout << msg << ": " << e.what() << std::endl; } }; - t("bad type at top", [&h](){ - h->handle(".", JSON::makeString("oops")); - }); - t("unexpected key", [&h](){ + t("bad type at top", [&h]() { h->handle(".", JSON::makeString("oops")); }); + t("unexpected key", [&h]() { JSON j = JSON::parse(R"({"x": "y"})"); h->handle(".", j); }); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { test_scalar(); test_all(); diff --git a/libtests/json_parse.cc b/libtests/json_parse.cc index a2f004f5..13d29b65 100644 --- a/libtests/json_parse.cc +++ b/libtests/json_parse.cc @@ -2,25 +2,22 @@ #include <qpdf/QUtil.hh> #include <iostream> -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if (argc != 2) - { + if (argc != 2) { std::cerr << "Usage: json_parse file" << std::endl; return 2; } char const* filename = argv[1]; - try - { + try { PointerHolder<char> buf; size_t size; QUtil::read_file_into_memory(filename, buf, size); std::string s(buf.get(), size); std::cout << JSON::parse(s).unparse() << std::endl; - } - catch (std::exception& e) - { - std::cerr << "exception: " << filename<< ": " << e.what() << std::endl; + } catch (std::exception& e) { + std::cerr << "exception: " << filename << ": " << e.what() << std::endl; return 2; } return 0; diff --git a/libtests/lzw.cc b/libtests/lzw.cc index 1c87045b..788d3dfc 100644 --- a/libtests/lzw.cc +++ b/libtests/lzw.cc @@ -6,23 +6,21 @@ #include <stdlib.h> #include <string.h> -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { bool early_code_change = true; - if ((argc == 4) && (strcmp(argv[3], "--no-early-code-change") == 0)) - { + if ((argc == 4) && (strcmp(argv[3], "--no-early-code-change") == 0)) { early_code_change = false; } - if (argc < 3) - { + if (argc < 3) { std::cerr << "Usage: lzw infile outfile [ --no-early-code-change ]" << std::endl; exit(2); } - try - { + try { char* infilename = argv[1]; char* outfilename = argv[2]; @@ -34,22 +32,16 @@ int main(int argc, char* argv[]) unsigned char buf[10000]; bool done = false; - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), infile); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { decode.write(buf, len); } } decode.finish(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cerr << e.what() << std::endl; exit(2); } diff --git a/libtests/main_from_wmain.cc b/libtests/main_from_wmain.cc index 842db52e..b2290b2e 100644 --- a/libtests/main_from_wmain.cc +++ b/libtests/main_from_wmain.cc @@ -2,13 +2,15 @@ #include <iostream> #ifndef QPDF_NO_WCHAR_T -void wmain_test() +void +wmain_test() { // writable args and function args auto realmain = [](int argc, char* argv[]) { for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; - } return 0; + } + return 0; }; wchar_t* argv[3]; // This works because call_main_from_wmain doesn't actually write @@ -20,13 +22,15 @@ void wmain_test() QUtil::call_main_from_wmain(3, argv, realmain); } -void cwmain_test() +void +cwmain_test() { // const args and function args auto realmain = [](int argc, char const* const argv[]) { for (int i = 0; i < argc; ++i) { std::cout << "const " << argv[i] << std::endl; - } return 0; + } + return 0; }; wchar_t const* argv[3] = { L"ascii", @@ -37,16 +41,14 @@ void cwmain_test() } #endif // QPDF_NO_WCHAR_T -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { #ifndef QPDF_NO_WCHAR_T - try - { + try { wmain_test(); cwmain_test(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << "unexpected exception: " << e.what() << std::endl; } #endif // QPDF_NO_WCHAR_T diff --git a/libtests/matrix.cc b/libtests/matrix.cc index 210c022f..7dc73390 100644 --- a/libtests/matrix.cc +++ b/libtests/matrix.cc @@ -8,45 +8,50 @@ #endif #include <cassert> -static void check(QPDFMatrix const& m, std::string const& exp) +static void +check(QPDFMatrix const& m, std::string const& exp) { std::string u = m.unparse(); - if (u != exp) - { + if (u != exp) { std::cout << "got " << u << ", wanted " << exp << std::endl; } } -static void check_xy(double x, double y, std::string const& exp) +static void +check_xy(double x, double y, std::string const& exp) { - std::string u = (QUtil::double_to_string(x, 2) + " " + - QUtil::double_to_string(y, 2)); - if (u != exp) - { + std::string u = + (QUtil::double_to_string(x, 2) + " " + QUtil::double_to_string(y, 2)); + if (u != exp) { std::cout << "got " << u << ", wanted " << exp << std::endl; } } -static void check_rect(QPDFObjectHandle::Rectangle const& r, - double llx, double lly, double urx, double ury) +static void +check_rect( + QPDFObjectHandle::Rectangle const& r, + double llx, + double lly, + double urx, + double ury) { - std::string actual = ( - QUtil::double_to_string(r.llx, 2) + " " + - QUtil::double_to_string(r.lly, 2) + " " + - QUtil::double_to_string(r.urx, 2) + " " + - QUtil::double_to_string(r.ury, 2)); - std::string wanted = ( - QUtil::double_to_string(llx, 2) + " " + - QUtil::double_to_string(lly, 2) + " " + - QUtil::double_to_string(urx, 2) + " " + - QUtil::double_to_string(ury, 2)); - if (actual != wanted) - { + std::string actual = + (QUtil::double_to_string(r.llx, 2) + " " + + QUtil::double_to_string(r.lly, 2) + " " + + QUtil::double_to_string(r.urx, 2) + " " + + QUtil::double_to_string(r.ury, 2)); + std::string wanted = + (QUtil::double_to_string(llx, 2) + " " + + QUtil::double_to_string(lly, 2) + " " + + QUtil::double_to_string(urx, 2) + " " + + QUtil::double_to_string(ury, 2)); + if (actual != wanted) { std::cout << "got " << actual << ", wanted " << wanted << std::endl; } } -int main() +int +main() { QPDFMatrix m; check(m, "1 0 0 1 0 0"); @@ -78,17 +83,20 @@ int main() m.transform(240, 480, xp, yp); check_xy(xp, yp, "2582.5 4912"); - check(QPDFMatrix( - QPDFObjectHandle::parse( - "[3 1 4 1 5 9.26535]").getArrayAsMatrix()), - "3 1 4 1 5 9.26535"); + check( + QPDFMatrix( + QPDFObjectHandle::parse("[3 1 4 1 5 9.26535]").getArrayAsMatrix()), + "3 1 4 1 5 9.26535"); m = QPDFMatrix(); m.rotatex90(90); m.translate(200, -100); check_rect( m.transformRectangle(QPDFObjectHandle::Rectangle(10, 20, 30, 50)), - 50, 210, 80, 230); + 50, + 210, + 80, + 230); std::cout << "matrix tests done" << std::endl; return 0; diff --git a/libtests/md5.cc b/libtests/md5.cc index 04935804..0fe4205e 100644 --- a/libtests/md5.cc +++ b/libtests/md5.cc @@ -1,18 +1,20 @@ #include <qpdf/MD5.hh> -#include <qpdf/Pl_MD5.hh> #include <qpdf/Pl_Discard.hh> +#include <qpdf/Pl_MD5.hh> #include <qpdf/QUtil.hh> #include <iostream> #include <stdio.h> -static void test_string(char const* str) +static void +test_string(char const* str) { MD5 a; a.encodeString(str); a.print(); } -int main(int, char*[]) +int +main(int, char*[]) { test_string(""); test_string("a"); @@ -42,7 +44,6 @@ int main(int, char*[]) << MD5::checkFileChecksum("6f4b4321873433daae578f85c72f9e74", "glerbl") << std::endl; - Pl_Discard d; Pl_MD5 p("MD5", &d); // Create a second pipeline, protect against finish, and call @@ -52,25 +53,19 @@ int main(int, char*[]) // calling finalize. Pl_MD5 p2("MD5", &d); p2.persistAcrossFinish(true); - for (int i = 0; i < 2; ++i) - { + for (int i = 0; i < 2; ++i) { FILE* f = QUtil::safe_fopen("md5.in", "rb"); // buffer size < size of md5.in unsigned char buf[50]; bool done = false; - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), f); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { p.write(buf, len); p2.write(buf, len); - if (i == 1) - { + if (i == 1) { // Partial digest -- resets after each call to write std::cout << p.getHexDigest() << std::endl; } diff --git a/libtests/nntree.cc b/libtests/nntree.cc index 1579ab46..4a874578 100644 --- a/libtests/nntree.cc +++ b/libtests/nntree.cc @@ -1,12 +1,13 @@ -#include <qpdf/QPDFNumberTreeObjectHelper.hh> -#include <qpdf/QPDFNameTreeObjectHelper.hh> #include <qpdf/QPDF.hh> +#include <qpdf/QPDFNameTreeObjectHelper.hh> +#include <qpdf/QPDFNumberTreeObjectHelper.hh> #include <qpdf/QUtil.hh> #include <iostream> static bool any_failures = false; -bool report(QPDF& q, QPDFObjectHandle oh, long long item, long long exp_item) +bool +report(QPDF& q, QPDFObjectHandle oh, long long item, long long exp_item) { QPDFNumberTreeObjectHelper nh(oh, q); QPDFObjectHandle o1; @@ -16,61 +17,54 @@ bool report(QPDF& q, QPDFObjectHandle oh, long long item, long long exp_item) bool f2 = nh.findObject(item, o2); bool failed = false; - auto show = [&failed, &oh, &item] () { - if (! failed) - { + auto show = [&failed, &oh, &item]() { + if (!failed) { failed = true; - std::cout << "key = " << item << ", oh = " - << oh.unparseResolved() << std::endl; + std::cout << "key = " << item << ", oh = " << oh.unparseResolved() + << std::endl; } }; auto mk_wanted = [](long long i) { - return ((i == -1) - ? "end" - : (QUtil::int_to_string(i) + - "/(-" + QUtil::int_to_string(i) + "-)")); + return ( + (i == -1) ? "end" + : (QUtil::int_to_string(i) + "/(-" + + QUtil::int_to_string(i) + "-)")); }; std::string i1_wanted = mk_wanted(exp_item); std::string i2_wanted = mk_wanted(item == exp_item ? item : -1); auto mk_actual = [](bool found, long long v, QPDFObjectHandle& o) { - return (found - ? QUtil::int_to_string(v) + "/" + o.unparse() - : "end"); + return (found ? QUtil::int_to_string(v) + "/" + o.unparse() : "end"); }; std::string i1_actual = mk_actual(f1, item - offset, o1); std::string i2_actual = mk_actual(f2, item, o2); - if (i1_wanted != i1_actual) - { + if (i1_wanted != i1_actual) { show(); - std::cout << "i1: wanted " << i1_wanted - << ", got " << i1_actual + std::cout << "i1: wanted " << i1_wanted << ", got " << i1_actual << std::endl; } - if (i2_wanted != i2_actual) - { + if (i2_wanted != i2_actual) { show(); - std::cout << "i2: wanted " << i2_wanted - << ", got " << i2_actual + std::cout << "i2: wanted " << i2_wanted << ", got " << i2_actual << std::endl; } return failed; } -void test_bsearch() +void +test_bsearch() { QPDF q; q.emptyPDF(); - auto mk = [&q] (std::vector<int> const& v) { + auto mk = [&q](std::vector<int> const& v) { auto nums = QPDFObjectHandle::newArray(); - for (auto i: v) - { + for (auto i : v) { nums.appendItem(QPDFObjectHandle::newInteger(i)); nums.appendItem(QPDFObjectHandle::newString( - "-" + QUtil::int_to_string(i) + "-")); + "-" + QUtil::int_to_string(i) + "-")); } auto limits = QPDFObjectHandle::newArray(); limits.appendItem(QPDFObjectHandle::newInteger(v.at(0))); @@ -82,8 +76,7 @@ void test_bsearch() }; auto r = [&q](QPDFObjectHandle& oh, int item, int exp) { - if (report(q, oh, item, exp)) - { + if (report(q, oh, item, exp)) { any_failures = true; } }; @@ -119,36 +112,37 @@ void test_bsearch() r(d, 20, 20); r(d, 25, 20); - if (! any_failures) - { + if (!any_failures) { std::cout << "bsearch tests passed" << std::endl; } } -QPDFObjectHandle new_node(QPDF& q, std::string const& key) +QPDFObjectHandle +new_node(QPDF& q, std::string const& key) { auto dict = QPDFObjectHandle::newDictionary(); dict.replaceKey(key, QPDFObjectHandle::newArray()); return q.makeIndirectObject(dict); } -static void check_find(QPDFNameTreeObjectHelper& nh, - std::string const& key, bool prev_if_not_found) +static void +check_find( + QPDFNameTreeObjectHelper& nh, + std::string const& key, + bool prev_if_not_found) { auto i = nh.find(key, prev_if_not_found); std::cout << "find " << key << " (" << prev_if_not_found << "): "; - if (i == nh.end()) - { + if (i == nh.end()) { std::cout << "not found"; - } - else - { + } else { std::cout << (*i).first << " -> " << (*i).second.unparse(); } std::cout << std::endl; } -void test_depth() +void +test_depth() { int constexpr NITEMS = 3; QPDF q; @@ -157,40 +151,29 @@ void test_depth() auto n0 = new_node(q, "/Kids"); root.replaceKey("/NT", n0); auto k0 = root.getKey("/NT").getKey("/Kids"); - for (int i1 = 0; i1 < NITEMS; ++i1) - { + for (int i1 = 0; i1 < NITEMS; ++i1) { auto n1 = new_node(q, "/Kids"); k0.appendItem(n1); auto k1 = n1.getKey("/Kids"); - for (int i2 = 0; i2 < NITEMS; ++i2) - { + for (int i2 = 0; i2 < NITEMS; ++i2) { auto n2 = new_node(q, "/Kids"); k1.appendItem(n2); auto k2 = n2.getKey("/Kids"); - for (int i3 = 0; i3 < NITEMS; ++i3) - { + for (int i3 = 0; i3 < NITEMS; ++i3) { auto n3 = new_node(q, "/Names"); k2.appendItem(n3); auto items = n3.getKey("/Names"); std::string first; std::string last; - for (int i4 = 0; i4 < NITEMS; ++i4) - { - int val = (((((i1 - * NITEMS) + i2) - * NITEMS) + i3) - * NITEMS) + i4; + for (int i4 = 0; i4 < NITEMS; ++i4) { + int val = + (((((i1 * NITEMS) + i2) * NITEMS) + i3) * NITEMS) + i4; std::string str = QUtil::int_to_string(10 * val, 6); - items.appendItem( - QPDFObjectHandle::newString(str)); - items.appendItem( - QPDFObjectHandle::newString("val " + str)); - if (i4 == 0) - { + items.appendItem(QPDFObjectHandle::newString(str)); + items.appendItem(QPDFObjectHandle::newString("val " + str)); + if (i4 == 0) { first = str; - } - else if (i4 == NITEMS - 1) - { + } else if (i4 == NITEMS - 1) { last = str; } } @@ -201,35 +184,26 @@ void test_depth() } auto limits = QPDFObjectHandle::newArray(); n2.replaceKey("/Limits", limits); - limits.appendItem(k2.getArrayItem(0) - .getKey("/Limits") - .getArrayItem(0)); - limits.appendItem(k2.getArrayItem(NITEMS - 1) - .getKey("/Limits") - .getArrayItem(1)); + limits.appendItem( + k2.getArrayItem(0).getKey("/Limits").getArrayItem(0)); + limits.appendItem( + k2.getArrayItem(NITEMS - 1).getKey("/Limits").getArrayItem(1)); } auto limits = QPDFObjectHandle::newArray(); n1.replaceKey("/Limits", limits); - limits.appendItem(k1.getArrayItem(0) - .getKey("/Limits") - .getArrayItem(0)); - limits.appendItem(k1.getArrayItem(NITEMS - 1) - .getKey("/Limits") - .getArrayItem(1)); + limits.appendItem(k1.getArrayItem(0).getKey("/Limits").getArrayItem(0)); + limits.appendItem( + k1.getArrayItem(NITEMS - 1).getKey("/Limits").getArrayItem(1)); } QPDFNameTreeObjectHelper nh(n0, q); std::cout << "--- forward ---" << std::endl; - for (auto i: nh) - { - std::cout << i.first << " -> " - << i.second.unparse() << std::endl; + for (auto i : nh) { + std::cout << i.first << " -> " << i.second.unparse() << std::endl; } std::cout << "--- backward ---" << std::endl; - for (auto i = nh.last(); i.valid(); --i) - { - std::cout << (*i).first << " -> " - << (*i).second.unparse() << std::endl; + for (auto i = nh.last(); i.valid(); --i) { + std::cout << (*i).first << " -> " << (*i).second.unparse() << std::endl; } // Find @@ -243,11 +217,11 @@ void test_depth() check_find(nh, "000805", true); } -int main() +int +main() { test_bsearch(); test_depth(); return 0; } - diff --git a/libtests/numrange.cc b/libtests/numrange.cc index bc3a4036..d1548fe9 100644 --- a/libtests/numrange.cc +++ b/libtests/numrange.cc @@ -1,33 +1,29 @@ #include <qpdf/QUtil.hh> #include <iostream> -static void test_numrange(char const* range) +static void +test_numrange(char const* range) { - if (range == 0) - { + if (range == 0) { std::cout << "null" << std::endl; - } - else - { + } else { std::vector<int> result = QUtil::parse_numrange(range, 15); std::cout << "numeric range " << range << " ->"; for (std::vector<int>::iterator iter = result.begin(); - iter != result.end(); ++iter) - { + iter != result.end(); + ++iter) { std::cout << " " << *iter; } std::cout << std::endl; } } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - try - { + try { test_numrange(argv[1]); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << e.what() << std::endl; return 2; } diff --git a/libtests/pdf_version.cc b/libtests/pdf_version.cc index 2471cb0b..e9bfb00b 100644 --- a/libtests/pdf_version.cc +++ b/libtests/pdf_version.cc @@ -8,7 +8,8 @@ #endif #include <cassert> -int main() +int +main() { PDFVersion v1; assert(v1.getMajor() == 0); @@ -32,8 +33,8 @@ int main() assert(v3 == v1); v1.updateIfGreater(v2); assert(v3 == v1); - assert(! (v3 == v2)); - assert(! (v2 == v1)); + assert(!(v3 == v2)); + assert(!(v2 == v1)); v2.updateIfGreater(v1); assert(v2 == v1); v2.getVersion(version, extension_level); @@ -42,12 +43,12 @@ int main() assert(PDFVersion(1, 2) < PDFVersion(1, 3)); assert(PDFVersion(1, 2) < PDFVersion(1, 2, 1)); assert(PDFVersion(1, 2, 1) < PDFVersion(1, 2, 2)); - assert(! (PDFVersion(1, 2, 2) < PDFVersion(1, 2, 2))); - assert(! (PDFVersion(1, 2, 3) < PDFVersion(1, 2, 2))); + assert(!(PDFVersion(1, 2, 2) < PDFVersion(1, 2, 2))); + assert(!(PDFVersion(1, 2, 3) < PDFVersion(1, 2, 2))); assert(PDFVersion(1, 1) < PDFVersion(1, 2, 2)); - assert(! (PDFVersion(1, 3) < PDFVersion(1, 2, 2))); - assert(! (PDFVersion(2, 0) < PDFVersion(1, 9, 9))); - assert(! (PDFVersion(2, 0) < PDFVersion(2, 0))); + assert(!(PDFVersion(1, 3) < PDFVersion(1, 2, 2))); + assert(!(PDFVersion(2, 0) < PDFVersion(1, 9, 9))); + assert(!(PDFVersion(2, 0) < PDFVersion(2, 0))); assert(PDFVersion(2, 0) == PDFVersion(2, 0)); assert(PDFVersion(2, 0, 1) == PDFVersion(2, 0, 1)); diff --git a/libtests/pointer_holder.cc b/libtests/pointer_holder.cc index 67c13d5a..70c83aeb 100644 --- a/libtests/pointer_holder.cc +++ b/libtests/pointer_holder.cc @@ -6,8 +6,8 @@ #include <qpdf/PointerHolder.hh> #include <iostream> -#include <stdlib.h> #include <list> +#include <stdlib.h> class Object { @@ -22,7 +22,6 @@ class Object int id; }; - int Object::next_id = 0; Object::Object() @@ -50,21 +49,24 @@ Object::hello() const typedef PointerHolder<Object> ObjectHolder; -void callHello(ObjectHolder& oh) +void +callHello(ObjectHolder& oh) { oh.getPointer()->hello(); oh->hello(); (*oh).hello(); } -void callHelloWithGet(ObjectHolder const& oh) +void +callHelloWithGet(ObjectHolder const& oh) { oh.get()->hello(); oh->hello(); (*oh).hello(); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { std::list<ObjectHolder> ol1; @@ -81,18 +83,15 @@ int main(int argc, char* argv[]) ObjectHolder oh4; ObjectHolder oh5; std::cout << "oh5 refcount = " << oh5.getRefcount() << std::endl; - if (oh4 == oh5) - { + if (oh4 == oh5) { std::cout << "nulls equal" << std::endl; } oh3 = oh1; oh4 = oh2; - if (oh3 == oh4) - { + if (oh3 == oh4) { std::cout << "equal okay" << std::endl; } - if ((! (oh3 < oh4)) && (! (oh4 < oh3))) - { + if ((!(oh3 < oh4)) && (!(oh4 < oh3))) { std::cout << "less than okay" << std::endl; } ol1.push_back(oh3); diff --git a/libtests/predictors.cc b/libtests/predictors.cc index f9fa759e..c024cbd6 100644 --- a/libtests/predictors.cc +++ b/libtests/predictors.cc @@ -1,13 +1,13 @@ #include <qpdf/Pl_PNGFilter.hh> -#include <qpdf/Pl_TIFFPredictor.hh> #include <qpdf/Pl_StdioFile.hh> -#include <qpdf/QUtil.hh> +#include <qpdf/Pl_TIFFPredictor.hh> #include <qpdf/QIntC.hh> +#include <qpdf/QUtil.hh> -#include <iostream> #include <errno.h> -#include <string.h> +#include <iostream> #include <stdlib.h> +#include <string.h> #ifdef NDEBUG // We need assert even in a release build for test code. @@ -15,53 +15,54 @@ #endif #include <cassert> -void run(char const* filename, char const* filter, - bool encode, unsigned int columns, - unsigned int bits_per_sample, unsigned int samples_per_pixel) +void +run(char const* filename, + char const* filter, + bool encode, + unsigned int columns, + unsigned int bits_per_sample, + unsigned int samples_per_pixel) { FILE* in = QUtil::safe_fopen(filename, "rb"); FILE* o1 = QUtil::safe_fopen("out", "wb"); Pipeline* out = new Pl_StdioFile("out", o1); Pipeline* pl = 0; - if (strcmp(filter, "png") == 0) - { + if (strcmp(filter, "png") == 0) { pl = new Pl_PNGFilter( - "png", out, + "png", + out, encode ? Pl_PNGFilter::a_encode : Pl_PNGFilter::a_decode, - columns, samples_per_pixel, bits_per_sample); - } - else if (strcmp(filter, "tiff") == 0) - { + columns, + samples_per_pixel, + bits_per_sample); + } else if (strcmp(filter, "tiff") == 0) { pl = new Pl_TIFFPredictor( - "png", out, + "png", + out, encode ? Pl_TIFFPredictor::a_encode : Pl_TIFFPredictor::a_decode, - columns, samples_per_pixel, bits_per_sample); - } - else - { + columns, + samples_per_pixel, + bits_per_sample); + } else { std::cerr << "unknown filter " << filter << std::endl; exit(2); } assert((2 * (columns + 1)) < 1024); unsigned char buf[1024]; size_t len; - while (true) - { + while (true) { len = fread(buf, 1, (2 * columns) + 1, in); - if (len == 0) - { + if (len == 0) { break; } pl->write(buf, len); len = fread(buf, 1, 1, in); - if (len == 0) - { + if (len == 0) { break; } pl->write(buf, len); len = fread(buf, 1, 1, in); - if (len == 0) - { + if (len == 0) { break; } pl->write(buf, len); @@ -76,13 +77,12 @@ void run(char const* filename, char const* filter, std::cout << "done" << std::endl; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if (argc != 7) - { + if (argc != 7) { std::cerr << "Usage: predictor {png|tiff} {en,de}code filename" - << " columns samples-per-pixel bits-per-sample" - << std::endl; + << " columns samples-per-pixel bits-per-sample" << std::endl; exit(2); } char* filter = argv[1]; @@ -92,15 +92,14 @@ int main(int argc, char* argv[]) int samples_per_pixel = QUtil::string_to_int(argv[5]); int bits_per_sample = QUtil::string_to_int(argv[6]); - try - { - run(filename, filter, encode, + try { + run(filename, + filter, + encode, QIntC::to_uint(columns), QIntC::to_uint(bits_per_sample), QIntC::to_uint(samples_per_pixel)); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << e.what() << std::endl; } return 0; diff --git a/libtests/qintc.cc b/libtests/qintc.cc index 85806d1a..d3e19f5b 100644 --- a/libtests/qintc.cc +++ b/libtests/qintc.cc @@ -8,22 +8,22 @@ #include <cassert> #define try_convert(exp_pass, fn, i) \ - try_convert_real(#fn "(" #i ")", exp_pass, fn, i) + try_convert_real(#fn "(" #i ")", exp_pass, fn, i) template <typename From, typename To> -static void try_convert_real( - char const* description, bool exp_pass, - To (*fn)(From const&), From const& i) +static void +try_convert_real( + char const* description, + bool exp_pass, + To (*fn)(From const&), + From const& i) { bool passed = false; - try - { + try { To result = fn(i); passed = true; std::cout << description << ": " << i << " " << result; - } - catch (std::range_error& e) - { + } catch (std::range_error& e) { std::cout << description << ": " << e.what(); passed = false; } @@ -31,22 +31,19 @@ static void try_convert_real( } #define try_range_check(exp_pass, a, b) \ - try_range_check_real(#a " + " #b, exp_pass, a, b) + try_range_check_real(#a " + " #b, exp_pass, a, b) template <typename T> -static void try_range_check_real( - char const* description, bool exp_pass, - T const& a, T const& b) +static void +try_range_check_real( + char const* description, bool exp_pass, T const& a, T const& b) { bool passed = false; - try - { + try { QIntC::range_check(a, b); std::cout << description << ": okay"; passed = true; - } - catch (std::range_error& e) - { + } catch (std::range_error& e) { std::cout << description << ": " << e.what(); passed = false; } @@ -54,29 +51,27 @@ static void try_range_check_real( } #define try_range_check_subtract(exp_pass, a, b) \ - try_range_check_subtract_real(#a " - " #b, exp_pass, a, b) + try_range_check_subtract_real(#a " - " #b, exp_pass, a, b) template <typename T> -static void try_range_check_subtract_real( - char const* description, bool exp_pass, - T const& a, T const& b) +static void +try_range_check_subtract_real( + char const* description, bool exp_pass, T const& a, T const& b) { bool passed = false; - try - { + try { QIntC::range_check_substract(a, b); std::cout << description << ": okay"; passed = true; - } - catch (std::range_error& e) - { + } catch (std::range_error& e) { std::cout << description << ": " << e.what(); passed = false; } std::cout << ((passed == exp_pass) ? " PASSED" : " FAILED") << std::endl; } -int main() +int +main() { uint32_t u1 = 3141592653U; // Too big for signed type int32_t i1 = -1153374643; // Same bit pattern as u1 @@ -84,28 +79,28 @@ int main() uint64_t ul2 = 12345; // Fits into 32-bit int32_t i2 = 81; // Fits in char and uchar signed char c1 = static_cast<signed char>('\xf7'); // Signed value when char - char c2 = 'W'; // char; may be signed or unsigned + char c2 = 'W'; // char; may be signed or unsigned // Verify i1 and u1 have same bit pattern assert(static_cast<uint32_t>(i1) == u1); // Verify that we can unsafely convert between signed and unsigned char assert(c1 == static_cast<signed char>(static_cast<unsigned char>(c1))); - try_convert(true, QIntC::to_int<int32_t>, i1); - try_convert(true, QIntC::to_uint<uint32_t>, u1); + try_convert(true, QIntC::to_int<int32_t>, i1); + try_convert(true, QIntC::to_uint<uint32_t>, u1); try_convert(false, QIntC::to_int<uint32_t>, u1); try_convert(false, QIntC::to_uint<int32_t>, i1); try_convert(false, QIntC::to_int<uint64_t>, ul1); - try_convert(true, QIntC::to_int<uint64_t>, ul2); - try_convert(true, QIntC::to_uint<uint64_t>, ul2); - try_convert(true, QIntC::to_offset<uint32_t>, u1); - try_convert(true, QIntC::to_offset<int32_t>, i1); + try_convert(true, QIntC::to_int<uint64_t>, ul2); + try_convert(true, QIntC::to_uint<uint64_t>, ul2); + try_convert(true, QIntC::to_offset<uint32_t>, u1); + try_convert(true, QIntC::to_offset<int32_t>, i1); try_convert(false, QIntC::to_ulonglong<int32_t>, i1); - try_convert(true, QIntC::to_char<int32_t>, i2); - try_convert(true, QIntC::to_uchar<int32_t>, i2); + try_convert(true, QIntC::to_char<int32_t>, i2); + try_convert(true, QIntC::to_uchar<int32_t>, i2); try_convert(false, QIntC::to_uchar<signed char>, c1); - try_convert(true, QIntC::to_uchar<char>, c2); - try_convert(true, QIntC::to_char<char>, c2); + try_convert(true, QIntC::to_uchar<char>, c2); + try_convert(true, QIntC::to_char<char>, c2); auto constexpr max_ll = std::numeric_limits<long long>::max(); auto constexpr max_ull = std::numeric_limits<unsigned long long>::max(); diff --git a/libtests/qutil.cc b/libtests/qutil.cc index c3a9b163..fea9fca1 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -1,16 +1,16 @@ -#include <iostream> -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <qpdf/QUtil.hh> +#include <qpdf/Pl_Buffer.hh> #include <qpdf/PointerHolder.hh> #include <qpdf/QPDFSystemError.hh> -#include <qpdf/Pl_Buffer.hh> -#include <string.h> -#include <limits.h> +#include <qpdf/QUtil.hh> +#include <fcntl.h> #include <fstream> +#include <iostream> +#include <limits.h> #include <locale> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> #ifdef _WIN32 # include <io.h> @@ -25,96 +25,86 @@ #include <cassert> template <class int_T> -void test_to_number(char const* str, int_T wanted, bool error, - int_T (*fn)(char const*)) +void +test_to_number( + char const* str, int_T wanted, bool error, int_T (*fn)(char const*)) { bool threw = false; bool worked = false; int_T result = 0; std::string msg; - try - { + try { result = fn(str); worked = (wanted == result); - } - catch (std::runtime_error const& e) - { + } catch (std::runtime_error const& e) { threw = true; msg = e.what(); } - if (threw) - { - if (error) - { - std::cout << str << " to int threw (" << msg << "): PASSED" << std::endl; - } - else - { - std::cout << str << " to int threw but wanted " - << wanted << std::endl; + if (threw) { + if (error) { + std::cout << str << " to int threw (" << msg << "): PASSED" + << std::endl; + } else { + std::cout << str << " to int threw but wanted " << wanted + << std::endl; } - } - else - { - if (worked) - { + } else { + if (worked) { std::cout << str << " to int: PASSED" << std::endl; - } - else - { + } else { std::cout << str << " to int failed; got " << result << std::endl; } } } -void test_to_int(char const* str, int wanted, bool error) +void +test_to_int(char const* str, int wanted, bool error) { test_to_number(str, wanted, error, QUtil::string_to_int); } -void test_to_ll(char const* str, long long wanted, bool error) +void +test_to_ll(char const* str, long long wanted, bool error) { test_to_number(str, wanted, error, QUtil::string_to_ll); } -void test_to_uint(char const* str, unsigned int wanted, bool error) +void +test_to_uint(char const* str, unsigned int wanted, bool error) { test_to_number(str, wanted, error, QUtil::string_to_uint); } -void test_to_ull(char const* str, unsigned long long wanted, bool error) +void +test_to_ull(char const* str, unsigned long long wanted, bool error) { test_to_number(str, wanted, error, QUtil::string_to_ull); } -static void set_locale() +static void +set_locale() { - try - { + try { // First try a locale known to put commas in numbers. std::locale::global(std::locale("en_US.UTF-8")); - } - catch (std::runtime_error&) - { - try - { + } catch (std::runtime_error&) { + try { // If that fails, fall back to the user's default locale. std::locale::global(std::locale("")); - } - catch (std::runtime_error& e) - { + } catch (std::runtime_error& e) { // Ignore this error on Windows without MSVC. We get // enough test coverage on other platforms, and mingw // seems to have limited locale support (as of // 2020-10). -#if ! defined(_WIN32) || defined(_MSC_VER) +#if !defined(_WIN32) || defined(_MSC_VER) throw e; #endif } } } -void string_conversion_test() +void +string_conversion_test() { // Make sure the code produces consistent results even if we load // a non-C locale. @@ -146,32 +136,23 @@ void string_conversion_test() std::cout << embedded_null.c_str() << std::endl; std::cout << embedded_null.length() << std::endl; char* tmp = QUtil::copy_string(embedded_null); - if (memcmp(tmp, embedded_null.c_str(), 7) == 0) - { + if (memcmp(tmp, embedded_null.c_str(), 7) == 0) { std::cout << "compare okay" << std::endl; - } - else - { + } else { std::cout << "compare failed" << std::endl; } - delete [] tmp; + delete[] tmp; // Also test with make_shared_cstr and make_unique_cstr auto tmp2 = QUtil::make_shared_cstr(embedded_null); - if (memcmp(tmp2.get(), embedded_null.c_str(), 7) == 0) - { + if (memcmp(tmp2.get(), embedded_null.c_str(), 7) == 0) { std::cout << "compare okay" << std::endl; - } - else - { + } else { std::cout << "compare failed" << std::endl; } auto tmp3 = QUtil::make_unique_cstr(embedded_null); - if (memcmp(tmp3.get(), embedded_null.c_str(), 7) == 0) - { + if (memcmp(tmp3.get(), embedded_null.c_str(), 7) == 0) { std::cout << "compare okay" << std::endl; - } - else - { + } else { std::cout << "compare failed" << std::endl; } @@ -198,41 +179,37 @@ void string_conversion_test() test_to_ull(small_negative.c_str(), 0, true); } -void os_wrapper_test() +void +os_wrapper_test() { - try - { + try { std::cout << "before remove" << std::endl; - QUtil::os_wrapper("remove file", - remove("/this/file/does/not/exist")); + QUtil::os_wrapper("remove file", remove("/this/file/does/not/exist")); std::cout << "after remove" << std::endl; - } - catch (std::runtime_error& s) - { + } catch (std::runtime_error& s) { std::cout << "exception: " << s.what() << std::endl; } } -void fopen_wrapper_test() +void +fopen_wrapper_test() { - try - { + try { std::cout << "before fopen" << std::endl; FILE* f = QUtil::safe_fopen("/this/file/does/not/exist", "r"); std::cout << "after fopen" << std::endl; - (void) fclose(f); - } - catch (QPDFSystemError& s) - { + (void)fclose(f); + } catch (QPDFSystemError& s) { std::cout << "exception: " << s.what() << std::endl; assert(s.getErrno() != 0); } assert(QUtil::file_can_be_opened("qutil.out")); - assert(! QUtil::file_can_be_opened("/does/not/exist")); + assert(!QUtil::file_can_be_opened("/does/not/exist")); } -void getenv_test() +void +getenv_test() { std::string val; std::cout << "IN_TESTSUITE: " << QUtil::get_env("IN_TESTSUITE", &val) @@ -242,60 +219,63 @@ void getenv_test() << std::endl; } -static void print_utf8(unsigned long val) +static void +print_utf8(unsigned long val) { std::string result = QUtil::toUTF8(val); std::cout << "0x" << QUtil::uint_to_string_base(val, 16) << " ->"; - if (val < 0xfffe) - { + if (val < 0xfffe) { std::cout << " " << result; - } - else - { + } else { // Emacs has trouble with utf-8 encoding files with characters // outside the 16-bit portion, so just show the character // values. - for (std::string::iterator iter = result.begin(); - iter != result.end(); ++iter) - { - std::cout << " " << QUtil::int_to_string_base( - static_cast<int>(static_cast<unsigned char>(*iter)), 16, 2); + for (std::string::iterator iter = result.begin(); iter != result.end(); + ++iter) { + std::cout << " " + << QUtil::int_to_string_base( + static_cast<int>( + static_cast<unsigned char>(*iter)), + 16, + 2); } } std::cout << std::endl; } -void to_utf8_test() +void +to_utf8_test() { print_utf8(0x41UL); print_utf8(0xF7UL); print_utf8(0x3c0UL); print_utf8(0x16059UL); print_utf8(0x7fffffffUL); - try - { + try { print_utf8(0x80000000UL); - } - catch (std::runtime_error& e) - { + } catch (std::runtime_error& e) { std::cout << "0x80000000: " << e.what() << std::endl; } } -static void print_utf16(unsigned long val) +static void +print_utf16(unsigned long val) { std::string result = QUtil::toUTF16(val); std::cout << "0x" << QUtil::uint_to_string_base(val, 16) << " ->"; - for (std::string::iterator iter = result.begin(); - iter != result.end(); ++iter) - { - std::cout << " " << QUtil::int_to_string_base( - static_cast<int>(static_cast<unsigned char>(*iter)), 16, 2); + for (std::string::iterator iter = result.begin(); iter != result.end(); + ++iter) { + std::cout << " " + << QUtil::int_to_string_base( + static_cast<int>(static_cast<unsigned char>(*iter)), + 16, + 2); } std::cout << std::endl; } -void to_utf16_test() +void +to_utf16_test() { print_utf16(0x41UL); print_utf16(0xF7UL); @@ -308,88 +288,88 @@ void to_utf16_test() std::string s(QUtil::utf8_to_utf16("\xcf\x80")); std::cout << QUtil::utf16_to_utf8(s) << std::endl; std::cout << QUtil::utf16_to_utf8(s + ".") << std::endl; - std::cout << "LE: " << QUtil::utf16_to_utf8("\xff\xfe\xc0\x03") << std::endl; + std::cout << "LE: " << QUtil::utf16_to_utf8("\xff\xfe\xc0\x03") + << std::endl; } -void utf8_to_ascii_test() +void +utf8_to_ascii_test() { char const* input = "\302\277Does \317\200 have fingers?"; - std::cout << input - << std::endl - << QUtil::utf8_to_ascii(input) - << std::endl - << QUtil::utf8_to_ascii(input, '*') - << std::endl; + std::cout << input << std::endl + << QUtil::utf8_to_ascii(input) << std::endl + << QUtil::utf8_to_ascii(input, '*') << std::endl; std::string a = QUtil::utf8_to_win_ansi(input, '*'); std::string b = QUtil::utf8_to_mac_roman(input, '*'); std::cout - << "<" << QUtil::int_to_string_base( - static_cast<unsigned char>(a.at(0)), 16, 2) + << "<" + << QUtil::int_to_string_base(static_cast<unsigned char>(a.at(0)), 16, 2) << ">" << a.substr(1) << std::endl - << "<" << QUtil::int_to_string_base( - static_cast<unsigned char>(b.at(0)), 16, 2) + << "<" + << QUtil::int_to_string_base(static_cast<unsigned char>(b.at(0)), 16, 2) << ">" << b.substr(1) << std::endl; } -void transcoding_test(std::string (*to_utf8)(std::string const&), - std::string (*from_utf8)(std::string const&, char), - int first, int last, std::string unknown) +void +transcoding_test( + std::string (*to_utf8)(std::string const&), + std::string (*from_utf8)(std::string const&, char), + int first, + int last, + std::string unknown) { std::string in(" "); std::string out; std::string back; - for (int i = first; i <= last; ++i) - { + for (int i = first; i <= last; ++i) { in.at(0) = static_cast<char>(static_cast<unsigned char>(i)); out = (*to_utf8)(in); std::string wanted = (out == "\xef\xbf\xbd") ? unknown : in; back = (*from_utf8)(out, '?'); - if (back != wanted) - { - std::cout << i << ": " << in << " -> " << out - << " -> " << back << " (wanted " << wanted << ")" - << std::endl; + if (back != wanted) { + std::cout << i << ": " << in << " -> " << out << " -> " << back + << " (wanted " << wanted << ")" << std::endl; } } } -void check_analyze(std::string const& str, bool has8bit, bool utf8, bool utf16) +void +check_analyze(std::string const& str, bool has8bit, bool utf8, bool utf16) { bool has_8bit_chars = false; bool is_valid_utf8 = false; bool is_utf16 = false; QUtil::analyze_encoding(str, has_8bit_chars, is_valid_utf8, is_utf16); - if (! ((has_8bit_chars == has8bit) && - (is_valid_utf8 == utf8) && - (is_utf16 == utf16))) - { + if (!((has_8bit_chars == has8bit) && (is_valid_utf8 == utf8) && + (is_utf16 == utf16))) { std::cout << "analysis failed: " << str << std::endl; } } -void print_alternatives(std::string const& str) +void +print_alternatives(std::string const& str) { std::vector<std::string> result = QUtil::possible_repaired_encodings(str); size_t n = result.size(); - for (size_t i = 0; i < n; ++i) - { + for (size_t i = 0; i < n; ++i) { std::cout << i << ": " << QUtil::hex_encode(result.at(i)) << std::endl; } } -void transcoding_test() +void +transcoding_test() { - transcoding_test(&QUtil::pdf_doc_to_utf8, - &QUtil::utf8_to_pdf_doc, 127, 160, "\x9f"); + transcoding_test( + &QUtil::pdf_doc_to_utf8, &QUtil::utf8_to_pdf_doc, 127, 160, "\x9f"); std::cout << "bidirectional pdf doc done" << std::endl; - transcoding_test(&QUtil::pdf_doc_to_utf8, - &QUtil::utf8_to_pdf_doc, 24, 31, "?"); + transcoding_test( + &QUtil::pdf_doc_to_utf8, &QUtil::utf8_to_pdf_doc, 24, 31, "?"); std::cout << "bidirectional pdf doc low done" << std::endl; - transcoding_test(&QUtil::win_ansi_to_utf8, - &QUtil::utf8_to_win_ansi, 128, 160, "?"); + transcoding_test( + &QUtil::win_ansi_to_utf8, &QUtil::utf8_to_win_ansi, 128, 160, "?"); std::cout << "bidirectional win ansi done" << std::endl; - transcoding_test(&QUtil::mac_roman_to_utf8, - &QUtil::utf8_to_mac_roman, 128, 255, "?"); + transcoding_test( + &QUtil::mac_roman_to_utf8, &QUtil::utf8_to_mac_roman, 128, 255, "?"); std::cout << "bidirectional mac roman done" << std::endl; check_analyze("pi = \317\200", true, true, false); check_analyze("pi != \317", true, false, false); @@ -401,17 +381,17 @@ void transcoding_test() std::string input2("a\317\200b"); std::string input3("ab"); std::string output; - assert(! QUtil::utf8_to_ascii(input1, output)); - assert(! QUtil::utf8_to_ascii(input2, output)); + assert(!QUtil::utf8_to_ascii(input1, output)); + assert(!QUtil::utf8_to_ascii(input2, output)); assert(QUtil::utf8_to_ascii(input3, output)); assert(QUtil::utf8_to_win_ansi(input1, output)); - assert(! QUtil::utf8_to_win_ansi(input2, output)); + assert(!QUtil::utf8_to_win_ansi(input2, output)); assert(QUtil::utf8_to_win_ansi(input3, output)); assert(QUtil::utf8_to_mac_roman(input1, output)); - assert(! QUtil::utf8_to_mac_roman(input2, output)); + assert(!QUtil::utf8_to_mac_roman(input2, output)); assert(QUtil::utf8_to_mac_roman(input3, output)); assert(QUtil::utf8_to_pdf_doc(input1, output)); - assert(! QUtil::utf8_to_pdf_doc(input2, output)); + assert(!QUtil::utf8_to_pdf_doc(input2, output)); assert(QUtil::utf8_to_pdf_doc(input3, output)); std::cout << "alternatives" << std::endl; // char name mac win pdf-doc @@ -434,18 +414,20 @@ void transcoding_test() std::string other_utf8 = other + QUtil::toUTF8(0x9f) + "w" + QUtil::toUTF8(0xad) + "w"; std::string other_to_utf8; - assert(! QUtil::utf8_to_pdf_doc(other_utf8, other_to_utf8)); + assert(!QUtil::utf8_to_pdf_doc(other_utf8, other_to_utf8)); std::cout << other_to_utf8 << std::endl; std::cout << "done other characters" << std::endl; } -void print_whoami(char const* str) +void +print_whoami(char const* str) { auto dup = QUtil::make_unique_cstr(str); std::cout << QUtil::getWhoami(dup.get()) << std::endl; } -void get_whoami_test() +void +get_whoami_test() { print_whoami("a/b/c/quack1"); print_whoami("a/b/c/quack2.exe"); @@ -453,26 +435,24 @@ void get_whoami_test() print_whoami("a\\b\\c\\quack4.exe"); } -void assert_same_file(char const* file1, char const* file2, bool expected) +void +assert_same_file(char const* file1, char const* file2, bool expected) { bool actual = QUtil::same_file(file1, file2); std::cout << "file1: -" << (file1 ? file1 : "(null)") << "-, file2: -" - << (file2 ? file2 : "(null)") << "-; same: " - << actual << ": " << ((actual == expected) ? "PASS" : "FAIL") - << std::endl; + << (file2 ? file2 : "(null)") << "-; same: " << actual << ": " + << ((actual == expected) ? "PASS" : "FAIL") << std::endl; } -void same_file_test() +void +same_file_test() { - try - { + try { fclose(QUtil::safe_fopen("qutil.out", "r")); fclose(QUtil::safe_fopen("other-file", "r")); - } - catch (std::exception const&) - { + } catch (std::exception const&) { std::cout << "same_file_test expects to have qutil.out and other-file" - " exist in the current directory\n"; + " exist in the current directory\n"; return; } assert_same_file("qutil.out", "./qutil.out", true); @@ -483,12 +463,12 @@ void same_file_test() assert_same_file("", "qutil.out", false); } -void path_test() +void +path_test() { auto check = [](bool print, std::string const& a, std::string const& b) { auto result = QUtil::path_basename(a); - if (print) - { + if (print) { std::cout << a << " -> " << result << std::endl; } assert(result == b); @@ -506,12 +486,13 @@ void path_test() check(true, "quack", "quack"); } -void read_from_file_test() +void +read_from_file_test() { std::list<std::string> lines = QUtil::read_lines_from_file("other-file"); for (std::list<std::string>::iterator iter = lines.begin(); - iter != lines.end(); ++iter) - { + iter != lines.end(); + ++iter) { std::cout << *iter << std::endl; } // Test the other versions and make sure we get the same results @@ -565,36 +546,34 @@ void read_from_file_test() assert(memcmp(buf2->getBuffer(), p, size) == 0); } -void assert_hex_encode(std::string const& input, std::string const& expected) +void +assert_hex_encode(std::string const& input, std::string const& expected) { std::string actual = QUtil::hex_encode(input); - if (expected != actual) - { - std::cout << "hex encode " << input - << ": expected = " << expected - << "; actual = " << actual - << std::endl; + if (expected != actual) { + std::cout << "hex encode " << input << ": expected = " << expected + << "; actual = " << actual << std::endl; } } -void assert_hex_decode(std::string const& input, std::string const& expected) +void +assert_hex_decode(std::string const& input, std::string const& expected) { std::string actual = QUtil::hex_decode(input); - if (expected != actual) - { - std::cout << "hex encode " << input - << ": expected = " << expected - << "; actual = " << actual - << std::endl; + if (expected != actual) { + std::cout << "hex encode " << input << ": expected = " << expected + << "; actual = " << actual << std::endl; } } -void hex_encode_decode_test() +void +hex_encode_decode_test() { std::cout << "begin hex encode/decode\n"; assert_hex_encode("", ""); assert_hex_encode("Potato", "506f7461746f"); - std::string with_null("a\367" "00w"); + std::string with_null("a\367" + "00w"); with_null[3] = '\0'; assert_hex_encode(with_null, "61f7300077"); assert_hex_decode("", ""); @@ -603,58 +582,60 @@ void hex_encode_decode_test() std::cout << "end hex encode/decode\n"; } -static void assert_no_file(char const* filename) +static void +assert_no_file(char const* filename) { - try - { + try { fclose(QUtil::safe_fopen(filename, "r")); assert(false); - } - catch (QPDFSystemError&) - { + } catch (QPDFSystemError&) { } } -void rename_delete_test() +void +rename_delete_test() { PointerHolder<char> buf; size_t size = 0; - try - { + try { QUtil::remove_file("old\xcf\x80"); - } - catch (QPDFSystemError&) - { + } catch (QPDFSystemError&) { } assert_no_file("old\xcf\x80"); - std::cout << "create file" << std::endl;; + std::cout << "create file" << std::endl; + ; FILE* f1 = QUtil::safe_fopen("old\xcf\x80", "w"); fprintf(f1, "one"); fclose(f1); QUtil::read_file_into_memory("old\xcf\x80", buf, size); assert(memcmp(buf.get(), "one", 3) == 0); - std::cout << "rename file" << std::endl;; + std::cout << "rename file" << std::endl; + ; QUtil::rename_file("old\xcf\x80", "old\xcf\x80.~tmp"); QUtil::read_file_into_memory("old\xcf\x80.~tmp", buf, size); assert(memcmp(buf.get(), "one", 3) == 0); assert_no_file("old\xcf\x80"); - std::cout << "create file" << std::endl;; + std::cout << "create file" << std::endl; + ; f1 = QUtil::safe_fopen("old\xcf\x80", "w"); fprintf(f1, "two"); fclose(f1); - std::cout << "rename over existing" << std::endl;; + std::cout << "rename over existing" << std::endl; + ; QUtil::rename_file("old\xcf\x80", "old\xcf\x80.~tmp"); QUtil::read_file_into_memory("old\xcf\x80.~tmp", buf, size); assert(memcmp(buf.get(), "two", 3) == 0); assert_no_file("old\xcf\x80"); - std::cout << "delete file" << std::endl;; + std::cout << "delete file" << std::endl; + ; QUtil::remove_file("old\xcf\x80.~tmp"); assert_no_file("old\xcf\x80"); assert_no_file("old\xcf\x80.~tmp"); } -void timestamp_test() +void +timestamp_test() { auto check = [](QUtil::QPDFTime const& t) { std::string pdf = QUtil::qpdf_time_to_pdf_time(t); @@ -666,7 +647,7 @@ void timestamp_test() check(QUtil::QPDFTime(2021, 2, 9, 14, 49, 25, 300)); check(QUtil::QPDFTime(2021, 2, 10, 1, 19, 25, -330)); check(QUtil::QPDFTime(2021, 2, 9, 19, 19, 25, 0)); - assert(! QUtil::pdf_time_to_qpdf_time("potato")); + assert(!QUtil::pdf_time_to_qpdf_time("potato")); assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743Z")); assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743-05'00'")); assert(QUtil::pdf_time_to_qpdf_time("D:20210211064743+05'30'")); @@ -675,14 +656,13 @@ void timestamp_test() // Manual testing was done to ensure that we are actually getting // back the current time in various time zones. assert(QUtil::pdf_time_to_qpdf_time( - QUtil::qpdf_time_to_pdf_time( - QUtil::get_current_qpdf_time()))); + QUtil::qpdf_time_to_pdf_time(QUtil::get_current_qpdf_time()))); } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - try - { + try { std::cout << "---- string conversion" << std::endl; string_conversion_test(); std::cout << "---- os wrapper" << std::endl; @@ -713,9 +693,7 @@ int main(int argc, char* argv[]) rename_delete_test(); std::cout << "---- timestamp" << std::endl; timestamp_test(); - } - catch (std::exception& e) - { + } catch (std::exception& e) { std::cout << "unexpected exception: " << e.what() << std::endl; } diff --git a/libtests/random.cc b/libtests/random.cc index 70e5135e..2148111c 100644 --- a/libtests/random.cc +++ b/libtests/random.cc @@ -1,7 +1,7 @@ -#include <qpdf/QUtil.hh> -#include <qpdf/qpdf-config.h> #include <qpdf/InsecureRandomDataProvider.hh> +#include <qpdf/QUtil.hh> #include <qpdf/SecureRandomDataProvider.hh> +#include <qpdf/qpdf-config.h> #include <iostream> class BogusRandomDataProvider: public RandomDataProvider @@ -13,67 +13,58 @@ class BogusRandomDataProvider: public RandomDataProvider BogusRandomDataProvider() { } - virtual void provideRandomData(unsigned char* data, size_t len) + virtual void + provideRandomData(unsigned char* data, size_t len) { - for (size_t i = 0; i < len; ++i) - { + for (size_t i = 0; i < len; ++i) { data[i] = static_cast<unsigned char>(i & 0xff); } } }; -int main() +int +main() { RandomDataProvider* orig_rdp = QUtil::getRandomDataProvider(); long r1 = QUtil::random(); long r2 = QUtil::random(); - if (r1 == r2) - { + if (r1 == r2) { std::cout << "fail: two randoms were the same\n"; } InsecureRandomDataProvider irdp; irdp.provideRandomData(reinterpret_cast<unsigned char*>(&r1), 4); irdp.provideRandomData(reinterpret_cast<unsigned char*>(&r2), 4); - if (r1 == r2) - { + if (r1 == r2) { std::cout << "fail: two insecure randoms were the same\n"; } #ifndef SKIP_OS_SECURE_RANDOM SecureRandomDataProvider srdp; srdp.provideRandomData(reinterpret_cast<unsigned char*>(&r1), 4); srdp.provideRandomData(reinterpret_cast<unsigned char*>(&r2), 4); - if (r1 == r2) - { + if (r1 == r2) { std::cout << "fail: two secure randoms were the same\n"; } #endif BogusRandomDataProvider brdp; QUtil::setRandomDataProvider(&brdp); - if (QUtil::getRandomDataProvider() != &brdp) - { + if (QUtil::getRandomDataProvider() != &brdp) { std::cout << "fail: getRandomDataProvider didn't" - " return our provider\n"; + " return our provider\n"; } r1 = QUtil::random(); r2 = QUtil::random(); - if (r1 != r2) - { + if (r1 != r2) { std::cout << "fail: two bogus randoms were different\n"; } unsigned char buf[4]; QUtil::initializeWithRandomBytes(buf, 4); - if (! ((buf[0] == 0) && - (buf[1] == 1) && - (buf[2] == 2) && - (buf[3] == 3))) - { + if (!((buf[0] == 0) && (buf[1] == 1) && (buf[2] == 2) && (buf[3] == 3))) { std::cout << "fail: bogus random didn't provide correct bytes\n"; } QUtil::setRandomDataProvider(0); - if (QUtil::getRandomDataProvider() != orig_rdp) - { + if (QUtil::getRandomDataProvider() != orig_rdp) { std::cout << "fail: passing null to setRandomDataProvider " - "didn't reset the random data provider\n"; + "didn't reset the random data provider\n"; } std::cout << "random: end of tests\n"; return 0; diff --git a/libtests/rc4.cc b/libtests/rc4.cc index b7aecd4b..7245ef86 100644 --- a/libtests/rc4.cc +++ b/libtests/rc4.cc @@ -1,12 +1,12 @@ #include <qpdf/Pl_RC4.hh> #include <qpdf/Pl_StdioFile.hh> -#include <qpdf/QUtil.hh> #include <qpdf/QIntC.hh> +#include <qpdf/QUtil.hh> -#include <stdio.h> -#include <string.h> #include <iostream> +#include <stdio.h> #include <stdlib.h> +#include <string.h> #ifdef NDEBUG // We need assert even in a release build for test code. @@ -14,7 +14,8 @@ #endif #include <cassert> -static void other_tests() +static void +other_tests() { // Test cases not covered by the pipeline: string as key, convert // in place @@ -26,16 +27,15 @@ static void other_tests() std::cout << "passed" << std::endl; } -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if ((argc == 2) && (strcmp(argv[1], "other") == 0)) - { + if ((argc == 2) && (strcmp(argv[1], "other") == 0)) { other_tests(); return 0; } - if (argc != 4) - { + if (argc != 4) { std::cerr << "Usage: rc4 hex-key infile outfile" << std::endl; exit(2); } @@ -49,35 +49,30 @@ int main(int argc, char* argv[]) key[keylen] = '\0'; FILE* infile = QUtil::safe_fopen(infilename, "rb"); - for (unsigned int i = 0; i < strlen(hexkey); i += 2) - { + for (unsigned int i = 0; i < strlen(hexkey); i += 2) { char t[3]; t[0] = hexkey[i]; t[1] = hexkey[i + 1]; t[2] = '\0'; long val = strtol(t, 0, 16); - key[i/2] = static_cast<unsigned char>(val); + key[i / 2] = static_cast<unsigned char>(val); } FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); // Use a small buffer size (64) for testing Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, QIntC::to_int(keylen), 64U); - delete [] key; + delete[] key; // 64 < buffer size < 512, buffer_size is not a power of 2 for testing unsigned char buf[100]; bool done = false; - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), infile); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { rc4->write(buf, len); } } diff --git a/libtests/runlength.cc b/libtests/runlength.cc index f54e7300..456e33a2 100644 --- a/libtests/runlength.cc +++ b/libtests/runlength.cc @@ -2,15 +2,15 @@ #include <qpdf/Pl_StdioFile.hh> #include <qpdf/QUtil.hh> -#include <stdio.h> -#include <string.h> #include <iostream> +#include <stdio.h> #include <stdlib.h> +#include <string.h> -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { - if (argc != 4) - { + if (argc != 4) { std::cerr << "Usage: runlength {-encode|-decode} infile outfile" << std::endl; exit(2); @@ -26,17 +26,14 @@ int main(int argc, char* argv[]) unsigned char buf[100]; bool done = false; Pl_RunLength rl( - "runlength", &out, + "runlength", + &out, (encode ? Pl_RunLength::a_encode : Pl_RunLength::a_decode)); - while (! done) - { + while (!done) { size_t len = fread(buf, 1, sizeof(buf), infile); - if (len <= 0) - { + if (len <= 0) { done = true; - } - else - { + } else { rl.write(buf, len); } } diff --git a/libtests/sha2.cc b/libtests/sha2.cc index 5110b89f..57bc0c12 100644 --- a/libtests/sha2.cc +++ b/libtests/sha2.cc @@ -1,69 +1,99 @@ #include <qpdf/Pl_SHA2.hh> +#include <qpdf/QUtil.hh> #include <iostream> #include <stdlib.h> #include <string.h> -#include <qpdf/QUtil.hh> -static void test(Pl_SHA2& sha2, char const* description, int bits, - char const* input, std::string const& output) +static void +test( + Pl_SHA2& sha2, + char const* description, + int bits, + char const* input, + std::string const& output) { sha2.resetBits(bits); sha2.write(QUtil::unsigned_char_pointer(input), strlen(input)); sha2.finish(); std::cout << description << ": "; - if (output == sha2.getHexDigest()) - { + if (output == sha2.getHexDigest()) { std::cout << "passed\n"; - } - else - { + } else { std::cout << "failed\n" << " expected: " << output << "\n" << " actual: " << sha2.getHexDigest() << "\n"; } } -int main( int argc, char *argv[] ) +int +main(int argc, char* argv[]) { Pl_SHA2 sha2; char million_a[1000001]; memset(million_a, 'a', 1000000); million_a[1000000] = '\0'; - test(sha2, "256 short", 256, - "abc", - "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); - test(sha2, "256 long", 256, - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); - test(sha2, "256 million", 256, - million_a, - "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"); - test(sha2, "384 short", 384, - "abc", - "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163" - "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"); - test(sha2, "384 long", 384, - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2" - "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"); - test(sha2, "384 million", 384, - million_a, - "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852" - "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"); - test(sha2, "512 short", 512, - "abc", - "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" - "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); - test(sha2, "512 long", 512, - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" - "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); - test(sha2, "512 million", 512, - million_a, - "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" - "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"); + test( + sha2, + "256 short", + 256, + "abc", + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + test( + sha2, + "256 long", + 256, + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); + test( + sha2, + "256 million", + 256, + million_a, + "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"); + test( + sha2, + "384 short", + 384, + "abc", + "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163" + "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"); + test( + sha2, + "384 long", + 384, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2" + "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"); + test( + sha2, + "384 million", + 384, + million_a, + "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852" + "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"); + test( + sha2, + "512 short", + 512, + "abc", + "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); + test( + sha2, + "512 long", + 512, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); + test( + sha2, + "512 million", + 512, + million_a, + "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" + "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"); return 0; } diff --git a/libtests/sparse_array.cc b/libtests/sparse_array.cc index 5636abd1..8d37bc2f 100644 --- a/libtests/sparse_array.cc +++ b/libtests/sparse_array.cc @@ -7,7 +7,8 @@ #endif #include <cassert> -int main() +int +main() { SparseOHArray a; assert(a.size() == 0); |