File: 08hash_entersub.t

package info (click to toggle)
libclass-xsaccessor-perl 1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 520 kB
  • sloc: perl: 841; ansic: 359; makefile: 6
file content (348 lines) | stat: -rw-r--r-- 16,375 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env perl

use strict;
use warnings;

use Class::XSAccessor;

BEGIN {
    unless (Class::XSAccessor::__entersub_optimized__()) {
        print "1..0 # Skip entersub optimization not enabled", $/;
        exit;
    }
}

use Test::More tests => 103;
# use Data::Dumper; $Data::Dumper::Terse = $Data::Dumper::Indent = 1;

our @WARNINGS = ();

use Class::XSAccessor
    constructor => 'new',
    __tests__   => [ qw(foo bar) ];

sub baz {
    my $self = shift;
    @_ ? $self->{baz} = shift : $self->{baz}
}

# standard: verify that the subs work as expected
sub test1 {
    my $self = shift;
    is($self->foo('foo1'), 'foo1');
    is($self->foo(), 'foo1');
    is($self->{foo}, 'foo1');
    is($self->bar('bar1'), 'bar1');
    is($self->bar(), 'bar1');
    is($self->{bar}, 'bar1');
}

# loop: verify that the second time through, the optimized entersub is called
sub test2 {
    my $self = shift;
    for (1 .. 2) {
        is($self->foo('foo2'), 'foo2');
        is($self->foo(), 'foo2');
        is($self->{foo}, 'foo2');
        is($self->bar('bar2'), 'bar2');
        is($self->bar(), 'bar2');
        is($self->{bar}, 'bar2');
    }
}

# dynamic
sub test3 {
    my $self = shift;
    for my $name (qw(foo bar)) {
        is($self->$name("${name}3"), "${name}3");
        is($self->$name(), "${name}3");
        is($self->{$name}, "${name}3");
    }
}

# dynamic with a twist: the second sub isn't a Class::XSAccessor XSUB.
# this should disable the optimization for the two entersub calls,
# and (of course) still work as expected for foo and baz.
# the bar accessor should still be optimizing
sub test4 {
    my $self = shift;
    for my $name (qw(foo baz)) {
        is($self->$name("${name}4"), "${name}4");
        is($self->$name(), "${name}4");
        is($self->{$name}, "${name}4");
    }
    is($self->bar('bar4'), 'bar4');
    is($self->bar(), 'bar4');
    is($self->{bar}, 'bar4');
}

# call the methods as subs to see how this impacts the optimized entersub. XXX: passed as GVs
sub test5 {
    my $self = shift;
    is(foo($self, 'foo5'), 'foo5');
    is(foo($self), 'foo5');
    is($self->{foo}, 'foo5');
    is(bar($self, 'bar5'), 'bar5');
    is(bar($self), 'bar5');
    is($self->{bar}, 'bar5');
}

# call the methods as subs with & (this sets a flag in the entersub's op_private)
# XXX: these are passed in as GVs rather than CVs, which the optimization doesn't currently support
sub test6 {
    my $self = shift;
    is(&foo($self, 'foo6'), 'foo6');
    is(&foo($self), 'foo6');
    is($self->{foo}, 'foo6');
    is(&bar($self, 'bar6'), 'bar6');
    is(bar($self), 'bar6');
    is($self->{bar}, 'bar6');
}

# call the methods with $self->can('accessor_name') to see how this impacts the optimized entersub.
# XXX: methods found by can() are passed in as GVs, which the optimization doesn't currently
# support
sub test7 {
    my $self = shift;
    is($self->can('foo')->($self, 'foo7'), 'foo7');
    is($self->can('foo')->($self), 'foo7');
    is($self->{foo}, 'foo7');
    is($self->can('bar')->($self, 'bar7'), 'bar7');
    is($self->can('bar')->($self), 'bar7');
    is($self->{bar}, 'bar7');
}

$SIG{__WARN__} = sub {
    my $warning = join '', @_;

    if ($warning =~ m{^cxah: (.+)\n$}) {
        push @WARNINGS, $1;
    } else {
        warn @_; # from perldoc -f warn: "__WARN__ hooks are not called from inside one"
    }
};

my $self = main->new();

$self->test1();
$self->test2();
$self->test3();
$self->test4();
$self->test5();
$self->test6();
$self->test7();

$self->test1();
$self->test2();
$self->test3();
$self->test4();
$self->test5();
$self->test6();
$self->test7();

# The best way to verify this test is to a) look for lines above that should disable
# optimization and search for "disabling" below (e.g. ack disabling t/08hash_entersub.t),
# and/or b) look for "disabling" below and make sure it matches the behaviours above

