Skip to content

Commit

Permalink
Adding unit test for checking local lambda conversion
Browse files Browse the repository at this point in the history
Verifies the structure mimics the construction of an anonymous class
that implements the interface
  • Loading branch information
thk123 committed Mar 27, 2018
1 parent 46fa176 commit 2348d10
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SRC += unit_tests.cpp \
goto-programs/class_hierarchy_graph.cpp \
goto-programs/remove_virtual_functions_without_fallback.cpp \
java_bytecode/java_bytecode_convert_class/convert_abstract_class.cpp \
java_bytecode/java_bytecode_convert_method/convert_invoke_dynamic.cpp \
java_bytecode/java_bytecode_parse_generics/parse_generic_class.cpp \
java_bytecode/java_object_factory/gen_nondet_string_init.cpp \
java_bytecode/java_bytecode_parse_lambdas/java_bytecode_parse_lambda_method_table.cpp \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*******************************************************************\
Module: Unit tests for converting invokedynamic instructions into codet
Author: Diffblue Ltd.
\*******************************************************************/

#include <testing-utils/catch.hpp>
#include <testing-utils/load_java_class.h>
#include <testing-utils/require_goto_statements.h>
#include <testing-utils/require_expr.h>
#include <testing-utils/require_type.h>
#include <testing-utils/run_test_with_compilers.h>
#include <testing-utils/require_symbol.h>

SCENARIO(
"Converting invokedynamic with a local lambda",
"[core]"
"[lamdba][java_bytecode][java_bytecode_convert_method][!mayfail]")
{
// NOLINTNEXTLINE(whitespace/braces)
run_test_with_compilers([](const std::string &compiler) {
GIVEN(
"A class with a static lambda variables from " + compiler + " compiler.")
{
symbol_tablet symbol_table = load_java_class(
"LocalLambdas",
"./java_bytecode/java_bytecode_parser/lambda_examples/" + compiler +
"_classes/",
"LocalLambdas.test");

WHEN("Inspecting the assignments of the entry function")
{
const std::vector<codet> &instructions =
require_goto_statements::get_all_statements(
symbol_table.lookup_ref("java::LocalLambdas.test:()V").value);

THEN("The local variable should be assigned a non-null pointer")
{
// TODO(tkiley): we don't want 11 here
// TODO(tkiley): This is the actual lambda which doesn't currently work
const auto lambda_assignment =
require_goto_statements::find_pointer_assignments(
"java::LocalLambdas.test:()V::11::simpleLambda", instructions);

REQUIRE(lambda_assignment.non_null_assignments.size() == 1);
REQUIRE_FALSE(lambda_assignment.null_assignment.has_value());

const typecast_exprt &rhs_value = require_expr::require_typecast(
lambda_assignment.non_null_assignments[0].rhs());

const symbol_exprt &rhs_symbol =
require_expr::require_symbol(rhs_value.op0());

const irep_idt &tmp_object_symbol = rhs_symbol.get_identifier();

const auto tmp_object_assignments =
require_goto_statements::find_pointer_assignments(
tmp_object_symbol, instructions);

REQUIRE(tmp_object_assignments.non_null_assignments.size() == 1);
REQUIRE_FALSE(tmp_object_assignments.null_assignment.has_value());

const side_effect_exprt &side_effect_expr =
require_expr::require_side_effect_expr(
tmp_object_assignments.non_null_assignments[0].rhs(),
ID_java_new);

const pointer_typet &lambda_temp_type =
require_type::require_pointer(side_effect_expr.type(), {});

const symbol_typet &lambda_implementor_type =
require_type::require_symbol(lambda_temp_type.subtype());

const irep_idt &tmp_class_identifier =
lambda_implementor_type.get_identifier();

const symbolt &lambda_implementor_type_symbol =
require_symbol::require_symbol_exists(
symbol_table, tmp_class_identifier);

REQUIRE(lambda_implementor_type_symbol.is_type);
const class_typet &tmp_lambda_class_type =
require_type::require_complete_class(
lambda_implementor_type_symbol.type);

REQUIRE(tmp_lambda_class_type.has_base("java::SimpleLambda"));
}
}
}
});
}

0 comments on commit 2348d10

Please sign in to comment.