File: java_utils_test.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (398 lines) | stat: -rw-r--r-- 12,159 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*******************************************************************\

Module: Unit tests for parsing generic classes

Author: Diffblue Ltd.

 Note: Created by fotis on 09/10/2017.

\*******************************************************************/

#include <map>
#include <string>
#include <vector>

#include <testing-utils/use_catch.h>

#include <java_bytecode/java_types.h>
#include <java_bytecode/java_utils.h>

#include <util/symbol.h>

SCENARIO("Test that the generic signature delimiter lookup works reliably",
         "[core][java_util_test]")
{
  GIVEN("Given the signatures of some generic classes")
  {
    std::vector<std::string> generic_sigs
      {
        // Valid inputs
        "List<Integer>",
        "HashMap<String, Integer>",
        "List<List<Int>>",
        "List<List<List<Int>>>",
        // Invalid inputs
        "<",
        "List<Integer",
        "List<List<Integer>",
      };

    WHEN("We check if the closing tag is recognised correctly")
    {
      // TEST VALID CASES

      // List<Integer>
      REQUIRE(find_closing_delimiter(generic_sigs[0], 4, '<', '>')==12);
      // HashMap<String, Integer>
      REQUIRE(find_closing_delimiter(generic_sigs[1], 7, '<', '>')==23);
      // List<List<Int>>
      REQUIRE(find_closing_delimiter(generic_sigs[2], 4, '<', '>')==14);
      REQUIRE(find_closing_delimiter(generic_sigs[2], 9, '<', '>')==13);
      // List<List<List<Int>>>
      REQUIRE(find_closing_delimiter(generic_sigs[3], 14, '<', '>')==18);

      // TEST INVALID CASES

      // <
      REQUIRE(find_closing_delimiter(generic_sigs[4], 0, '<', '>')
              ==std::string::npos);
      // List<Integer
      REQUIRE(find_closing_delimiter(generic_sigs[5], 4, '<', '>')
              ==std::string::npos);
      // List<List<Integer>
      // (make sure that we still recognise the first delimiter correctly)
      REQUIRE(find_closing_delimiter(generic_sigs[6], 4, '<', '>')
              ==std::string::npos);
      REQUIRE(find_closing_delimiter(generic_sigs[6], 9, '<', '>')==17);
    }
  }
  GIVEN("Some bracketed functions")
  {
    std::vector<std::string> bracket_sigs{
      // Valid inputs
      "(Entry)",
      "Something(Else)",
      "(Nested(Bracket))",
      // Invalid inputs
      "(",
      "(Integer>",
    };
    WHEN("We check if the closing tag is recognised correctly")
    {
      // TEST VALID CASES

      // (Entry)
      REQUIRE(find_closing_delimiter(bracket_sigs[0], 0, '(', ')') == 6);
      // Something(Else)
      REQUIRE(find_closing_delimiter(bracket_sigs[1], 9, '(', ')') == 14);
      // (Nested(Bracket))
      REQUIRE(find_closing_delimiter(bracket_sigs[2], 0, '(', ')') == 16);
      REQUIRE(find_closing_delimiter(bracket_sigs[2], 7, '(', ')') == 15);

      // TEST INVALID CASES

      // (
      REQUIRE(
        find_closing_delimiter(bracket_sigs[3], 0, '(', ')') ==
        std::string::npos);
      // (Integer>
      REQUIRE(
        find_closing_delimiter(bracket_sigs[4], 0, '(', ')') ==
        std::string::npos);
    }
  }
}

