Add a comment to force emacs to use C mode.
[p5sagit/Devel-Size.git] / t / magic.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More tests => 18;
5 use Devel::Size ':all';
6 require Tie::Scalar;
7
8 {
9     my $string = 'Perl Rules';
10     my $before_size = total_size($string);
11     is($string =~ /Perl/g, 1, 'It had better match');
12     cmp_ok($before_size, '>', length $string,
13            'Our string has a non-zero length');
14     cmp_ok(total_size($string), '>', $before_size,
15            'size increases due to magic');
16 }
17
18 {
19     my $string = 'Perl Rules';
20     my $before_size = total_size($string);
21     formline $string;
22     my $compiled_size = total_size($string);
23     cmp_ok($before_size, '>', length $string,
24            'Our string has a non-zero length');
25     cmp_ok($compiled_size, '>', $before_size,
26            'size increases due to magic (and the compiled state)');
27     # Not fully sure why (didn't go grovelling) but need to use a temporary to
28     # avoid the magic being copied.
29     $string = '' . $string;
30     my $after_size = total_size($string);
31     cmp_ok($after_size, '>', $before_size, 'Still larger than initial size');
32     cmp_ok($after_size, '<', $compiled_size, 'size decreases due to unmagic');
33 }
34
35 {
36     my $string = 'Perl Rules';
37     my $before_size = total_size($string);
38     cmp_ok($before_size, '>', length $string,
39            'Our string has a non-zero length');
40     tie $string, 'Tie::StdScalar';
41     my $after_size = total_size($string);
42     cmp_ok($after_size, '>', $before_size, 'size increases due to magic');
43     is($string, undef, 'No value yet');
44     # This is defineately cheating, in that we're poking inside the
45     # implementation of Tie::StdScalar, but if we just write to $string, the way
46     # magic works, the (nice long) value is first written to the regular scalar,
47     # then picked up by the magic. So it grows, which defeats the purpose of the
48     # test.
49     ${tied $string} = 'X' x 1024;
50     cmp_ok(total_size($string), '>', $after_size + 1024,
51            'the magic object is counted');
52 }
53
54 SKIP: {
55     skip("v-strings didn't use magic before 5.8.1", 2) if $] < 5.008001;
56     my $v = eval 'v' . (0 x 1024);
57     is($v, "\0", 'v-string is \0');
58     cmp_ok(total_size($v), '>', 1024, 'total_size follows MG_PTR');
59 }
60
61 SKIP: {
62     skip("no UTF-8 caching before 5.8.1", 5) if $] < 5.008001;
63     my $string = "a\x{100}b";
64     my $before_size = total_size($string);
65     cmp_ok($before_size, '>', 0, 'Our string has a non-zero length');
66     is(length $string, 3, 'length is sane');
67     my $with_magic = total_size($string);
68     cmp_ok($with_magic, '>', $before_size, 'UTF-8 caching fired and counted');
69     is(index($string, "b"), 2, 'b is where we expect it');
70     cmp_ok(total_size($string), '>', $with_magic,
71            'UTF-8 caching length table now present');
72 }