Skip to content

Commit

Permalink
Merge 2bd23f8 into 19f7d5c
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWF committed Nov 29, 2018
2 parents 19f7d5c + 2bd23f8 commit 39a7046
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion test/output_test_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include <map>
#include <memory>
#include <random>
#include <sstream>
#include <streambuf>

Expand Down Expand Up @@ -438,11 +439,50 @@ int SubstrCnt(const std::string& haystack, const std::string& pat) {
return count;
}

static char ToHex(int ch) {
return ch < 10 ? static_cast<char>('0' + ch)
: static_cast<char>('a' + (ch - 10));
}

static char RandomHexChar() {
static std::mt19937 rd{std::random_device{}()};
static std::uniform_int_distribution<int> mrand{0, 15};
return ToHex(mrand(rd));
}

static std::string GetRandomFileName() {
std::string model = "test.%%%%%%";
for (auto & ch : model) {
if (ch == '%')
ch = RandomHexChar();
}
return model;
}

static bool FileExists(std::string const& name) {
std::ifstream in(name.c_str());
return in.good();
}

static std::string GetTempFileName() {
// This function attempts to avoid race conditions where two tests
// create the same file at the same time. However, it still introduces races
// similar to tmpnam.
int retries = 3;
while (--retries) {
std::string name = GetRandomFileName();
if (!FileExists(name))
return name;
}
std::cerr << "Failed to create unique temporary file name" << std::endl;
std::abort();
}

std::string GetFileReporterOutput(int argc, char* argv[]) {
std::vector<char*> new_argv(argv, argv + argc);
assert(static_cast<decltype(new_argv)::size_type>(argc) == new_argv.size());

std::string tmp_file_name = std::tmpnam(nullptr);
std::string tmp_file_name = GetTempFileName();
std::cout << "Will be using this as the tmp file: " << tmp_file_name << '\n';

std::string tmp = "--benchmark_out=";
Expand Down

0 comments on commit 39a7046

Please sign in to comment.