-
Notifications
You must be signed in to change notification settings - Fork 283
Add some unit test helper functions, useful for Java generics unit tests #1550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,21 @@ code_typet require_type::require_code(const typet &type) | |
| return to_code_type(type); | ||
| } | ||
|
|
||
| /// Verify a given type is an code_typet, and that the code it represents | ||
| /// accepts a given number of parameters | ||
| /// \param type The type to check | ||
| /// \param num_params check the the given code_typet expects this | ||
| /// number of parameters | ||
| /// \return The type cast to a code_typet | ||
| code_typet require_type::require_code( | ||
| const typet &type, | ||
| const size_t num_params) | ||
| { | ||
| code_typet code_type=require_code(type); | ||
| REQUIRE(code_type.parameters().size()==num_params); | ||
| return code_type; | ||
| } | ||
|
|
||
| /// Verify that a function has a parameter of a specific name. | ||
| /// \param function_type: The type of the function | ||
| /// \param param_name: The name of the parameter | ||
|
|
@@ -78,3 +93,105 @@ code_typet::parametert require_type::require_parameter( | |
| REQUIRE(param != function_type.parameters().end()); | ||
| return *param; | ||
| } | ||
|
|
||
| /// Verify a given type is a java_generic_type, optionally checking | ||
| /// that it's associated type variables match a given set of identifiers | ||
| /// \param type The type to check | ||
| /// \param type_variables An optional set of type variable identifiers which | ||
| /// should be expected as the type parameters of the generic type. | ||
| /// \return The given type, cast to a java_generic_typet | ||
| const java_generic_typet &require_type::require_java_generic_type_variables( | ||
| const typet &type, | ||
| const optionalt<std::initializer_list<irep_idt>> &type_variables) | ||
|
||
| { | ||
| REQUIRE(is_java_generic_type(type)); | ||
| const java_generic_typet &generic_type=to_java_generic_type(type); | ||
| if(type_variables) | ||
| { | ||
| const java_generic_typet::generic_type_variablest &generic_type_vars= | ||
| generic_type.generic_type_variables(); | ||
| REQUIRE(generic_type_vars.size()==type_variables.value().size()); | ||
| REQUIRE( | ||
| std::equal( | ||
| type_variables->begin(), | ||
| type_variables->end(), | ||
| generic_type_vars.begin(), | ||
| [](const irep_idt &type_var_name, const java_generic_parametert ¶m) | ||
| { | ||
| REQUIRE(!is_java_generic_inst_parameter((param))); | ||
| return param.type_variable().get_identifier()==type_var_name; | ||
| })); | ||
| } | ||
|
|
||
| return generic_type; | ||
| } | ||
|
|
||
| /// Verify a given type is a java_generic_type, optionally checking | ||
| /// that it's associated type variables match a given set of identifiers | ||
| /// \param type The type to check | ||
| /// \param type_variables An optional set of type variable identifiers which | ||
| /// should be expected as the type parameters of the generic type. | ||
| /// \return The given type, cast to a java_generic_typet | ||
| const java_generic_typet | ||
| &require_type::require_java_generic_type_instantiations( | ||
|
||
| const typet &type, | ||
| const optionalt<std::initializer_list<irep_idt>> &type_instantiations) | ||
|
||
| { | ||
| REQUIRE(is_java_generic_type(type)); | ||
| const java_generic_typet &generic_type=to_java_generic_type(type); | ||
| if(type_instantiations) | ||
| { | ||
| const java_generic_typet::generic_type_variablest &generic_type_vars= | ||
| generic_type.generic_type_variables(); | ||
| REQUIRE(generic_type_vars.size()==type_instantiations.value().size()); | ||
| REQUIRE( | ||
| std::equal( | ||
| type_instantiations->begin(), | ||
| type_instantiations->end(), | ||
| generic_type_vars.begin(), | ||
| [](const irep_idt &type_id, const java_generic_parametert ¶m) | ||
| { | ||
| REQUIRE(is_java_generic_inst_parameter((param))); | ||
| return param.subtype()==symbol_typet(type_id); | ||
| })); | ||
| } | ||
|
|
||
|
|
||
| return generic_type; | ||
| } | ||
|
|
||
| /// Verify a given type is a java_generic_parameter, optionally checking | ||
| /// that it's associated type variables match a given set of identifiers | ||
| /// \param type The type to check | ||
| /// \param type_variables An optional set of type variable identifiers which | ||
| /// should be expected as the type parameters of the generic type. | ||
| /// \return The given type, cast to a java_generic_typet | ||
| const java_generic_parametert | ||
| &require_type::require_java_generic_parameter_variables( | ||
|
||
| const typet &type, | ||
| const optionalt<irep_idt> &type_variable) | ||
| { | ||
| REQUIRE(is_java_generic_parameter(type)); | ||
| const java_generic_parametert &generic_param=to_java_generic_parameter(type); | ||
| if(type_variable) | ||
| { | ||
| const java_generic_parametert::type_variablet &generic_type_var= | ||
| generic_param.type_variable(); | ||
| REQUIRE(!is_java_generic_inst_parameter((generic_param))); | ||
| REQUIRE(generic_type_var.get_identifier()==type_variable.value()); | ||
| } | ||
|
|
||
| return generic_param; | ||
| } | ||
|
|
||
| const typet &require_type::require_java_non_generic_type( | ||
| const typet &type, | ||
| const optionalt<irep_idt> &expect_type) | ||
|
||
| { | ||
| REQUIRE(!is_java_generic_parameter(type)); | ||
| REQUIRE(!is_java_generic_type(type)); | ||
| REQUIRE(!is_java_generic_inst_parameter(type)); | ||
| if(expect_type) | ||
| REQUIRE(type.subtype()==symbol_typet(expect_type.value())); | ||
| return type; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make it a bit more clear what this does, could you please rename it to
require_java_generic_type_with_variables?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh and as a reminder - removing
const &form the return type (if you're returning a local variable) might fix the CI issues.