Skip to content

Commit fc2f020

Browse files
committed
Port Ruby test of vector<shared_ptr<>> to Python
This test code tests the upcast code: swig_assert_equal_simple(-1, base_num2([Derived(7)])) Although there is no explicit memory leak fix test, it does at least run the code. Handling of None needs fixing in Python (it is working in Ruby) Note that the Ruby implementation has a partial template specialization for shared_ptr, whereas the Python implementation is in the generic code! Issue swig#1512
1 parent 339d427 commit fc2f020

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from cpp11_shared_ptr_upcast import *
2+
3+
# This is a port from the Ruby equivalent test and some tests ported from
4+
# Examples/test-suite/ruby/cpp11_shared_ptr_upcast_runme.rb are not working and commented out with:
5+
# not working:
6+
7+
def swig_assert_equal_simple(expected, got):
8+
if expected != got:
9+
raise RuntimeError("Expected: {}. Got: {}")
10+
11+
# non-overloaded
12+
swig_assert_equal_simple(7, derived_num1(Derived(7)))
13+
swig_assert_equal_simple(7, derived_num2([Derived(7)]))
14+
swig_assert_equal_simple(7, derived_num3({0: Derived(7)}))
15+
16+
swig_assert_equal_simple(-1, base_num1(Derived(7)))
17+
swig_assert_equal_simple(-1, base_num2([Derived(7)]))
18+
swig_assert_equal_simple(-1, base_num3({0: Derived(7)}))
19+
20+
swig_assert_equal_simple(999, derived_num1(None))
21+
# not working: swig_assert_equal_simple(999, derived_num2([None]))
22+
# not working: swig_assert_equal_simple(999, derived_num3({0: None}))
23+
24+
swig_assert_equal_simple(999, base_num1(None))
25+
# not working: swig_assert_equal_simple(999, base_num2([None]))
26+
# not working: swig_assert_equal_simple(999, base_num3({0: None}))
27+
28+
# overloaded
29+
swig_assert_equal_simple(7, derived_num(Derived(7)))
30+
swig_assert_equal_simple(7, derived_num([Derived(7)]))
31+
swig_assert_equal_simple(7, derived_num({0: Derived(7)}))
32+
33+
swig_assert_equal_simple(-1, base_num(Derived(7)))
34+
swig_assert_equal_simple(-1, base_num([Derived(7)]))
35+
swig_assert_equal_simple(-1, base_num({0: Derived(7)}))
36+
37+
# ptr to shared_ptr
38+
swig_assert_equal_simple(7, derived2_num1(Derived2(7)))
39+
swig_assert_equal_simple(7, derived2_num2([Derived2(7)]))
40+
swig_assert_equal_simple(7, derived2_num3({0: Derived2(7)}))
41+
42+
swig_assert_equal_simple(-1, base2_num1(Derived2(7)))
43+
44+
# not working: try:
45+
# not working: # Upcast for pointers to shared_ptr in this generic framework has not been implemented
46+
# not working: swig_assert_equal_simple(-1, base2_num2([Derived2(7)]))
47+
# not working: raise RuntimeError, "Failed to catch TypeError"
48+
# not working: except TypeError:
49+
# not working: pass
50+
# not working: try:
51+
# not working: # Upcast for pointers to shared_ptr in this generic framework has not been implemented
52+
# not working: swig_assert_equal_simple(-1, base2_num3({0: Derived2(7)}))
53+
# not working: raise RuntimeError, "Failed to catch TypeError"
54+
# not working: except TypeError:
55+
# not working: pass
56+
57+
swig_assert_equal_simple(888, derived2_num1(None))
58+
swig_assert_equal_simple(999, derived2_num2([None])) # although 888 would be more consistent
59+
swig_assert_equal_simple(999, derived2_num3({0: None})) # although 888 would be more consistent
60+
61+
swig_assert_equal_simple(888, base2_num1(None))
62+
swig_assert_equal_simple(999, base2_num2([None])) # although 888 would be more consistent
63+
swig_assert_equal_simple(999, base2_num3({0: None})) # although 888 would be more consistent

0 commit comments

Comments
 (0)