blob: 9b175fbcef68a4ab08dc7b3a1824282d27c15dac [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001#!/usr/bin/env python
2from __future__ import print_function
3import sys
4sys.path.append('.')
5
6from example import ExampleVirt, runExampleVirt, runExampleVirtVirtual, runExampleVirtBool
Jason Rhinelander0ca96e22016-08-05 17:02:33 -04007from example import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_MI, B_MI, C_MI, D_MI, A_Tpl, B_Tpl, C_Tpl, D_Tpl
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04008
9class ExtendedExampleVirt(ExampleVirt):
10 def __init__(self, state):
11 super(ExtendedExampleVirt, self).__init__(state + 1)
12 self.data = "Hello world"
13
14 def run(self, value):
15 print('ExtendedExampleVirt::run(%i), calling parent..' % value)
16 return super(ExtendedExampleVirt, self).run(value + 1)
17
18 def run_bool(self):
19 print('ExtendedExampleVirt::run_bool()')
20 return False
21
22 def pure_virtual(self):
23 print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)
24
25
26ex12 = ExampleVirt(10)
27print(runExampleVirt(ex12, 20))
28try:
29 runExampleVirtVirtual(ex12)
30except Exception as e:
31 print("Caught expected exception: " + str(e))
32
33ex12p = ExtendedExampleVirt(10)
34print(runExampleVirt(ex12p, 20))
35print(runExampleVirtBool(ex12p))
36runExampleVirtVirtual(ex12p)
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040037
38sys.stdout.flush()
39
40class VI_AR(A_Repeat):
41 def unlucky_number(self):
42 return 99
43class VI_AMI(A_MI):
44 def unlucky_number(self):
45 return 990
46 def say_something(self, times):
47 return A_MI.say_something(self, 2*times)
48class VI_AT(A_Tpl):
49 def unlucky_number(self):
50 return 999
51
52class VI_CR(C_Repeat):
53 def lucky_number(self):
54 return C_Repeat.lucky_number(self) + 1.25
55class VI_CMI(C_MI):
56 def lucky_number(self):
57 return 1.75
58class VI_CT(C_Tpl):
59 pass
60class VI_CCR(VI_CR):
61 def lucky_number(self):
62 return VI_CR.lucky_number(self) * 10
63class VI_CCMI(VI_CMI):
64 def lucky_number(self):
65 return VI_CMI.lucky_number(self) * 100
66class VI_CCT(VI_CT):
67 def lucky_number(self):
68 return VI_CT.lucky_number(self) * 1000
69
70
71class VI_DR(D_Repeat):
72 def unlucky_number(self):
73 return 123
74 def lucky_number(self):
75 return 42.0
76class VI_DMI(D_MI):
77 def unlucky_number(self):
78 return 1230
79 def lucky_number(self):
80 return -9.5
81class VI_DT(D_Tpl):
82 def say_something(self, times):
83 print("VI_DT says:" + (' quack' * times))
84 def unlucky_number(self):
85 return 1234
86 def lucky_number(self):
87 return -4.25
88
89classes = [
90 # A_Repeat, A_MI, A_Tpl, # abstract (they have a pure virtual unlucky_number)
91 VI_AR, VI_AMI, VI_AT,
92 B_Repeat, B_MI, B_Tpl,
93 C_Repeat, C_MI, C_Tpl,
94 VI_CR, VI_CMI, VI_CT, VI_CCR, VI_CCMI, VI_CCT,
95 D_Repeat, D_MI, D_Tpl, VI_DR, VI_DMI, VI_DT
96]
97
98for cl in classes:
99 print("\n%s:" % cl.__name__)
100 obj = cl()
101 obj.say_something(3)
102 print("Unlucky = %d" % obj.unlucky_number())
103 if hasattr(obj, "lucky_number"):
104 print("Lucky = %.2f" % obj.lucky_number())
105