|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Invariant violation testing |
| 4 | +
|
| 5 | +Author: Chris Smowton, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Invariant violation testing |
| 11 | + |
| 12 | +#include <string> |
| 13 | +#include <sstream> |
| 14 | +#include <util/invariant.h> |
| 15 | + |
| 16 | +/// An example of structured invariants-- this contains fields to |
| 17 | +/// describe the error to a catcher, and also produces a human-readable |
| 18 | +/// message containing all the information for use by the current aborting |
| 19 | +/// invariant implementation and/or any generic error catcher in the future. |
| 20 | +class structured_error_testt: public invariant_failedt |
| 21 | +{ |
| 22 | + std::string pretty_print(int code, const std::string &desc) |
| 23 | + { |
| 24 | + std::ostringstream ret; |
| 25 | + ret << "Error code: " << code |
| 26 | + << "\nDescription: " << desc; |
| 27 | + return ret.str(); |
| 28 | + } |
| 29 | + |
| 30 | +public: |
| 31 | + const int error_code; |
| 32 | + const std::string description; |
| 33 | + |
| 34 | + structured_error_testt( |
| 35 | + const std::string &file, |
| 36 | + const std::string &function, |
| 37 | + int line, |
| 38 | + const std::string &backtrace, |
| 39 | + int code, |
| 40 | + const std::string &_description): |
| 41 | + invariant_failedt( |
| 42 | + file, |
| 43 | + function, |
| 44 | + line, |
| 45 | + backtrace, |
| 46 | + pretty_print(code, _description)), |
| 47 | + error_code(code), |
| 48 | + description(_description) |
| 49 | + { |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +/// Causes an invariant failure dependent on first argument value. |
| 54 | +/// One ignored argument is accepted to conform with the test.pl script, |
| 55 | +/// which would be the input source file for other cbmc driver programs. |
| 56 | +/// Returns 1 on unexpected arguments. |
| 57 | +int main(int argc, char** argv) |
| 58 | +{ |
| 59 | + if(argc!=3) |
| 60 | + return 1; |
| 61 | + std::string arg=argv[1]; |
| 62 | + if(arg=="structured") |
| 63 | + INVARIANT_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT |
| 64 | + else if(arg=="string") |
| 65 | + INVARIANT(false, "Test invariant failure"); |
| 66 | + else if(arg=="precondition-structured") |
| 67 | + PRECONDITION_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT |
| 68 | + else if(arg=="precondition-string") |
| 69 | + PRECONDITION(false); |
| 70 | + else if(arg=="postcondition-structured") |
| 71 | + POSTCONDITION_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT |
| 72 | + else if(arg=="postcondition-string") |
| 73 | + POSTCONDITION(false); |
| 74 | + else if(arg=="check-return-structured") |
| 75 | + CHECK_RETURN_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT |
| 76 | + else if(arg=="check-return-string") |
| 77 | + CHECK_RETURN(false); |
| 78 | + else if(arg=="unreachable-structured") |
| 79 | + UNREACHABLE_STRUCTURED(structured_error_testt, 1, "Structured error"); // NOLINT |
| 80 | + else if(arg=="unreachable-string") |
| 81 | + UNREACHABLE; |
| 82 | + else if(arg=="data-invariant-structured") |
| 83 | + DATA_INVARIANT_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT |
| 84 | + else if(arg=="data-invariant-string") |
| 85 | + DATA_INVARIANT(false, "Test invariant failure"); |
| 86 | + else |
| 87 | + return 1; |
| 88 | +} |
0 commit comments