Skip to content

Commit

Permalink
Improve consistency of char[] arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Nov 13, 2023
1 parent 1c03185 commit 3c31d36
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ master: 3.1.0
* Check for ``nullptr`` as ``false`` in ``operator bool()``
* Automatically array-ify std::vector<some struct>::data() results
* Use __name__ to stringify if an annotation object provides it
* Improve consistency of ``char[]`` arrays
* Extended Numba support
* Update to latest Cling release (6.30)

Expand Down
6 changes: 3 additions & 3 deletions test/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,11 +740,11 @@ def test05_char_multidim(self):

cppyy.cppdef(r"""\
namespace StringArray {
char str_array[3][8] = {"s1\0", "s23\0", "s456\0"};
char str_array[3][7] = {"s1\0", "s23\0", "s456\0"};
}""")

ns = cppyy.gbl.StringArray

for i, v in enumerate(("s1", "s23", "s456")):
assert len(ns.str_array[i]) == 8
assert str(ns.str_array[i])[:len(v)] == v
assert len(ns.str_array[i]) == 7
assert ns.str_array[i].as_string() == v
41 changes: 41 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,44 @@ def test41_typedefed_enums(self):
ns = cppyy.gbl.TypedefedEnum

assert ns.func(ns.WEDNESDAY) == 2

def test42_char_arrays_consistence(self):
"""Consistent usage of char[] arrays"""

import cppyy

cppyy.cppdef(r"""
namespace CharArrays {
struct Foo {
char val[10], *ptr;
char values[2][10], *pointers[2];
};
void set_pointers(struct Foo* foo) {
// populate arrays
strcpy(foo->val, "howdy!");
strcpy(foo->values[0], "hello");
strcpy(foo->values[1], "world!");
// set pointers
foo->ptr = foo->val;
foo->pointers[0] = foo->values[0];
foo->pointers[1] = foo->values[1];
} }""")

ns = cppyy.gbl.CharArrays

foo = ns.Foo()
ns.set_pointers(foo)

howdy = 'howdy!'
hello = 'hello'
world = 'world!'

assert ''.join(foo.val)[:len(howdy)] == howdy
assert foo.ptr == howdy
assert foo.values[0].as_string() == hello
assert foo.values[1].as_string() == world
assert foo.pointers[0] == 'hello'
assert foo.pointers[1] == 'world!'

0 comments on commit 3c31d36

Please sign in to comment.