File: test_simple.py

package info (click to toggle)
pycxx 7.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,276 kB
  • sloc: cpp: 6,617; python: 874; sh: 102; ansic: 60; makefile: 54
file content (136 lines) | stat: -rw-r--r-- 4,106 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
print( 'TEST: import simple' )
import simple

print( 'TEST: call module functions' )
simple.func()
simple.func( 4, 5 )
simple.func( 4, 5, name=6, value=7 )

def callback_good( arg ):
    print( 'callback_good with %r' % (arg,) )
    return 'good result'

print( 'TEST: raise user defined exception' )
try:
    raise simple.SimpleError( 'Testing simple error' )

except simple.SimpleError as e:
    print( 'PASS SimpleError %s' % (e,) )


def callback_bad( arg ):
    print( 'callback_bad with %r' % (arg,) )
    raise ValueError( 'callback_bad error' )

def callback_raise_simple_error( arg ):
    print( 'callback_bad with %r' % (arg,) )
    raise simple.SimpleError( 'callback_raise_simple_error' )

print( 'TEST: call C++ with Python callback_good' )
answer = simple.func_with_callback( callback_good, 'fred' )
print( 'PASS callback_good returned %r' % (answer,) )

print( 'TEST: call C++ with Python callback_bad' )
try:
    answer = simple.func_with_callback( callback_bad, 'fred' )
    print( 'FAILED callback_bad %r' % (answer,) )

except Exception as e:
    print( 'PASS callback_bad: error %s' % (e,) )

print( 'TEST: call C++ with Python callback_raise_simple_error' )
try:
    answer = simple.func_with_callback( callback_raise_simple_error, 'fred' )
    print( 'FAIL callback_raise_simple_error returned %r' % (answer,) )

except simple.SimpleError as e:
    print( 'PASS callback_raise_simple_error: %s' % (e,) )

print( 'TEST: call C++ that will catch SimpleError' )
try:
    answer = simple.func_with_callback_catch_simple_error( callback_raise_simple_error, 'fred' )
    print( 'PASS func_with_callback_catch_simple_error returned %r' % (answer,) )

except simple.SimpleError as e:
    print( 'FAIL func_with_callback_catch_simple_error: %s' % (e,) )

print( 'TEST: raise SimpleError' )
try:
    raise simple.SimpleError( 'Hello!' )

except simple.SimpleError as e:
    print( 'PASS caught SimpleError - %s' % (e,) )

print( 'TEST: call old style class functions' )
old_style_class = simple.old_style_class()
old_style_class.old_style_class_func_noargs()
old_style_class.old_style_class_func_varargs()
old_style_class.old_style_class_func_varargs( 4 )
old_style_class.old_style_class_func_keyword()
old_style_class.old_style_class_func_keyword( name=6, value=7 )
old_style_class.old_style_class_func_keyword( 4, 5 )
old_style_class.old_style_class_func_keyword( 4, 5, name=6, value=7 )

print( 'TEST: Derived class functions' )
class Derived(simple.new_style_class):
    def __init__( self ):
        simple.new_style_class.__init__( self )

    def derived_func( self, arg ):
        print( 'derived_func' )
        super().func_noargs()

    def derived_func_bad( self, arg ):
        print( 'derived_func_bad' )
        raise ValueError( 'derived_func_bad value error' )

    def func_noargs( self ):
        print( 'derived func_noargs' )

d = Derived()
print( dir( d ) )
d.derived_func( "arg" )
d.func_noargs()
d.func_varargs()
d.func_varargs( 4 )
d.func_keyword()
d.func_keyword( name=6, value=7 )
d.func_keyword( 4, 5 )
d.func_keyword( 4, 5, name=6, value=7 )

print( d.value )
d.value = "a string"
print( d.value )
d.new_var = 99

d.func_varargs_call_member( "derived_func" )
result = d.func_varargs_call_member( "derived_func_bad" )
print( 'derived_func_bad caught error: %r' % (result,) )

print( 'TEST: pass derived class to C++ world' )
result = simple.derived_class_test( d, 5, 9 )
print( 'derived_class_test result %r' % (result,) )

print( 'TEST: new_style_class functions' )
new_style_class = simple.new_style_class()
print( dir( new_style_class ) )
new_style_class.func_noargs()
new_style_class.func_varargs()
new_style_class.func_varargs( 4 )
new_style_class.func_keyword()
new_style_class.func_keyword( name=6, value=7 )
new_style_class.func_keyword( 4, 5 )
new_style_class.func_keyword( 4, 5, name=6, value=7 )

try:
    new_style_class.func_noargs_raise_exception()
    print( 'Error: did not raised RuntimeError' )
    sys.exit( 1 )

except RuntimeError as e:
    print( 'Raised %r' % (str(e),) )


print( 'TEST: dereference new style class' )

new_style_class = None