Skip to content

Commit 652f07d

Browse files
committed
Generate correct answers using #defines
1 parent 74e61ef commit 652f07d

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ int main() {
66
TestSignature max{ "std::max", { tInt, tInt }, tInt };
77

88
std::mt19937 gen { std::random_device{}() };
9-
std::cout << Test::generate(gen, "ExampleTest", max).print() << std::endl;
9+
auto test = Test::generate(gen, "ExampleTest", max);
10+
test.print(std::cout);
11+
std::cout << std::endl << std::endl;
12+
test.printPreludeGenerator(std::cout);
13+
std::cout << std::endl << std::endl;
1014
return 0;
1115
}

src/test.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,30 @@ std::string printPrimitiveType(PrimitiveType type) {
2323
return "<unknown>";
2424
}
2525

26-
std::string Test::print() const {
27-
std::stringstream ss;
26+
void Test::print(std::ostream &ss) const {
2827
ss << "TEST(" << signature.name << "Test, " << name << ") {\n";
29-
ss << " " << signature.name << "(";
28+
ss << " ASSERT_EQ(";
29+
printFunctionCall(ss);
30+
ss << ", " << resultMacroName() << ");\n";
31+
ss << "}\n";
32+
}
33+
34+
std::string Test::resultMacroName() const {
35+
return "FUZZ_TEST_" + name;
36+
}
37+
38+
void Test::printPreludeGenerator(std::ostream &ss) const {
39+
auto macro = resultMacroName();
40+
auto var = macro + "_result";
41+
ss << "auto " << var << " = ";
42+
printFunctionCall(ss);
43+
ss << ";\n";
44+
45+
ss << "std::cout << \"#define " << macro << " \" << " << var << " << \"\\n\"";
46+
}
47+
48+
void Test::printFunctionCall(std::ostream &ss) const {
49+
ss << signature.name << "(";
3050

3151
bool first = true;
3252

@@ -39,7 +59,6 @@ std::string Test::print() const {
3959

4060
first = false;
4161
}
42-
ss << ");\n";
43-
ss << "}\n";
44-
return ss.str();
62+
63+
ss << ")";
4564
}

src/test.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ struct Test
5454
// after test launch
5555
std::optional<Value> returnValue;
5656

57-
[[nodiscard]] std::string print() const;
57+
[[nodiscard]] std::string resultMacroName() const;
58+
void print(std::ostream &) const;
59+
void printPreludeGenerator(std::ostream &) const;
60+
void printFunctionCall(std::ostream &) const;
5861

5962
template <class Generator>
6063
static Test generate(Generator gen, std::string name, TestSignature signature) {

0 commit comments

Comments
 (0)