test_interface_parser.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2020, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Tests for interface_parser.
9 
10 Author: Varun Agrawal
11 """
12 
13 # pylint: disable=import-error,wrong-import-position
14 
15 import os
16 import sys
17 import unittest
18 
19 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
20 
21 from gtwrap.interface_parser import (ArgumentList, Class, Constructor, Enum,
22  Enumerator, ForwardDeclaration,
23  GlobalFunction, Include, Method, Module,
24  Namespace, Operator, ReturnType,
25  StaticMethod, TemplatedType, Type,
26  TypedefTemplateInstantiation, Typename,
27  Variable)
28 from gtwrap.template_instantiator.classes import InstantiatedClass
29 
30 
31 class TestInterfaceParser(unittest.TestCase):
32  """Test driver for all classes in interface_parser.py."""
33 
34  def test_typename(self):
35  """Test parsing of Typename."""
36  typename = Typename.rule.parseString("size_t")[0]
37  self.assertEqual("size_t", typename.name)
38 
39  def test_basic_type(self):
40  """Tests for BasicType."""
41  # Check basic type
42  t = Type.rule.parseString("int x")[0]
43  self.assertEqual("int", t.typename.name)
44  self.assertTrue(t.is_basic)
45 
46  # Check const
47  t = Type.rule.parseString("const int x")[0]
48  self.assertEqual("int", t.typename.name)
49  self.assertTrue(t.is_basic)
50  self.assertTrue(t.is_const)
51 
52  # Check shared pointer
53  t = Type.rule.parseString("int* x")[0]
54  self.assertEqual("int", t.typename.name)
55  self.assertTrue(t.is_shared_ptr)
56 
57  # Check raw pointer
58  t = Type.rule.parseString("int@ x")[0]
59  self.assertEqual("int", t.typename.name)
60  self.assertTrue(t.is_ptr)
61 
62  # Check reference
63  t = Type.rule.parseString("int& x")[0]
64  self.assertEqual("int", t.typename.name)
65  self.assertTrue(t.is_ref)
66 
67  # Check const reference
68  t = Type.rule.parseString("const int& x")[0]
69  self.assertEqual("int", t.typename.name)
70  self.assertTrue(t.is_const)
71  self.assertTrue(t.is_ref)
72 
73  def test_custom_type(self):
74  """Tests for CustomType."""
75  # Check qualified type
76  t = Type.rule.parseString("gtsam::Pose3 x")[0]
77  self.assertEqual("Pose3", t.typename.name)
78  self.assertEqual(["gtsam"], t.typename.namespaces)
79  self.assertTrue(not t.is_basic)
80 
81  # Check const
82  t = Type.rule.parseString("const gtsam::Pose3 x")[0]
83  self.assertEqual("Pose3", t.typename.name)
84  self.assertEqual(["gtsam"], t.typename.namespaces)
85  self.assertTrue(t.is_const)
86 
87  # Check shared pointer
88  t = Type.rule.parseString("gtsam::Pose3* x")[0]
89  self.assertEqual("Pose3", t.typename.name)
90  self.assertEqual(["gtsam"], t.typename.namespaces)
91  self.assertTrue(t.is_shared_ptr)
92  self.assertEqual("std::shared_ptr<gtsam::Pose3>", t.to_cpp())
93 
94  # Check raw pointer
95  t = Type.rule.parseString("gtsam::Pose3@ x")[0]
96  self.assertEqual("Pose3", t.typename.name)
97  self.assertEqual(["gtsam"], t.typename.namespaces)
98  self.assertTrue(t.is_ptr)
99 
100  # Check reference
101  t = Type.rule.parseString("gtsam::Pose3& x")[0]
102  self.assertEqual("Pose3", t.typename.name)
103  self.assertEqual(["gtsam"], t.typename.namespaces)
104  self.assertTrue(t.is_ref)
105 
106  # Check const reference
107  t = Type.rule.parseString("const gtsam::Pose3& x")[0]
108  self.assertEqual("Pose3", t.typename.name)
109  self.assertEqual(["gtsam"], t.typename.namespaces)
110  self.assertTrue(t.is_const)
111  self.assertTrue(t.is_ref)
112 
114  """Test a templated type."""
115  t = TemplatedType.rule.parseString("Eigen::Matrix<double, 3, 4>")[0]
116  self.assertEqual("Matrix", t.typename.name)
117  self.assertEqual(["Eigen"], t.typename.namespaces)
118  self.assertEqual("double", t.typename.instantiations[0].name)
119  self.assertEqual("3", t.typename.instantiations[1].name)
120  self.assertEqual("4", t.typename.instantiations[2].name)
121 
122  t = TemplatedType.rule.parseString(
123  "gtsam::PinholeCamera<gtsam::Cal3S2>")[0]
124  self.assertEqual("PinholeCamera", t.typename.name)
125  self.assertEqual(["gtsam"], t.typename.namespaces)
126  self.assertEqual("Cal3S2", t.typename.instantiations[0].name)
127  self.assertEqual(["gtsam"], t.typename.instantiations[0].namespaces)
128 
129  t = TemplatedType.rule.parseString("PinholeCamera<Cal3S2*>")[0]
130  self.assertEqual("PinholeCamera", t.typename.name)
131  self.assertEqual("Cal3S2", t.typename.instantiations[0].name)
132  self.assertTrue(t.template_params[0].is_shared_ptr)
133 
135  """Test no arguments."""
136  empty_args = ArgumentList.rule.parseString("")[0]
137  self.assertEqual(0, len(empty_args))
138 
140  """Test arguments list for a method/function."""
141  arg_string = "int a, C1 c1, C2& c2, C3* c3, "\
142  "const C4 c4, const C5& c5,"\
143  "const C6* c6"
144  args = ArgumentList.rule.parseString(arg_string)[0]
145 
146  self.assertEqual(7, len(args.list()))
147  self.assertEqual(['a', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6'],
148  args.names())
149 
151  """
152  Test arguments list where the arguments are qualified with `const`
153  and can be either raw pointers, shared pointers or references.
154  """
155  arg_string = "double x1, double* x2, double& x3, double@ x4, " \
156  "const double x5, const double* x6, const double& x7, const double@ x8"
157  args = ArgumentList.rule.parseString(arg_string)[0].list()
158  self.assertEqual(8, len(args))
159  self.assertFalse(args[1].ctype.is_ptr and args[1].ctype.is_shared_ptr
160  and args[1].ctype.is_ref)
161  self.assertTrue(args[1].ctype.is_shared_ptr)
162  self.assertTrue(args[2].ctype.is_ref)
163  self.assertTrue(args[3].ctype.is_ptr)
164  self.assertTrue(args[4].ctype.is_const)
165  self.assertTrue(args[5].ctype.is_shared_ptr and args[5].ctype.is_const)
166  self.assertTrue(args[6].ctype.is_ref and args[6].ctype.is_const)
167  self.assertTrue(args[7].ctype.is_ptr and args[7].ctype.is_const)
168 
170  """Test arguments list where the arguments can be templated."""
171  arg_string = "std::pair<string, double> steps, vector<T*> vector_of_pointers"
172  args = ArgumentList.rule.parseString(arg_string)[0]
173  args_list = args.list()
174  self.assertEqual(2, len(args_list))
175  self.assertEqual("std::pair<string, double>",
176  args_list[0].ctype.to_cpp())
177  self.assertEqual("vector<std::shared_ptr<T>>",
178  args_list[1].ctype.to_cpp())
179 
181  """Tests any expression that is a valid default argument"""
182  args = ArgumentList.rule.parseString("""
183  string c = "", int z = 0, double z2 = 0.0, bool f = false,
184  string s="hello"+"goodbye", char c='a', int a=3,
185  int b, double pi = 3.1415""")[0].list()
186 
187  # Test for basic types
188  self.assertEqual(args[0].default, '""')
189  self.assertEqual(args[1].default, '0')
190  self.assertEqual(args[2].default, '0.0')
191  self.assertEqual(args[3].default, "false")
192  self.assertEqual(args[4].default, '"hello"+"goodbye"')
193  self.assertEqual(args[5].default, "'a'")
194  self.assertEqual(args[6].default, '3')
195  # No default argument should set `default` to None
196  self.assertIsNone(args[7].default)
197  self.assertEqual(args[8].default, '3.1415')
198 
199  arg0 = 'gtsam::DefaultKeyFormatter'
200  arg1 = 'std::vector<size_t>()'
201  arg2 = '{1, 2}'
202  arg3 = '[&c1, &c2](string s=5, int a){return s+"hello"+a+c1+c2;}'
203  arg4 = 'gtsam::Pose3()'
204  arg5 = 'Factor<gtsam::Pose3, gtsam::Point3>()'
205  arg6 = 'gtsam::Point3(1, 2, 3)'
206  arg7 = 'ns::Class<T, U>(3, 2, 1, "name")'
207 
208  argument_list = """
209  gtsam::KeyFormatter kf = {arg0},
210  std::vector<size_t> v = {arg1},
211  std::vector<size_t> l = {arg2},
212  gtsam::KeyFormatter lambda = {arg3},
213  gtsam::Pose3 p = {arg4},
214  Factor<gtsam::Pose3, gtsam::Point3> x = {arg5},
215  gtsam::Point3 x = {arg6},
216  ns::Class<T, U> obj = {arg7}
217  """.format(arg0=arg0,
218  arg1=arg1,
219  arg2=arg2,
220  arg3=arg3,
221  arg4=arg4,
222  arg5=arg5,
223  arg6=arg6,
224  arg7=arg7)
225  args = ArgumentList.rule.parseString(argument_list)[0].list()
226 
227  # Test non-basic type
228  self.assertEqual(args[0].default, arg0)
229  # Test templated type
230  self.assertEqual(args[1].default, arg1)
231  self.assertEqual(args[2].default, arg2)
232  self.assertEqual(args[3].default, arg3)
233  self.assertEqual(args[4].default, arg4)
234  self.assertEqual(args[5].default, arg5)
235  self.assertEqual(args[6].default, arg6)
236  # Test for default argument with multiple templates and params
237  self.assertEqual(args[7].default, arg7)
238 
239  def test_return_type(self):
240  """Test ReturnType"""
241  # Test void
242  return_type = ReturnType.rule.parseString("void")[0]
243  self.assertEqual("void", return_type.type1.typename.name)
244  self.assertTrue(return_type.type1.is_basic)
245 
246  # Test basic type
247  return_type = ReturnType.rule.parseString("size_t")[0]
248  self.assertEqual("size_t", return_type.type1.typename.name)
249  self.assertTrue(not return_type.type2)
250  self.assertTrue(return_type.type1.is_basic)
251 
252  # Test with qualifiers
253  return_type = ReturnType.rule.parseString("int&")[0]
254  self.assertEqual("int", return_type.type1.typename.name)
255  self.assertTrue(return_type.type1.is_basic
256  and return_type.type1.is_ref)
257 
258  return_type = ReturnType.rule.parseString("const int")[0]
259  self.assertEqual("int", return_type.type1.typename.name)
260  self.assertTrue(return_type.type1.is_basic
261  and return_type.type1.is_const)
262 
263  # Test pair return
264  return_type = ReturnType.rule.parseString("pair<char, int>")[0]
265  self.assertEqual("char", return_type.type1.typename.name)
266  self.assertEqual("int", return_type.type2.typename.name)
267 
268  return_type = ReturnType.rule.parseString("pair<Test ,Test*>")[0]
269  self.assertEqual("Test", return_type.type1.typename.name)
270  self.assertEqual("Test", return_type.type2.typename.name)
271  self.assertTrue(return_type.type2.is_shared_ptr)
272 
273  def test_method(self):
274  """Test for a class method."""
275  ret = Method.rule.parseString("int f();")[0]
276  self.assertEqual("f", ret.name)
277  self.assertEqual(0, len(ret.args))
278  self.assertTrue(not ret.is_const)
279 
280  ret = Method.rule.parseString("int f() const;")[0]
281  self.assertEqual("f", ret.name)
282  self.assertEqual(0, len(ret.args))
283  self.assertTrue(ret.is_const)
284 
285  ret = Method.rule.parseString(
286  "int f(const int x, const Class& c, Class* t) const;")[0]
287  self.assertEqual("f", ret.name)
288  self.assertEqual(3, len(ret.args))
289 
290  ret = Method.rule.parseString(
291  "pair<First ,Second*> create_MixedPtrs();")[0]
292  self.assertEqual("create_MixedPtrs", ret.name)
293  self.assertEqual(0, len(ret.args))
294  self.assertEqual("First", ret.return_type.type1.typename.name)
295  self.assertEqual("Second", ret.return_type.type2.typename.name)
296 
298  """Test for static methods."""
299  ret = StaticMethod.rule.parseString("static int f();")[0]
300  self.assertEqual("f", ret.name)
301  self.assertEqual(0, len(ret.args))
302 
303  ret = StaticMethod.rule.parseString(
304  "static int f(const int x, const Class& c, Class* t);")[0]
305  self.assertEqual("f", ret.name)
306  self.assertEqual(3, len(ret.args))
307 
308  def test_constructor(self):
309  """Test for class constructor."""
310  ret = Constructor.rule.parseString("f();")[0]
311  self.assertEqual("f", ret.name)
312  self.assertEqual(0, len(ret.args))
313 
314  ret = Constructor.rule.parseString(
315  "f(const int x, const Class& c, Class* t);")[0]
316  self.assertEqual("f", ret.name)
317  self.assertEqual(3, len(ret.args))
318 
319  ret = Constructor.rule.parseString(
320  """ForwardKinematics(const gtdynamics::Robot& robot,
321  const string& start_link_name, const string& end_link_name,
322  const gtsam::Values& joint_angles,
323  const gtsam::Pose3& l2Tp = gtsam::Pose3());""")[0]
324  self.assertEqual("ForwardKinematics", ret.name)
325  self.assertEqual(5, len(ret.args))
326  self.assertEqual("gtsam::Pose3()", ret.args.list()[4].default)
327 
329  """Test for templated class constructor."""
330  f = """
331  template<T = {double, int}>
332  Class();
333  """
334  ret = Constructor.rule.parseString(f)[0]
335  self.assertEqual("Class", ret.name)
336  self.assertEqual(0, len(ret.args))
337 
338  f = """
339  template<T = {double, int}>
340  Class(const T& name);
341  """
342  ret = Constructor.rule.parseString(f)[0]
343  self.assertEqual("Class", ret.name)
344  self.assertEqual(1, len(ret.args))
345  self.assertEqual("const T & name", ret.args.args_list[0].to_cpp())
346 
348  """Test for operator overloading."""
349  # Unary operator
350  wrap_string = "gtsam::Vector2 operator-() const;"
351  ret = Operator.rule.parseString(wrap_string)[0]
352  self.assertEqual("operator", ret.name)
353  self.assertEqual("-", ret.operator)
354  self.assertEqual("Vector2", ret.return_type.type1.typename.name)
355  self.assertEqual("gtsam::Vector2",
356  ret.return_type.type1.typename.to_cpp())
357  self.assertTrue(len(ret.args) == 0)
358  self.assertTrue(ret.is_unary)
359 
360  # Binary operator
361  wrap_string = "gtsam::Vector2 operator*(const gtsam::Vector2 &v) const;"
362  ret = Operator.rule.parseString(wrap_string)[0]
363  self.assertEqual("operator", ret.name)
364  self.assertEqual("*", ret.operator)
365  self.assertEqual("Vector2", ret.return_type.type1.typename.name)
366  self.assertEqual("gtsam::Vector2",
367  ret.return_type.type1.typename.to_cpp())
368  self.assertTrue(len(ret.args) == 1)
369  self.assertEqual("const gtsam::Vector2 &",
370  repr(ret.args.list()[0].ctype))
371  self.assertTrue(not ret.is_unary)
372 
374  """Test for typedef'd instantiation of a template."""
375  typedef = TypedefTemplateInstantiation.rule.parseString("""
376  typedef gtsam::BearingFactor<gtsam::Pose2, gtsam::Point2, gtsam::Rot2>
377  BearingFactor2D;
378  """)[0]
379  self.assertEqual("BearingFactor2D", typedef.new_name)
380  self.assertEqual("BearingFactor", typedef.typename.name)
381  self.assertEqual(["gtsam"], typedef.typename.namespaces)
382  self.assertEqual(3, len(typedef.typename.instantiations))
383 
384  def test_base_class(self):
385  """Test a base class."""
386  ret = Class.rule.parseString("""
387  virtual class Base {
388  };
389  """)[0]
390  self.assertEqual("Base", ret.name)
391  self.assertEqual(0, len(ret.ctors))
392  self.assertEqual(0, len(ret.methods))
393  self.assertEqual(0, len(ret.static_methods))
394  self.assertEqual(0, len(ret.properties))
395  self.assertTrue(ret.is_virtual)
396 
397  def test_empty_class(self):
398  """Test an empty class declaration."""
399  ret = Class.rule.parseString("""
400  class FactorIndices {};
401  """)[0]
402  self.assertEqual("FactorIndices", ret.name)
403  self.assertEqual(0, len(ret.ctors))
404  self.assertEqual(0, len(ret.methods))
405  self.assertEqual(0, len(ret.static_methods))
406  self.assertEqual(0, len(ret.properties))
407  self.assertTrue(not ret.is_virtual)
408 
409  def test_class(self):
410  """Test a non-trivial class."""
411  ret = Class.rule.parseString("""
412  class SymbolicFactorGraph {
413  SymbolicFactorGraph();
414  SymbolicFactorGraph(const gtsam::SymbolicBayesNet& bayesNet);
415  SymbolicFactorGraph(const gtsam::SymbolicBayesTree& bayesTree);
416 
417  // Dummy static method
418  static gtsam::SymbolidFactorGraph CreateGraph();
419 
420  void push_back(gtsam::SymbolicFactor* factor);
421  void print(string s) const;
422  bool equals(const gtsam::SymbolicFactorGraph& rhs, double tol) const;
423  size_t size() const;
424  bool exists(size_t idx) const;
425 
426  // Standard interface
427  gtsam::KeySet keys() const;
428  void push_back(const gtsam::SymbolicFactorGraph& graph);
429  void push_back(const gtsam::SymbolicBayesNet& bayesNet);
430  void push_back(const gtsam::SymbolicBayesTree& bayesTree);
431 
432  /* Advanced interface */
433  void push_factor(size_t key);
434  void push_factor(size_t key1, size_t key2);
435  void push_factor(size_t key1, size_t key2, size_t key3);
436  void push_factor(size_t key1, size_t key2, size_t key3, size_t key4);
437 
438  gtsam::SymbolicBayesNet* eliminateSequential();
439  gtsam::SymbolicBayesNet* eliminateSequential(
440  const gtsam::Ordering& ordering);
441  gtsam::SymbolicBayesTree* eliminateMultifrontal();
442  gtsam::SymbolicBayesTree* eliminateMultifrontal(
443  const gtsam::Ordering& ordering);
444  pair<gtsam::SymbolicBayesNet*, gtsam::SymbolicFactorGraph*>
445  eliminatePartialSequential(const gtsam::Ordering& ordering);
446  pair<gtsam::SymbolicBayesNet*, gtsam::SymbolicFactorGraph*>
447  eliminatePartialSequential(const gtsam::KeyVector& keys);
448  pair<gtsam::SymbolicBayesTree*, gtsam::SymbolicFactorGraph*>
449  eliminatePartialMultifrontal(const gtsam::Ordering& ordering);
450  gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(
451  const gtsam::Ordering& ordering);
452  gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(
453  const gtsam::KeyVector& key_vector,
454  const gtsam::Ordering& marginalizedVariableOrdering);
455  gtsam::SymbolicFactorGraph* marginal(const gtsam::KeyVector& key_vector);
456  };
457  """)[0]
458 
459  self.assertEqual("SymbolicFactorGraph", ret.name)
460  self.assertEqual(3, len(ret.ctors))
461  self.assertEqual(23, len(ret.methods))
462  self.assertEqual(1, len(ret.static_methods))
463  self.assertEqual(0, len(ret.properties))
464  self.assertTrue(not ret.is_virtual)
465 
467  """Test a templated class."""
468  ret = Class.rule.parseString("""
469  template<POSE, POINT>
470  class MyFactor {};
471  """)[0]
472 
473  self.assertEqual("MyFactor", ret.name)
474  self.assertEqual("<POSE, POINT>", repr(ret.template))
475 
477  """Test for class inheritance."""
478  ret = Class.rule.parseString("""
479  virtual class Null: gtsam::noiseModel::mEstimator::Base {
480  Null();
481  void print(string s) const;
482  static gtsam::noiseModel::mEstimator::Null* Create();
483 
484  // enabling serialization functionality
485  void serializable() const;
486  };
487  """)[0]
488  self.assertEqual("Null", ret.name)
489  self.assertEqual(1, len(ret.ctors))
490  self.assertEqual(2, len(ret.methods))
491  self.assertEqual(1, len(ret.static_methods))
492  self.assertEqual(0, len(ret.properties))
493  self.assertEqual("Base", ret.parent_class.name)
494  self.assertEqual(["gtsam", "noiseModel", "mEstimator"],
495  ret.parent_class.namespaces)
496  self.assertTrue(ret.is_virtual)
497 
498  ret = Class.rule.parseString(
499  "class ForwardKinematicsFactor : gtsam::BetweenFactor<gtsam::Pose3> {};"
500  )[0]
501  ret = InstantiatedClass(ret,
502  []) # Needed to correctly parse parent class
503  self.assertEqual("ForwardKinematicsFactor", ret.name)
504  self.assertEqual("BetweenFactor", ret.parent_class.name)
505  self.assertEqual(["gtsam"], ret.parent_class.namespaces)
506  self.assertEqual("Pose3", ret.parent_class.instantiations[0].name)
507  self.assertEqual(["gtsam"],
508  ret.parent_class.instantiations[0].namespaces)
509 
511  """Test for class with nested enum."""
512  ret = Class.rule.parseString("""
513  class Pet {
514  Pet(const string &name, Kind type);
515  enum Kind { Dog, Cat };
516  };
517  """)[0]
518  self.assertEqual(ret.name, "Pet")
519  self.assertEqual(ret.enums[0].name, "Kind")
520 
521  def test_include(self):
522  """Test for include statements."""
523  include = Include.rule.parseString(
524  "#include <gtsam/slam/PriorFactor.h>")[0]
525  self.assertEqual("gtsam/slam/PriorFactor.h", include.header)
526 
528  """Test for forward declarations."""
529  fwd = ForwardDeclaration.rule.parseString(
530  "virtual class Test:gtsam::Point3;")[0]
531 
532  self.assertEqual("Test", fwd.name)
533  self.assertTrue(fwd.is_virtual)
534 
535  def test_function(self):
536  """Test for global/free function."""
537  func = GlobalFunction.rule.parseString("""
538  gtsam::Values localToWorld(const gtsam::Values& local,
539  const gtsam::Pose2& base, const gtsam::KeyVector& keys);
540  """)[0]
541  self.assertEqual("localToWorld", func.name)
542  self.assertEqual("Values", func.return_type.type1.typename.name)
543  self.assertEqual(3, len(func.args))
544 
546  """Test for global variable."""
547  variable = Variable.rule.parseString("string kGravity;")[0]
548  self.assertEqual(variable.name, "kGravity")
549  self.assertEqual(variable.ctype.typename.name, "string")
550 
551  variable = Variable.rule.parseString("string kGravity = 9.81;")[0]
552  self.assertEqual(variable.name, "kGravity")
553  self.assertEqual(variable.ctype.typename.name, "string")
554  self.assertEqual(variable.default, "9.81")
555 
556  variable = Variable.rule.parseString(
557  "const string kGravity = 9.81;")[0]
558  self.assertEqual(variable.name, "kGravity")
559  self.assertEqual(variable.ctype.typename.name, "string")
560  self.assertTrue(variable.ctype.is_const)
561  self.assertEqual(variable.default, "9.81")
562 
563  variable = Variable.rule.parseString(
564  "gtsam::Pose3 wTc = gtsam::Pose3();")[0]
565  self.assertEqual(variable.name, "wTc")
566  self.assertEqual(variable.ctype.typename.name, "Pose3")
567  self.assertEqual(variable.default, "gtsam::Pose3()")
568 
569  variable = Variable.rule.parseString(
570  "gtsam::Pose3 wTc = gtsam::Pose3(1, 2, 0);")[0]
571  self.assertEqual(variable.name, "wTc")
572  self.assertEqual(variable.ctype.typename.name, "Pose3")
573  self.assertEqual(variable.default, "gtsam::Pose3(1, 2, 0)")
574 
575  def test_enumerator(self):
576  """Test for enumerator."""
577  enumerator = Enumerator.rule.parseString("Dog")[0]
578  self.assertEqual(enumerator.name, "Dog")
579 
580  enumerator = Enumerator.rule.parseString("Cat")[0]
581  self.assertEqual(enumerator.name, "Cat")
582 
583  def test_enum(self):
584  """Test for enums."""
585  enum = Enum.rule.parseString("""
586  enum Kind {
587  Dog,
588  Cat
589  };
590  """)[0]
591  self.assertEqual(enum.name, "Kind")
592  self.assertEqual(enum.enumerators[0].name, "Dog")
593  self.assertEqual(enum.enumerators[1].name, "Cat")
594 
595  def test_namespace(self):
596  """Test for namespace parsing."""
597  namespace = Namespace.rule.parseString("""
598  namespace gtsam {
599  #include <gtsam/geometry/Point2.h>
600  class Point2 {
601  Point2();
602  Point2(double x, double y);
603  double x() const;
604  double y() const;
605  int dim() const;
606  char returnChar() const;
607  void argChar(char a) const;
608  void argUChar(unsigned char a) const;
609  };
610 
611  #include <gtsam/geometry/Point3.h>
612  class Point3 {
613  Point3(double x, double y, double z);
614  double norm() const;
615 
616  // static functions - use static keyword and uppercase
617  static double staticFunction();
618  static gtsam::Point3 StaticFunctionRet(double z);
619 
620  // enabling serialization functionality
621  void serialize() const; // Just triggers a flag internally
622  };
623  }""")[0]
624  self.assertEqual("gtsam", namespace.name)
625 
626  def test_module(self):
627  """Test module parsing."""
628  module = Module.parseString("""
629  namespace one {
630  namespace two {
631  namespace three {
632  class Class123 {
633  };
634  }
635  class Class12a {
636  };
637  }
638  namespace two_dummy {
639  namespace three_dummy{
640 
641  }
642  namespace fourth_dummy{
643 
644  }
645  }
646  namespace two {
647  class Class12b {
648 
649  };
650  }
651  int oneVar;
652  }
653 
654  class Global{
655  };
656  int globalVar;
657  """)
658 
659  self.assertEqual(["one", "Global", "globalVar"],
660  [x.name for x in module.content])
661  self.assertEqual(["two", "two_dummy", "two", "oneVar"],
662  [x.name for x in module.content[0].content])
663 
664 
665 if __name__ == '__main__':
666  unittest.main()
Definition: pytypes.h:1979
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
str repr(handle h)
Definition: pytypes.h:2265
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2244


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45