2 # Test for malfunctions of utf8 cache
15 skip_without_dynamic_extension("Devel::Peek");
17 my $pid = open CHILD, '-|';
18 die "kablam: $!\n" unless defined $pid;
20 open STDERR, ">&STDOUT";
21 $a = "hello \x{1234}";
23 bar(substr($a, $_, 1));
27 Devel::Peek::Dump($_[0]);
32 { local $/; $_ = <CHILD> }
34 my $utf8magic = qr{ ^ \s+ MAGIC \s = .* \n
35 \s+ MG_VIRTUAL \s = .* \n
36 \s+ MG_TYPE \s = \s PERL_MAGIC_utf8 .* \n
37 \s+ MG_LEN \s = .* \n }xm;
39 unlike($_, qr{ $utf8magic $utf8magic }x);
43 # With bad caching, this code used to go quadratic and take 10s of minutes.
44 # The 'test' in this case is simply that it doesn't hang.
47 local ${^UTF8CACHE} = 1; # enable cache, disable debugging
48 my $x = "\x{100}" x 1000000;
52 pass("quadratic pos");
55 # Get-magic can reallocate the PV. Check that the cache is reset in
62 is ord substr($1, 1, 1), 0x100, 'get-magic resets utf8cache on match vars';
66 my $l = \substr $x, 0;
67 () = substr $$l, 1, 1;
68 substr $x, 0, 1, = "\x{100}";
69 is ord substr($$l, 1, 1), 0x100, 'get-magic resets utf8cache on LVALUEs';
75 () = ord substr $_[0], 1, 1;
77 is ord substr($_[0], 1, 1), 0x100,
78 'get-magic resets uf8cache on defelems';
82 # Overloading can also reallocate the PV.
85 use overload '""' => 'stringify', fallback => 1;
91 return bless [$value, $state], $class;
96 $self->[1] = ! $self->[1];
98 utf8::downgrade($self->[0]);
100 utf8::upgrade($self->[0]);
105 my $u = UTF8Toggle->new(" \x{c2}7 ");
108 is pos $u, 2, 'pos on overloaded utf8 toggler';
109 () = "$u"; # flip flag
111 is pos $u, 2, 'pos on overloaded utf8 toggler (again)';
113 () = ord ${\substr $u, 1};
114 is ord ${\substr($u, 1)}, 0xc2,
115 'utf8 cache + overloading does not confuse substr lvalues';
116 () = "$u"; # flip flag
117 () = ord substr $u, 1;
118 is ord substr($u, 1), 0xc2,
119 'utf8 cache + overloading does not confuse substr lvalues (again)';
121 $u = UTF8Toggle->new(" \x{c2}7 ");
122 () = ord ${\substr $u, 2};
123 { no warnings; ${\substr($u, 2, 1)} = 0; }
125 'utf8 cache + overloading does not confuse substr lvalue assignment';
126 $u = UTF8Toggle->new(" \x{c2}7 ");
127 () = "$u"; # flip flag
128 () = ord ${\substr $u, 2};
129 { no warnings; ${\substr($u, 2, 1)} = 0; }
131 'utf8 cache + overload does not confuse substr lv assignment (again)';
134 # Typeglobs and references should not get a cache
138 my $globref = \*αabcdefg_::_;
139 () = substr($$globref, 2, 3);
140 *_abcdefgα:: = \%αabcdefg_::;
142 { no strict; () = *{"_abcdefgα::_"} }
143 is substr($$globref, 2, 3), "abc", 'no utf8 pos cache on globs';
145 my $ref = bless [], "αabcd_";
146 () = substr($ref, 1, 3);
147 bless $ref, "_abcdα";
148 is substr($ref, 1, 3), "abc", 'no utf8 pos cache on references';
151 $globref = \*αabcdefg_::_;
152 () = "$$globref"; # turn utf8 flag on
153 () = length($$globref);
154 *_abcdefgα:: = \%αabcdefg_::;
156 { no strict; () = *{"_abcdefgα::_"} }
157 is length($$globref), length("$$globref"), 'no utf8 length cache on globs';
159 $ref = bless [], "αabcd_";
160 () = "$ref"; # turn utf8 flag on
163 is length $ref, length "$ref", 'no utf8 length cache on references';