cprover
require_parse_tree.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Unit test utilities
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
9 #include "require_parse_tree.h"
10 
11 #include <iterator>
12 
23  const java_bytecode_parse_treet::classt &parsed_class,
24  const std::string &lambda_method_ref,
25  const std::string &method_type)
26 {
27  typedef java_bytecode_parse_treet::classt::lambda_method_handle_mapt::
28  value_type lambda_method_entryt;
29 
30  INFO(
31  "Looking for entry with lambda_method_ref: " << lambda_method_ref
32  << " and method_type: "
33  << method_type);
34  std::vector<lambda_method_entryt> matches;
35  std::copy_if(
36  parsed_class.lambda_method_handle_map.begin(),
37  parsed_class.lambda_method_handle_map.end(),
38  back_inserter(matches),
39  [&method_type,
40  &lambda_method_ref](const lambda_method_entryt &entry) { //NOLINT
41  return (
42  entry.second.method_type == method_type &&
43  entry.second.lambda_method_ref == lambda_method_ref);
44  });
45  REQUIRE(matches.size() == 1);
46  return matches.at(0).second;
47 }
48 
54  const java_bytecode_parse_treet::classt &parsed_class,
55  const irep_idt &method_name)
56 {
57  const auto method = std::find_if(
58  parsed_class.methods.begin(),
59  parsed_class.methods.end(),
60  [&method_name](const java_bytecode_parse_treet::methodt &method) {
61  return method.name == method_name;
62  });
63 
64  INFO("Looking for method: " << method_name);
65  std::ostringstream found_methods;
66  for(const auto entry : parsed_class.methods)
67  {
68  found_methods << id2string(entry.name) << std::endl;
69  }
70  INFO("Found methods:\n" << found_methods.str());
71 
72  REQUIRE(method != parsed_class.methods.end());
73 
74  return *method;
75 }
76 
81  const expected_instructionst &expected_instructions,
83 {
84  REQUIRE(instructions.size() == expected_instructions.size());
85  auto actual_instruction_it = instructions.begin();
86  for(const auto expected_instruction : expected_instructions)
87  {
88  expected_instruction.require_instructions_equal(*actual_instruction_it);
89  ++actual_instruction_it;
90  }
91 }
92 
96  java_bytecode_parse_treet::instructiont actual_instruction) const
97 {
98  REQUIRE(
99  instruction_mnemoic == bytecode_info[actual_instruction.bytecode].mnemonic);
100  REQUIRE(instruction_arguments.size() == actual_instruction.args.size());
101  auto actual_arg_it = actual_instruction.args.begin();
102  for(const exprt &expected_arg : actual_instruction.args)
103  {
104  INFO("Expected argument" << expected_arg.pretty());
105  INFO("Actual argument" << actual_arg_it->pretty());
106  REQUIRE(*actual_arg_it == expected_arg);
107  ++actual_arg_it;
108  }
109 }
java_bytecode_parse_treet::instructiont::args
argst args
Definition: java_bytecode_parse_tree.h:61
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
java_bytecode_parse_treet::classt::lambda_method_handle_map
lambda_method_handle_mapt lambda_method_handle_map
Definition: java_bytecode_parse_tree.h:267
require_parse_tree::expected_instructiont::instruction_mnemoic
irep_idt instruction_mnemoic
Definition: require_parse_tree.h:50
bytecode_infot::mnemonic
const char * mnemonic
Definition: bytecode_info.h:46
java_bytecode_parse_treet::methodt
Definition: java_bytecode_parse_tree.h:85
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:488
require_parse_tree::expected_instructiont::instruction_arguments
std::vector< exprt > instruction_arguments
Definition: require_parse_tree.h:51
java_bytecode_parse_treet::methodt::instructionst
std::vector< instructiont > instructionst
Definition: java_bytecode_parse_tree.h:91
require_parse_tree::require_method
const methodt require_method(const java_bytecode_parse_treet::classt &parsed_class, const irep_idt &method_name)
Finds a specific method in the parsed class with a matching name.
Definition: require_parse_tree.cpp:53
java_bytecode_parse_treet::instructiont::bytecode
u8 bytecode
Definition: java_bytecode_parse_tree.h:59
java_bytecode_parse_treet::instructiont
Definition: java_bytecode_parse_tree.h:56
exprt
Base class for all expressions.
Definition: expr.h:53
require_parse_tree.h
require_parse_tree::expected_instructiont::require_instructions_equal
void require_instructions_equal(java_bytecode_parse_treet::instructiont actual_instruction) const
Check whether a given instruction matches an expectation of the instruction.
Definition: require_parse_tree.cpp:95
java_bytecode_parse_treet::classt::lambda_method_handlet
Definition: java_bytecode_parse_tree.h:235
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
java_bytecode_parse_treet::classt::methods
methodst methods
Definition: java_bytecode_parse_tree.h:275
require_parse_tree::require_lambda_entry_for_descriptor
lambda_method_handlet require_lambda_entry_for_descriptor(const java_bytecode_parse_treet::classt &parsed_class, const std::string &lambda_method_ref, const std::string &method_type)
Find in the parsed class a specific entry within the lambda_method_handle_map with a matching descrip...
Definition: require_parse_tree.cpp:22
require_parse_tree::expected_instructionst
std::vector< expected_instructiont > expected_instructionst
Definition: require_parse_tree.h:54
require_parse_tree::require_instructions_match_expectation
void require_instructions_match_expectation(const expected_instructionst &expected_instructions, const java_bytecode_parse_treet::methodt::instructionst instructions)
Verify whether a given methods instructions match an expectation.
Definition: require_parse_tree.cpp:80
java_bytecode_parse_treet::classt
Definition: java_bytecode_parse_tree.h:197
bytecode_info
struct bytecode_infot const bytecode_info[]
Definition: bytecode_info.cpp:16