Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Jan 15, 2024
1 parent 535fe4f commit a9e896d
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 23 deletions.
36 changes: 36 additions & 0 deletions tests/expected/matlab/FastSet.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%class FastSet, see Doxygen page for details
%at https://gtsam.org/doxygen/
%
%-------Constructors-------
%FastSet()
%
classdef FastSet < handle
properties
ptr_FastSet = 0
end
methods
function obj = FastSet(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(73, my_ptr);
elseif nargin == 0
my_ptr = class_wrapper(74);
else
error('Arguments do not match any overload of FastSet constructor');
end
obj.ptr_FastSet = my_ptr;
end

function delete(obj)
class_wrapper(75, obj.ptr_FastSet);
end

function display(obj), obj.print(''); end
%DISPLAY Calls print on the object
function disp(obj), obj.display; end
%DISP Calls print on the object
end

methods(Static = true)
end
end
12 changes: 6 additions & 6 deletions tests/expected/matlab/MyFactorPosePoint2.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
function obj = MyFactorPosePoint2(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(73, my_ptr);
class_wrapper(76, my_ptr);
elseif nargin == 4 && isa(varargin{1},'numeric') && isa(varargin{2},'numeric') && isa(varargin{3},'double') && isa(varargin{4},'gtsam.noiseModel.Base')
my_ptr = class_wrapper(74, varargin{1}, varargin{2}, varargin{3}, varargin{4});
my_ptr = class_wrapper(77, varargin{1}, varargin{2}, varargin{3}, varargin{4});
else
error('Arguments do not match any overload of MyFactorPosePoint2 constructor');
end
obj.ptr_MyFactorPosePoint2 = my_ptr;
end

function delete(obj)
class_wrapper(75, obj.ptr_MyFactorPosePoint2);
class_wrapper(78, obj.ptr_MyFactorPosePoint2);
end

function display(obj), obj.print(''); end
Expand All @@ -36,19 +36,19 @@ function delete(obj)
% PRINT usage: print(string s, KeyFormatter keyFormatter) : returns void
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 2 && isa(varargin{1},'char') && isa(varargin{2},'gtsam.KeyFormatter')
class_wrapper(76, this, varargin{:});
class_wrapper(79, this, varargin{:});
return
end
% PRINT usage: print(string s) : returns void
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 1 && isa(varargin{1},'char')
class_wrapper(77, this, varargin{:});
class_wrapper(80, this, varargin{:});
return
end
% PRINT usage: print() : returns void
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 0
class_wrapper(78, this, varargin{:});
class_wrapper(81, this, varargin{:});
return
end
error('Arguments do not match any overload of function MyFactorPosePoint2.print');
Expand Down
74 changes: 62 additions & 12 deletions tests/expected/matlab/class_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ typedef std::set<std::shared_ptr<ForwardKinematics>*> Collector_ForwardKinematic
static Collector_ForwardKinematics collector_ForwardKinematics;
typedef std::set<std::shared_ptr<TemplatedConstructor>*> Collector_TemplatedConstructor;
static Collector_TemplatedConstructor collector_TemplatedConstructor;
typedef std::set<std::shared_ptr<FastSet>*> Collector_FastSet;
static Collector_FastSet collector_FastSet;
typedef std::set<std::shared_ptr<MyFactorPosePoint2>*> Collector_MyFactorPosePoint2;
static Collector_MyFactorPosePoint2 collector_MyFactorPosePoint2;

Expand Down Expand Up @@ -101,6 +103,12 @@ void _deleteAllObjects()
collector_TemplatedConstructor.erase(iter++);
anyDeleted = true;
} }
{ for(Collector_FastSet::iterator iter = collector_FastSet.begin();
iter != collector_FastSet.end(); ) {
delete *iter;
collector_FastSet.erase(iter++);
anyDeleted = true;
} }
{ for(Collector_MyFactorPosePoint2::iterator iter = collector_MyFactorPosePoint2.begin();
iter != collector_MyFactorPosePoint2.end(); ) {
delete *iter;
Expand Down Expand Up @@ -844,7 +852,40 @@ void TemplatedConstructor_deconstructor_72(int nargout, mxArray *out[], int narg
delete self;
}

void MyFactorPosePoint2_collectorInsertAndMakeBase_73(int nargout, mxArray *out[], int nargin, const mxArray *in[])
void FastSet_collectorInsertAndMakeBase_73(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
mexAtExit(&_deleteAllObjects);
typedef std::shared_ptr<FastSet> Shared;

Shared *self = *reinterpret_cast<Shared**> (mxGetData(in[0]));
collector_FastSet.insert(self);
}

void FastSet_constructor_74(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
mexAtExit(&_deleteAllObjects);
typedef std::shared_ptr<FastSet> Shared;

Shared *self = new Shared(new FastSet());
collector_FastSet.insert(self);
out[0] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);
*reinterpret_cast<Shared**> (mxGetData(out[0])) = self;
}

void FastSet_deconstructor_75(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
typedef std::shared_ptr<FastSet> Shared;
checkArguments("delete_FastSet",nargout,nargin,1);
Shared *self = *reinterpret_cast<Shared**>(mxGetData(in[0]));
Collector_FastSet::iterator item;
item = collector_FastSet.find(self);
if(item != collector_FastSet.end()) {
collector_FastSet.erase(item);
}
delete self;
}

void MyFactorPosePoint2_collectorInsertAndMakeBase_76(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
mexAtExit(&_deleteAllObjects);
typedef std::shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>> Shared;
Expand All @@ -853,7 +894,7 @@ void MyFactorPosePoint2_collectorInsertAndMakeBase_73(int nargout, mxArray *out[
collector_MyFactorPosePoint2.insert(self);
}

void MyFactorPosePoint2_constructor_74(int nargout, mxArray *out[], int nargin, const mxArray *in[])
void MyFactorPosePoint2_constructor_77(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
mexAtExit(&_deleteAllObjects);
typedef std::shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>> Shared;
Expand All @@ -868,7 +909,7 @@ void MyFactorPosePoint2_constructor_74(int nargout, mxArray *out[], int nargin,
*reinterpret_cast<Shared**> (mxGetData(out[0])) = self;
}

void MyFactorPosePoint2_deconstructor_75(int nargout, mxArray *out[], int nargin, const mxArray *in[])
void MyFactorPosePoint2_deconstructor_78(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
typedef std::shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>> Shared;
checkArguments("delete_MyFactorPosePoint2",nargout,nargin,1);
Expand All @@ -881,7 +922,7 @@ void MyFactorPosePoint2_deconstructor_75(int nargout, mxArray *out[], int nargin
delete self;
}

void MyFactorPosePoint2_print_76(int nargout, mxArray *out[], int nargin, const mxArray *in[])
void MyFactorPosePoint2_print_79(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
checkArguments("print",nargout,nargin-1,2);
auto obj = unwrap_shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>>(in[0], "ptr_MyFactorPosePoint2");
Expand All @@ -890,15 +931,15 @@ void MyFactorPosePoint2_print_76(int nargout, mxArray *out[], int nargin, const
obj->print(s,keyFormatter);
}

void MyFactorPosePoint2_print_77(int nargout, mxArray *out[], int nargin, const mxArray *in[])
void MyFactorPosePoint2_print_80(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
checkArguments("print",nargout,nargin-1,1);
auto obj = unwrap_shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>>(in[0], "ptr_MyFactorPosePoint2");
string& s = *unwrap_shared_ptr< string >(in[1], "ptr_string");
obj->print(s,gtsam::DefaultKeyFormatter);
}

void MyFactorPosePoint2_print_78(int nargout, mxArray *out[], int nargin, const mxArray *in[])
void MyFactorPosePoint2_print_81(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
checkArguments("print",nargout,nargin-1,0);
auto obj = unwrap_shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>>(in[0], "ptr_MyFactorPosePoint2");
Expand Down Expand Up @@ -1137,22 +1178,31 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
TemplatedConstructor_deconstructor_72(nargout, out, nargin-1, in+1);
break;
case 73:
MyFactorPosePoint2_collectorInsertAndMakeBase_73(nargout, out, nargin-1, in+1);
FastSet_collectorInsertAndMakeBase_73(nargout, out, nargin-1, in+1);
break;
case 74:
MyFactorPosePoint2_constructor_74(nargout, out, nargin-1, in+1);
FastSet_constructor_74(nargout, out, nargin-1, in+1);
break;
case 75:
MyFactorPosePoint2_deconstructor_75(nargout, out, nargin-1, in+1);
FastSet_deconstructor_75(nargout, out, nargin-1, in+1);
break;
case 76:
MyFactorPosePoint2_print_76(nargout, out, nargin-1, in+1);
MyFactorPosePoint2_collectorInsertAndMakeBase_76(nargout, out, nargin-1, in+1);
break;
case 77:
MyFactorPosePoint2_print_77(nargout, out, nargin-1, in+1);
MyFactorPosePoint2_constructor_77(nargout, out, nargin-1, in+1);
break;
case 78:
MyFactorPosePoint2_print_78(nargout, out, nargin-1, in+1);
MyFactorPosePoint2_deconstructor_78(nargout, out, nargin-1, in+1);
break;
case 79:
MyFactorPosePoint2_print_79(nargout, out, nargin-1, in+1);
break;
case 80:
MyFactorPosePoint2_print_80(nargout, out, nargin-1, in+1);
break;
case 81:
MyFactorPosePoint2_print_81(nargout, out, nargin-1, in+1);
break;
}
} catch(const std::exception& e) {
Expand Down
6 changes: 6 additions & 0 deletions tests/expected/python/class_pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ PYBIND11_MODULE(class_py, m_) {
.def(py::init<const int&>(), py::arg("arg"))
.def(py::init<const double&>(), py::arg("arg"));

py::class_<FastSet, std::shared_ptr<FastSet>>(m_, "FastSet")
.def(py::init<>())
.def("__len__",[](FastSet* self){return self->size();})
.def("__contains__",[](FastSet* self, size_t key){return self->find(key) != self->end();}, py::arg("key"))
.def("__iter__",[](FastSet* self){return py::make_iterator(self->begin(), self->end());});

py::class_<MyFactor<gtsam::Pose2, gtsam::Matrix>, std::shared_ptr<MyFactor<gtsam::Pose2, gtsam::Matrix>>>(m_, "MyFactorPosePoint2")
.def(py::init<size_t, size_t, double, const std::shared_ptr<gtsam::noiseModel::Base>>(), py::arg("key1"), py::arg("key2"), py::arg("measured"), py::arg("noiseModel"))
.def("print",[](MyFactor<gtsam::Pose2, gtsam::Matrix>* self, const string& s, const gtsam::KeyFormatter& keyFormatter){ py::scoped_ostream_redirect output; self->print(s, keyFormatter);}, py::arg("s") = "factor: ", py::arg("keyFormatter") = gtsam::DefaultKeyFormatter)
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/class.i
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ class TemplatedConstructor {

class SuperCoolFactor;
typedef SuperCoolFactor<gtsam::Pose3> SuperCoolFactorPose3;

/// @brief class with dunder methods for container behavior
class FastSet {
FastSet();

__len__();
__contains__(size_t key);
__iter__();
};
22 changes: 17 additions & 5 deletions tests/test_interface_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from gtwrap.interface_parser import (ArgumentList, Class, Constructor, Enum,
Enumerator, ForwardDeclaration,
GlobalFunction, Include, Method, Module,
Namespace, Operator, ReturnType,
StaticMethod, TemplatedType, Type,
from gtwrap.interface_parser import (ArgumentList, Class, Constructor,
DunderMethod, Enum, Enumerator,
ForwardDeclaration, GlobalFunction,
Include, Method, Module, Namespace,
Operator, ReturnType, StaticMethod,
TemplatedType, Type,
TypedefTemplateInstantiation, Typename,
Variable)
from gtwrap.template_instantiator.classes import InstantiatedClass
Expand Down Expand Up @@ -344,6 +345,17 @@ def test_constructor_templated(self):
self.assertEqual(1, len(ret.args))
self.assertEqual("const T & name", ret.args.args_list[0].to_cpp())

def test_dunder_method(self):
"""Test for special python dunder methods."""
iter_string = "__iter__();"
ret = DunderMethod.rule.parse_string(iter_string)[0]
self.assertEqual("iter", ret.name)

contains_string = "__contains__(size_t key);"
ret = DunderMethod.rule.parse_string(contains_string)[0]
self.assertEqual("contains", ret.name)
self.assertTrue(len(ret.args) == 1)

def test_operator_overload(self):
"""Test for operator overloading."""
# Unary operator
Expand Down

0 comments on commit a9e896d

Please sign in to comment.