Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,4 @@ tests/test_files/enums.pyx
# generated typestubs
tests/test_files/*.pyi

_codeql_detected_source_root
16 changes: 16 additions & 0 deletions tests/test_code_generator_new_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ def test_new_stl_code_generation():
"getDeque method should be generated"
assert "def sumDeque(" in pyx_content, \
"sumDeque method should be generated"
assert "def doubleDequeElements(" in pyx_content, \
"doubleDequeElements method should be generated"
assert "def getList(" in pyx_content, \
"getList method should be generated"
assert "def sumList(" in pyx_content, \
"sumList method should be generated"
assert "def doubleListElements(" in pyx_content, \
"doubleListElements method should be generated"
assert "def getOptionalValue(" in pyx_content, \
"getOptionalValue method should be generated"
assert "def unwrapOptional(" in pyx_content, \
Expand Down Expand Up @@ -104,6 +108,11 @@ def test_new_stl_code_generation():
sum_deque_result = obj.sumDeque([5, 10, 15])
assert sum_deque_result == 30, f"sumDeque returned {sum_deque_result}"

# Test deque mutable reference (cleanup code)
deque_data = [1, 2, 3, 4]
obj.doubleDequeElements(deque_data)
assert deque_data == [2, 4, 6, 8], f"doubleDequeElements should modify list in place: {deque_data}"

# Test list (std::list)
result_list = obj.getList()
assert isinstance(result_list, list), "std::list should return list"
Expand All @@ -114,6 +123,13 @@ def test_new_stl_code_generation():
sum_list_result = obj.sumList([1.0, 2.0, 3.0])
assert abs(sum_list_result - 6.0) < 0.0001, f"sumList returned {sum_list_result}"

# Test list mutable reference (cleanup code)
list_data = [1.0, 2.0, 3.0]
obj.doubleListElements(list_data)
expected_doubled = [2.0, 4.0, 6.0]
for i, (r, e) in enumerate(zip(list_data, expected_doubled)):
assert abs(r - e) < 0.0001, f"doubleListElements should modify list in place: {list_data}"

# Test optional
opt_with_value = obj.getOptionalValue(True)
assert opt_with_value == 42, f"Optional with value should return 42, got {opt_with_value}"
Expand Down
8 changes: 7 additions & 1 deletion tests/test_files/inherited.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Generated with autowrap 0.23.0 and Cython (Parser) 3.2.0
#Generated with autowrap 0.23.0 and Cython (Parser) 3.2.1
#cython: c_string_encoding=ascii
#cython: embedsignature=False
from enum import Enum as _PyEnum
Expand All @@ -11,6 +11,12 @@ from libcpp.set cimport set as libcpp_set
from libcpp.vector cimport vector as libcpp_vector
from libcpp.pair cimport pair as libcpp_pair
from libcpp.map cimport map as libcpp_map
from libcpp.unordered_map cimport unordered_map as libcpp_unordered_map
from libcpp.unordered_set cimport unordered_set as libcpp_unordered_set
from libcpp.deque cimport deque as libcpp_deque
from libcpp.list cimport list as libcpp_list
from libcpp.optional cimport optional as libcpp_optional
from libcpp.string_view cimport string_view as libcpp_string_view
from libcpp cimport bool
from libc.string cimport const_char
from cython.operator cimport dereference as deref, preincrement as inc, address as address
Expand Down
14 changes: 14 additions & 0 deletions tests/test_files/new_stl_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class NewSTLTest {
return sum;
}

// Mutable reference test - doubles each element
void doubleDequeElements(std::deque<int>& d) {
for (size_t i = 0; i < d.size(); i++) {
d[i] *= 2;
}
}

// =========================================================================
// std::list tests
// =========================================================================
Expand All @@ -84,6 +91,13 @@ class NewSTLTest {
return sum;
}

// Mutable reference test - doubles each element
void doubleListElements(std::list<double>& l) {
for (auto& v : l) {
v *= 2;
}
}

// =========================================================================
// std::optional tests
// =========================================================================
Expand Down
2 changes: 2 additions & 0 deletions tests/test_files/new_stl_test.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ cdef extern from "new_stl_test.hpp":
# deque tests
libcpp_deque[int] getDeque()
int sumDeque(libcpp_deque[int]& d)
void doubleDequeElements(libcpp_deque[int]& d)

# list tests
libcpp_list[double] getList()
double sumList(libcpp_list[double]& l)
void doubleListElements(libcpp_list[double]& l)

# optional tests
libcpp_optional[int] getOptionalValue(bool hasValue)
Expand Down
240 changes: 240 additions & 0 deletions tests/test_files/new_stl_test.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
#Generated with autowrap 0.23.0 and Cython (Parser) 3.2.1
#cython: c_string_encoding=ascii
#cython: embedsignature=False
from enum import Enum as _PyEnum
from cpython cimport Py_buffer
from cpython cimport bool as pybool_t
from libcpp.string cimport string as libcpp_string
from libcpp.string cimport string as libcpp_utf8_string
from libcpp.string cimport string as libcpp_utf8_output_string
from libcpp.set cimport set as libcpp_set
from libcpp.vector cimport vector as libcpp_vector
from libcpp.pair cimport pair as libcpp_pair
from libcpp.map cimport map as libcpp_map
from libcpp.unordered_map cimport unordered_map as libcpp_unordered_map
from libcpp.unordered_set cimport unordered_set as libcpp_unordered_set
from libcpp.deque cimport deque as libcpp_deque
from libcpp.list cimport list as libcpp_list
from libcpp.optional cimport optional as libcpp_optional
from libcpp.string_view cimport string_view as libcpp_string_view
from libcpp cimport bool
from libc.string cimport const_char
from cython.operator cimport dereference as deref, preincrement as inc, address as address
from AutowrapRefHolder cimport AutowrapRefHolder
from AutowrapPtrHolder cimport AutowrapPtrHolder
from AutowrapConstPtrHolder cimport AutowrapConstPtrHolder
from smart_ptr cimport shared_ptr
from new_stl_test cimport _NewSTLTest as __NewSTLTest

cdef extern from "autowrap_tools.hpp":
char * _cast_const_away(char *)

cdef class _NewSTLTest:
"""
Cython implementation of __NewSTLTest
"""

cdef shared_ptr[__NewSTLTest] inst

def __dealloc__(self):
self.inst.reset()


def __init__(self):
"""
__init__(self) -> None
"""
self.inst = shared_ptr[__NewSTLTest](new __NewSTLTest())

def getUnorderedMap(self):
"""
getUnorderedMap(self) -> Dict[bytes, int]
"""
_r = self.inst.get().getUnorderedMap()
py_result = dict()
cdef libcpp_unordered_map[libcpp_string, int].iterator it__r = _r.begin()
while it__r != _r.end():
py_result[<libcpp_string>(deref(it__r).first)] = <int>(deref(it__r).second)
inc(it__r)
return py_result

def sumUnorderedMapValues(self, dict m ):
"""
sumUnorderedMapValues(self, m: Dict[bytes, int] ) -> int
"""
assert isinstance(m, dict) and all(isinstance(k, bytes) for k in m.keys()) and all(isinstance(v, int) for v in m.values()), 'arg m wrong type'
cdef libcpp_unordered_map[libcpp_string, int] * v0 = new libcpp_unordered_map[libcpp_string, int]()
for key, value in m.items():
deref(v0)[ <libcpp_string> key ] = <int> value
cdef int _r = self.inst.get().sumUnorderedMapValues(deref(v0))
replace = dict()
cdef libcpp_unordered_map[libcpp_string, int].iterator it_m = v0.begin()
while it_m != v0.end():
replace[<libcpp_string> deref(it_m).first] = <int> deref(it_m).second
inc(it_m)
m.clear()
m.update(replace)
del v0
py_result = <int>_r
return py_result

def getUnorderedSet(self):
"""
getUnorderedSet(self) -> Set[int]
"""
_r = self.inst.get().getUnorderedSet()
py_result = set()
cdef libcpp_unordered_set[int].iterator it__r = _r.begin()
while it__r != _r.end():
py_result.add(<int>deref(it__r))
inc(it__r)
return py_result

def sumUnorderedSet(self, set s ):
"""
sumUnorderedSet(self, s: Set[int] ) -> int
"""
assert isinstance(s, set) and all(isinstance(li, int) for li in s), 'arg s wrong type'
cdef libcpp_unordered_set[int] * v0 = new libcpp_unordered_set[int]()
for item0 in s:
v0.insert(<int> item0)
cdef int _r = self.inst.get().sumUnorderedSet(deref(v0))
replace = set()
cdef libcpp_unordered_set[int].iterator it_s = v0.begin()
while it_s != v0.end():
replace.add(<int> deref(it_s))
inc(it_s)
s.clear()
s.update(replace)
del v0
py_result = <int>_r
return py_result

def getDeque(self):
"""
getDeque(self) -> List[int]
"""
_r = self.inst.get().getDeque()
py_result = [<int>_r.at(i) for i in range(_r.size())]
return py_result

def sumDeque(self, list d ):
"""
sumDeque(self, d: List[int] ) -> int
"""
assert isinstance(d, list) and all(isinstance(li, int) for li in d), 'arg d wrong type'
cdef libcpp_deque[int] v0
for item0 in d:
v0.push_back(<int> item0)
cdef int _r = self.inst.get().sumDeque(v0)
d[:] = [<int>v0.at(i) for i in range(v0.size())]
py_result = <int>_r
return py_result

def doubleDequeElements(self, list d ):
"""
doubleDequeElements(self, d: List[int] ) -> None
"""
assert isinstance(d, list) and all(isinstance(li, int) for li in d), 'arg d wrong type'
cdef libcpp_deque[int] v0
for item0 in d:
v0.push_back(<int> item0)
self.inst.get().doubleDequeElements(v0)
d[:] = [<int>v0.at(i) for i in range(v0.size())]

def getList(self):
"""
getList(self) -> List[float]
"""
_r = self.inst.get().getList()
py_result = []
cdef libcpp_list[double].iterator it__r = _r.begin()
while it__r != _r.end():
py_result.append(deref(it__r))
inc(it__r)
return py_result

def sumList(self, list l ):
"""
sumList(self, l: List[float] ) -> float
"""
assert isinstance(l, list) and all(isinstance(li, float) for li in l), 'arg l wrong type'
cdef libcpp_list[double] v0
for item in l:
v0.push_back(item)
cdef double _r = self.inst.get().sumList(v0)
l[:] = []
cdef libcpp_list[double].iterator it_l = v0.begin()
while it_l != v0.end():
l.append(deref(it_l))
inc(it_l)
py_result = <double>_r
return py_result

def doubleListElements(self, list l ):
"""
doubleListElements(self, l: List[float] ) -> None
"""
assert isinstance(l, list) and all(isinstance(li, float) for li in l), 'arg l wrong type'
cdef libcpp_list[double] v0
for item in l:
v0.push_back(item)
self.inst.get().doubleListElements(v0)
l[:] = []
cdef libcpp_list[double].iterator it_l = v0.begin()
while it_l != v0.end():
l.append(deref(it_l))
inc(it_l)

def getOptionalValue(self, bool hasValue ):
"""
getOptionalValue(self, hasValue: bool ) -> Optional[int]
"""
assert isinstance(hasValue, pybool_t), 'arg hasValue wrong type'

_r = self.inst.get().getOptionalValue((<bool>hasValue))
if _r.has_value():
py_result = _r.value()
else:
py_result = None
return py_result

def unwrapOptional(self, object opt ):
"""
unwrapOptional(self, opt: Optional[int] ) -> int
"""
assert (opt is None or isinstance(opt, int)), 'arg opt wrong type'
cdef libcpp_optional[int] v0
if opt is not None:
v0 = libcpp_optional[int](<int>opt)
cdef int _r = self.inst.get().unwrapOptional(v0)
py_result = <int>_r
return py_result

def getStringViewLength(self, bytes sv ):
"""
getStringViewLength(self, sv: bytes ) -> int
"""
assert isinstance(sv, (bytes, str)), 'arg sv wrong type'
cdef bytes v0
if isinstance(sv, str):
v0 = sv.encode('utf-8')
else:
v0 = sv
cdef size_t _r = self.inst.get().getStringViewLength((<libcpp_string_view>v0))
py_result = <size_t>_r
return py_result

def stringViewToString(self, bytes sv ):
"""
stringViewToString(self, sv: bytes ) -> bytes
"""
assert isinstance(sv, (bytes, str)), 'arg sv wrong type'
cdef bytes v0
if isinstance(sv, str):
v0 = sv.encode('utf-8')
else:
v0 = sv
cdef libcpp_string _r = self.inst.get().stringViewToString((<libcpp_string_view>v0))
py_result = <libcpp_string>_r
return py_result
Loading