2015-08-10 13:27:42 +00:00
|
|
|
# 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.
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
import hi
|
|
|
|
|
2015-07-25 15:35:14 +00:00
|
|
|
print "--- doc(hi)..."
|
|
|
|
print hi.__doc__
|
|
|
|
|
2015-07-31 14:54:24 +00:00
|
|
|
print "--- hi.GetUniverse():", hi.GetUniverse()
|
|
|
|
print "--- hi.GetVersion():", hi.GetVersion()
|
|
|
|
|
|
|
|
print "--- hi.GetDebug():",hi.GetDebug()
|
|
|
|
print "--- hi.SetDebug(true)"
|
|
|
|
hi.SetDebug(True)
|
|
|
|
print "--- hi.GetDebug():",hi.GetDebug()
|
|
|
|
print "--- hi.SetDebug(false)"
|
|
|
|
hi.SetDebug(False)
|
|
|
|
print "--- hi.GetDebug():",hi.GetDebug()
|
|
|
|
|
|
|
|
print "--- hi.GetAnon():",hi.GetAnon()
|
|
|
|
anon = hi.NewPerson('you',24)
|
|
|
|
print "--- new anon:",anon
|
|
|
|
print "--- hi.SetAnon(hi.NewPerson('you', 24))..."
|
|
|
|
hi.SetAnon(anon)
|
|
|
|
print "--- hi.GetAnon():",hi.GetAnon()
|
|
|
|
|
2015-07-25 15:35:14 +00:00
|
|
|
print "--- doc(hi.Hi)..."
|
|
|
|
print hi.Hi.__doc__
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
print "--- hi.Hi()..."
|
|
|
|
hi.Hi()
|
|
|
|
|
2015-07-25 15:35:14 +00:00
|
|
|
print "--- doc(hi.Hello)..."
|
|
|
|
print hi.Hello.__doc__
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
print "--- hi.Hello('you')..."
|
|
|
|
hi.Hello("you")
|
|
|
|
|
2015-07-25 15:35:14 +00:00
|
|
|
print "--- doc(hi.Add)..."
|
|
|
|
print hi.Add.__doc__
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
print "--- hi.Add(1, 41)..."
|
|
|
|
print hi.Add(1,41)
|
|
|
|
|
|
|
|
print "--- hi.Concat('4', '2')..."
|
|
|
|
print hi.Concat("4","2")
|
|
|
|
|
|
|
|
print "--- doc(hi.Person):"
|
|
|
|
print hi.Person.__doc__
|
|
|
|
|
|
|
|
print "--- p = hi.Person()..."
|
|
|
|
p = hi.Person()
|
|
|
|
print dir(p)
|
2015-07-29 10:37:32 +00:00
|
|
|
print "--- p:", p
|
2015-07-24 14:16:31 +00:00
|
|
|
|
|
|
|
print "--- p.Name:", p.Name
|
|
|
|
print "--- p.Age:",p.Age
|
|
|
|
|
2015-07-27 16:57:27 +00:00
|
|
|
print "--- doc(hi.Greet):"
|
|
|
|
print p.Greet.__doc__
|
|
|
|
print "--- p.Greet()..."
|
|
|
|
print p.Greet()
|
2015-07-27 16:46:30 +00:00
|
|
|
|
2015-07-28 08:08:27 +00:00
|
|
|
print "--- p.String()..."
|
|
|
|
print p.String()
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
print "--- doc(p):"
|
|
|
|
print p.__doc__
|
|
|
|
|
2015-07-29 09:44:37 +00:00
|
|
|
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
|
|
|
|
|
2015-07-29 12:15:31 +00:00
|
|
|
print "--- p.Work(2)..."
|
|
|
|
p.Work(2)
|
|
|
|
|
|
|
|
print "--- p.Work(24)..."
|
|
|
|
try:
|
|
|
|
p.Work(24)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err
|
|
|
|
pass
|
|
|
|
|
2015-07-29 12:50:02 +00:00
|
|
|
print "--- p.Salary(2):", p.Salary(2)
|
|
|
|
try:
|
|
|
|
print "--- p.Salary(24):",p.Salary(24)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err
|
|
|
|
pass
|
2015-07-29 14:54:26 +00:00
|
|
|
|
2015-08-10 16:02:49 +00:00
|
|
|
## test ctor args
|
|
|
|
print "--- Person.__init__"
|
2015-07-29 14:54:26 +00:00
|
|
|
try:
|
|
|
|
hi.Person(1)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
2015-07-29 15:46:02 +00:00
|
|
|
print "caught:", err, "| err-type:",type(err)
|
2015-07-29 14:54:26 +00:00
|
|
|
pass
|
2015-08-10 17:11:57 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
hi.Person("name","2")
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err, "| err-type:",type(err)
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
hi.Person("name",2,3)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err, "| err-type:",type(err)
|
|
|
|
pass
|
|
|
|
|
2015-08-10 16:02:49 +00:00
|
|
|
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
|
2015-07-29 14:54:26 +00:00
|
|
|
|
2015-07-29 15:32:51 +00:00
|
|
|
## test ctors
|
|
|
|
print "--- hi.NewPerson('me', 666):", hi.NewPerson("me", 666)
|
|
|
|
print "--- hi.NewPersonWithAge(666):", hi.NewPersonWithAge(666)
|
|
|
|
print "--- hi.NewActivePerson(4):", hi.NewActivePerson(4)
|
2015-07-29 14:54:26 +00:00
|
|
|
|
2015-07-30 14:06:31 +00:00
|
|
|
## test Couple
|
|
|
|
print "--- c = hi.Couple()..."
|
|
|
|
c = hi.Couple()
|
|
|
|
print c
|
2015-07-31 15:13:45 +00:00
|
|
|
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
|
2015-07-30 14:06:31 +00:00
|
|
|
|
2015-08-10 16:02:49 +00:00
|
|
|
## test Couple.__init__
|
|
|
|
print "--- Couple.__init__"
|
|
|
|
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
|
|
|
|
|
2015-08-10 17:11:57 +00:00
|
|
|
try:
|
|
|
|
hi.Couple(1)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err, "| err-type:",type(err)
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
hi.Couple(1,2)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err, "| err-type:",type(err)
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
hi.Couple(P2=1)
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:", err, "| err-type:",type(err)
|
|
|
|
pass
|
2015-08-10 16:02:49 +00:00
|
|
|
|
2015-08-03 11:24:48 +00:00
|
|
|
### 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]"
|
2015-08-05 15:44:49 +00:00
|
|
|
|
|
|
|
print "--- testing array..."
|
|
|
|
arr = hi.GetIntArray()
|
|
|
|
print "arr:",arr
|
|
|
|
print "len(arr):",len(arr)
|
2015-08-05 15:48:15 +00:00
|
|
|
print "arr[0]:",arr[0]
|
|
|
|
print "arr[1]:",arr[1]
|
|
|
|
try:
|
|
|
|
print "arr[2]:", arr[2]
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:",err
|
|
|
|
pass
|
2015-08-05 17:42:24 +00:00
|
|
|
arr[1] = 42
|
|
|
|
print "arr:",arr
|
|
|
|
print "len(arr):",len(arr)
|
2015-08-06 08:01:00 +00:00
|
|
|
print "mem(arr):",len(memoryview(arr))
|
2015-08-05 15:44:49 +00:00
|
|
|
|
|
|
|
print "--- testing slice..."
|
|
|
|
s = hi.GetIntSlice()
|
|
|
|
print "slice:",s
|
|
|
|
print "len(slice):",len(s)
|
2015-08-05 15:48:15 +00:00
|
|
|
print "slice[0]:",s[0]
|
|
|
|
print "slice[1]:",s[1]
|
|
|
|
try:
|
|
|
|
print "slice[2]:", s[2]
|
|
|
|
print "*ERROR* no exception raised!"
|
|
|
|
except Exception, err:
|
|
|
|
print "caught:",err
|
|
|
|
pass
|
2015-08-05 17:42:24 +00:00
|
|
|
s[1] = 42
|
|
|
|
print "slice:",s
|
|
|
|
print "len(slice):",len(s)
|
2015-08-06 08:01:00 +00:00
|
|
|
print "mem(slice):",len(memoryview(s))
|
2015-08-05 15:44:49 +00:00
|
|
|
|