-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathtest.py
267 lines (224 loc) · 5.76 KB
/
test.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Copyright 2015 The go-python Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from __future__ import print_function
import hi
# NOTE: the output from python2 is different from that of 3, and test
# targets python3 so it appears to fail for 2.
print("--- doc(hi)...")
print(hi.__doc__)
print("--- hi.Universe:", hi.Universe)
print("--- hi.Version:", hi.Version)
print("--- hi.Debug():",hi.Debug())
print("--- hi.Set_Debug(true)")
hi.Set_Debug(True)
print("--- hi.Debug():",hi.Debug())
print("--- hi.Set_Debug(false)")
hi.Set_Debug(False)
print("--- hi.Debug():",hi.Debug())
print("--- hi.Anon():",hi.Anon())
anon = hi.NewPerson('you',24)
print("--- new anon:",anon)
print("--- hi.Set_Anon(hi.NewPerson('you', 24))...")
hi.Set_Anon(anon)
print("--- hi.Anon():",hi.Anon())
print("--- doc(hi.Hi)...")
print(hi.Hi.__doc__)
print("--- hi.Hi()...")
hi.Hi()
print("--- doc(hi.Hello)...")
print(hi.Hello.__doc__)
print("--- hi.Hello('you')...")
hi.Hello("you")
print("--- doc(hi.Add)...")
print(hi.Add.__doc__)
print("--- hi.Add(1, 41)...")
print(hi.Add(1,41))
print("--- hi.Concat('4', '2')...")
print(hi.Concat("4","2"))
print("--- hi.LookupQuestion(42)...")
print(hi.LookupQuestion(42))
print("--- hi.LookupQuestion(12)...")
try:
hi.LookupQuestion(12)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err)
pass
print("--- doc(hi.Person):")
print(hi.Person.__doc__)
print("--- p = hi.Person()...")
p = hi.Person()
print("--- p:", p)
print("--- p.Name:", p.Name)
print("--- p.Age:",p.Age)
print("--- doc(hi.Greet):")
print(p.Greet.__doc__)
print("--- p.Greet()...")
print(p.Greet())
print("--- p.String()...")
print(p.String())
print("--- doc(p):")
print(p.__doc__)
print("--- p.Name = \"foo\"...")
p.Name = "foo"
print("--- p.Age = 42...")
p.Age = 42
print("--- p.String()...")
print(p.String())
print("--- p.Age:", p.Age)
print("--- p.Name:",p.Name)
print("--- p.Work(2)...")
p.Work(2)
print("--- p.Work(24)...")
try:
p.Work(24)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err)
pass
print("--- p.Salary(2):", p.Salary(2))
try:
print("--- p.Salary(24):",p.Salary(24))
print("*ERROR* no exception raised!")
except Exception as err:
print("--- p.Salary(24): caught:", err)
pass
## test ctor args
print("--- Person.__init__")
try:
hi.Person(1)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err, "| err-type:",type(err))
pass
try:
hi.Person("name","2")
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err, "| err-type:",type(err))
pass
try:
hi.Person("name",2,3)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err, "| err-type:",type(err))
pass
p = hi.Person("name")
print(p)
p = hi.Person("name", 42)
print(p)
p = hi.Person(Name="name", Age=42)
print(p)
p = hi.Person(Age=42, Name="name")
print(p)
## test ctors
print("--- hi.NewPerson('me', 666):", hi.NewPerson("me", 666))
print("--- hi.NewPersonWithAge(666):", hi.NewPersonWithAge(666))
print("--- hi.NewActivePerson(4):")
p = hi.NewActivePerson(4)
print(p)
## test Couple
print("--- c = hi.Couple()...")
c = hi.Couple()
print(c)
print("--- c.P1:", c.P1)
c.P1 = hi.NewPerson("tom", 5)
c.P2 = hi.NewPerson("bob", 2)
print("--- c:", c)
print("--- c = hi.NewCouple(tom, bob)...")
c = hi.NewCouple(hi.NewPerson("tom", 50), hi.NewPerson("bob", 41))
print(c)
c.P1.Name = "mom"
c.P2.Age = 51
print(c)
## test Couple.__init__
print("--- Couple.__init__")
# Note: pybindgen does not automatically support varargs, so in general
# all python calls need to provide the full Go signature of args.
#c = hi.Couple(hi.Person("p1", 42))
#print(c)
c = hi.Couple(hi.Person("p1", 42), hi.Person("p2", 52))
print(c)
c = hi.Couple(P1=hi.Person("p1", 42), P2=hi.Person("p2", 52))
print(c)
c = hi.Couple(P2=hi.Person("p1", 42), P1=hi.Person("p2", 52))
print(c)
try:
hi.Couple(1)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err, "| err-type:",type(err))
pass
try:
hi.Couple(1, 2)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err, "| err-type:",type(err))
pass
try:
hi.Couple(P2=1)
print("*ERROR* no exception raised!")
except Exception as err:
print("caught:", err, "| err-type:",type(err))
pass
### test gc
print("--- testing GC...")
NMAX = 100000
objs = []
for i in range(NMAX):
p1 = hi.NewPerson("p1-%d" % i, i)
p2 = hi.NewPerson("p2-%d" % i, i)
objs.append(hi.NewCouple(p1,p2))
pass
print("--- len(objs):",len(objs))
vs = []
for i,o in enumerate(objs):
v = "%d: %s" % (i, o)
vs.append(v)
pass
print("--- len(vs):",len(vs))
del objs
print("--- testing GC... [ok]")
print("--- testing array...")
arr = hi.IntArray()
print("arr:",arr)
print("len(arr):",len(arr))
print("arr[0]:",arr[0])
print("arr[1]:",arr[1])
try:
print("arr[2]:", arr[2])
print("*ERROR* no exception raised!")
except Exception as err:
print("arr[2]: caught:",err)
pass
arr[1] = 42
print("arr:",arr)
print("len(arr):",len(arr))
try:
print("mem(arr):",len(memoryview(arr)))
except Exception as err:
print("mem(arr): caught:",err)
pass
print("--- testing slice...")
s = hi.IntSlice()
print("slice:",s)
print("len(slice):",len(s))
print("slice[0]:",s[0])
print("slice[1]:",s[1])
try:
print("slice[2]:", s[2])
print("*ERROR* no exception raised!")
except Exception as err:
print("slice[2]: caught:",err)
pass
s[1] = 42
print("slice:",s)
print("slice repr:",s.__repr__())
print("len(slice):",len(s))
try:
print("mem(slice):",len(memoryview(s)))
except Exception as err:
print("mem(slice): caught:",err)
pass
print("OK")