|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit test utilities |
| 4 | +
|
| 5 | + Author: DiffBlue Limited. All rights reserved. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "require_type.h" |
| 10 | + |
| 11 | +#include <testing-utils/catch.hpp> |
| 12 | +#include <util/base_type.h> |
| 13 | + |
| 14 | +/// Checks a type is a pointer type optionally with a specific subtype |
| 15 | +/// \param type: The type to check |
| 16 | +/// \param subtype: An optional subtype. If provided, checks the subtype of the |
| 17 | +/// pointer is this. |
| 18 | +/// \return A cast to pointer_typet version of type |
| 19 | +pointer_typet require_type::require_pointer( |
| 20 | + const typet &type, |
| 21 | + const optionalt<typet> &subtype) |
| 22 | +{ |
| 23 | + REQUIRE(type.id() == ID_pointer); |
| 24 | + const pointer_typet &pointer = to_pointer_type(type); |
| 25 | + |
| 26 | + if(subtype) |
| 27 | + { |
| 28 | + // TODO: use base_type_eq |
| 29 | + REQUIRE(pointer.subtype() == subtype.value()); |
| 30 | + } |
| 31 | + return pointer; |
| 32 | +} |
| 33 | + |
| 34 | +/// Checks a struct like type has a component with a specific name |
| 35 | +/// \param struct_type: The structure that should have the component |
| 36 | +/// \param component_name: The name of the component |
| 37 | +/// \return The component with the specified name |
| 38 | +struct_union_typet::componentt require_type::require_component( |
| 39 | + const struct_typet &struct_type, |
| 40 | + const irep_idt &component_name) |
| 41 | +{ |
| 42 | + const auto &componet = std::find_if( |
| 43 | + struct_type.components().begin(), |
| 44 | + struct_type.components().end(), |
| 45 | + [&component_name](const struct_union_typet::componentt &component) { |
| 46 | + return component.get_name() == component_name; |
| 47 | + }); |
| 48 | + |
| 49 | + REQUIRE(componet != struct_type.components().end()); |
| 50 | + return *componet; |
| 51 | +} |
0 commit comments