-
Notifications
You must be signed in to change notification settings - Fork 42
/
type_traits_tester.py
452 lines (383 loc) · 16.4 KB
/
type_traits_tester.py
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Copyright 2014-2016 Insight Software Consortium.
# Copyright 2004-2008 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt
import unittest
import autoconfig
import parser_test_case
from pygccxml import parser
from pygccxml import declarations
from pygccxml import utils
class Test(parser_test_case.parser_test_case_t):
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
declarations = None
def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = 'type_traits.hpp'
self.declarations = None
def setUp(self):
if not Test.declarations:
Test.declarations = parser.parse([self.header], self.config)
self.declarations = Test.declarations
def __test_type_category(self, ns_name, controller):
ns_control = declarations.find_declaration(
self.declarations,
decl_type=declarations.namespace_t,
name=ns_name)
self.assertTrue(ns_control, "unable to find '%s' namespace" % ns_name)
ns_yes = declarations.find_declaration(
ns_control,
decl_type=declarations.namespace_t,
name='yes')
self.assertTrue(ns_yes, "unable to find 'yes' namespace")
ns_no = declarations.find_declaration(
ns_control,
decl_type=declarations.namespace_t,
name='no')
self.assertTrue(ns_no, "unable to find 'no' namespace")
er = 'for type "%s" the answer to the question "%s" should be True'
for decl in ns_yes.declarations:
if isinstance(decl, declarations.variable_t):
self.assertTrue(
controller(decl.decl_type),
er % (decl.decl_type.decl_string, ns_name))
elif isinstance(decl, declarations.calldef_t) and \
decl.name.startswith('test_'):
continue
else:
self.assertTrue(
controller(decl), er % (decl.decl_string, ns_name))
er = 'for type "%s" the answer to the question "%s" should be False'
for decl in ns_no.declarations:
if isinstance(decl, declarations.calldef_t) and \
decl.name.startswith('test_'):
continue
if ("CastXML" in utils.xml_generator and
utils.xml_output_version < 1.138 and
decl.name in ['const_item', 'const_container']):
# Skip this test to workaround CastXML bug.
# See https://github.com/CastXML/CastXML/issues/55
continue
self.assertFalse(
controller(decl),
er % (decl.decl_string, ns_name))
def __test_type_transformation(self, ns_name, transformer):
ns_control = declarations.find_declaration(
self.declarations,
decl_type=declarations.namespace_t,
name=ns_name)
self.assertTrue(ns_control, "unable to find '%s' namespace" % ns_name)
ns_before = declarations.find_declaration(
ns_control,
decl_type=declarations.namespace_t,
name='before')
self.assertTrue(ns_before, "unable to find 'before' namespace")
ns_after = declarations.find_declaration(
ns_control,
decl_type=declarations.namespace_t,
name='after')
self.assertTrue(ns_after, "unable to find 'after' namespace")
for tbefore in ns_before.declarations:
tafter = declarations.find_declaration(
ns_after,
name=tbefore.name)
self.assertTrue(
tafter,
"unable to find transformed type definition for type '%s'" %
tbefore.decl_string)
transformed = transformer(tbefore)
self.assertTrue(
declarations.is_same(
transformed,
tafter),
("there is a difference between expected type '{0}' " +
"and result '{1}'. typedef name: {2}").format(
declarations.remove_declarated(tafter).decl_string,
declarations.remove_declarated(transformed).decl_string,
tbefore.decl_string))
def test_is_enum(self):
self.__test_type_category('is_enum', declarations.is_enum)
def test_is_void(self):
self.__test_type_category('is_void', declarations.is_void)
def test_is_integral(self):
self.__test_type_category('is_integral', declarations.is_integral)
def test_is_pointer(self):
self.__test_type_category('is_pointer', declarations.is_pointer)
def test_is_const(self):
self.__test_type_category('is_const', declarations.is_const)
def test_is_volatile(self):
self.__test_type_category('is_volatile', declarations.is_volatile)
def test_is_reference(self):
self.__test_type_category('is_reference', declarations.is_reference)
def test_is_floating_point(self):
self.__test_type_category(
'is_floating_point',
declarations.is_floating_point)
def test_is_array(self):
self.__test_type_category('is_array', declarations.is_array)
def test_is_fundamental(self):
self.__test_type_category(
'is_fundamental',
declarations.is_fundamental)
def test_is_noncopyable(self):
self.__test_type_category(
'is_noncopyable',
declarations.is_noncopyable)
def test_is_std_ostream(self):
self.__test_type_category(
'is_std_ostream',
declarations.is_std_ostream)
def test_is_std_wostream(self):
self.__test_type_category(
'is_std_wostream',
declarations.is_std_wostream)
def test_is_calldef_pointer(self):
self.__test_type_category(
'is_calldef_pointer',
declarations.is_calldef_pointer)
def test_has_trivial_constructor(self):
self.__test_type_category(
'has_trivial_constructor',
declarations.has_trivial_constructor)
def test_has_public_constructor(self):
self.__test_type_category(
'has_public_constructor',
declarations.has_public_constructor)
def test_has_public_destructor(self):
self.__test_type_category(
'has_public_destructor',
declarations.has_public_destructor)
def test_has_any_non_copyconstructor(self):
self.__test_type_category(
'has_any_non_copyconstructor',
declarations.has_any_non_copyconstructor)
def test_has_copy_constructor(self):
self.__test_type_category(
'has_copy_constructor',
declarations.has_copy_constructor)
def test_is_base_and_derived(self):
ns = declarations.find_declaration(
self.declarations,
decl_type=declarations.namespace_t,
name='is_base_and_derived')
self.assertTrue(ns, "unable to find 'is_base_and_derived' namespace")
base = declarations.find_declaration(
ns.declarations,
decl_type=declarations.class_t,
name='base')
derived = declarations.find_declaration(
ns.declarations,
decl_type=declarations.class_t,
name='derived')
self.assertTrue(
base and derived and declarations.is_base_and_derived(
base,
derived))
self.assertTrue(
base and derived and
declarations.is_base_and_derived(base, (derived, derived)))
unrelated1 = declarations.find_declaration(
ns.declarations,
decl_type=declarations.class_t,
name='unrelated1')
unrelated2 = declarations.find_declaration(
ns.declarations,
decl_type=declarations.class_t,
name='unrelated2')
self.assertTrue(
base and derived and not declarations.is_base_and_derived(
unrelated1,
unrelated2))
def test_is_same(self):
self.assertTrue(
declarations.is_same(
declarations.int_t,
declarations.int_t))
self.assertFalse(
declarations.is_same(
declarations.int_t,
declarations.float_t))
def test_remove_const(self):
self.__test_type_transformation(
'remove_const',
declarations.remove_const)
def test_remove_reference(self):
self.__test_type_transformation(
'remove_reference',
declarations.remove_reference)
def test_remove_volatile(self):
self.__test_type_transformation(
'remove_volatile',
declarations.remove_volatile)
def test_remove_cv(self):
self.__test_type_transformation('remove_cv', declarations.remove_cv)
def test_remove_pointer(self):
self.__test_type_transformation(
'remove_pointer',
declarations.remove_pointer)
def test_is_unary_binary_operator(self):
operator_not = declarations.find_declaration(
self.declarations,
decl_type=declarations.operator_t,
fullname='::is_unary_operator::dummy::operator!')
self.assertTrue(operator_not, 'operator! was not found')
self.assertTrue(
declarations.is_unary_operator(operator_not),
'operator! should be idenitified as unary operator')
self.assertTrue(
not declarations.is_binary_operator(operator_not),
'operator! should be idenitified as unary operator')
operator_class_p = declarations.find_declaration(
self.declarations,
decl_type=declarations.operator_t,
fullname='::is_unary_operator::dummy::operator+')
self.assertTrue(operator_class_p, 'operator+ was not found')
self.assertTrue(
not declarations.is_unary_operator(operator_class_p),
'operator+ should be idenitified as binary operator')
self.assertTrue(
declarations.is_binary_operator(operator_class_p),
'operator! should be idenitified as binary operator')
operator_class_pp = declarations.find_declaration(
self.declarations,
decl_type=declarations.operator_t,
fullname='::is_unary_operator::dummy::operator++')
self.assertTrue(operator_class_pp, 'operator++ was not found')
self.assertTrue(
declarations.is_unary_operator(operator_class_pp),
'operator++ should be idenitified as unary operator')
self.assertTrue(
not declarations.is_binary_operator(operator_class_pp),
'operator++ should be idenitified as unary operator')
operator_pp = declarations.find_declaration(
self.declarations,
decl_type=declarations.operator_t,
fullname='::is_unary_operator::operator++')
self.assertTrue(operator_pp, 'operator++ was not found')
self.assertTrue(
declarations.is_unary_operator(operator_pp),
'operator++ should be idenitified as unary operator')
self.assertTrue(
not declarations.is_binary_operator(operator_pp),
'operator++ should be idenitified as unary operator')
operator_mm = declarations.find_declaration(
self.declarations,
decl_type=declarations.operator_t,
fullname='::is_unary_operator::operator*')
self.assertTrue(operator_mm, 'operator-- was not found')
self.assertTrue(
not declarations.is_unary_operator(operator_mm),
'operator-- should be idenitified as binary operator')
self.assertTrue(
declarations.is_binary_operator(operator_mm),
'operator-- should be idenitified as binray operator')
operator_pe = declarations.find_declaration(
self.declarations,
decl_type=declarations.operator_t,
fullname='::is_unary_operator::operator+=')
self.assertTrue(operator_pe, 'operator+= was not found')
self.assertTrue(
not declarations.is_unary_operator(operator_pe),
'operator+= should be idenitified as binary operator')
self.assertTrue(
declarations.is_binary_operator(operator_pe),
'operator+= should be idenitified as binray operator')
def __is_convertible_impl(self, decl):
defs = decl.bases[0].related_class.declarations
source_type = declarations.find_declaration(defs, name='source_type')
target_type = declarations.find_declaration(defs, name='target_type')
expected_type = declarations.find_declaration(
defs,
name='expected',
decl_type=declarations.enumeration_t)
expected_value = bool(expected_type.get_name2value_dict()['value'])
self.assertTrue(
expected_value == declarations.is_convertible(
source_type,
target_type),
'Check conversion failed for ' +
decl.name)
def test_is_convertible(self):
ns_is_convertible = declarations.find_declaration(
self.declarations,
decl_type=declarations.namespace_t,
name="is_convertible")
self.assertTrue(
ns_is_convertible,
"namespace is_convertible was not found")
for tester in [
decl for decl in ns_is_convertible.declarations if
decl.name.startswith('x')]:
self.__is_convertible_impl(tester)
class missing_decls_tester_t(unittest.TestCase):
def test(self):
config = autoconfig.cxx_parsers_cfg.gccxml
code = "struct const_item{ const int values[10]; };"
global_ns = parser.parse_string(code, config)[0]
ci = global_ns.class_('const_item')
if ("CastXML" not in utils.xml_generator or
utils.xml_output_version >= 1.138):
# Prior to version 1.138, CastXML would incorrect create a default
# constructor definition.
# See https://github.com/CastXML/CastXML/issues/55
# Copy constructor, destructor, variable
self.assertEqual(len(ci.declarations), 3)
# class tester_diff_t( parser_test_case.parser_test_case_t ):
# COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
# declarations = None
# def __init__(self, *args ):
# parser_test_case.parser_test_case_t.__init__( self, *args )
# self.header = 'type_traits.hpp'
# self.declarations = None
# def setUp(self):
# if not Test.declarations:
# Test.declarations = parser.parse([self.header], self.config)
# self.declarations = Test.declarations
# def test( self ):
# x = declarations.find_declaration( self.declarations
# , decl_type=declarations.typedef_t
# , name="s2s_multimap_type" )
# print declarations.is_noncopyable( x)
# declarations.print_declarations(
# [declarations.class_traits.get_declaration( x )] )
class class_traits_tester_t(unittest.TestCase):
def test_get_declaration(self):
code = """
namespace A{
struct B{
int c;
};
template <class T>
struct C: public T{
int d;
};
template <class T>
struct D{
int dD;
};
typedef C<B> easy;
typedef D<easy> Deasy;
inline void instantiate(){
int val = sizeof(easy);
}
}
"""
global_ns = parser.parse_string(
code,
autoconfig.cxx_parsers_cfg.gccxml)
global_ns = declarations.get_global_namespace(global_ns)
easy = global_ns.typedef('easy')
declarations.class_traits.get_declaration(easy)
deasy = global_ns.typedef('Deasy')
d_a = declarations.class_traits.get_declaration(deasy)
self.assertTrue(isinstance(d_a, declarations.class_types))
def create_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(class_traits_tester_t))
suite.addTest(unittest.makeSuite(Test))
suite.addTest(unittest.makeSuite(missing_decls_tester_t))
return suite
def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())
if __name__ == "__main__":
run_suite()