my $WANT = [
    'accessor: inside test_init at t/08hash_entersub.t line 32.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 32.',
    'accessor: inside test_init at t/08hash_entersub.t line 33.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 33.',
    'accessor: inside test_init at t/08hash_entersub.t line 35.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 35.',
    'accessor: inside test_init at t/08hash_entersub.t line 36.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 36.',
    'accessor: inside test_init at t/08hash_entersub.t line 44.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 44.',
    'accessor: inside test_init at t/08hash_entersub.t line 45.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 45.',
    'accessor: inside test_init at t/08hash_entersub.t line 47.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 47.',
    'accessor: inside test_init at t/08hash_entersub.t line 48.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 48.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 44.',
    'accessor: inside test at t/08hash_entersub.t line 44.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 45.',
    'accessor: inside test at t/08hash_entersub.t line 45.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 47.',
    'accessor: inside test at t/08hash_entersub.t line 47.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 48.',
    'accessor: inside test at t/08hash_entersub.t line 48.',
    'accessor: inside test_init at t/08hash_entersub.t line 57.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 57.',
    'accessor: inside test_init at t/08hash_entersub.t line 58.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 58.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 57.',
    'accessor: inside test at t/08hash_entersub.t line 57.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 58.',
    'accessor: inside test at t/08hash_entersub.t line 58.',
    'accessor: inside test_init at t/08hash_entersub.t line 70.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 70.',
    'accessor: inside test_init at t/08hash_entersub.t line 71.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 71.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 70.',
    'entersub: disabling optimization: CV is not test at t/08hash_entersub.t line 70.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 71.',
    'entersub: disabling optimization: CV is not test at t/08hash_entersub.t line 71.',
    'accessor: inside test_init at t/08hash_entersub.t line 74.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 74.',
    'accessor: inside test_init at t/08hash_entersub.t line 75.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 75.',
    'accessor: inside test_init at t/08hash_entersub.t line 82.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 82.',
    'accessor: inside test_init at t/08hash_entersub.t line 83.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 83.',
    'accessor: inside test_init at t/08hash_entersub.t line 85.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 85.',
    'accessor: inside test_init at t/08hash_entersub.t line 86.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 86.',
    'accessor: inside test_init at t/08hash_entersub.t line 94.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 94.',
    'accessor: inside test_init at t/08hash_entersub.t line 95.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 95.',
    'accessor: inside test_init at t/08hash_entersub.t line 97.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 97.',
    'accessor: inside test_init at t/08hash_entersub.t line 98.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 98.',
    'accessor: inside test_init at t/08hash_entersub.t line 107.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 107.',
    'accessor: inside test_init at t/08hash_entersub.t line 108.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 108.',
    'accessor: inside test_init at t/08hash_entersub.t line 110.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 110.',
    'accessor: inside test_init at t/08hash_entersub.t line 111.',
    'accessor: op_spare: 0',
    'accessor: optimizing entersub at t/08hash_entersub.t line 111.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 32.',
    'accessor: inside test at t/08hash_entersub.t line 32.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 33.',
    'accessor: inside test at t/08hash_entersub.t line 33.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 35.',
    'accessor: inside test at t/08hash_entersub.t line 35.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 36.',
    'accessor: inside test at t/08hash_entersub.t line 36.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 44.',
    'accessor: inside test at t/08hash_entersub.t line 44.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 45.',
    'accessor: inside test at t/08hash_entersub.t line 45.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 47.',
    'accessor: inside test at t/08hash_entersub.t line 47.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 48.',
    'accessor: inside test at t/08hash_entersub.t line 48.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 44.',
    'accessor: inside test at t/08hash_entersub.t line 44.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 45.',
    'accessor: inside test at t/08hash_entersub.t line 45.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 47.',
    'accessor: inside test at t/08hash_entersub.t line 47.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 48.',
    'accessor: inside test at t/08hash_entersub.t line 48.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 57.',
    'accessor: inside test at t/08hash_entersub.t line 57.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 58.',
    'accessor: inside test at t/08hash_entersub.t line 58.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 57.',
    'accessor: inside test at t/08hash_entersub.t line 57.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 58.',
    'accessor: inside test at t/08hash_entersub.t line 58.',
    'accessor: inside test_init at t/08hash_entersub.t line 70.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 70.',
    'accessor: inside test_init at t/08hash_entersub.t line 71.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 71.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 74.',
    'accessor: inside test at t/08hash_entersub.t line 74.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 75.',
    'accessor: inside test at t/08hash_entersub.t line 75.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 82.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 82.',
    'accessor: inside test_init at t/08hash_entersub.t line 82.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 82.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 83.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 83.',
    'accessor: inside test_init at t/08hash_entersub.t line 83.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 83.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 85.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 85.',
    'accessor: inside test_init at t/08hash_entersub.t line 85.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 85.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 86.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 86.',
    'accessor: inside test_init at t/08hash_entersub.t line 86.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 86.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 94.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 94.',
    'accessor: inside test_init at t/08hash_entersub.t line 94.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 94.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 95.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 95.',
    'accessor: inside test_init at t/08hash_entersub.t line 95.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 95.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 97.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 97.',
    'accessor: inside test_init at t/08hash_entersub.t line 97.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 97.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 98.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 98.',
    'accessor: inside test_init at t/08hash_entersub.t line 98.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 98.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 107.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 107.',
    'accessor: inside test_init at t/08hash_entersub.t line 107.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 107.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 108.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 108.',
    'accessor: inside test_init at t/08hash_entersub.t line 108.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 108.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 110.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 110.',
    'accessor: inside test_init at t/08hash_entersub.t line 110.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 110.',
    'entersub: inside optimized entersub at t/08hash_entersub.t line 111.',
    'entersub: disabling optimization: sv is not a CV at t/08hash_entersub.t line 111.',
    'accessor: inside test_init at t/08hash_entersub.t line 111.',
    'accessor: op_spare: 1',
    'accessor: entersub optimization has been disabled at t/08hash_entersub.t line 111.'
];

is_deeply(\@WARNINGS, $WANT);

# print STDERR Dumper(\@WARNINGS), $/;