RoundTripTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2016-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. */
  9. extern "C" {
  10. #include "datagen.h"
  11. }
  12. #include "Options.h"
  13. #include "test/RoundTrip.h"
  14. #include "utils/ScopeGuard.h"
  15. #include <cstddef>
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include <memory>
  19. #include <random>
  20. using namespace std;
  21. using namespace pzstd;
  22. namespace {
  23. string
  24. writeData(size_t size, double matchProba, double litProba, unsigned seed) {
  25. std::unique_ptr<uint8_t[]> buf(new uint8_t[size]);
  26. RDG_genBuffer(buf.get(), size, matchProba, litProba, seed);
  27. string file = tmpnam(nullptr);
  28. auto fd = std::fopen(file.c_str(), "wb");
  29. auto guard = makeScopeGuard([&] { std::fclose(fd); });
  30. auto bytesWritten = std::fwrite(buf.get(), 1, size, fd);
  31. if (bytesWritten != size) {
  32. std::abort();
  33. }
  34. return file;
  35. }
  36. template <typename Generator>
  37. string generateInputFile(Generator& gen) {
  38. // Use inputs ranging from 1 Byte to 2^16 Bytes
  39. std::uniform_int_distribution<size_t> size{1, 1 << 16};
  40. std::uniform_real_distribution<> prob{0, 1};
  41. return writeData(size(gen), prob(gen), prob(gen), gen());
  42. }
  43. template <typename Generator>
  44. Options generateOptions(Generator& gen, const string& inputFile) {
  45. Options options;
  46. options.inputFiles = {inputFile};
  47. options.overwrite = true;
  48. std::uniform_int_distribution<unsigned> numThreads{1, 32};
  49. std::uniform_int_distribution<unsigned> compressionLevel{1, 10};
  50. options.numThreads = numThreads(gen);
  51. options.compressionLevel = compressionLevel(gen);
  52. return options;
  53. }
  54. }
  55. int main() {
  56. std::mt19937 gen(std::random_device{}());
  57. auto newlineGuard = makeScopeGuard([] { std::fprintf(stderr, "\n"); });
  58. for (unsigned i = 0; i < 10000; ++i) {
  59. if (i % 100 == 0) {
  60. std::fprintf(stderr, "Progress: %u%%\r", i / 100);
  61. }
  62. auto inputFile = generateInputFile(gen);
  63. auto inputGuard = makeScopeGuard([&] { std::remove(inputFile.c_str()); });
  64. for (unsigned i = 0; i < 10; ++i) {
  65. auto options = generateOptions(gen, inputFile);
  66. if (!roundTrip(options)) {
  67. std::fprintf(stderr, "numThreads: %u\n", options.numThreads);
  68. std::fprintf(stderr, "level: %u\n", options.compressionLevel);
  69. std::fprintf(stderr, "decompress? %u\n", (unsigned)options.decompress);
  70. std::fprintf(stderr, "file: %s\n", inputFile.c_str());
  71. return 1;
  72. }
  73. }
  74. }
  75. return 0;
  76. }