| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 |
#include "datagen.h" |
| 10 |
#include "Options.h" |
| 11 |
#include "test/RoundTrip.h" |
| 12 |
#include "utils/ScopeGuard.h" |
| 13 |
|
| 14 |
#include <cstddef> |
| 15 |
#include <cstdio> |
| 16 |
#include <cstdlib> |
| 17 |
#include <memory> |
| 18 |
#include <random> |
| 19 |
|
| 20 |
using namespace std; |
| 21 |
using namespace pzstd; |
| 22 |
|
| 23 |
namespace { |
| 24 |
string |
| 25 |
writeData(size_t size, double matchProba, double litProba, unsigned seed) { |
| 26 |
std::unique_ptr<uint8_t[]> buf(new uint8_t[size]); |
| 27 |
RDG_genBuffer(buf.get(), size, matchProba, litProba, seed); |
| 28 |
string file = tmpnam(nullptr); |
| 29 |
auto fd = std::fopen(file.c_str(), "wb"); |
| 30 |
auto guard = makeScopeGuard([&] { std::fclose(fd); }); |
| 31 |
auto bytesWritten = std::fwrite(buf.get(), 1, size, fd); |
| 32 |
if (bytesWritten != size) { |
| 33 |
std::abort(); |
| 34 |
} |
| 35 |
return file; |
| 36 |
} |
| 37 |
|
| 38 |
template <typename Generator> |
| 39 |
string generateInputFile(Generator& gen) { |
| 40 |
// Use inputs ranging from 1 Byte to 2^16 Bytes |
| 41 |
std::uniform_int_distribution<size_t> size{1, 1 << 16}; |
| 42 |
std::uniform_real_distribution<> prob{0, 1}; |
| 43 |
return writeData(size(gen), prob(gen), prob(gen), gen()); |
| 44 |
} |
| 45 |
|
| 46 |
template <typename Generator> |
| 47 |
Options generateOptions(Generator& gen, const string& inputFile) { |
| 48 |
Options options; |
| 49 |
options.inputFiles = {inputFile}; |
| 50 |
options.overwrite = true; |
| 51 |
|
| 52 |
std::uniform_int_distribution<unsigned> numThreads{1, 32}; |
| 53 |
std::uniform_int_distribution<unsigned> compressionLevel{1, 10}; |
| 54 |
|
| 55 |
options.numThreads = numThreads(gen); |
| 56 |
options.compressionLevel = compressionLevel(gen); |
| 57 |
|
| 58 |
return options; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
int main() { |
| 63 |
std::mt19937 gen(std::random_device{}()); |
| 64 |
|
| 65 |
auto newlineGuard = makeScopeGuard([] { std::fprintf(stderr, "\n"); }); |
| 66 |
for (unsigned i = 0; i < 10000; ++i) { |
| 67 |
if (i % 100 == 0) { |
| 68 |
std::fprintf(stderr, "Progress: %u%%\r", i / 100); |
| 69 |
} |
| 70 |
auto inputFile = generateInputFile(gen); |
| 71 |
auto inputGuard = makeScopeGuard([&] { std::remove(inputFile.c_str()); }); |
| 72 |
for (unsigned i = 0; i < 10; ++i) { |
| 73 |
auto options = generateOptions(gen, inputFile); |
| 74 |
if (!roundTrip(options)) { |
| 75 |
std::fprintf(stderr, "numThreads: %u\n", options.numThreads); |
| 76 |
std::fprintf(stderr, "level: %u\n", options.compressionLevel); |
| 77 |
std::fprintf(stderr, "decompress? %u\n", (unsigned)options.decompress); |
| 78 |
std::fprintf(stderr, "file: %s\n", inputFile.c_str()); |
| 79 |
return 1; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
return 0; |
| 84 |
} |