Skip to content

Commit 6e1c9f6

Browse files
committed
Basic unit test generation
1 parent f0764e5 commit 6e1c9f6

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
.idea
35+
cmake-build-*

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(fuzz VERSION 0.1)
3+
4+
add_executable(fuzz src/test.cpp src/test.h)
5+
set_target_properties(fuzz PROPERTIES CXX_STANDARD 17)

src/test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "test.h"
2+
3+
#include <sstream>
4+
5+
std::string Value::print() const {
6+
return "static_cast<" + type.print() + ">(" + std::to_string(value) + ")";
7+
}
8+
9+
std::string Type::print() const {
10+
switch (kind) {
11+
case Kind::PRIMITIVE_TYPE: return printPrimitiveType(primitiveType);
12+
case Kind::POINTER: return pointerTo->print() + "*";
13+
}
14+
}
15+
16+
std::string printPrimitiveType(PrimitiveType type) {
17+
switch (type) {
18+
case PrimitiveType::CHAR: return "char";
19+
case PrimitiveType::INT: return "int";
20+
}
21+
}
22+
23+
std::string Test::print() const {
24+
std::stringstream ss;
25+
ss << "TEST(" << signature.name << "Test, " << name << ") {\n";
26+
ss << " EXPECT_EQ(" << signature.name << "(";
27+
28+
bool first = true;
29+
30+
for (const auto &arg : arguments) {
31+
if (!first) {
32+
ss << ", ";
33+
}
34+
35+
ss << arg.print();
36+
37+
first = false;
38+
}
39+
ss << "), " << returnValue.print() << ");\n";
40+
ss << "}\n";
41+
return ss.str();
42+
}

src/test.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <string>
5+
#include <any>
6+
#include <optional>
7+
#include <memory>
8+
9+
enum class Kind {
10+
PRIMITIVE_TYPE,
11+
POINTER
12+
};
13+
14+
enum class PrimitiveType {
15+
CHAR,
16+
INT
17+
};
18+
19+
std::string printPrimitiveType(PrimitiveType type);
20+
21+
struct Type {
22+
union {
23+
PrimitiveType primitiveType;
24+
std::unique_ptr<Type> pointerTo;
25+
};
26+
27+
Kind kind;
28+
29+
[[nodiscard]] std::string print() const;
30+
};
31+
32+
std::any fillValue(Type x);
33+
34+
struct TestSignature {
35+
std::string name;
36+
std::vector<Type> parameterTypes;
37+
Type returnType;
38+
};
39+
40+
struct Value {
41+
Type type;
42+
int value;
43+
[[nodiscard]] std::string print() const;
44+
};
45+
46+
struct Test
47+
{
48+
std::string name;
49+
TestSignature signature;
50+
std::vector<Value> arguments;
51+
52+
// after test launch
53+
Value returnValue;
54+
55+
[[nodiscard]] std::string print() const;
56+
};

0 commit comments

Comments
 (0)