Skip to content

Commit 74e61ef

Browse files
committed
Refactor some stuff
1 parent 6e1c9f6 commit 74e61ef

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(fuzz VERSION 0.1)
33

4-
add_executable(fuzz src/test.cpp src/test.h)
4+
add_executable(fuzz src/test.cpp src/test.h src/main.cpp)
55
set_target_properties(fuzz PROPERTIES CXX_STANDARD 17)

src/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "test.h"
2+
#include <iostream>
3+
4+
int main() {
5+
Type tInt { PrimitiveType::INT };
6+
TestSignature max{ "std::max", { tInt, tInt }, tInt };
7+
8+
std::mt19937 gen { std::random_device{}() };
9+
std::cout << Test::generate(gen, "ExampleTest", max).print() << std::endl;
10+
return 0;
11+
}

src/test.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55
std::string Value::print() const {
66
return "static_cast<" + type.print() + ">(" + std::to_string(value) + ")";
77
}
8-
98
std::string Type::print() const {
10-
switch (kind) {
11-
case Kind::PRIMITIVE_TYPE: return printPrimitiveType(primitiveType);
12-
case Kind::POINTER: return pointerTo->print() + "*";
9+
if (auto primitiveType = std::get_if<PrimitiveType>(&type)) {
10+
return printPrimitiveType(*primitiveType);
11+
} else if (auto pointerTo = std::get_if<PointerTo>(&type)) {
12+
return pointerTo->type->print() + "*";
1313
}
14+
15+
return "<unknown>";
1416
}
1517

1618
std::string printPrimitiveType(PrimitiveType type) {
1719
switch (type) {
1820
case PrimitiveType::CHAR: return "char";
1921
case PrimitiveType::INT: return "int";
2022
}
23+
return "<unknown>";
2124
}
2225

2326
std::string Test::print() const {
2427
std::stringstream ss;
2528
ss << "TEST(" << signature.name << "Test, " << name << ") {\n";
26-
ss << " EXPECT_EQ(" << signature.name << "(";
29+
ss << " " << signature.name << "(";
2730

2831
bool first = true;
2932

@@ -36,7 +39,7 @@ std::string Test::print() const {
3639

3740
first = false;
3841
}
39-
ss << "), " << returnValue.print() << ");\n";
42+
ss << ");\n";
4043
ss << "}\n";
4144
return ss.str();
4245
}

src/test.h

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
#include <vector>
44
#include <string>
5-
#include <any>
5+
#include <variant>
66
#include <optional>
77
#include <memory>
8+
#include <random>
89

9-
enum class Kind {
10-
PRIMITIVE_TYPE,
11-
POINTER
12-
};
10+
struct Type;
1311

1412
enum class PrimitiveType {
1513
CHAR,
@@ -18,19 +16,16 @@ enum class PrimitiveType {
1816

1917
std::string printPrimitiveType(PrimitiveType type);
2018

21-
struct Type {
22-
union {
23-
PrimitiveType primitiveType;
24-
std::unique_ptr<Type> pointerTo;
25-
};
19+
struct PointerTo {
20+
std::shared_ptr<Type> type;
21+
};
2622

27-
Kind kind;
23+
struct Type {
24+
std::variant<PrimitiveType, PointerTo> type;
2825

2926
[[nodiscard]] std::string print() const;
3027
};
3128

32-
std::any fillValue(Type x);
33-
3429
struct TestSignature {
3530
std::string name;
3631
std::vector<Type> parameterTypes;
@@ -40,7 +35,14 @@ struct TestSignature {
4035
struct Value {
4136
Type type;
4237
int value;
38+
4339
[[nodiscard]] std::string print() const;
40+
41+
template <class Generator>
42+
static Value generate(Generator& gen, const Type& type) {
43+
std::uniform_int_distribution<int> distribution;
44+
return Value { type, distribution(gen) };
45+
}
4446
};
4547

4648
struct Test
@@ -50,7 +52,20 @@ struct Test
5052
std::vector<Value> arguments;
5153

5254
// after test launch
53-
Value returnValue;
55+
std::optional<Value> returnValue;
5456

5557
[[nodiscard]] std::string print() const;
58+
59+
template <class Generator>
60+
static Test generate(Generator gen, std::string name, TestSignature signature) {
61+
Test test;
62+
test.name = std::move(name);
63+
test.signature = std::move(signature);
64+
65+
for (const auto &type : test.signature.parameterTypes) {
66+
test.arguments.push_back(Value::generate(gen, type));
67+
}
68+
69+
return test;
70+
}
5671
};

0 commit comments

Comments
 (0)