Author: Wim Lavrijsen <[email protected]>
Branch: cppyy-packaging
Changeset: r94852:8fe8681b8102
Date: 2018-07-11 22:41 -0700
http://bitbucket.org/pypy/pypy/changeset/8fe8681b8102/
Log: bring in a load of tests from cppyy/test
diff --git a/pypy/module/_cppyy/test/advancedcpp.cxx
b/pypy/module/_cppyy/test/advancedcpp.cxx
--- a/pypy/module/_cppyy/test/advancedcpp.cxx
+++ b/pypy/module/_cppyy/test/advancedcpp.cxx
@@ -4,20 +4,32 @@
// for testing of default arguments
-#define IMPLEMENT_DEFAULTER_CLASS(type, tname) \
+#define IMPLEMENT_DEFAULTERS(type, tname) \
tname##_defaulter::tname##_defaulter(type a, type b, type c) { \
- m_a = a; m_b = b; m_c = c; \
+ m_a = a; m_b = b; m_c = c; \
+} \
+type tname##_defaulter_func(int idx, type a, type b, type c) { \
+ if (idx == 0) return a; \
+ if (idx == 1) return b; \
+ if (idx == 2) return c; \
+ return (type)idx; \
}
-IMPLEMENT_DEFAULTER_CLASS(short, short)
-IMPLEMENT_DEFAULTER_CLASS(unsigned short, ushort)
-IMPLEMENT_DEFAULTER_CLASS(int, int)
-IMPLEMENT_DEFAULTER_CLASS(unsigned, uint)
-IMPLEMENT_DEFAULTER_CLASS(long, long)
-IMPLEMENT_DEFAULTER_CLASS(unsigned long, ulong)
-IMPLEMENT_DEFAULTER_CLASS(long long, llong)
-IMPLEMENT_DEFAULTER_CLASS(unsigned long long, ullong)
-IMPLEMENT_DEFAULTER_CLASS(float, float)
-IMPLEMENT_DEFAULTER_CLASS(double, double)
+IMPLEMENT_DEFAULTERS(short, short)
+IMPLEMENT_DEFAULTERS(unsigned short, ushort)
+IMPLEMENT_DEFAULTERS(int, int)
+IMPLEMENT_DEFAULTERS(unsigned, uint)
+IMPLEMENT_DEFAULTERS(long, long)
+IMPLEMENT_DEFAULTERS(unsigned long, ulong)
+IMPLEMENT_DEFAULTERS(long long, llong)
+IMPLEMENT_DEFAULTERS(unsigned long long, ullong)
+IMPLEMENT_DEFAULTERS(float, float)
+IMPLEMENT_DEFAULTERS(double, double)
+
+std::string string_defaulter_func(int idx, const std::string& name1,
std::string name2) {
+ if (idx == 0) return name1;
+ if (idx == 1) return name2;
+ return "mies";
+}
// for esoteric inheritance testing
@@ -77,11 +89,11 @@
double my_global_array[500];
static double sd = 1234.;
double* my_global_ptr = &sd;
+const char my_global_string2[] = "zus jet teun";
some_int_holder my_global_int_holders[5] = {
some_int_holder(13), some_int_holder(42), some_int_holder(88),
some_int_holder(-1), some_int_holder(17) };
-
// for life-line and identity testing
int some_class_with_data::some_data::s_num_data = 0;
diff --git a/pypy/module/_cppyy/test/advancedcpp.h
b/pypy/module/_cppyy/test/advancedcpp.h
--- a/pypy/module/_cppyy/test/advancedcpp.h
+++ b/pypy/module/_cppyy/test/advancedcpp.h
@@ -4,24 +4,27 @@
//===========================================================================
-#define DECLARE_DEFAULTER_CLASS(type, tname) \
+#define DECLARE_DEFAULTERS(type, tname) \
class tname##_defaulter { \
public: \
tname##_defaulter(type a = 11, type b = 22, type c = 33); \
\
public: \
type m_a, m_b, m_c; \
-};
-DECLARE_DEFAULTER_CLASS(short, short) // for testing of default arguments
-DECLARE_DEFAULTER_CLASS(unsigned short, ushort)
-DECLARE_DEFAULTER_CLASS(int, int)
-DECLARE_DEFAULTER_CLASS(unsigned, uint)
-DECLARE_DEFAULTER_CLASS(long, long)
-DECLARE_DEFAULTER_CLASS(unsigned long, ulong)
-DECLARE_DEFAULTER_CLASS(long long, llong)
-DECLARE_DEFAULTER_CLASS(unsigned long long, ullong)
-DECLARE_DEFAULTER_CLASS(float, float)
-DECLARE_DEFAULTER_CLASS(double, double)
+}; \
+type tname##_defaulter_func(int idx = 0, type a = 11, type b = 22, type c =
33);
+DECLARE_DEFAULTERS(short, short) // for testing of default arguments
+DECLARE_DEFAULTERS(unsigned short, ushort)
+DECLARE_DEFAULTERS(int, int)
+DECLARE_DEFAULTERS(unsigned, uint)
+DECLARE_DEFAULTERS(long, long)
+DECLARE_DEFAULTERS(unsigned long, ulong)
+DECLARE_DEFAULTERS(long long, llong)
+DECLARE_DEFAULTERS(unsigned long long, ullong)
+DECLARE_DEFAULTERS(float, float)
+DECLARE_DEFAULTERS(double, double)
+
+std::string string_defaulter_func(int idx, const std::string& name1 = "aap",
std::string name2 = "noot");
//===========================================================================
@@ -274,7 +277,8 @@
extern double my_global_double; // a couple of globals for access testing
extern double my_global_array[500];
extern double* my_global_ptr;
-static const char my_global_string[] = "aap " " noot " " mies";
+static const char my_global_string1[] = "aap " " noot " " mies";
+extern const char my_global_string2[];
class some_int_holder {
public:
@@ -311,6 +315,11 @@
some_data m_data;
};
+class refers_to_self { // for data member reuse testing
+public:
+ refers_to_self* m_other = nullptr;
+};
+
//===========================================================================
class pointer_pass { // for testing passing of void*'s
@@ -419,3 +428,34 @@
void throw_anything();
void throw_exception();
};
+
+
+//===========================================================================
+class UsingBase { // using declaration testing
+public:
+ UsingBase(int n = 13) : m_int(n) {}
+ virtual char vcheck() { return 'A'; }
+ int m_int;
+};
+
+class UsingDerived : public UsingBase {
+public:
+ using UsingBase::UsingBase;
+ virtual char vcheck() { return 'B'; }
+ int m_int2 = 42;
+};
+
+
+//===========================================================================
+class TypedefToPrivateClass { // typedef resolution testing
+private:
+ class PC {
+ public:
+ PC(int i) : m_val(i) {}
+ int m_val;
+ };
+
+public:
+ typedef PC PP;
+ PP f() { return PC(42); }
+};
diff --git a/pypy/module/_cppyy/test/advancedcpp.xml
b/pypy/module/_cppyy/test/advancedcpp.xml
--- a/pypy/module/_cppyy/test/advancedcpp.xml
+++ b/pypy/module/_cppyy/test/advancedcpp.xml
@@ -1,6 +1,7 @@
<lcgdict>
<class pattern="*_defaulter" />
+ <function pattern="*_defaulter_func" />
<class name="base_class" />
<class name="derived_class" />
@@ -32,6 +33,7 @@
<class name="some_convertible" />
<class name="some_class_with_data" />
<class name="some_class_with_data::some_data" />
+ <class name="refers_to_self" />
<class name="some_comparable" />
<function name="operator==" />
@@ -59,5 +61,7 @@
<class name="overload_the_other_way" />
<class name="Thrower" />
+ <class pattern="Using*" />
+ <class name="TypedefToPrivateClass" />
</lcgdict>
diff --git a/pypy/module/_cppyy/test/test_advancedcpp.py
b/pypy/module/_cppyy/test/test_advancedcpp.py
--- a/pypy/module/_cppyy/test/test_advancedcpp.py
+++ b/pypy/module/_cppyy/test/test_advancedcpp.py
@@ -56,6 +56,12 @@
assert d.m_b == t(4)
assert d.m_c == t(5)
d.__destruct__()
+
+ defaulter_func = getattr(cppyy.gbl, '%s_defaulter_func' %n)
+ answers = [11, 22, 33, 3]
+ for idx in range(4):
+ assert defaulter_func(idx) == answers[idx]
+
test_defaulter('short', int)
test_defaulter('ushort', int)
test_defaulter('int', int)
@@ -67,6 +73,13 @@
test_defaulter('float', float)
test_defaulter('double', float)
+ assert cppyy.gbl.string_defaulter_func(0) == "aap"
+ assert cppyy.gbl.string_defaulter_func(0, "zus") == "zus"
+ assert cppyy.gbl.string_defaulter_func(1) == "noot"
+ assert cppyy.gbl.string_defaulter_func(1, "zus") == "noot"
+ assert cppyy.gbl.string_defaulter_func(1, "zus", "jet") == "jet"
+ assert cppyy.gbl.string_defaulter_func(2) == "mies"
+
def test02_simple_inheritance(self):
"""Test binding of a basic inheritance structure"""
@@ -655,10 +668,18 @@
assert cppyy.gbl.my_global_double == 12.
assert len(cppyy.gbl.my_global_array) == 500
- assert cppyy.gbl.my_global_string == "aap noot mies"
+ assert cppyy.gbl.my_global_string1 == "aap noot mies"
+ return # next line currently crashes
+ assert cppyy.gbl.my_global_string2 == "zus jet teun"
# TODO: currently fails b/c double** not understood as &double*
#assert cppyy.gbl.my_global_ptr[0] == 1234.
+ v = cppyy.gbl.my_global_int_holders
+ assert len(v) == 5
+ expected_vals = [13, 42, 88, -1, 17]
+ for i in range(len(v)):
+ assert v[i].m_val == expected_vals[i]
+
def test22_exceptions(self):
"""Catching of C++ exceptions"""
@@ -677,3 +698,35 @@
t.throw_exception()
except Exception as e:
"C++ function failed" in str(e)
+
+ def test23_using(self):
+ """Accessibility of using declarations"""
+
+ import _cppyy as cppyy
+
+ assert cppyy.gbl.UsingBase().vcheck() == 'A'
+
+ B = cppyy.gbl.UsingDerived
+ assert not 'UsingBase' in B.__init__.__doc__
+
+ b1 = B()
+ assert b1.m_int == 13
+ assert b1.m_int2 == 42
+ assert b1.vcheck() == 'B'
+
+ b2 = B(10)
+ assert b2.m_int == 10
+ assert b2.m_int2 == 42
+ assert b2.vcheck() == 'B'
+
+ b3 = B(b2)
+ assert b3.m_int == 10
+ assert b3.m_int2 == 42
+ assert b3.vcheck() == 'B'
+
+ def test24_typedef_to_private_class(self):
+ """Typedefs to private classes should not resolve"""
+
+ import _cppyy as cppyy
+
+ assert cppyy.gbl.TypedefToPrivateClass().f().m_val == 42
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit