Skip to content

Commit 33afe48

Browse files
thk123svorenova
thk123
authored and
svorenova
committed
Adding unit tests for parsing wild card functions
1 parent a2344f8 commit 33afe48

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
interface BasicInterface
2+
{
3+
int getX();
4+
}
5+
6+
class Foo implements BasicInterface
7+
{
8+
public int x;
9+
10+
public int getX() {
11+
return x;
12+
}
13+
}
14+
15+
class Bar extends Foo
16+
{}
17+
18+
class SimpleGeneric<T>
19+
{
20+
public T t;
21+
}
22+
23+
public class WildcardGenericFunctions
24+
{
25+
// Test a wild card generic type
26+
public static void processSimpleGeneric(SimpleGeneric<?> x) {
27+
assert(x.t.equals(null));
28+
}
29+
30+
// Test a wildcard generic bound by an interface
31+
public static void processUpperBoundInterfaceGeneric(SimpleGeneric<? extends BasicInterface> x) {
32+
assert(x.t.getX() == 4);
33+
}
34+
35+
// Test a wild card generic bound by a class
36+
public static void processUpperBoundClassGeneric(SimpleGeneric<? extends Foo> x) {
37+
assert(x.t.getX() == 4);
38+
}
39+
40+
// It isn't legal to have an wild card with two upper bounds
41+
// Per language spec on intersection types
42+
43+
public static void processLowerBoundGeneric(SimpleGeneric<? super Foo> x, Foo assign) {
44+
x.t = assign;
45+
}
46+
47+
// It is not legal Java to specify both an upper and lower bound
48+
// public static void processBoundSuperClassGeneric(SimpleGeneric<? extends Object super Foo> x, Foo assign) {
49+
// x.t = assign;
50+
// }
51+
52+
// Test a wild card generic bound by a class
53+
// public static void processBoundClassGenericDoubleBound(SimpleGeneric<? extends Foo & BasicInterface> x) {
54+
// assert(x.t.getX() == 4);
55+
// }
56+
57+
public static void test()
58+
{
59+
SimpleGeneric<Foo> myGenericValue = new SimpleGeneric<Foo>();
60+
myGenericValue.t = null;
61+
processSimpleGeneric(myGenericValue);
62+
63+
myGenericValue.t = new Foo();
64+
myGenericValue.t.x = 4;
65+
processUpperBoundInterfaceGeneric(myGenericValue);
66+
67+
SimpleGeneric<Bar> anotherGenericValue = new SimpleGeneric<Bar>();
68+
anotherGenericValue.t = new Bar();
69+
anotherGenericValue.t.x = 4;
70+
processUpperBoundClassGeneric(anotherGenericValue);
71+
72+
73+
SimpleGeneric<Object> baseGenericValue = new SimpleGeneric<Object>();
74+
processLowerBoundGeneric(baseGenericValue, new Foo());
75+
}
76+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for parsing generic classes
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <util/config.h>
12+
#include <util/cmdline.h>
13+
#include <util/language.h>
14+
#include <util/prefix.h>
15+
16+
#include <java_bytecode/java_bytecode_language.h>
17+
18+
#include <iostream>
19+
20+
SCENARIO(
21+
"java_bytecode_parse_generic_wildcard",
22+
"[core][java_bytecode][java_bytecode_parse_generics]")
23+
{
24+
std::unique_ptr<languaget>java_lang(new_java_bytecode_language());
25+
26+
// Configure the path loading
27+
cmdlinet command_line;
28+
command_line.set(
29+
"java-cp-include-files",
30+
"./java_bytecode/java_bytecode_parse_generics");
31+
config.java.classpath.push_back(
32+
"./java_bytecode/java_bytecode_parse_generics");
33+
34+
std::istringstream java_code_stream("ignored");
35+
null_message_handlert message_handler;
36+
37+
java_lang->get_language_options(command_line);
38+
java_lang->set_message_handler(message_handler);
39+
java_lang->parse(java_code_stream, "WildcardGenericFunctions.class");
40+
symbol_tablet new_symbol_table;
41+
java_lang->typecheck(new_symbol_table, "");
42+
java_lang->final(new_symbol_table);
43+
44+
std::string class_prefix="java::WildcardGenericFunctions";
45+
46+
// Validate loaded the java file
47+
REQUIRE(new_symbol_table.has_symbol(class_prefix));
48+
49+
THEN("There should be a symbol for processSimpleGeneric")
50+
{
51+
const std::string func_name=".processSimpleGeneric";
52+
const std::string func_descriptor=":(LSimpleGeneric;)V";
53+
const std::string process_func_name=class_prefix+func_name+func_descriptor;
54+
55+
REQUIRE(new_symbol_table.has_symbol(process_func_name));
56+
}
57+
58+
THEN("There should be a symbol for processUpperBoundInterfaceGeneric")
59+
{
60+
const std::string func_name=".processUpperBoundInterfaceGeneric";
61+
const std::string func_descriptor=":(LSimpleGeneric;)V";
62+
const std::string process_func_name=class_prefix+func_name+func_descriptor;
63+
64+
REQUIRE(new_symbol_table.has_symbol(process_func_name));
65+
}
66+
67+
THEN("There should be a symbol for processUpperBoundClassGeneric")
68+
{
69+
const std::string func_name=".processUpperBoundClassGeneric";
70+
const std::string func_descriptor=":(LSimpleGeneric;)V";
71+
const std::string process_func_name=class_prefix+func_name+func_descriptor;
72+
73+
REQUIRE(new_symbol_table.has_symbol(process_func_name));
74+
}
75+
76+
THEN("There should be a symbol for processLowerBoundGeneric")
77+
{
78+
const std::string func_name=".processLowerBoundGeneric";
79+
const std::string func_descriptor=":(LSimpleGeneric;LFoo;)V";
80+
const std::string process_func_name=class_prefix+func_name+func_descriptor;
81+
82+
REQUIRE(new_symbol_table.has_symbol(process_func_name));
83+
}
84+
}

0 commit comments

Comments
 (0)