|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit tests for converting abstract classes |
| 4 | +
|
| 5 | + Author: DiffBlue Limited. All rights reserved. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <catch.hpp> |
| 10 | + |
| 11 | +#include <istream> |
| 12 | +#include <memory> |
| 13 | + |
| 14 | +#include <util/config.h> |
| 15 | +#include <util/language.h> |
| 16 | +#include <util/message.h> |
| 17 | +#include <java_bytecode/java_bytecode_language.h> |
| 18 | + |
| 19 | +SCENARIO("java_bytecode_convert_abstract_class", |
| 20 | + "[core][java_bytecode][java_bytecode_convert_class]") |
| 21 | +{ |
| 22 | + std::unique_ptr<languaget>java_lang(new_java_bytecode_language()); |
| 23 | + |
| 24 | + // Configure the path loading |
| 25 | + cmdlinet command_line; |
| 26 | + command_line.set( |
| 27 | + "java-cp-include-files", |
| 28 | + "./java_bytecode/java_bytecode_convert_class"); |
| 29 | + config.java.classpath.push_back( |
| 30 | + "./java_bytecode/java_bytecode_convert_class"); |
| 31 | + |
| 32 | + // Configure the language |
| 33 | + null_message_handlert message_handler; |
| 34 | + java_lang->get_language_options(command_line); |
| 35 | + java_lang->set_message_handler(message_handler); |
| 36 | + |
| 37 | + std::istringstream java_code_stream("ignored"); |
| 38 | + |
| 39 | + GIVEN("Some class files in the class path") |
| 40 | + { |
| 41 | + WHEN("Parsing an interface") |
| 42 | + { |
| 43 | + java_lang->parse(java_code_stream, "I.class"); |
| 44 | + |
| 45 | + symbol_tablet new_symbol_table; |
| 46 | + java_lang->typecheck(new_symbol_table, ""); |
| 47 | + |
| 48 | + java_lang->final(new_symbol_table); |
| 49 | + |
| 50 | + REQUIRE(new_symbol_table.has_symbol("java::I")); |
| 51 | + THEN("The symbol type should be abstract") |
| 52 | + { |
| 53 | + const symbolt &class_symbol=new_symbol_table.lookup("java::I"); |
| 54 | + const typet &symbol_type=class_symbol.type; |
| 55 | + |
| 56 | + REQUIRE(symbol_type.id()==ID_struct); |
| 57 | + class_typet class_type=to_class_type(symbol_type); |
| 58 | + REQUIRE(class_type.is_class()); |
| 59 | + REQUIRE(class_type.is_abstract()); |
| 60 | + } |
| 61 | + } |
| 62 | + WHEN("Parsing an abstract class") |
| 63 | + { |
| 64 | + java_lang->parse(java_code_stream, "A.class"); |
| 65 | + |
| 66 | + symbol_tablet new_symbol_table; |
| 67 | + java_lang->typecheck(new_symbol_table, ""); |
| 68 | + |
| 69 | + java_lang->final(new_symbol_table); |
| 70 | + |
| 71 | + REQUIRE(new_symbol_table.has_symbol("java::A")); |
| 72 | + THEN("The symbol type should be abstract") |
| 73 | + { |
| 74 | + const symbolt &class_symbol=new_symbol_table.lookup("java::A"); |
| 75 | + const typet &symbol_type=class_symbol.type; |
| 76 | + |
| 77 | + REQUIRE(symbol_type.id()==ID_struct); |
| 78 | + class_typet class_type=to_class_type(symbol_type); |
| 79 | + REQUIRE(class_type.is_class()); |
| 80 | + REQUIRE(class_type.is_abstract()); |
| 81 | + } |
| 82 | + } |
| 83 | + WHEN("Passing a concrete class") |
| 84 | + { |
| 85 | + java_lang->parse(java_code_stream, "C.class"); |
| 86 | + |
| 87 | + symbol_tablet new_symbol_table; |
| 88 | + java_lang->typecheck(new_symbol_table, ""); |
| 89 | + |
| 90 | + java_lang->final(new_symbol_table); |
| 91 | + |
| 92 | + REQUIRE(new_symbol_table.has_symbol("java::C")); |
| 93 | + THEN("The symbol type should not be abstract") |
| 94 | + { |
| 95 | + const symbolt &class_symbol=new_symbol_table.lookup("java::C"); |
| 96 | + const typet &symbol_type=class_symbol.type; |
| 97 | + |
| 98 | + REQUIRE(symbol_type.id()==ID_struct); |
| 99 | + class_typet class_type=to_class_type(symbol_type); |
| 100 | + REQUIRE(class_type.is_class()); |
| 101 | + REQUIRE_FALSE(class_type.is_abstract()); |
| 102 | + } |
| 103 | + } |
| 104 | + WHEN("Passing a concrete class that implements an interface") |
| 105 | + { |
| 106 | + java_lang->parse(java_code_stream, "Implementor.class"); |
| 107 | + |
| 108 | + symbol_tablet new_symbol_table; |
| 109 | + java_lang->typecheck(new_symbol_table, ""); |
| 110 | + |
| 111 | + java_lang->final(new_symbol_table); |
| 112 | + |
| 113 | + REQUIRE(new_symbol_table.has_symbol("java::Implementor")); |
| 114 | + THEN("The symbol type should not be abstract") |
| 115 | + { |
| 116 | + const symbolt &class_symbol= |
| 117 | + new_symbol_table.lookup("java::Implementor"); |
| 118 | + const typet &symbol_type=class_symbol.type; |
| 119 | + |
| 120 | + REQUIRE(symbol_type.id()==ID_struct); |
| 121 | + class_typet class_type=to_class_type(symbol_type); |
| 122 | + REQUIRE(class_type.is_class()); |
| 123 | + REQUIRE_FALSE(class_type.is_abstract()); |
| 124 | + } |
| 125 | + } |
| 126 | + WHEN("Passing a concrete class that extends an abstract class") |
| 127 | + { |
| 128 | + java_lang->parse(java_code_stream, "Extendor.class"); |
| 129 | + |
| 130 | + symbol_tablet new_symbol_table; |
| 131 | + java_lang->typecheck(new_symbol_table, ""); |
| 132 | + |
| 133 | + java_lang->final(new_symbol_table); |
| 134 | + |
| 135 | + REQUIRE(new_symbol_table.has_symbol("java::Extendor")); |
| 136 | + THEN("The symbol type should not be abstract") |
| 137 | + { |
| 138 | + const symbolt &class_symbol= |
| 139 | + new_symbol_table.lookup("java::Extendor"); |
| 140 | + const typet &symbol_type=class_symbol.type; |
| 141 | + |
| 142 | + REQUIRE(symbol_type.id()==ID_struct); |
| 143 | + class_typet class_type=to_class_type(symbol_type); |
| 144 | + REQUIRE(class_type.is_class()); |
| 145 | + REQUIRE_FALSE(class_type.is_abstract()); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments