123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- /*
- * Copyright (c) 2016-present, Facebook, Inc.
- * All rights reserved.
- *
- * This source code is licensed under both the BSD-style license (found in the
- * LICENSE file in the root directory of this source tree) and the GPLv2 (found
- * in the COPYING file in the root directory of this source tree).
- */
- #include "Options.h"
- #include <array>
- #include <gtest/gtest.h>
- using namespace pzstd;
- namespace pzstd {
- bool operator==(const Options &lhs, const Options &rhs) {
- return lhs.numThreads == rhs.numThreads &&
- lhs.maxWindowLog == rhs.maxWindowLog &&
- lhs.compressionLevel == rhs.compressionLevel &&
- lhs.decompress == rhs.decompress && lhs.inputFiles == rhs.inputFiles &&
- lhs.outputFile == rhs.outputFile && lhs.overwrite == rhs.overwrite &&
- lhs.keepSource == rhs.keepSource && lhs.writeMode == rhs.writeMode &&
- lhs.checksum == rhs.checksum && lhs.verbosity == rhs.verbosity;
- }
- std::ostream &operator<<(std::ostream &out, const Options &opt) {
- out << "{";
- {
- out << "\n\t"
- << "numThreads: " << opt.numThreads;
- out << ",\n\t"
- << "maxWindowLog: " << opt.maxWindowLog;
- out << ",\n\t"
- << "compressionLevel: " << opt.compressionLevel;
- out << ",\n\t"
- << "decompress: " << opt.decompress;
- out << ",\n\t"
- << "inputFiles: {";
- {
- bool first = true;
- for (const auto &file : opt.inputFiles) {
- if (!first) {
- out << ",";
- }
- first = false;
- out << "\n\t\t" << file;
- }
- }
- out << "\n\t}";
- out << ",\n\t"
- << "outputFile: " << opt.outputFile;
- out << ",\n\t"
- << "overwrite: " << opt.overwrite;
- out << ",\n\t"
- << "keepSource: " << opt.keepSource;
- out << ",\n\t"
- << "writeMode: " << static_cast<int>(opt.writeMode);
- out << ",\n\t"
- << "checksum: " << opt.checksum;
- out << ",\n\t"
- << "verbosity: " << opt.verbosity;
- }
- out << "\n}";
- return out;
- }
- }
- namespace {
- #ifdef _WIN32
- const char nullOutput[] = "nul";
- #else
- const char nullOutput[] = "/dev/null";
- #endif
- constexpr auto autoMode = Options::WriteMode::Auto;
- } // anonymous namespace
- #define EXPECT_SUCCESS(...) EXPECT_EQ(Options::Status::Success, __VA_ARGS__)
- #define EXPECT_FAILURE(...) EXPECT_EQ(Options::Status::Failure, __VA_ARGS__)
- #define EXPECT_MESSAGE(...) EXPECT_EQ(Options::Status::Message, __VA_ARGS__)
- template <typename... Args>
- std::array<const char *, sizeof...(Args) + 1> makeArray(Args... args) {
- return {{nullptr, args...}};
- }
- TEST(Options, ValidInputs) {
- {
- Options options;
- auto args = makeArray("--processes", "5", "-o", "x", "y", "-f");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {5, 23, 3, false, {"y"}, "x",
- true, true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("-p", "1", "input", "-19");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {1, 23, 19, false, {"input"}, "",
- false, true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args =
- makeArray("--ultra", "-22", "-p", "1", "-o", "x", "-d", "x.zst", "-f");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {1, 0, 22, true, {"x.zst"}, "x",
- true, true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("--processes", "100", "hello.zst", "--decompress",
- "--force");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {100, 23, 3, true, {"hello.zst"}, "", true,
- true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("x", "-dp", "1", "-c");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {1, 23, 3, true, {"x"}, "-",
- false, true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("x", "-dp", "1", "--stdout");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {1, 23, 3, true, {"x"}, "-",
- false, true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("-p", "1", "x", "-5", "-fo", "-", "--ultra", "-d");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {1, 0, 5, true, {"x"}, "-",
- true, true, autoMode, true, 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("silesia.tar", "-o", "silesia.tar.pzstd", "-p", "2");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {2,
- 23,
- 3,
- false,
- {"silesia.tar"},
- "silesia.tar.pzstd",
- false,
- true,
- autoMode,
- true,
- 2};
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("x", "-p", "1");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "-p", "1");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, GetOutputFile) {
- {
- Options options;
- auto args = makeArray("x");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ("x.zst", options.getOutputFile(options.inputFiles[0]));
- }
- {
- Options options;
- auto args = makeArray("x", "y", "-o", nullOutput);
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(nullOutput, options.getOutputFile(options.inputFiles[0]));
- }
- {
- Options options;
- auto args = makeArray("x.zst", "-do", nullOutput);
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(nullOutput, options.getOutputFile(options.inputFiles[0]));
- }
- {
- Options options;
- auto args = makeArray("x.zst", "-d");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ("x", options.getOutputFile(options.inputFiles[0]));
- }
- {
- Options options;
- auto args = makeArray("xzst", "-d");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ("", options.getOutputFile(options.inputFiles[0]));
- }
- {
- Options options;
- auto args = makeArray("xzst", "-doxx");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ("xx", options.getOutputFile(options.inputFiles[0]));
- }
- }
- TEST(Options, MultipleFiles) {
- {
- Options options;
- auto args = makeArray("x", "y", "z");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected;
- expected.inputFiles = {"x", "y", "z"};
- expected.verbosity = 1;
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("x", "y", "z", "-o", nullOutput);
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected;
- expected.inputFiles = {"x", "y", "z"};
- expected.outputFile = nullOutput;
- expected.verbosity = 1;
- EXPECT_EQ(expected, options);
- }
- {
- Options options;
- auto args = makeArray("x", "y", "-o-");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "y", "-o", "file");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("-qqvd12qp4", "-f", "x", "--", "--rm", "-c");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- Options expected = {4, 23, 12, true, {"x", "--rm", "-c"},
- "", true, true, autoMode, true,
- 0};
- EXPECT_EQ(expected, options);
- }
- }
- TEST(Options, NumThreads) {
- {
- Options options;
- auto args = makeArray("x", "-dfo", "-");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "-p", "0", "-fo", "-");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("-f", "-p", "-o", "-");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, BadCompressionLevel) {
- {
- Options options;
- auto args = makeArray("x", "-20");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "--ultra", "-23");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "--1"); // negative 1?
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, InvalidOption) {
- {
- Options options;
- auto args = makeArray("x", "-x");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, BadOutputFile) {
- {
- Options options;
- auto args = makeArray("notzst", "-d", "-p", "1");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ("", options.getOutputFile(options.inputFiles.front()));
- }
- }
- TEST(Options, BadOptionsWithArguments) {
- {
- Options options;
- auto args = makeArray("x", "-pf");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "-p", "10f");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "-p");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "-o");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("x", "-o");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, KeepSource) {
- {
- Options options;
- auto args = makeArray("x", "--rm", "-k");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.keepSource);
- }
- {
- Options options;
- auto args = makeArray("x", "--rm", "--keep");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.keepSource);
- }
- {
- Options options;
- auto args = makeArray("x");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.keepSource);
- }
- {
- Options options;
- auto args = makeArray("x", "--rm");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(false, options.keepSource);
- }
- }
- TEST(Options, Verbosity) {
- {
- Options options;
- auto args = makeArray("x");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(2, options.verbosity);
- }
- {
- Options options;
- auto args = makeArray("--quiet", "-qq", "x");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(-1, options.verbosity);
- }
- {
- Options options;
- auto args = makeArray("x", "y");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(1, options.verbosity);
- }
- {
- Options options;
- auto args = makeArray("--", "x", "y");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(1, options.verbosity);
- }
- {
- Options options;
- auto args = makeArray("-qv", "x", "y");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(1, options.verbosity);
- }
- {
- Options options;
- auto args = makeArray("-v", "x", "y");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(3, options.verbosity);
- }
- {
- Options options;
- auto args = makeArray("-v", "x");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(3, options.verbosity);
- }
- }
- TEST(Options, TestMode) {
- {
- Options options;
- auto args = makeArray("x", "-t");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.keepSource);
- EXPECT_EQ(true, options.decompress);
- EXPECT_EQ(nullOutput, options.outputFile);
- }
- {
- Options options;
- auto args = makeArray("x", "--test", "--rm", "-ohello");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.keepSource);
- EXPECT_EQ(true, options.decompress);
- EXPECT_EQ(nullOutput, options.outputFile);
- }
- }
- TEST(Options, Checksum) {
- {
- Options options;
- auto args = makeArray("x.zst", "--no-check", "-Cd");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.checksum);
- }
- {
- Options options;
- auto args = makeArray("x");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.checksum);
- }
- {
- Options options;
- auto args = makeArray("x", "--no-check", "--check");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(true, options.checksum);
- }
- {
- Options options;
- auto args = makeArray("x", "--no-check");
- EXPECT_SUCCESS(options.parse(args.size(), args.data()));
- EXPECT_EQ(false, options.checksum);
- }
- }
- TEST(Options, InputFiles) {
- {
- Options options;
- auto args = makeArray("-cd");
- options.parse(args.size(), args.data());
- EXPECT_EQ(1, options.inputFiles.size());
- EXPECT_EQ("-", options.inputFiles[0]);
- EXPECT_EQ("-", options.outputFile);
- }
- {
- Options options;
- auto args = makeArray();
- options.parse(args.size(), args.data());
- EXPECT_EQ(1, options.inputFiles.size());
- EXPECT_EQ("-", options.inputFiles[0]);
- EXPECT_EQ("-", options.outputFile);
- }
- {
- Options options;
- auto args = makeArray("-d");
- options.parse(args.size(), args.data());
- EXPECT_EQ(1, options.inputFiles.size());
- EXPECT_EQ("-", options.inputFiles[0]);
- EXPECT_EQ("-", options.outputFile);
- }
- {
- Options options;
- auto args = makeArray("x", "-");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, InvalidOptions) {
- {
- Options options;
- auto args = makeArray("-ibasdf");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("- ");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("-n15");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("-0", "x");
- EXPECT_FAILURE(options.parse(args.size(), args.data()));
- }
- }
- TEST(Options, Extras) {
- {
- Options options;
- auto args = makeArray("-h");
- EXPECT_MESSAGE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("-H");
- EXPECT_MESSAGE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("-V");
- EXPECT_MESSAGE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("--help");
- EXPECT_MESSAGE(options.parse(args.size(), args.data()));
- }
- {
- Options options;
- auto args = makeArray("--version");
- EXPECT_MESSAGE(options.parse(args.size(), args.data()));
- }
- }
|