36 CheckResult::CheckResult()
37 : reportError_(false), expected_(
""), actual_(
"")
41 CheckResult::CheckResult(
string message)
42 : reportError_(true), expected_(
""), actual_(message)
46 CheckResult::CheckResult(
string expected,
string actual)
47 : reportError_(true), expected_(expected), actual_(actual)
51 CheckResult::CheckResult(CheckResult&& other)
52 : reportError_(other.reportError_),
53 expected_(other.expected_), actual_(other.actual_),
54 message_(
std::move(other.message_))
56 other.reportError_ =
false;
68 if (not x.error() and not y.error())
79 else if (not y.error())
82 const string exp =
"(" + x.expected() +
" and " + y.expected() +
")";
84 (x.actual() == y.actual())
86 :
"(" + x.actual() +
" or " + y.actual() +
")"
93 CheckResult(exp, actual)
94 << x.message() << y.message()
101 return std::move(x) and std::move(y);
107 if (not x.error() or not y.error())
112 return CheckResult();
115 const string exp =
"(" + x.expected() +
" or " + y.expected() +
")";
116 const string actual =
117 (x.actual() == y.actual())
119 :
"(" + x.actual() +
" or " + y.actual() +
")"
125 return CheckResult(exp, actual);
131 return std::move(x) or std::move(y);
135 CheckResult& CheckResult::operator << (const std::vector<std::string>& v)
139 for (
size_t i = 0; i < v.size(); i++)
141 message_ <<
"'" << v[i] <<
"'";
142 if (i < (v.size() - 1))
156 CheckResult
Check(
bool condition,
string description)
159 return CheckResult();
161 return CheckResult(description);
166 if (expected == actual)
167 return CheckResult();
169 return CheckResult(to_string(expected), to_string(actual));
172 CheckResult
CheckFloat(
double exp,
double actual,
double tolerance)
174 const double error = fabs(actual - exp);
176 if (error < tolerance)
177 return CheckResult();
179 const double relativeTolerance = exp * tolerance;
181 if (error < relativeTolerance)
182 return CheckResult();
184 return CheckResult(to_string(exp), to_string(actual));
187 CheckResult
CheckString(
string expected,
string actual,
size_t maxDistance)
189 if (expected == actual)
190 return CheckResult();
192 const size_t editDistance =
static_cast<size_t>(
193 levenshtein_d(expected.c_str(), expected.length(),
194 actual.c_str(), actual.length()));
196 if (editDistance <= maxDistance)
197 return CheckResult();
199 return CheckResult(expected, actual);
A library for grading C- and C++-based assignments.
CheckResult CheckInt(int expected, int actual)
Check that two integers are equal, failing the test if they are not.
CheckResult Check(bool, std::string description)
Check an arbitrary condition, failing the test if false.
CheckResult operator||(CheckResult &&, CheckResult &&)
Combine the results of two checks using a sum (OR): at least one must pass.
CheckResult CheckString(std::string expected, std::string actual, size_t maxEditDistance=0)
Check that two strings are (approximately) equal.
CheckResult operator&&(CheckResult &&, CheckResult &&)
Combine the results of two checks using a product (AND): both must pass.
CheckResult CheckFloat(double exp, double act, double tolerance=0.000001)
Check that two floating-point numbers are equal within some tolerance.
Container for all libgrading names.