|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +// Note that this test should not include any headers which are internal to |
| 4 | +// cbmc. API headers (and STL/catch) should be included only. This is in order |
| 5 | +// to confirm that the API can be used without reference to cmbc internals. |
| 6 | + |
| 7 | +#include <libcprover-cpp/api.h> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <sstream> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include "../catch/catch.hpp" |
| 15 | + |
| 16 | +class contains_in_ordert final |
| 17 | + : public Catch::MatcherBase<std::vector<std::string>> |
| 18 | +{ |
| 19 | +public: |
| 20 | + contains_in_ordert(std::initializer_list<std::string> expected); |
| 21 | + bool match(const std::vector<std::string> &actual) const override; |
| 22 | + |
| 23 | +protected: |
| 24 | + std::string describe() const override; |
| 25 | + |
| 26 | +private: |
| 27 | + std::vector<std::string> expected; |
| 28 | +}; |
| 29 | + |
| 30 | +contains_in_ordert::contains_in_ordert( |
| 31 | + std::initializer_list<std::string> expected) |
| 32 | + : expected{ |
| 33 | + std::make_move_iterator(expected.begin()), |
| 34 | + std::make_move_iterator(expected.end())} |
| 35 | +{ |
| 36 | +} |
| 37 | + |
| 38 | +bool contains_in_ordert::match(const std::vector<std::string> &actual) const |
| 39 | +{ |
| 40 | + auto begin_search = actual.begin(); |
| 41 | + for(const auto &expected_item : expected) |
| 42 | + { |
| 43 | + auto find_result = std::find(begin_search, actual.end(), expected_item); |
| 44 | + if(find_result == actual.end()) |
| 45 | + return false; |
| 46 | + else |
| 47 | + begin_search = ++find_result; |
| 48 | + } |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +std::string contains_in_ordert::describe() const |
| 53 | +{ |
| 54 | + std::stringstream description; |
| 55 | + description << "contains these strings in order "; |
| 56 | + description << '"' << *expected.begin() << '"'; |
| 57 | + for(auto iterator = std::next(expected.begin()); iterator != expected.end(); |
| 58 | + ++iterator) |
| 59 | + { |
| 60 | + description << ", \"" << *iterator << '"'; |
| 61 | + } |
| 62 | + return description.str(); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_CASE("Test loading model from file.", "[core][libcprover-cpp]") |
| 66 | +{ |
| 67 | + api_sessiont api(api_optionst::create()); |
| 68 | + |
| 69 | + std::vector<std::string> output; |
| 70 | + // This lambda needs to be non-capturing in order for it to be convertible |
| 71 | + // to a function pointer, to pass to the API. |
| 72 | + const auto write_output = |
| 73 | + [](const api_messaget &message, api_call_back_contextt context) { |
| 74 | + std::vector<std::string> &output = |
| 75 | + *static_cast<std::vector<std::string> *>(context); |
| 76 | + output.emplace_back(api_message_get_string(message)); |
| 77 | + }; |
| 78 | + |
| 79 | + api.set_message_callback(write_output, &output); |
| 80 | + api.load_model_from_files({"test.c"}); |
| 81 | + CHECK_THAT( |
| 82 | + output, |
| 83 | + (contains_in_ordert{ |
| 84 | + "Parsing test.c", |
| 85 | + "Converting", |
| 86 | + "Type-checking test", |
| 87 | + "Generating GOTO Program"})); |
| 88 | +} |
0 commit comments