Skip to content

Commit

Permalink
Avoid calling rand() in main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
christianparpart committed Oct 15, 2021
1 parent 61f57b8 commit a641c3e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
39 changes: 33 additions & 6 deletions libtermbench/termbench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,27 @@ class ManyLines: public Test
public:
ManyLines() noexcept: Test("many_lines", "") {}

void run(Buffer& _sink) noexcept override
void setup(unsigned short, unsigned short) override
{
while (_sink.good())
text.resize(4 * 1024 * 1024);
for (auto i = text.data(), e = i + text.size(); i != e; ++i)
{
char const value = randomAsciiChar();
writeChar(_sink, value);
if (value % 26 == 0)
_sink.write("\n"sv);
if (value % 26 != 0)
*i = value;
else
*i = '\n';
}
}

void run(Buffer& _sink) noexcept override
{
while (_sink.good())
_sink.write(text);
}

private:
std::string text;
};

class LongLines: public Test
Expand Down Expand Up @@ -318,18 +329,34 @@ class Binary: public Test
public:
Binary() noexcept: Test("binary", "") {}

void setup(unsigned short, unsigned short) override
{
text.resize(4 * 1024 * 1024);
for (auto i = text.data(), e = i + text.size(); i != e; ++i)
{
char const value = randomAsciiChar();
if (value % 26 != 0)
*i = value;
else
*i = '\n';
}
}

void run(Buffer& _sink) noexcept override
{
while (_sink.good())
{
writeChar(_sink, static_cast<char>(rand() % 256));
_sink.write(text);
}
}

void teardown(Buffer& _sink) noexcept override
{
_sink.write("\033c");
}

private:
std::string text;
};

}
Expand Down
4 changes: 2 additions & 2 deletions libtermbench/termbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ struct Test
description{_description}
{}

virtual void setup(unsigned short, unsigned short) noexcept {}
virtual void setup(unsigned short, unsigned short) {}
virtual void run(Buffer&) noexcept = 0;
virtual void teardown(Buffer&) noexcept {}
virtual void teardown(Buffer&) {}
};

struct Result
Expand Down

0 comments on commit a641c3e

Please sign in to comment.