Exemplo n.º 1
0
    def test_namespaces(self):
        """
        Test interface file with full namespace definition.
        """
        file = osp.join(self.INTERFACE_DIR, 'namespaces.i')

        wrapper = MatlabWrapper(
            module_name='namespaces',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'namespaces_wrapper.cpp',
            '+ns1/aGlobalFunction.m',
            '+ns1/ClassA.m',
            '+ns1/ClassB.m',
            '+ns2/+ns3/ClassB.m',
            '+ns2/aGlobalFunction.m',
            '+ns2/ClassA.m',
            '+ns2/ClassC.m',
            '+ns2/overloadedGlobalFunction.m',
            'ClassD.m',
            '+gtsam/Values.m',
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 2
0
    def test_class(self):
        """Test interface file with only class info."""
        file = osp.join(self.INTERFACE_DIR, 'class.i')

        wrapper = MatlabWrapper(
            module_name='class',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'class_wrapper.cpp',
            'FunDouble.m',
            'FunRange.m',
            'MultipleTemplatesIntDouble.m',
            'MultipleTemplatesIntFloat.m',
            'MyFactorPosePoint2.m',
            'MyVector3.m',
            'MyVector12.m',
            'PrimitiveRefDouble.m',
            'Test.m',
            'ForwardKinematics.m',
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 3
0
    def test_multiple_files(self):
        """
        Test for when multiple interface files are specified.
        """
        file1 = osp.join(self.INTERFACE_DIR, 'part1.i')
        file2 = osp.join(self.INTERFACE_DIR, 'part2.i')

        wrapper = MatlabWrapper(
            module_name='multiple_files',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        wrapper.wrap([file1, file2], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'multiple_files_wrapper.cpp',
            '+gtsam/Class1.m',
            '+gtsam/Class2.m',
            '+gtsam/ClassA.m',
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 4
0
    def test_functions(self):
        """Test interface file with function info."""
        file = osp.join(self.INTERFACE_DIR, 'functions.i')

        wrapper = MatlabWrapper(
            module_name='functions',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'functions_wrapper.cpp',
            'aGlobalFunction.m',
            'load2D.m',
            'MultiTemplatedFunctionDoubleSize_tDouble.m',
            'MultiTemplatedFunctionStringSize_tDouble.m',
            'overloadedGlobalFunction.m',
            'TemplatedFunctionRot3.m',
            'DefaultFuncInt.m',
            'DefaultFuncObj.m',
            'DefaultFuncString.m',
            'DefaultFuncVector.m',
            'DefaultFuncZero.m',
            'setPose.m',
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 5
0
    def test_special_cases(self):
        """
        Tests for some unique, non-trivial features.
        """
        with open(osp.join(self.INTERFACE_DIR, 'special_cases.i'), 'r') as f:
            content = f.read()

        if not osp.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        wrapper = MatlabWrapper(
            module_name='special_cases',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap(content)

        self.generate_content(cc_content)

        files = [
            'special_cases_wrapper.cpp',
            '+gtsam/PinholeCameraCal3Bundler.m',
            '+gtsam/NonlinearFactorGraph.m',
        ]

        for file in files:
            self.compare_and_diff(file)
Exemplo n.º 6
0
    def test_namespaces(self):
        """
        Test interface file with full namespace definition.
        """
        with open(osp.join(self.INTERFACE_DIR, 'namespaces.i'), 'r') as f:
            content = f.read()

        if not osp.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        wrapper = MatlabWrapper(
            module_name='namespaces',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap(content)

        self.generate_content(cc_content)

        files = [
            'namespaces_wrapper.cpp', '+ns1/aGlobalFunction.m',
            '+ns1/ClassA.m', '+ns1/ClassB.m', '+ns2/+ns3/ClassB.m',
            '+ns2/aGlobalFunction.m', '+ns2/ClassA.m', '+ns2/ClassC.m',
            '+ns2/overloadedGlobalFunction.m', 'ClassD.m'
        ]

        for file in files:
            self.compare_and_diff(file)
Exemplo n.º 7
0
    def test_inheritance(self):
        """Test interface file with class inheritance definitions."""
        with open(osp.join(self.INTERFACE_DIR, 'inheritance.i'), 'r') as f:
            content = f.read()

        if not osp.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        wrapper = MatlabWrapper(
            module_name='inheritance',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap(content)

        self.generate_content(cc_content)

        files = [
            'inheritance_wrapper.cpp', 'MyBase.m', 'MyTemplateMatrix.m',
            'MyTemplatePoint2.m'
        ]

        for file in files:
            self.compare_and_diff(file)
Exemplo n.º 8
0
    def test_class(self):
        """Test interface file with only class info."""
        with open(osp.join(self.INTERFACE_DIR, 'class.i'), 'r') as f:
            content = f.read()

        if not osp.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        wrapper = MatlabWrapper(
            module_name='class',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap(content)

        self.generate_content(cc_content)

        files = [
            'class_wrapper.cpp', 'FunDouble.m', 'FunRange.m',
            'MultipleTemplatesIntDouble.m', 'MultipleTemplatesIntFloat.m',
            'MyFactorPosePoint2.m', 'MyVector3.m', 'MyVector12.m',
            'PrimitiveRefDouble.m', 'Test.m'
        ]

        for file in files:
            self.compare_and_diff(file)
Exemplo n.º 9
0
    def test_functions(self):
        """Test interface file with function info."""
        with open(osp.join(self.INTERFACE_DIR, 'functions.i'), 'r') as f:
            content = f.read()

        if not osp.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        wrapper = MatlabWrapper(
            module_name='functions',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap(content)

        self.generate_content(cc_content)

        files = [
            'functions_wrapper.cpp', 'aGlobalFunction.m', 'load2D.m',
            'MultiTemplatedFunctionDoubleSize_tDouble.m',
            'MultiTemplatedFunctionStringSize_tDouble.m',
            'overloadedGlobalFunction.m', 'TemplatedFunctionRot3.m'
        ]

        for file in files:
            self.compare_and_diff(file)
Exemplo n.º 10
0
    def test_geometry(self):
        """
        Check generation of matlab geometry wrapper.
        python3 wrap/matlab_wrapper.py --src wrap/tests/geometry.h
            --module_name geometry --out wrap/tests/actual-matlab
        """
        with open(osp.join(self.INTERFACE_DIR, 'geometry.i'), 'r') as f:
            content = f.read()

        if not osp.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        # Create MATLAB wrapper instance
        wrapper = MatlabWrapper(
            module_name='geometry',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap(content)

        self.generate_content(cc_content)

        self.assertTrue(osp.isdir(osp.join(self.MATLAB_ACTUAL_DIR, '+gtsam')))

        files = ['+gtsam/Point2.m', '+gtsam/Point3.m', 'geometry_wrapper.cpp']

        for file in files:
            self.compare_and_diff(file)
Exemplo n.º 11
0
    def test_geometry_matlab(self):
        """ Check generation of matlab geometry wrapper.
        python3 wrap/matlab_wrapper.py --src wrap/tests/geometry.h
            --module_name geometry --out wrap/tests/actual-matlab
        """
        with open(self.TEST_DIR + 'geometry.h', 'r') as f:
            content = f.read()

        if not os.path.exists(self.MATLAB_ACTUAL_DIR):
            os.mkdir(self.MATLAB_ACTUAL_DIR)

        module = parser.Module.parseString(content)

        instantiator.instantiate_namespace_inplace(module)

        # Create MATLAB wrapper instance
        wrapper = MatlabWrapper(
            module=module,
            module_name='geometry',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        cc_content = wrapper.wrap()

        self.generate_content(cc_content)

        def compare_and_diff(file):
            output = self.MATLAB_ACTUAL_DIR + file
            expected = self.MATLAB_TEST_DIR + file
            success = filecmp.cmp(output, expected)
            if not success:
                print("Differ in file: {}".format(file))
                os.system("diff {} {}".format(output, expected))
            self.assertTrue(success)

        self.assertTrue(os.path.isdir(self.MATLAB_ACTUAL_DIR + '+gtsam'))

        files = [
            '+gtsam/Point2.m',
            '+gtsam/Point3.m',
            'Test.m',
            'MyBase.m',
            'load2D.m',
            'MyTemplatePoint2.m',
            'MyTemplateMatrix.m',
            'MyVector3.m',
            'MyVector12.m',
            'MyFactorPosePoint2.m',
            'aGlobalFunction.m',
            'overloadedGlobalFunction.m',
            'geometry_wrapper.cpp'
        ]

        for file in files:
            compare_and_diff(file)
Exemplo n.º 12
0
    def test_templates(self):
        """Test interface file with template info."""
        file = osp.join(self.INTERFACE_DIR, 'templates.i')

        wrapper = MatlabWrapper(
            module_name='template',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'template_wrapper.cpp', 'ScopedTemplateResult.m',
            'TemplatedConstructor.m'
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 13
0
    def test_inheritance(self):
        """Test interface file with class inheritance definitions."""
        file = osp.join(self.INTERFACE_DIR, 'inheritance.i')

        wrapper = MatlabWrapper(
            module_name='inheritance',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )
        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'inheritance_wrapper.cpp',
            'MyBase.m',
            'MyTemplateMatrix.m',
            'MyTemplatePoint2.m',
            'ForwardKinematicsFactor.m',
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 14
0
    def test_geometry(self):
        """
        Check generation of matlab geometry wrapper.
        python3 wrap/matlab_wrapper.py --src wrap/tests/geometry.h
            --module_name geometry --out wrap/tests/actual-matlab
        """
        file = osp.join(self.INTERFACE_DIR, 'geometry.i')

        # Create MATLAB wrapper instance
        wrapper = MatlabWrapper(
            module_name='geometry',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )

        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = ['+gtsam/Point2.m', '+gtsam/Point3.m', 'geometry_wrapper.cpp']

        self.assertTrue(osp.isdir(osp.join(self.MATLAB_ACTUAL_DIR, '+gtsam')))

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 15
0
    def test_special_cases(self):
        """
        Tests for some unique, non-trivial features.
        """
        file = osp.join(self.INTERFACE_DIR, 'special_cases.i')

        wrapper = MatlabWrapper(
            module_name='special_cases',
            top_module_namespace=['gtsam'],
            ignore_classes=[''],
        )
        wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)

        files = [
            'special_cases_wrapper.cpp',
            '+gtsam/GeneralSFMFactorCal3Bundler.m',
            '+gtsam/NonlinearFactorGraph.m',
            '+gtsam/PinholeCameraCal3Bundler.m',
            '+gtsam/SfmTrack.m',
        ]

        for file in files:
            actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
            self.compare_and_diff(file, actual)
Exemplo n.º 16
0
        "For example, `import <module_name>` gives you access to a Python "
        "`<module_name>.Class` of the corresponding C++ `ns1::ns2::ns3::Class`"
        ", and `from <module_name> import ns4` gives you access to a Python "
        "`ns4.Class` of the C++ `ns1::ns2::ns3::ns4::Class`. ")
    arg_parser.add_argument(
        "--ignore",
        nargs='*',
        type=str,
        help="A space-separated list of classes to ignore. "
        "Class names must include their full namespaces.")
    args = arg_parser.parse_args()

    top_module_namespaces = args.top_module_namespaces.split("::")
    if top_module_namespaces[0]:
        top_module_namespaces = [''] + top_module_namespaces

    with open(args.src, 'r') as f:
        content = f.read()

    if not os.path.exists(args.src):
        os.mkdir(args.src)

    print("Ignoring classes: {}".format(args.ignore), file=sys.stderr)
    wrapper = MatlabWrapper(module_name=args.module_name,
                            top_module_namespace=top_module_namespaces,
                            ignore_classes=args.ignore)

    cc_content = wrapper.wrap(content)

    generate_content(cc_content, args.out)
Exemplo n.º 17
0
        type=str,
        default="",
        help="C++ namespace for the top module, e.g. `ns1::ns2::ns3`. "
        "Only the content within this namespace and its sub-namespaces "
        "will be wrapped. The content of this namespace will be available at "
        "the top module level, and its sub-namespaces' in the submodules.\n"
        "For example, `import <module_name>` gives you access to a Python "
        "`<module_name>.Class` of the corresponding C++ `ns1::ns2::ns3::Class`"
        ", and `from <module_name> import ns4` gives you access to a Python "
        "`ns4.Class` of the C++ `ns1::ns2::ns3::ns4::Class`. ")
    arg_parser.add_argument(
        "--ignore",
        nargs='*',
        type=str,
        help="A space-separated list of classes to ignore. "
        "Class names must include their full namespaces.")
    args = arg_parser.parse_args()

    top_module_namespaces = args.top_module_namespaces.split("::")
    if top_module_namespaces[0]:
        top_module_namespaces = [''] + top_module_namespaces

    print("[MatlabWrapper] Ignoring classes: {}".format(args.ignore),
          file=sys.stderr)
    wrapper = MatlabWrapper(module_name=args.module_name,
                            top_module_namespace=top_module_namespaces,
                            ignore_classes=args.ignore)

    sources = args.src.split(';')
    cc_content = wrapper.wrap(sources, path=args.out)