2 GTSAM Copyright 2010-2020, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
6 See LICENSE for the license information
8 Tests for template_instantiator.
19 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
22 Constructor, ForwardDeclaration,
23 GlobalFunction, Include, Method,
24 Namespace, ReturnType, StaticMethod,
27 InstantiatedClass, InstantiatedConstructor, InstantiatedDeclaration,
28 InstantiatedGlobalFunction, InstantiatedMethod, InstantiatedStaticMethod,
29 InstantiationHelper, instantiate_args_list, instantiate_name,
30 instantiate_namespace, instantiate_return_type, instantiate_type,
35 """Tests for the InstantiationHelper class."""
37 """Test constructor."""
39 self.assertEqual(helper.instantiation_type, InstantiatedClass)
41 self.assertEqual(helper.instantiation_type, InstantiatedConstructor)
43 self.assertEqual(helper.instantiation_type, InstantiatedDeclaration)
45 self.assertEqual(helper.instantiation_type, InstantiatedGlobalFunction)
47 self.assertEqual(helper.instantiation_type, InstantiatedMethod)
49 self.assertEqual(helper.instantiation_type, InstantiatedStaticMethod)
52 """Test instantiate method."""
53 method = Method.rule.parseString(
"""
55 double method(const T x, const U& param);
57 cls = Class.rule.parseString(
"""
61 typenames = [
'T',
'U']
62 class_instantiations = [Typename.rule.parseString(
"string")[0]]
63 method_instantiations = [Typename.rule.parseString(
"double")[0]]
68 instantiated_methods = helper.instantiate([], method, typenames,
70 method_instantiations,
73 self.assertEqual(
len(instantiated_methods), 1)
74 args_list = instantiated_methods[0].args.list()
75 self.assertEqual(args_list[0].ctype.get_typename(),
'string')
76 self.assertEqual(args_list[1].ctype.get_typename(),
'double')
80 Test method for multilevel instantiation
81 i.e. instantiation at both the class and method level.
83 cls = Class.rule.parseString(
"""
87 double method1(const T x, const U& param);
95 class_instantiations = [Typename.rule.parseString(
"string")[0]]
100 instantiated_methods = helper.multilevel_instantiation(
101 cls.methods, typenames, parent)
102 self.assertEqual(
len(instantiated_methods), 2)
104 instantiated_methods[0].args.list()[0].ctype.get_typename(),
107 instantiated_methods[0].args.list()[1].ctype.get_typename(),
110 instantiated_methods[1].args.list()[0].ctype.get_typename(),
113 instantiated_methods[1].return_type.type1.get_typename(),
'int')
117 """Tests for the InstantiatedGlobalFunction class."""
119 original = GlobalFunction.rule.parseString(
"""
120 template<T={int}, R={double}>
121 R function(const T& x);
124 Typename.rule.parseString(
"int")[0],
125 Typename.rule.parseString(
"double")[0]
130 """Test constructor."""
131 self.assertIsInstance(self.
func, InstantiatedGlobalFunction)
132 self.assertIsInstance(self.
func.original, GlobalFunction)
133 self.assertEqual(self.
func.name,
"functionIntDouble")
134 self.assertEqual(
len(self.
func.args.list()), 1)
135 self.assertEqual(self.
func.args.list()[0].ctype.get_typename(),
"int")
136 self.assertEqual(self.
func.return_type.type1.get_typename(),
"double")
139 """Test to_cpp method."""
141 self.assertEqual(actual,
"function<int,double>")
145 """Tests for the InstantiatedConstructor class."""
147 constructor = Constructor.rule.parseString(
"""
149 Class(C x, const U& param);
152 Typename.rule.parseString(
"double")[0],
153 Typename.rule.parseString(
"string")[0]
158 """Test constructor."""
159 self.assertIsInstance(self.
constructor, InstantiatedConstructor)
160 self.assertIsInstance(self.
constructor.original, Constructor)
163 """Test the construct classmethod."""
164 constructor = Constructor.rule.parseString(
"""
166 Class(C x, const U& param);
168 c = Class.rule.parseString(
"""
172 class_instantiations = [Typename.rule.parseString(
"double")[0]]
173 method_instantiations = [Typename.rule.parseString(
"string")[0]]
174 typenames = [
'C',
'U']
177 constructor.args.list(),
178 typenames, class_instantiations + method_instantiations,
179 parent.cpp_typename())
181 instantiated_constructor = InstantiatedConstructor.construct(
182 constructor, typenames, class_instantiations,
183 method_instantiations, instantiated_args, parent)
184 self.assertEqual(instantiated_constructor.name,
"ClassDouble")
186 instantiated_constructor.args.list()[0].ctype.get_typename(),
189 instantiated_constructor.args.list()[1].ctype.get_typename(),
193 """Test the to_cpp method."""
195 self.assertEqual(actual,
"Class<double,string>")
199 """Tests for the InstantiatedMethod class."""
201 method = Method.rule.parseString(
"""
203 double method(const U& param);
205 instantiations = [Typename.rule.parseString(
"double")[0]]
209 """Test constructor."""
210 self.assertIsInstance(self.
method, InstantiatedMethod)
211 self.assertIsInstance(self.
method.original, Method)
212 self.assertEqual(self.
method.name,
"methodDouble")
215 """Test the construct classmethod."""
216 method = Method.rule.parseString(
"""
220 method_instantiations = [Typename.rule.parseString(
"double")[0]]
221 c = Class.rule.parseString(
"""
225 class_instantiations = [Typename.rule.parseString(
"string")[0]]
227 typenames = [
'T',
'U']
231 typenames, class_instantiations + method_instantiations,
232 parent.cpp_typename())
234 instantiated_method = InstantiatedMethod.construct(
235 method, typenames, class_instantiations, method_instantiations,
236 instantiated_args, parent)
237 self.assertEqual(instantiated_method.name,
"methodDouble")
239 instantiated_method.args.list()[0].ctype.get_typename(),
"double")
240 self.assertEqual(instantiated_method.return_type.type1.get_typename(),
244 """Test the to_cpp method."""
246 self.assertEqual(actual,
"method<double>")
250 """Tests for the InstantiatedStaticMethod class."""
252 static_method = StaticMethod.rule.parseString(
"""
254 static T staticMethod(const U& param);
256 instantiations = [Typename.rule.parseString(
"double")[0]]
261 """Test constructor."""
262 self.assertIsInstance(self.
static_method, InstantiatedStaticMethod)
263 self.assertIsInstance(self.
static_method.original, StaticMethod)
264 self.assertEqual(self.
static_method.name,
"staticMethodDouble")
267 """Test the construct classmethod."""
268 static_method = StaticMethod.rule.parseString(
"""
270 static T staticMethod(U& param);
272 method_instantiations = [Typename.rule.parseString(
"double")[0]]
273 c = Class.rule.parseString(
"""
277 class_instantiations = [Typename.rule.parseString(
"string")[0]]
279 typenames = [
'T',
'U']
282 static_method.args.list(),
283 typenames, class_instantiations + method_instantiations,
284 parent.cpp_typename())
286 instantiated_static_method = InstantiatedStaticMethod.construct(
287 static_method, typenames, class_instantiations,
288 method_instantiations, instantiated_args, parent)
289 self.assertEqual(instantiated_static_method.name,
"staticMethodDouble")
291 instantiated_static_method.args.list()[0].ctype.get_typename(),
294 instantiated_static_method.return_type.type1.get_typename(),
298 """Test the to_cpp method."""
300 self.assertEqual(actual,
"staticMethod<double>")
304 """Tests for the InstantiatedClass class."""
306 cl = Class.rule.parseString(
"""
313 static T staticMethod(const S& s);
316 T method(const M& m);
318 T operator*(T other) const;
323 class_instantiations = [Typename.rule.parseString(
'string')[0]]
325 Typename.rule.parseString(
'int')[0],
326 Typename.rule.parseString(
'char')[0],
327 Typename.rule.parseString(
'double')[0],
333 """Test constructor."""
334 self.assertIsInstance(self.
cl, InstantiatedClass)
335 self.assertIsInstance(self.
cl.original, Class)
336 self.assertEqual(self.
cl.name,
"FooString")
339 """Test instantiate_ctors method."""
341 self.assertEqual(
len(ctors), 1)
342 self.assertEqual(ctors[0].name,
"FooString")
343 self.assertEqual(ctors[0].args.list()[0].ctype.get_typename(),
"int")
346 """Test instantiate_static_methods method."""
347 static_methods = self.
cl.instantiate_static_methods(self.
typenames)
348 self.assertEqual(
len(static_methods), 1)
349 self.assertEqual(static_methods[0].name,
"staticMethodChar")
350 self.assertEqual(static_methods[0].args.list()[0].ctype.get_typename(),
352 self.assertEqual(static_methods[0].return_type.type1.get_typename(),
356 """Test instantiate_methods method."""
357 methods = self.
cl.instantiate_methods(self.
typenames)
358 self.assertEqual(
len(methods), 1)
359 self.assertEqual(methods[0].name,
"methodDouble")
360 self.assertEqual(methods[0].args.list()[0].ctype.get_typename(),
362 self.assertEqual(methods[0].return_type.type1.get_typename(),
"string")
365 """Test instantiate_operators method."""
366 operators = self.
cl.instantiate_operators(self.
typenames)
367 self.assertEqual(
len(operators), 1)
368 self.assertEqual(operators[0].operator,
"*")
369 self.assertEqual(operators[0].args.list()[0].ctype.get_typename(),
371 self.assertEqual(operators[0].return_type.type1.get_typename(),
375 """Test instantiate_properties method."""
376 properties = self.
cl.instantiate_properties(self.
typenames)
377 self.assertEqual(
len(properties), 1)
378 self.assertEqual(properties[0].name,
"prop")
379 self.assertEqual(properties[0].ctype.get_typename(),
"string")
382 """Test cpp_typename method."""
383 actual = self.
cl.cpp_typename()
384 self.assertEqual(actual.name,
"Foo<string>")
387 """Test to_cpp method."""
389 self.assertEqual(actual,
"Foo<string>")
393 """Tests for the InstantiatedDeclaration class."""
396 forward_declaration = ForwardDeclaration.rule.parseString(
"""
399 instantiations = [Typename.rule.parseString(
"double")[0]]
401 forward_declaration, instantiations=instantiations)
404 """Test constructor."""
405 self.assertIsInstance(self.
declaration, InstantiatedDeclaration)
406 self.assertIsInstance(self.
declaration.original, ForwardDeclaration)
407 self.assertEqual(self.
declaration.instantiations[0].name,
"double")
410 """Test to_cpp method."""
412 self.assertEqual(cpp,
"FooBar<double>")
417 Test overall template instantiation and the functions in the module.
420 """Test is_scoped_template."""
422 template_typenames = [
'T']
423 str_arg_typename =
"double"
425 template_typenames, str_arg_typename)
426 self.assertFalse(scoped_template)
427 self.assertEqual(scoped_idx, -1)
430 template_typenames = [
'T']
431 str_arg_typename =
"gtsam::Matrix"
433 template_typenames, str_arg_typename)
434 self.assertFalse(scoped_template)
435 self.assertEqual(scoped_idx, -1)
438 template_typenames = [
'T']
439 str_arg_typename =
"T::Value"
441 template_typenames, str_arg_typename)
442 self.assertEqual(scoped_template,
"T")
443 self.assertEqual(scoped_idx, 0)
445 template_typenames = [
'U',
'T']
446 str_arg_typename =
"T::Value"
448 template_typenames, str_arg_typename)
449 self.assertEqual(scoped_template,
"T")
450 self.assertEqual(scoped_idx, 1)
453 """Test for instantiate_type."""
454 arg = Argument.rule.parseString(
"const T x")[0]
455 template_typenames = [
"T"]
456 instantiations = [Typename.rule.parseString(
"double")[0]]
457 cpp_typename =
"ExampleClass"
460 instantiations=instantiations,
461 cpp_typename=cpp_typename,
462 instantiated_class=
None)
464 new_typename = new_type.typename
465 self.assertEqual(new_typename.name,
"double")
466 self.assertEqual(new_typename.instantiated_name(),
"double")
469 """Test for instantiate_args_list."""
470 args = ArgumentList.rule.parseString(
"T x, double y, string z")[0]
471 args_list = args.list()
472 template_typenames = [
'T']
473 instantiations = [Typename.rule.parseString(
"double")[0]]
478 cpp_typename=
"ExampleClass")
480 self.assertEqual(instantiated_args_list[0].ctype.get_typename(),
483 args = ArgumentList.rule.parseString(
"T x, U y, string z")[0]
484 args_list = args.list()
485 template_typenames = [
'T',
'U']
487 Typename.rule.parseString(
"double")[0],
488 Typename.rule.parseString(
"Matrix")[0]
494 cpp_typename=
"ExampleClass")
495 self.assertEqual(instantiated_args_list[0].ctype.get_typename(),
497 self.assertEqual(instantiated_args_list[1].ctype.get_typename(),
500 args = ArgumentList.rule.parseString(
"T x, U y, T z")[0]
501 args_list = args.list()
502 template_typenames = [
'T',
'U']
504 Typename.rule.parseString(
"double")[0],
505 Typename.rule.parseString(
"Matrix")[0]
511 cpp_typename=
"ExampleClass")
512 self.assertEqual(instantiated_args_list[0].ctype.get_typename(),
514 self.assertEqual(instantiated_args_list[1].ctype.get_typename(),
516 self.assertEqual(instantiated_args_list[2].ctype.get_typename(),
520 """Test for instantiate_return_type."""
521 return_type = ReturnType.rule.parseString(
"T")[0]
522 template_typenames = [
'T']
523 instantiations = [Typename.rule.parseString(
"double")[0]]
528 cpp_typename=
"ExampleClass")
530 self.assertEqual(instantiated_return_type.type1.get_typename(),
533 return_type = ReturnType.rule.parseString(
"pair<T, U>")[0]
534 template_typenames = [
'T',
'U']
536 Typename.rule.parseString(
"double")[0],
537 Typename.rule.parseString(
"char")[0],
543 cpp_typename=
"ExampleClass")
545 self.assertEqual(instantiated_return_type.type1.get_typename(),
547 self.assertEqual(instantiated_return_type.type2.get_typename(),
"char")
550 """Test for instantiate_name."""
551 instantiations = [Typename.rule.parseString(
"Man")[0]]
553 self.assertEqual(instantiated_name,
"IronMan")
556 """Test for instantiate_namespace."""
557 namespace = Namespace.rule.parseString(
"""
559 #include <gtsam/nonlinear/Values.h>
560 template<T={gtsam::Basis}>
562 Values(const T& other);
564 template<V={Vector, Matrix}>
565 void insert(size_t j, V x);
568 static S staticMethod(const S& s);
574 self.assertEqual(instantiated_namespace.name,
"gtsam")
575 self.assertEqual(instantiated_namespace.parent,
"")
577 self.assertEqual(instantiated_namespace.content[0].header,
578 "gtsam/nonlinear/Values.h")
579 self.assertIsInstance(instantiated_namespace.content[0], Include)
581 self.assertEqual(instantiated_namespace.content[1].name,
"ValuesBasis")
582 self.assertIsInstance(instantiated_namespace.content[1], Class)
584 self.assertIsInstance(instantiated_namespace.content[1].ctors[0],
586 self.assertEqual(instantiated_namespace.content[1].ctors[0].name,
589 self.assertIsInstance(instantiated_namespace.content[1].methods[0],
591 self.assertIsInstance(instantiated_namespace.content[1].methods[1],
593 self.assertEqual(instantiated_namespace.content[1].methods[0].name,
595 self.assertEqual(instantiated_namespace.content[1].methods[1].name,
598 self.assertIsInstance(
599 instantiated_namespace.content[1].static_methods[0], StaticMethod)
601 instantiated_namespace.content[1].static_methods[0].name,
602 "staticMethodDouble")
605 if __name__ ==
'__main__':