libgrading
A simple library for grading C and C++ assignments.
 All Classes Namespaces Files Functions Enumerations Enumerator
libgrading.h
Go to the documentation of this file.
1 
22 #ifndef LIBGRADING_H
23 #define LIBGRADING_H
24 
25 #include <functional>
26 #include <iostream>
27 #include <memory>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 
34 namespace grading {
35 
36 
37 class CheckResult
38 {
39  public:
40  CheckResult();
41  CheckResult(std::string message);
42  CheckResult(std::string exected, std::string actual);
43 
44  CheckResult(const CheckResult&) = delete;
45  CheckResult(CheckResult&&);
46 
47  ~CheckResult();
48 
49  CheckResult& operator << (const std::vector<std::string>&);
50 
51  template<class T>
52  CheckResult& operator << (const T& x)
53  {
54  message_ << x;
55  return *this;
56  }
57 
58  bool error() const { return reportError_; }
59  void cancel() { reportError_ = false; }
60 
61  std::string expected() const { return expected_; }
62  std::string actual() const { return actual_; }
63  std::string message() const { return message_.str(); }
64 
65  private:
66  bool reportError_;
67 
68  const std::string expected_;
69  const std::string actual_;
70 
71  std::ostringstream message_;
72 };
73 
75 CheckResult operator && (CheckResult&&, CheckResult&&);
76 
78 CheckResult operator && (CheckResult&&, CheckResult&);
79 
81 CheckResult operator || (CheckResult&&, CheckResult&&);
82 
84 CheckResult operator || (CheckResult&&, CheckResult&);
85 
86 
87 //
88 // Checks for tests:
89 //
90 
92 CheckResult Check(bool, std::string description);
93 
95 CheckResult CheckInt(int expected, int actual);
96 
98 CheckResult CheckFloat(double exp, double act, double tolerance = 0.000001);
99 
108 CheckResult CheckString(std::string expected, std::string actual,
109  size_t maxEditDistance = 0);
110 
111 
113 enum class TestResult : char
114 {
115  Pass,
116  Fail,
117  Segfault,
118  Timeout,
119  OtherError
120 };
121 
123 std::ostream& operator << (std::ostream&, TestResult);
124 
125 
134 {
135  public:
136  virtual ~SharedMemory() {}
137 
142  virtual void* rawPointer() const = 0;
143 };
144 
145 
147 std::unique_ptr<SharedMemory> MapSharedData(size_t size);
148 
149 
163 TestResult RunTest(std::function<TestResult ()> test,
164  std::string name = "<unnamed test>",
165  time_t timeout = 0,
166  std::ostream& errorStream = std::cerr);
167 
168 
184 template<class T>
185 TestResult RunTest(std::function<TestResult (T&)> test, T& output,
186  std::string name = "<unnamed test>",
187  time_t timeout = 0,
188  std::ostream& errorStream = std::cerr)
189 {
190  std::unique_ptr<SharedMemory> mem(MapSharedData(sizeof(T)));
191  T *testOutput = static_cast<T*>(mem->rawPointer());
192 
193  TestResult result =
194  RunTest([&]() { return test(*testOutput); }, name,
195  timeout, errorStream);
196 
197  output = *testOutput;
198  return result;
199 }
200 
201 
218 template<class Expectation, class Output>
219 TestResult RunTest(std::function<TestResult (const Expectation&, Output&)> t,
220  const Expectation& expect, Output& output,
221  std::string name = "<unnamed test>", time_t timeout = 0,
222  std::ostream& errorStream = std::cerr)
223 {
224  using std::placeholders::_1;
225  return RunTest<Output>(
226  std::bind(t, expect, _1), output, name, timeout, errorStream);
227 }
228 
229 } // namespace grading
230 
231 #endif
the test took too long to run
virtual void * rawPointer() const =0
A pointer to the shared memory, which will be invalidated after this object is destructed.
TestResult
The result of running one test within a separate process.
Definition: libgrading.h:113
CheckResult CheckInt(int expected, int actual)
Check that two integers are equal, failing the test if they are not.
Definition: checks.cpp:164
the test succeeded
std::unique_ptr< SharedMemory > MapSharedData(size_t size)
Map data into the address space that can be shared with other processes.
Definition: posix.cpp:118
CheckResult Check(bool, std::string description)
Check an arbitrary condition, failing the test if false.
Definition: checks.cpp:156
CheckResult operator||(CheckResult &&, CheckResult &&)
Combine the results of two checks using a sum (OR): at least one must pass.
Definition: checks.cpp:105
the test caused a segmentation fault
CheckResult CheckString(std::string expected, std::string actual, size_t maxEditDistance=0)
Check that two strings are (approximately) equal.
Definition: checks.cpp:187
std::ostream & operator<<(std::ostream &, TestResult)
Output a human-readable representation of a TestResult.
Definition: TestResult.cpp:26
A representation of a shared memory object.
Definition: libgrading.h:133
CheckResult operator&&(CheckResult &&, CheckResult &&)
Combine the results of two checks using a product (AND): both must pass.
Definition: checks.cpp:66
CheckResult CheckFloat(double exp, double act, double tolerance=0.000001)
Check that two floating-point numbers are equal within some tolerance.
Definition: checks.cpp:172
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...
Definition: posix.cpp:128
the test terminated for another reason
Container for all libgrading names.
Definition: libgrading.h:34