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 
22  const java_bytecode_parse_treet::classt &parsed_class,
23  const std::string &lambda_method_ref)
24 {
25  typedef java_bytecode_parse_treet::classt::lambda_method_handle_mapt::
26  value_type lambda_method_entryt;
27 
28  INFO("Looking for entry with lambda_method_ref: " << lambda_method_ref);
29  const irep_idt method_ref_with_prefix =
30  "java::" + id2string(lambda_method_ref);
31 
32  std::vector<lambda_method_entryt> matches;
33  std::copy_if(
34  parsed_class.lambda_method_handle_map.begin(),
35  parsed_class.lambda_method_handle_map.end(),
36  back_inserter(matches),
37  [&method_ref_with_prefix](const lambda_method_entryt &entry) { //NOLINT
38  return (
39  entry.second.get_method_descriptor().get_identifier() ==
40  method_ref_with_prefix);
41  });
42  REQUIRE(matches.size() == 1);
43  return matches.at(0).second;
44 }
45 
51  const java_bytecode_parse_treet::classt &parsed_class,
52  const irep_idt &method_name)
53 {
54  const auto method = std::find_if(
55  parsed_class.methods.begin(),
56  parsed_class.methods.end(),
57  [&method_name](const java_bytecode_parse_treet::methodt &method) {
58  return method.name == method_name;
59  });
60 
61  INFO("Looking for method: " << method_name);
62  std::ostringstream found_methods;
63  for(const auto &entry : parsed_class.methods)
64  {
65  found_methods << id2string(entry.name) << std::endl;
66  }
67  INFO("Found methods:\n" << found_methods.str());
68 
69  REQUIRE(method != parsed_class.methods.end());
70 
71  return *method;
72 }
73 
78  const expected_instructionst &expected_instructions,
80 {
81  REQUIRE(instructions.size() == expected_instructions.size());
82  auto actual_instruction_it = instructions.begin();
83  for(const auto &expected_instruction : expected_instructions)
84  {
85  expected_instruction.require_instructions_equal(*actual_instruction_it);
86  ++actual_instruction_it;
87  }
88 }
89 
93  java_bytecode_parse_treet::instructiont actual_instruction) const
94 {
95  REQUIRE(
96  instruction_mnemoic == bytecode_info[actual_instruction.bytecode].mnemonic);
97  REQUIRE(instruction_arguments.size() == actual_instruction.args.size());
98  auto actual_arg_it = actual_instruction.args.begin();
99  for(const exprt &expected_arg : actual_instruction.args)
100  {
101  INFO("Expected argument" << expected_arg.pretty());
102  INFO("Actual argument" << actual_arg_it->pretty());
103  REQUIRE(*actual_arg_it == expected_arg);
104  ++actual_arg_it;
105  }
106 }
java_bytecode_parse_treet::instructiont::args
argst args
Definition: java_bytecode_parse_tree.h:62
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:273
require_parse_tree::expected_instructiont::instruction_mnemoic
irep_idt instruction_mnemoic
Definition: require_parse_tree.h:49
bytecode_infot::mnemonic
const char * mnemonic
Definition: bytecode_info.h:46
java_bytecode_parse_treet::methodt
Definition: java_bytecode_parse_tree.h:86
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:50
java_bytecode_parse_treet::methodt::instructionst
std::vector< instructiont > instructionst
Definition: java_bytecode_parse_tree.h:92
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:50
java_bytecode_parse_treet::instructiont::bytecode
u8 bytecode
Definition: java_bytecode_parse_tree.h:60
java_bytecode_parse_treet::instructiont
Definition: java_bytecode_parse_tree.h:57
exprt
Base class for all expressions.
Definition: expr.h:53
require_parse_tree.h
Utilties for inspecting java_parse_treet.
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:92
java_bytecode_parse_treet::classt::lambda_method_handlet
Definition: java_bytecode_parse_tree.h:230
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
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)
Find in the parsed class a specific entry within the lambda_method_handle_map with a matching descrip...
Definition: require_parse_tree.cpp:21
java_bytecode_parse_treet::classt::methods
methodst methods
Definition: java_bytecode_parse_tree.h:281
require_parse_tree::expected_instructionst
std::vector< expected_instructiont > expected_instructionst
Definition: require_parse_tree.h:53
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:77
java_bytecode_parse_treet::classt
Definition: java_bytecode_parse_tree.h:198
bytecode_info
struct bytecode_infot const bytecode_info[]
Definition: bytecode_info.cpp:16