Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 2 | |
| 3 | from pybind11_tests import virtual_functions as m |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 4 | from pybind11_tests import ConstructorStats |
| 5 | |
| 6 | |
| 7 | def test_override(capture, msg): |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 8 | class ExtendedExampleVirt(m.ExampleVirt): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 9 | def __init__(self, state): |
| 10 | super(ExtendedExampleVirt, self).__init__(state + 1) |
| 11 | self.data = "Hello world" |
| 12 | |
| 13 | def run(self, value): |
| 14 | print('ExtendedExampleVirt::run(%i), calling parent..' % value) |
| 15 | return super(ExtendedExampleVirt, self).run(value + 1) |
| 16 | |
| 17 | def run_bool(self): |
| 18 | print('ExtendedExampleVirt::run_bool()') |
| 19 | return False |
| 20 | |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 21 | def get_string1(self): |
| 22 | return "override1" |
| 23 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 24 | def pure_virtual(self): |
| 25 | print('ExtendedExampleVirt::pure_virtual(): %s' % self.data) |
| 26 | |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 27 | class ExtendedExampleVirt2(ExtendedExampleVirt): |
| 28 | def __init__(self, state): |
| 29 | super(ExtendedExampleVirt2, self).__init__(state + 1) |
| 30 | |
| 31 | def get_string2(self): |
| 32 | return "override2" |
| 33 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 34 | ex12 = m.ExampleVirt(10) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 35 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 36 | assert m.runExampleVirt(ex12, 20) == 30 |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 37 | assert capture == """ |
| 38 | Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2) |
| 39 | """ # noqa: E501 line too long |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 40 | |
| 41 | with pytest.raises(RuntimeError) as excinfo: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 42 | m.runExampleVirtVirtual(ex12) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 43 | assert msg(excinfo.value) == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"' |
| 44 | |
| 45 | ex12p = ExtendedExampleVirt(10) |
| 46 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 47 | assert m.runExampleVirt(ex12p, 20) == 32 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 48 | assert capture == """ |
| 49 | ExtendedExampleVirt::run(20), calling parent.. |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 50 | Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2) |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 51 | """ # noqa: E501 line too long |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 52 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 53 | assert m.runExampleVirtBool(ex12p) is False |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 54 | assert capture == "ExtendedExampleVirt::run_bool()" |
| 55 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 56 | m.runExampleVirtVirtual(ex12p) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 57 | assert capture == "ExtendedExampleVirt::pure_virtual(): Hello world" |
| 58 | |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 59 | ex12p2 = ExtendedExampleVirt2(15) |
| 60 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 61 | assert m.runExampleVirt(ex12p2, 50) == 68 |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 62 | assert capture == """ |
| 63 | ExtendedExampleVirt::run(50), calling parent.. |
| 64 | Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2) |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 65 | """ # noqa: E501 line too long |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 66 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 67 | cstats = ConstructorStats.get(m.ExampleVirt) |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 68 | assert cstats.alive() == 3 |
| 69 | del ex12, ex12p, ex12p2 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 70 | assert cstats.alive() == 0 |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 71 | assert cstats.values() == ['10', '11', '17'] |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 72 | assert cstats.copy_constructions == 0 |
| 73 | assert cstats.move_constructions >= 0 |
| 74 | |
| 75 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 76 | def test_alias_delay_initialization1(capture): |
| 77 | """`A` only initializes its trampoline class when we inherit from it |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 78 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 79 | If we just create and use an A instance directly, the trampoline initialization is |
| 80 | bypassed and we only initialize an A() instead (for performance reasons). |
| 81 | """ |
| 82 | class B(m.A): |
| 83 | def __init__(self): |
| 84 | super(B, self).__init__() |
| 85 | |
| 86 | def f(self): |
| 87 | print("In python f()") |
| 88 | |
| 89 | # C++ version |
| 90 | with capture: |
| 91 | a = m.A() |
| 92 | m.call_f(a) |
| 93 | del a |
| 94 | pytest.gc_collect() |
| 95 | assert capture == "A.f()" |
| 96 | |
| 97 | # Python version |
| 98 | with capture: |
| 99 | b = B() |
| 100 | m.call_f(b) |
| 101 | del b |
| 102 | pytest.gc_collect() |
| 103 | assert capture == """ |
| 104 | PyA.PyA() |
| 105 | PyA.f() |
| 106 | In python f() |
| 107 | PyA.~PyA() |
| 108 | """ |
| 109 | |
| 110 | |
| 111 | def test_alias_delay_initialization2(capture): |
| 112 | """`A2`, unlike the above, is configured to always initialize the alias |
| 113 | |
| 114 | While the extra initialization and extra class layer has small virtual dispatch |
| 115 | performance penalty, it also allows us to do more things with the trampoline |
| 116 | class such as defining local variables and performing construction/destruction. |
| 117 | """ |
| 118 | class B2(m.A2): |
| 119 | def __init__(self): |
| 120 | super(B2, self).__init__() |
| 121 | |
| 122 | def f(self): |
| 123 | print("In python B2.f()") |
| 124 | |
| 125 | # No python subclass version |
| 126 | with capture: |
| 127 | a2 = m.A2() |
| 128 | m.call_f(a2) |
| 129 | del a2 |
| 130 | pytest.gc_collect() |
| 131 | assert capture == """ |
| 132 | PyA2.PyA2() |
| 133 | PyA2.f() |
| 134 | A2.f() |
| 135 | PyA2.~PyA2() |
| 136 | """ |
| 137 | |
| 138 | # Python subclass version |
| 139 | with capture: |
| 140 | b2 = B2() |
| 141 | m.call_f(b2) |
| 142 | del b2 |
| 143 | pytest.gc_collect() |
| 144 | assert capture == """ |
| 145 | PyA2.PyA2() |
| 146 | PyA2.f() |
| 147 | In python B2.f() |
| 148 | PyA2.~PyA2() |
| 149 | """ |
| 150 | |
| 151 | |
| 152 | def test_inheriting_repeat(): |
| 153 | class AR(m.A_Repeat): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 154 | def unlucky_number(self): |
| 155 | return 99 |
| 156 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 157 | class AT(m.A_Tpl): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 158 | def unlucky_number(self): |
| 159 | return 999 |
| 160 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 161 | obj = AR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 162 | assert obj.say_something(3) == "hihihi" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 163 | assert obj.unlucky_number() == 99 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 164 | assert obj.say_everything() == "hi 99" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 165 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 166 | obj = AT() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 167 | assert obj.say_something(3) == "hihihi" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 168 | assert obj.unlucky_number() == 999 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 169 | assert obj.say_everything() == "hi 999" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 170 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 171 | for obj in [m.B_Repeat(), m.B_Tpl()]: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 172 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 173 | assert obj.unlucky_number() == 13 |
| 174 | assert obj.lucky_number() == 7.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 175 | assert obj.say_everything() == "B says hi 1 times 13" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 176 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 177 | for obj in [m.C_Repeat(), m.C_Tpl()]: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 178 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 179 | assert obj.unlucky_number() == 4444 |
| 180 | assert obj.lucky_number() == 888.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 181 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 182 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 183 | class CR(m.C_Repeat): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 184 | def lucky_number(self): |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 185 | return m.C_Repeat.lucky_number(self) + 1.25 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 186 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 187 | obj = CR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 188 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 189 | assert obj.unlucky_number() == 4444 |
| 190 | assert obj.lucky_number() == 889.25 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 191 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 192 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 193 | class CT(m.C_Tpl): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 194 | pass |
| 195 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 196 | obj = CT() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 197 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 198 | assert obj.unlucky_number() == 4444 |
| 199 | assert obj.lucky_number() == 888.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 200 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 201 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 202 | class CCR(CR): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 203 | def lucky_number(self): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 204 | return CR.lucky_number(self) * 10 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 205 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 206 | obj = CCR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 207 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 208 | assert obj.unlucky_number() == 4444 |
| 209 | assert obj.lucky_number() == 8892.5 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 210 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 211 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 212 | class CCT(CT): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 213 | def lucky_number(self): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 214 | return CT.lucky_number(self) * 1000 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 215 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 216 | obj = CCT() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 217 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 218 | assert obj.unlucky_number() == 4444 |
| 219 | assert obj.lucky_number() == 888000.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 220 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 221 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 222 | class DR(m.D_Repeat): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 223 | def unlucky_number(self): |
| 224 | return 123 |
| 225 | |
| 226 | def lucky_number(self): |
| 227 | return 42.0 |
| 228 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 229 | for obj in [m.D_Repeat(), m.D_Tpl()]: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 230 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 231 | assert obj.unlucky_number() == 4444 |
| 232 | assert obj.lucky_number() == 888.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 233 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 234 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 235 | obj = DR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 236 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 237 | assert obj.unlucky_number() == 123 |
| 238 | assert obj.lucky_number() == 42.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 239 | assert obj.say_everything() == "B says hi 1 times 123" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 240 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 241 | class DT(m.D_Tpl): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 242 | def say_something(self, times): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 243 | return "DT says:" + (' quack' * times) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 244 | |
| 245 | def unlucky_number(self): |
| 246 | return 1234 |
| 247 | |
| 248 | def lucky_number(self): |
| 249 | return -4.25 |
| 250 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 251 | obj = DT() |
| 252 | assert obj.say_something(3) == "DT says: quack quack quack" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 253 | assert obj.unlucky_number() == 1234 |
| 254 | assert obj.lucky_number() == -4.25 |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 255 | assert obj.say_everything() == "DT says: quack 1234" |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 256 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 257 | class DT2(DT): |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 258 | def say_something(self, times): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 259 | return "DT2: " + ('QUACK' * times) |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 260 | |
| 261 | def unlucky_number(self): |
| 262 | return -3 |
| 263 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 264 | class BT(m.B_Tpl): |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 265 | def say_something(self, times): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 266 | return "BT" * times |
| 267 | |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 268 | def unlucky_number(self): |
| 269 | return -7 |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 270 | |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 271 | def lucky_number(self): |
| 272 | return -1.375 |
| 273 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 274 | obj = BT() |
| 275 | assert obj.say_something(3) == "BTBTBT" |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 276 | assert obj.unlucky_number() == -7 |
| 277 | assert obj.lucky_number() == -1.375 |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 278 | assert obj.say_everything() == "BT -7" |
| 279 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 280 | |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 281 | # PyPy: Reference count > 1 causes call with noncopyable instance |
| 282 | # to fail in ncv1.print_nc() |
| 283 | @pytest.unsupported_on_pypy |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 284 | @pytest.mark.skipif(not hasattr(m, "NCVirt"), reason="NCVirt test broken on ICPC") |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 285 | def test_move_support(): |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 286 | class NCVirtExt(m.NCVirt): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 287 | def get_noncopyable(self, a, b): |
| 288 | # Constructs and returns a new instance: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 289 | nc = m.NonCopyable(a * a, b * b) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 290 | return nc |
| 291 | |
| 292 | def get_movable(self, a, b): |
| 293 | # Return a referenced copy |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 294 | self.movable = m.Movable(a, b) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 295 | return self.movable |
| 296 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 297 | class NCVirtExt2(m.NCVirt): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 298 | def get_noncopyable(self, a, b): |
| 299 | # Keep a reference: this is going to throw an exception |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 300 | self.nc = m.NonCopyable(a, b) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 301 | return self.nc |
| 302 | |
| 303 | def get_movable(self, a, b): |
| 304 | # Return a new instance without storing it |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 305 | return m.Movable(a, b) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 306 | |
| 307 | ncv1 = NCVirtExt() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 308 | assert ncv1.print_nc(2, 3) == "36" |
| 309 | assert ncv1.print_movable(4, 5) == "9" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 310 | ncv2 = NCVirtExt2() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 311 | assert ncv2.print_movable(7, 7) == "14" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 312 | # Don't check the exception message here because it differs under debug/non-debug mode |
| 313 | with pytest.raises(RuntimeError): |
| 314 | ncv2.print_nc(9, 9) |
| 315 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 316 | nc_stats = ConstructorStats.get(m.NonCopyable) |
| 317 | mv_stats = ConstructorStats.get(m.Movable) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 318 | assert nc_stats.alive() == 1 |
| 319 | assert mv_stats.alive() == 1 |
| 320 | del ncv1, ncv2 |
| 321 | assert nc_stats.alive() == 0 |
| 322 | assert mv_stats.alive() == 0 |
| 323 | assert nc_stats.values() == ['4', '9', '9', '9'] |
| 324 | assert mv_stats.values() == ['4', '5', '7', '7'] |
| 325 | assert nc_stats.copy_constructions == 0 |
| 326 | assert mv_stats.copy_constructions == 1 |
| 327 | assert nc_stats.move_constructions >= 0 |
| 328 | assert mv_stats.move_constructions >= 0 |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 329 | |
| 330 | |
| 331 | def test_dispatch_issue(msg): |
| 332 | """#159: virtual function dispatch has problems with similar-named functions""" |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 333 | class PyClass1(m.DispatchIssue): |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 334 | def dispatch(self): |
| 335 | return "Yay.." |
| 336 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 337 | class PyClass2(m.DispatchIssue): |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 338 | def dispatch(self): |
| 339 | with pytest.raises(RuntimeError) as excinfo: |
| 340 | super(PyClass2, self).dispatch() |
| 341 | assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"' |
| 342 | |
| 343 | p = PyClass1() |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 344 | return m.dispatch_issue_go(p) |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 345 | |
| 346 | b = PyClass2() |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 347 | assert m.dispatch_issue_go(b) == "Yay.." |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 348 | |
| 349 | |
| 350 | def test_override_ref(): |
| 351 | """#392/397: overridding reference-returning functions""" |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 352 | o = m.OverrideTest("asdf") |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 353 | |
| 354 | # Not allowed (see associated .cpp comment) |
| 355 | # i = o.str_ref() |
| 356 | # assert o.str_ref() == "asdf" |
| 357 | assert o.str_value() == "asdf" |
| 358 | |
| 359 | assert o.A_value().value == "hi" |
| 360 | a = o.A_ref() |
| 361 | assert a.value == "hi" |
| 362 | a.value = "bye" |
| 363 | assert a.value == "bye" |