Skip to content

Commit

Permalink
Adds support for exporting benchmarks in the old format.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoffitt committed Oct 27, 2023
1 parent d78865e commit 2026acb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
37 changes: 13 additions & 24 deletions src/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,26 @@ bool IncludeGaps(const Problem& problem) {

} // namespace

std::string ToCsv(const Problem& problem, Solution* solution) {
std::string ToCsv(const Problem& problem, Solution* solution, bool old_format) {
const bool include_alignment = IncludeAlignment(problem);
const bool include_gaps = IncludeGaps(problem);
const int addend = old_format ? -1 : 0;
std::vector<std::string> header = {std::string(kId),
std::string(kLower),
std::string(kUpper),
std::string(old_format ? kStart : kLower),
std::string(old_format ? kEnd : kUpper),
std::string(kSize)};
if (include_alignment) {
header.push_back(std::string(kAlignment));
}
if (include_gaps) {
header.push_back(std::string(kGaps));
}
if (solution) {
header.push_back(std::string(kOffset));
}
if (include_alignment) header.push_back(std::string(kAlignment));
if (include_gaps) header.push_back(std::string(kGaps));
if (solution) header.push_back(std::string(kOffset));
std::vector<std::vector<std::string>> input = { header };
for (auto buffer_idx = 0; buffer_idx < problem.buffers.size(); ++buffer_idx) {
const Buffer& buffer = problem.buffers[buffer_idx];
const auto& lifespan = buffer.lifespan;
std::vector<std::string> gaps;
gaps.reserve(buffer.gaps.size());
for (const Gap& gap : buffer.gaps) {
std::string gap_str =
absl::StrCat(gap.lifespan.lower(), "-", gap.lifespan.upper());
std::string gap_str = absl::StrCat(gap.lifespan.lower(), "-",
gap.lifespan.upper() + addend);
if (gap.window) {
gap_str +=
absl::StrCat("@", gap.window->lower(), ":", gap.window->upper());
Expand All @@ -99,17 +94,11 @@ std::string ToCsv(const Problem& problem, Solution* solution) {
}
std::vector<std::string> record = {absl::StrCat(buffer.id),
absl::StrCat(lifespan.lower()),
absl::StrCat(lifespan.upper()),
absl::StrCat(lifespan.upper() + addend),
absl::StrCat(buffer.size)};
if (include_alignment) {
record.push_back(absl::StrCat(buffer.alignment));
}
if (include_gaps) {
record.push_back(absl::StrJoin(gaps, " "));
}
if (solution) {
record.push_back(std::to_string(solution->offsets[buffer_idx]));
}
if (include_alignment) record.push_back(absl::StrCat(buffer.alignment));
if (include_gaps) record.push_back(absl::StrJoin(gaps, " "));
if (solution) record.push_back(absl::StrCat(solution->offsets[buffer_idx]));
input.push_back(record);
}
std::ostringstream oss;
Expand Down
4 changes: 3 additions & 1 deletion src/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ namespace minimalloc {
// 2,10,40,3,2
//
// If a solution is provided, an additional "offset" column will be created.
std::string ToCsv(const Problem& problem, Solution* solution = nullptr);
std::string ToCsv(const Problem& problem,
Solution* solution = nullptr,
bool old_format = false);

// Given a CSV like the one below (with buffers listed in any order), converts
// it into a Problem instance or returns a status if the problem is malformed:
Expand Down
19 changes: 19 additions & 0 deletions tests/converter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ TEST(ConverterTest, ToCsvStringIDs) {
"Little,5,10,15,1,\nBig,6,12,18,2,7-8 9-10\n");
}

TEST(ConverterTest, ToCsvOldFormat) {
EXPECT_EQ(
ToCsv({
.buffers = {
{.id = "Little", .lifespan = {5, 10}, .size = 15},
{
.id = "Big",
.lifespan = {6, 12},
.size = 18,
.alignment = 2,
.gaps = {{.lifespan = {7, 8}}, {.lifespan = {9, 10}}}
},
},
.capacity = 40
}, nullptr, true),
"id,start,end,size,alignment,gaps\n"
"Little,5,9,15,1,\nBig,6,11,18,2,7-7 9-9\n");
}

TEST(ConverterTest, FromCsvProblemOnly) {
EXPECT_EQ(
*FromCsv("lower,size,id,upper\n6,18,1,12\n5,15,0,10\n"),
Expand Down

0 comments on commit 2026acb

Please sign in to comment.