Skip to content

Commit a8a0994

Browse files
committed
Support for values in range
1 parent cc004ba commit a8a0994

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <iostream>
33

44
int main() {
5-
Type tInt { PrimitiveType::INT };
5+
auto tInt = primitiveType(PrimitiveType::INT);
66
TestSignature max{ "std::max", { tInt, tInt }, tInt };
77

88
std::mt19937 gen { std::random_device{}() };

src/test.cpp

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
#include <sstream>
44

55
std::string Value::print() const {
6-
return type.printValue(std::to_string(value));
6+
return type->printValue(std::to_string(value));
77
}
88

99
std::string Type::print() const {
1010
if (auto primitiveType = std::get_if<PrimitiveType>(&type)) {
1111
return printPrimitiveType(*primitiveType);
1212
} else if (auto pointerTo = std::get_if<PointerTo>(&type)) {
1313
return pointerTo->type->print() + "*";
14+
} else if (auto inRange = std::get_if<InRange>(&type)) {
15+
return inRange->type->print();
1416
}
1517

1618
return "<unknown>";
@@ -21,24 +23,33 @@ std::string Type::printValue(const std::string& value) const {
2123
return "static_cast<" + print() + ">(" + value + ")";
2224
} else if (auto pointerTo = std::get_if<PointerTo>(&type)) {
2325
return "new " + pointerTo->type->print() + "{" + pointerTo->type->printValue(value) + "}";
26+
} else if (auto inRange = std::get_if<InRange>(&type)) {
27+
return inRange->type->printValue(value);
2428
}
2529

2630
return "<unknown>";
2731
}
2832

29-
std::string printPrimitiveType(PrimitiveType type) {
30-
switch (type) {
31-
case PrimitiveType::CHAR: return "char";
32-
case PrimitiveType::INT: return "int";
33+
std::pair<PrimitiveInteger, PrimitiveInteger> Type::getRange() const {
34+
if (auto primitiveType = std::get_if<PrimitiveType>(&type)) {
35+
int bitness = primitiveTypeSize(*primitiveType);
36+
return { -(1ll << bitness), (1ll << bitness) - 1 };
37+
} else if (auto pointerTo = std::get_if<PointerTo>(&type)) {
38+
return pointerTo->type->getRange();
39+
} else if (auto inRange = std::get_if<InRange>(&type)) {
40+
auto enclosingRange = inRange->type->getRange();
41+
return { std::max(enclosingRange.first, inRange->min),
42+
std::min(enclosingRange.second, inRange->max) };
3343
}
34-
return "<unknown>";
44+
45+
return { 0, 0 };
3546
}
3647

3748
void Test::print(std::ostream &ss) const {
3849
ss << "TEST(" << signature.name << "Test, " << name << ") {\n";
3950
ss << " ASSERT_EQ(";
4051
printFunctionCall(ss);
41-
ss << ", " << signature.returnType.printValue(resultMacroName()) << ");\n";
52+
ss << ", " << signature.returnType->printValue(resultMacroName()) << ");\n";
4253
ss << "}\n";
4354
}
4455

@@ -73,3 +84,31 @@ void Test::printFunctionCall(std::ostream &ss) const {
7384

7485
ss << ")";
7586
}
87+
88+
std::string printPrimitiveType(PrimitiveType type) {
89+
switch (type) {
90+
case PrimitiveType::CHAR: return "char";
91+
case PrimitiveType::INT: return "int";
92+
}
93+
return "<unknown>";
94+
}
95+
96+
int primitiveTypeSize(PrimitiveType type) {
97+
switch (type) {
98+
case PrimitiveType::CHAR: return 8;
99+
case PrimitiveType::INT: return 32;
100+
}
101+
return 0;
102+
}
103+
104+
Type::ptr primitiveType(PrimitiveType type) {
105+
return std::make_shared<Type>(Type { type });
106+
}
107+
108+
Type::ptr primitiveType(PrimitiveType type, PrimitiveInteger min, PrimitiveInteger max) {
109+
return std::make_shared<Type>( Type { InRange { primitiveType(type), min, max } } );
110+
}
111+
112+
Type::ptr pointerTo(Type::ptr type) {
113+
return std::make_shared<Type>(Type { PointerTo { std::move(type) } } );
114+
}

src/test.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,58 @@
99

1010
struct Type;
1111

12+
using PrimitiveInteger = long;
13+
1214
enum class PrimitiveType {
1315
CHAR,
1416
INT
1517
};
1618

17-
std::string printPrimitiveType(PrimitiveType type);
19+
[[nodiscard]] std::string printPrimitiveType(PrimitiveType type);
20+
[[nodiscard]] int primitiveTypeSize(PrimitiveType type);
1821

1922
struct PointerTo {
2023
std::shared_ptr<Type> type;
2124
};
2225

26+
struct InRange {
27+
std::shared_ptr<Type> type;
28+
PrimitiveInteger min, max;
29+
};
30+
2331
struct Type {
24-
std::variant<PrimitiveType, PointerTo> type;
32+
std::variant<
33+
PrimitiveType,
34+
PointerTo,
35+
InRange
36+
> type;
37+
38+
using ptr = std::shared_ptr<Type>;
2539

2640
[[nodiscard]] std::string print() const;
2741
[[nodiscard]] std::string printValue(const std::string &value) const;
42+
[[nodiscard]] std::pair<PrimitiveInteger, PrimitiveInteger> getRange() const;
2843
};
2944

45+
[[nodiscard]] Type::ptr primitiveType(PrimitiveType type);
46+
[[nodiscard]] Type::ptr primitiveType(PrimitiveType type, PrimitiveInteger min, PrimitiveInteger max);
47+
[[nodiscard]] Type::ptr pointerTo(Type::ptr type);
48+
3049
struct TestSignature {
3150
std::string name;
32-
std::vector<Type> parameterTypes;
33-
Type returnType;
51+
std::vector<Type::ptr> parameterTypes;
52+
Type::ptr returnType;
3453
};
3554

3655
struct Value {
37-
Type type;
38-
int value;
56+
Type::ptr type;
57+
PrimitiveInteger value;
3958

4059
[[nodiscard]] std::string print() const;
4160

4261
template <class Generator>
43-
static Value generate(Generator& gen, const Type& type) {
44-
std::uniform_int_distribution<int> distribution;
62+
static Value generate(Generator& gen, const Type::ptr& type) {
63+
std::uniform_int_distribution<PrimitiveInteger> distribution;
4564
return Value { type, distribution(gen) };
4665
}
4766
};

0 commit comments

Comments
 (0)