libgrading
A simple library for grading C and C++ assignments.
 All Classes Namespaces Files Functions Enumerations Enumerator
Classes | Enumerations | Functions
grading Namespace Reference

Container for all libgrading names. More...

Classes

class  SharedMemory
 A representation of a shared memory object. More...
 

Enumerations

enum  TestResult : char {
  TestResult::Pass, TestResult::Fail, TestResult::Segfault, TestResult::Timeout,
  TestResult::OtherError
}
 The result of running one test within a separate process. More...
 

Functions

CheckResult operator&& (CheckResult &&, CheckResult &&)
 Combine the results of two checks using a product (AND): both must pass. More...
 
CheckResult operator&& (CheckResult &&, CheckResult &)
 L-value version of above. More...
 
CheckResult operator|| (CheckResult &&, CheckResult &&)
 Combine the results of two checks using a sum (OR): at least one must pass. More...
 
CheckResult operator|| (CheckResult &&, CheckResult &)
 L-value version of above. More...
 
CheckResult Check (bool, std::string description)
 Check an arbitrary condition, failing the test if false. More...
 
CheckResult CheckInt (int expected, int actual)
 Check that two integers are equal, failing the test if they are not. More...
 
CheckResult CheckFloat (double exp, double act, double tolerance=0.000001)
 Check that two floating-point numbers are equal within some tolerance. More...
 
CheckResult CheckString (std::string expected, std::string actual, size_t maxEditDistance=0)
 Check that two strings are (approximately) equal. More...
 
std::ostream & operator<< (std::ostream &, TestResult)
 Output a human-readable representation of a TestResult. More...
 
std::unique_ptr< SharedMemoryMapSharedData (size_t size)
 Map data into the address space that can be shared with other processes. More...
 
TestResult RunTest (std::function< TestResult()> test, std::string name="<unnamed test>", time_t timeout=0, std::ostream &errorStream=std::cerr)
 Run a test closure in a separate process, capturing segmentation faults and other errors that lead to termination. More...
 
template<class T >
TestResult RunTest (std::function< TestResult(T &)> test, T &output, std::string name="<unnamed test>", time_t timeout=0, std::ostream &errorStream=std::cerr)
 Run a test with output but no explicit input. More...
 
template<class Expectation , class Output >
TestResult RunTest (std::function< TestResult(const Expectation &, Output &)> t, const Expectation &expect, Output &output, std::string name="<unnamed test>", time_t timeout=0, std::ostream &errorStream=std::cerr)
 Run a test with input and output. More...
 

Detailed Description

Container for all libgrading names.

Enumeration Type Documentation

enum grading::TestResult : char
strong

The result of running one test within a separate process.

Enumerator
Pass 

the test succeeded

Fail 

the test failed

Segfault 

the test caused a segmentation fault

Timeout 

the test took too long to run

OtherError 

the test terminated for another reason

Definition at line 113 of file libgrading.h.

Function Documentation

CheckResult grading::Check ( bool  condition,
std::string  description 
)

Check an arbitrary condition, failing the test if false.

Definition at line 156 of file checks.cpp.

CheckResult grading::CheckFloat ( double  exp,
double  act,
double  tolerance = 0.000001 
)

Check that two floating-point numbers are equal within some tolerance.

Definition at line 172 of file checks.cpp.

CheckResult grading::CheckInt ( int  expected,
int  actual 
)

Check that two integers are equal, failing the test if they are not.

Definition at line 164 of file checks.cpp.

CheckResult grading::CheckString ( std::string  expected,
std::string  actual,
size_t  maxEditDistance = 0 
)

Check that two strings are (approximately) equal.

Parameters
expectedthe string we expected
actualthe string we got
maxEditDistancehow fuzzy the match can be: the maximum Levenshtein distance between them

Definition at line 187 of file checks.cpp.

unique_ptr< SharedMemory > grading::MapSharedData ( size_t  size)

Map data into the address space that can be shared with other processes.

Definition at line 118 of file posix.cpp.

Referenced by RunTest().

+ Here is the caller graph for this function:

CheckResult grading::operator&& ( CheckResult &&  x,
CheckResult &&  y 
)

Combine the results of two checks using a product (AND): both must pass.

Definition at line 66 of file checks.cpp.

CheckResult grading::operator&& ( CheckResult &&  x,
CheckResult &  y 
)

L-value version of above.

Definition at line 99 of file checks.cpp.

std::ostream & grading::operator<< ( std::ostream &  out,
TestResult  result 
)

Output a human-readable representation of a TestResult.

Definition at line 26 of file TestResult.cpp.

CheckResult grading::operator|| ( CheckResult &&  x,
CheckResult &&  y 
)

Combine the results of two checks using a sum (OR): at least one must pass.

Definition at line 105 of file checks.cpp.

CheckResult grading::operator|| ( CheckResult &&  x,
CheckResult &  y 
)

L-value version of above.

Definition at line 129 of file checks.cpp.

TestResult grading::RunTest ( std::function< TestResult()>  test,
std::string  name = "<unnamed test>",
time_t  timeout = 0,
std::ostream &  errorStream = std::cerr 
)

Run a test closure in a separate process, capturing segmentation faults and other errors that lead to termination.

Parameters
testthe test to run
namea developer-friendly name
timeoutthe number of seconds to let the test run (default 0, meaning "no timeout, run forever")
errorStreamwhere to write messages (e.g., "expected X, got Y")
Returns
the result of the test, either pass/fail as reported by test or else segfault/other as captured by libgrading

Definition at line 128 of file posix.cpp.

References Timeout.

Referenced by RunTest().

+ Here is the caller graph for this function:

template<class T >
TestResult grading::RunTest ( std::function< TestResult(T &)>  test,
T &  output,
std::string  name = "<unnamed test>",
time_t  timeout = 0,
std::ostream &  errorStream = std::cerr 
)

Run a test with output but no explicit input.

Parameters
testa test function that copies out an output value and returns a TestResult
outputthe output value produced by test (if it executes normally, e.g., without segfaulting)
namea developer-friendly name
timeoutthe number of seconds to let the test run (default 0, meaning "no timeout, run forever")
errorStreamwhere to write messages (e.g., "expected X, got Y")
Returns
the result of the test, either pass/fail as reported by test or else segfault/other as captured by libgrading

Definition at line 185 of file libgrading.h.

References MapSharedData(), grading::SharedMemory::rawPointer(), and RunTest().

+ Here is the call graph for this function:

template<class Expectation , class Output >
TestResult grading::RunTest ( std::function< TestResult(const Expectation &, Output &)>  t,
const Expectation &  expect,
Output &  output,
std::string  name = "<unnamed test>",
time_t  timeout = 0,
std::ostream &  errorStream = std::cerr 
)

Run a test with input and output.

Parameters
ta test function that takes an input expectation, copies out an output value and returns a TestResult
expectour test expectation (inputs, expected output, etc.)
outputthe output value produced by test (if it executes normally, e.g., without segfaulting)
namea developer-friendly name
timeoutthe number of seconds to let the test run (default 0, meaning "no timeout, run forever")
errorStreamwhere to write messages (e.g., "expected X, got Y")
Returns
the result of the test, either pass/fail as reported by test or else segfault/other as captured by libgrading

Definition at line 219 of file libgrading.h.