Skip to content

Commit

Permalink
move support and C++11 specific tests, also some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Aug 6, 2017
1 parent f09dab9 commit b7bcd2b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/cpp11features.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if __cplusplus >= 201103L

#include "cpp11features.h"


// for std::shared_ptr<> testing
int TestSharedPtr::s_counter = 0;

std::shared_ptr<TestSharedPtr> create_shared_ptr_instance() {
return std::shared_ptr<TestSharedPtr>(new TestSharedPtr);
}


// for move ctors etc.
int TestMoving1::s_move_counter = 0;
int TestMoving2::s_move_counter = 0;

#endif // c++11 and later
45 changes: 45 additions & 0 deletions test/cpp11features.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#if __cplusplus >= 201103L

#include <memory>


//===========================================================================
class TestSharedPtr { // for std::shared_ptr<> testing
public:
static int s_counter;

public:
TestSharedPtr() { ++s_counter; }
TestSharedPtr(const TestSharedPtr&) { ++s_counter; }
~TestSharedPtr() { --s_counter; }
};

std::shared_ptr<TestSharedPtr> create_shared_ptr_instance();


//===========================================================================
class TestMoving1 { // for move ctors etc.
public:
static int s_move_counter;

public:
TestMoving1() {}
TestMoving1(TestMoving1&&) { ++s_move_counter; }
TestMoving1(const TestMoving1&) {}
TestMoving1& operator=(TestMoving1&&) { ++s_move_counter; return *this; }
TestMoving1& operator=(TestMoving1&) { return *this; }
};

class TestMoving2 { // note opposite method order from TestMoving1
public:
static int s_move_counter;

public:
TestMoving2() {}
TestMoving2(const TestMoving2&) {}
TestMoving2(TestMoving2&& other) { ++s_move_counter; }
TestMoving2& operator=(TestMoving2&) { return *this; }
TestMoving2& operator=(TestMoving2&&) { ++s_move_counter; return *this; }
};

#endif // c++11 and later
6 changes: 6 additions & 0 deletions test/cpp11features.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<lcgdict>

<class name="TestSharedPtr" />
<class pattern="TestMoving*" />

</lcgdict>
78 changes: 78 additions & 0 deletions test/test_cpp11features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import py, os, sys
from pytest import raises
from .support import setup_make

currpath = py.path.local(__file__).dirpath()
test_dct = str(currpath.join("cpp11featuresDict.so"))

def setup_module(mod):
setup_make("cpp11featuresDict.so")


class TestCPP11FEATURES:
def setup_class(cls):
cls.test_dct = test_dct
import cppyy
cls.datatypes = cppyy.load_reflection_info(cls.test_dct)
cls.N = cppyy.gbl.N

def test01_shared_ptr(self):
"""Usage and access of std::shared_ptr<>"""

from cppyy.gbl import TestSharedPtr, create_shared_ptr_instance

# proper memory accounting
assert TestSharedPtr.s_counter == 0

ptr1 = create_shared_ptr_instance()
assert ptr1
assert not not ptr1
assert TestSharedPtr.s_counter == 1

ptr2 = create_shared_ptr_instance()
assert ptr2
assert not not ptr2
assert TestSharedPtr.s_counter == 2

del ptr2
import gc; gc.collect()
assert TestSharedPtr.s_counter == 1

del ptr1
gc.collect()
assert TestSharedPtr.s_counter == 0

def test02_nullptr(self):
"""Allow the programmer to pass NULL in certain cases"""

import cppyy

# test existence
nullptr = cppyy.nullptr

# TODO: test usage ...

def test03_move(self):
"""Move construction, assignment, and methods"""

import cppyy

def moveit(T):
from cppyy.gbl import std

i1 = T()
assert T.s_move_counter == 0

i2 = T(i1) # cctor
assert T.s_move_counter == 0

i3 = T(T()) # should call move, not memoized cctor
assert T.s_move_counter == 1

i4 = T(std.move(i1))
assert T.s_move_counter == 2

# order of moving and normal functions are reversed in 1, 2, for
# overload resolution testing
moveit(cppyy.gbl.TestMoving1)
moveit(cppyy.gbl.TestMoving2)

0 comments on commit b7bcd2b

Please sign in to comment.