33#include < sstream>
44
55std::string Value::print () const {
6- return type. printValue (std::to_string (value));
6+ return type-> printValue (std::to_string (value));
77}
88
99std::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
3748void 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+ }
0 commit comments