SCENARIO("gather_full_class_name")
{
  GIVEN("Descriptor: class")
  {
    std::string descriptor = "LClassName;";
    THEN("Should get ClassName back")
    {
      const std::string &class_name = gather_full_class_name(descriptor);
      REQUIRE(class_name == "ClassName");
    }
  }
  GIVEN("Descriptor: A packaged class")
  {
    std::string descriptor = "Ljava/lang/Object;";
    THEN("Should get java.lang.Object back")
    {
      const std::string &class_name = gather_full_class_name(descriptor);
      REQUIRE(class_name == "java.lang.Object");
    }
  }
  GIVEN("Descriptor: A inner class")
  {
    std::string descriptor = "LOuter$Inner;";
    THEN("Should get Outer$Inner")
    {
      const std::string &class_name = gather_full_class_name(descriptor);
      REQUIRE(class_name == "Outer$Inner");
    }
  }
  GIVEN("Descriptor: a doubly nested inner class")
  {
    std::string descriptor = "LOuter$Inner$Inner2;";
    THEN("Should get Outer$Inner$Inner2")
    {
      const std::string &class_name = gather_full_class_name(descriptor);
      REQUIRE(class_name == "Outer$Inner$Inner2");
    }
  }

  GIVEN("Signature: An generic class")
  {
    std::string signature = "LClassName<TT;>;";
    THEN("Should get ClassName back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName");
    }
  }
  GIVEN("Signature: An inner class in a generic class")
  {
    std::string signature = "LClassName<TT;>.Inner;";
    THEN("Should get ClassName$Inner back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner");
    }
  }
  GIVEN("Signature: An generic inner class in a generic class")
  {
    std::string signature = "LClassName<TT;>.Inner<TV;>;";
    THEN("Should get ClassName$Inner back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner");
    }
  }
  GIVEN("Signature: A generic inner class in a non generic class")
  {
    std::string signature = "LClassName.Inner<TV;>;";
    THEN("Should get ClassName$Inner back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner");
    }
  }
  GIVEN(
    "Signature: A generic inner class in a non generic class in a non "
    "generic "
    "class")
  {
    std::string signature = "LClassName.Inner.Inner2<TT;>;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
  GIVEN(
    "Signature: A generic inner class in a generic class in a non generic "
    "class")
  {
    std::string signature = "LClassName.Inner<UU;>.Inner2<TT;>;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
  GIVEN(
    "Signature: A generic inner class in a generic class in a generic "
    "class")
  {
    std::string signature = "LClassName<TV;>.Inner<UU;>.Inner2<TT;>;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
  GIVEN(
    "Signature: A non-generic inner class in a generic class in a non "
    "generic "
    "class")
  {
    std::string signature = "LClassName.Inner<UU;>.Inner2;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
  GIVEN(
    "Signature: A non-generic inner class in a generic class in a generic "
    "class")
  {
    std::string signature = "LClassName<TV;>.Inner<UU;>.Inner2;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
  GIVEN(
    "Signature: A non-generic inner class in a non-generic class in a "
    "generic "
    "class")
  {
    std::string signature = "LClassName<TV;>.Inner.Inner2;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
  GIVEN(
    "Signature: A generic inner class in a non-generic class in a generic "
    "class")
  {
    std::string signature = "LClassName<TV;>.Inner.Inner2<TT;>;";
    THEN("Should get ClassName$Inner$Inner2 back")
    {
      const std::string &class_name = gather_full_class_name(signature);
      REQUIRE(class_name == "ClassName$Inner$Inner2");
    }
  }
}

SCENARIO("find_closing_semi_colon_for_reference_type", "[core][java_util_test]")
{
  GIVEN("A simple reference type")
  {
    std::string descriptor = "LA;";
    //                        |
    //                      012
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 2);
  }
  GIVEN("A generic reference type")
  {
    std::string descriptor = "LA<TT;>;";
    //                             |
    //                      01234567
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 7);
  }
  GIVEN("A generic reference type with multiple generic params")
  {
    std::string descriptor = "LA<TT;TU;>;";
    //                                |
    //                      01234567890
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 10);
  }
  GIVEN("A descriptor with multiple reference type")
  {
    std::string descriptor = "LA;LB;";
    //                        |
    //                      012
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 2);
  }
  GIVEN("A descriptor with multiple reference types parsing the second")
  {
    std::string descriptor = "LA;LB;";
    //                         | |
    //                      012345
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 3) == 5);
  }
  GIVEN("A descriptor inner class")
  {
    std::string descriptor = "LA$B;";
    //                          |
    //                      01234
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 4);
  }
  GIVEN("A signature inner class")
  {
    std::string descriptor = "LA.B;";
    //                          |
    //                      01234
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 4);
  }
  GIVEN("A inner class of a generic class")
  {
    std::string descriptor = "LA<TT;>.B;";
    //                               |
    //                      0123456789
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 9);
  }
  GIVEN("A inner class of a instantiated generic class")
  {
    std::string descriptor = "LA<LFoo;>.B;";
    //                                 |
    //                      012345678901
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 11);
  }
  GIVEN(
    "A signature with multiple references and an inner class of a generic "
    "class")
  {
    std::string descriptor = "LA<TT;>.B;LA<TT;>.B;";
    //                               |
    //                      0123456789
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 0) == 9);
  }
  GIVEN(
    "A signature with multiple references and an inner class of a generic "
    "class")
  {
    std::string descriptor = "LA<TT;>.B;LA<TT;>.B;";
    //                                |        |
    //                      01234567890123456789
    REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 10) == 19);
  }
}

SCENARIO("Test pretty printing auxiliary function", "[core][java_util_test]")
{
  using std::map;
  using std::string;

  WHEN("We have a series of cbmc internal java types")
  {
    // NOLINTNEXTLINE
    const map<string, string> types{
      // map<Input, Output>
      {"java::java.lang.Integer", "Integer"},
      {"java::CustomClass", "CustomClass"},
      {"java.lang.String", "String"},
      {"Hashmap", "Hashmap"},
      // We shouldn't prune types not imported in default import
      {"java.util.HashSet", "java.util.HashSet"}};

    THEN("We need to make sure that the types get pruned correctly.")
    {
      for(const auto &pair : types)
      {
        REQUIRE(pretty_print_java_type(pair.first) == pair.second);
      }
    }
  }
}

SCENARIO("Test symbol declarers.", "[core][java_util_test]")
{
  WHEN("We have a new symbol.")
  {
    symbolt symbol;

    THEN("The symbol has no declarer.")
    {
      REQUIRE(!declaring_class(symbol).has_value());
    }
  }

  WHEN("The declaring class of a symbol is set.")
  {
    const auto declaring_class = "java::java.lang.object";
    symbolt symbol;
    set_declaring_class(symbol, declaring_class);

    THEN("Getting the declaring class of a symbol returns the class set.")
    {
      REQUIRE(id2string(*::declaring_class(symbol)) == declaring_class);
    }
  }
}