Skip to content

Commit bf10b1b

Browse files
author
thk123
committed
Manually call specalisation code
1 parent bba9f76 commit bf10b1b

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

unit/java_bytecode/generate_concrete_generic_type/generate_java_generic_type.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,33 @@
99

1010
#include <testing-utils/catch.hpp>
1111
#include <testing-utils/load_java_class.h>
12+
#include <testing-utils/require_type.h>
1213

1314
#include <util/symbol_table.h>
1415

1516
#include <java_bytecode/generate_java_generic_type.h>
1617
#include <testing-utils/require_type.h>
18+
#include <testing-utils/generic_utils.h>
1719
#include <util/ui_message.h>
1820

21+
/// Helper function to specalise a generic class from a named component of a
22+
/// named class
23+
/// \param class_name: The name of the class that has a generic component.
24+
/// \param component_name: The name of the generic component
25+
/// \param new_symbol_table: The symbol table to use.
26+
void specialise_generic_from_component(
27+
const irep_idt &class_name,
28+
const irep_idt &component_name,
29+
symbol_tablet &new_symbol_table)
30+
{
31+
const symbolt &harness_symbol = new_symbol_table.lookup_ref(class_name);
32+
const struct_typet::componentt &harness_component =
33+
require_type::require_component(
34+
to_struct_type(harness_symbol.type), component_name);
35+
generic_utils::specialise_generic(
36+
to_java_generic_type(harness_component.type()), new_symbol_table);
37+
}
38+
1939
SCENARIO(
2040
"generate_java_generic_type_from_file",
2141
"[core][java_bytecode][generate_java_generic_type]")
@@ -30,6 +50,9 @@ SCENARIO(
3050
load_java_class("generic_two_fields",
3151
"./java_bytecode/generate_concrete_generic_type");
3252

53+
specialise_generic_from_component(
54+
"java::generic_two_fields", "belem", new_symbol_table);
55+
3356
REQUIRE(new_symbol_table.has_symbol(expected_symbol));
3457
THEN("The class should contain two instantiated fields.")
3558
{
@@ -77,6 +100,9 @@ SCENARIO(
77100
load_java_class("generic_two_parameters",
78101
"./java_bytecode/generate_concrete_generic_type");
79102

103+
specialise_generic_from_component(
104+
"java::generic_two_parameters", "bomb", new_symbol_table);
105+
80106
REQUIRE(new_symbol_table.has_symbol(
81107
"java::generic_two_parameters$KeyValuePair"));
82108
THEN("The class should have two subtypes in the vector of the types of "
@@ -140,6 +166,11 @@ SCENARIO(
140166
load_java_class("generic_two_instances",
141167
"./java_bytecode/generate_concrete_generic_type");
142168

169+
specialise_generic_from_component(
170+
"java::generic_two_instances", "bool_element", new_symbol_table);
171+
specialise_generic_from_component(
172+
"java::generic_two_instances", "int_element", new_symbol_table);
173+
143174
REQUIRE(new_symbol_table.has_symbol(first_expected_symbol));
144175
auto first_symbol=new_symbol_table.lookup(first_expected_symbol);
145176
REQUIRE(first_symbol->type.id()==ID_struct);

unit/testing-utils/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SRC = \
22
c_to_expr.cpp \
3+
generic_utils.cpp \
34
load_java_class.cpp \
45
require_expr.cpp \
56
require_goto_statements.cpp \

unit/testing-utils/generic_utils.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
#include <util/ui_message.h>
10+
#include <java_bytecode/generate_java_generic_type.h>
11+
#include "generic_utils.h"
12+
#include "catch.hpp"
13+
#include "require_type.h"
14+
15+
/// Generic a specalised version of a Java generic class and add it to the
16+
/// symbol table.
17+
/// \param example_type: A reference type that is a specalised generic to use
18+
/// as the base for the specalised version (e.g. a variable of type
19+
/// `Generic<Integer>`
20+
/// \param new_symbol_table: The symbol table to store the new type in
21+
void generic_utils::specialise_generic(
22+
const java_generic_typet &example_type,
23+
symbol_tablet &new_symbol_table)
24+
{
25+
// Should be a fully instantiated generic type
26+
REQUIRE(
27+
std::all_of(
28+
example_type.generic_type_variables().begin(),
29+
example_type.generic_type_variables().end(),
30+
is_java_generic_inst_parameter));
31+
32+
// Generate the specialised version.
33+
ui_message_handlert message_handler;
34+
generate_java_generic_typet instantiate_generic_type(message_handler);
35+
instantiate_generic_type(
36+
to_java_generic_type(example_type), new_symbol_table);
37+
}

unit/testing-utils/generic_utils.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Utility methods for dealing with Java generics in unit tests
11+
12+
#ifndef CPROVER_TESTING_UTILS_GENERIC_UTILS_H
13+
#define CPROVER_TESTING_UTILS_GENERIC_UTILS_H
14+
15+
#include <util/irep.h>
16+
#include <util/symbol_table.h>
17+
18+
// NOLINTNEXTLINE(readability/namespace)
19+
namespace generic_utils
20+
{
21+
void specialise_generic(
22+
const java_generic_typet &example_type,
23+
symbol_tablet &new_symbol_table);
24+
}
25+
26+
#endif // CPROVER_TESTING_UTILS_GENERIC_UTILS_H

0 commit comments

Comments
 (0)