Skip to content

Commit

Permalink
test for malloc of pointer types (multi-dim arrays)
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Sep 11, 2021
1 parent 4d393aa commit 0e0f1a5
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,46 @@ def test03_3D_arrays(self):
val = elem_tp(3*i+2*j+k)
assert arr[i][j][k] == val
assert arr[i, j, k] == val

def test04_malloc(self):
"""Use of malloc to create multi-dim arrays"""

import cppyy
import cppyy.ll

cppyy.cppdef("""\
namespace MallocChecker {
template<typename T>
struct Foo {
T* bar;
Foo() {}
Foo(T* other) : bar(other) {}
bool eq(T* other) { return bar == other; }
};
template<typename T>
auto create(T* other) {
return Foo<T>(other);
} }""")

ns = cppyy.gbl.MallocChecker

for dtype in ["int", "int*", "int**",]:
bar = cppyy.ll.malloc[dtype](4)

# variable assignment
foo = ns.Foo[dtype]()
foo.bar = bar
assert foo.eq(bar)

# pointer passed to the constructor
foo2 = ns.Foo[dtype](bar)
assert foo2.eq(bar)

# pointer passed to a function
foo3 = ns.create[dtype](bar)
assert foo3.eq(bar)

cppyy.ll.free(bar)

0 comments on commit 0e0f1a5

Please sign in to comment.