Skip to content

Commit a0611a6

Browse files
committed
Implement TestMain
1 parent a8a0994 commit a0611a6

File tree

9 files changed

+97
-20
lines changed

9 files changed

+97
-20
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(fuzz VERSION 0.1)
33

4-
add_executable(fuzz src/test.cpp src/test.h src/main.cpp)
5-
set_target_properties(fuzz PROPERTIES CXX_STANDARD 17)
4+
add_library(fuzz src/test.cpp src/test.h src/test_main.h src/test_main.cpp)
5+
add_library(fuzz::fuzz ALIAS fuzz)
6+
target_include_directories(fuzz PUBLIC src/)
7+
8+
target_compile_features(fuzz PUBLIC cxx_std_17)
9+
10+
add_subdirectory(examples)

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(fib_generator fib_generator.cpp)
2+
target_link_libraries(fib_generator PRIVATE fuzz::fuzz)

examples/fib.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int fib(int n) {
2+
if (n <= 1) return 1;
3+
return fib(n-1) + fib(n-2);
4+
}

examples/fib_generator.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "test_main.h"
2+
3+
int main(int argc, char **argv) {
4+
auto n = primitiveType(PrimitiveType::INT, 0, 10);
5+
auto tInt = primitiveType(PrimitiveType::INT);
6+
TestSignature fib{ "fib", { n }, tInt };
7+
8+
TestMain(argc, argv)
9+
.fuzz(fib, 5)
10+
.run();
11+
return 0;
12+
}

src/main.cpp

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void Test::printPreludeGenerator(std::ostream &ss) const {
6464
printFunctionCall(ss);
6565
ss << ";\n";
6666

67-
ss << "std::cout << \"#define " << macro << " \" << " << var << " << \"\\n\"";
67+
ss << "std::cout << \"#define " << macro << " \" << " << var << " << \"\\n\"\n";
6868
}
6969

7070
void Test::printFunctionCall(std::ostream &ss) const {

src/test.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ struct Value {
6060

6161
template <class Generator>
6262
static Value generate(Generator& gen, const Type::ptr& type) {
63-
std::uniform_int_distribution<PrimitiveInteger> distribution;
63+
auto [a, b] = type->getRange();
64+
std::uniform_int_distribution<PrimitiveInteger> distribution{a, b};
6465
return Value { type, distribution(gen) };
6566
}
6667
};
@@ -80,7 +81,7 @@ struct Test
8081
void printFunctionCall(std::ostream &) const;
8182

8283
template <class Generator>
83-
static Test generate(Generator gen, std::string name, TestSignature signature) {
84+
static Test generate(Generator &gen, std::string name, TestSignature signature) {
8485
Test test;
8586
test.name = std::move(name);
8687
test.signature = std::move(signature);

src/test_main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "test_main.h"
2+
3+
#include <iostream>
4+
#include <cstdlib>
5+
6+
TestMain::TestMain(int argc, char **argv) : gen { std::random_device{}() } {
7+
for (int i = 0; i < argc; ++i) {
8+
args.emplace_back(argv[i]);
9+
}
10+
}
11+
12+
TestMain &TestMain::add(Test test) {
13+
tests.push_back(std::move(test));
14+
return *this;
15+
}
16+
17+
void TestMain::usage() {
18+
std::cerr << "Usage: " << args[0] << " <-g|-t>";
19+
std::exit(1);
20+
}
21+
22+
void TestMain::run() {
23+
if (args.size() != 2) {
24+
usage();
25+
}
26+
27+
if (args[1] == "-g") {
28+
for (const auto& test : tests) {
29+
test.printPreludeGenerator(std::cout);
30+
}
31+
} else if (args[1] == "-t") {
32+
for (const auto& test : tests) {
33+
test.print(std::cout);
34+
}
35+
} else {
36+
usage();
37+
}
38+
}
39+
40+
TestMain &TestMain::fuzz(const TestSignature& testSignature, int n) {
41+
for (int i = 0; i < n; ++i) {
42+
add(Test::generate(gen, "test_" + std::to_string(i), testSignature));
43+
}
44+
45+
return *this;
46+
}

src/test_main.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "test.h"
4+
#include <vector>
5+
#include <string>
6+
7+
class TestMain {
8+
private:
9+
std::vector<std::string> args;
10+
std::vector<Test> tests;
11+
std::mt19937 gen;
12+
13+
private:
14+
void usage();
15+
16+
public:
17+
TestMain(int argc, char **argv);
18+
19+
TestMain &add(Test test);
20+
TestMain &fuzz(const TestSignature& testSignature, int n);
21+
void run();
22+
};

0 commit comments

Comments
 (0)