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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
package t::DataSets;
use warnings;
use strict;
use Params::Classify qw(is_string is_ref is_strictly_blessed);
use XML::Easy::Content ();
use XML::Easy::Element ();
use parent "Exporter";
our @EXPORT_OK = map { ("COUNT_$_", "foreach_$_") } (
(map { ("no_$_") } qw(
string
array
hash
array_or_content_object
string_or_array_or_content_object_or_element
)),
(map { ("yes_$_", "ch_no_$_") } qw(
char
namestartchar
namechar
)),
(map { ("yes_$_", "string_no_$_", "no_$_") } qw(
name
encname
chardata
)),
(map { ("yes_$_", "hash_no_$_", "no_$_") } qw(
attributes
)),
(map { ("yes_$_", "array_no_$_", "no_$_") } qw(
content_twine
)),
(map { ("yes_$_", "no_$_") } qw(
content_object
content
element
)),
);
sub memoise($) {
my($evaluate) = @_;
my $value;
return sub () {
# Note perl bug (#63540): in some versions of perl,
# including 5.8.9 and 5.10.0, this function is liable to
# produce silly results, for reasons that are unknown at
# the time of writing. Empirically, adding the "if(0)"
# no-op here makes perl behave.
if(0) { }
if(defined $evaluate) {
$value = $evaluate->();
$evaluate = undef;
}
return $value;
};
}
sub count($) {
my($foreach_func) = @_;
my $count = 0;
$foreach_func->(sub { $count++ });
return $count;
}
my $c0 = XML::Easy::Content->new([ "bop" ]);
my $e0 = XML::Easy::Element->new("foo", {}, $c0);
my @interesting_value = (
undef,
"",
"abc",
do { no warnings "utf8"; "\x{d800}" },
do { no warnings "utf8"; "\x{ffffffff}" },
*STDOUT,
\"",
[],
{},
sub{},
bless({},"main"),
bless({},"ARRAY"),
bless([],"main"),
bless([],"HASH"),
$c0,
$e0,
[""],
);
sub foreach_interesting_value($) {
my($do) = @_;
$do->($_) foreach @interesting_value;
}
sub foreach_no_string($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless is_string($value);
}
}
*COUNT_no_string = memoise sub { count(\&foreach_no_string) };
sub foreach_no_array($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless is_ref($value, "ARRAY");
}
}
*COUNT_no_array = memoise sub { count(\&foreach_no_array) };
sub foreach_no_hash($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless is_ref($value, "HASH");
}
}
*COUNT_no_hash = memoise sub { count(\&foreach_no_hash) };
sub foreach_no_array_or_content_object($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless
is_ref($value, "ARRAY") ||
is_strictly_blessed($value, "XML::Easy::Content");
}
}
*COUNT_no_array_or_content_object =
memoise sub { count(\&foreach_no_array_or_content_object) };
sub foreach_no_string_or_array_or_content_object_or_element($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless
is_string($value) ||
is_ref($value, "ARRAY") ||
is_strictly_blessed($value, "XML::Easy::Content") ||
is_strictly_blessed($value, "XML::Easy::Element");
}
}
*COUNT_no_string_or_array_or_content_object_or_element = memoise sub {
count(\&foreach_no_string_or_array_or_content_object_or_element)
};
sub foreach_hex_char($@) {
my $do = shift(@_);
foreach(@_) {
no warnings "utf8";
my $c = chr(hex($_));
die "chr/ord failure" unless sprintf("%x", ord($c)) eq $_;
$do->($c);
}
}
*COUNT_yes_char = memoise sub { 3 + 11 + 3 + 4 + 4 };
sub foreach_yes_char($) {
foreach_hex_char $_[0], qw(
9 a d
20 26 3a 3c 3e 7f 80 9f a0 a3 d7ff
e000 fdd0 fffd
10000 1fffd 1fffe 1ffff
20000 10fffd 10fffe 10ffff
);
}
*COUNT_ch_no_char = memoise sub { 3 + 2 + 2 + 2 + 2 + 4 };
sub foreach_ch_no_char($) {
foreach_hex_char $_[0], qw(
0 1 8
b c
e 1f
d800 dfff
fffe ffff
110000 7fffffff 80000000 ffffffff
);
}
*COUNT_yes_namestartchar = memoise sub { 1 + 2 + 3 + 1 + 1 };
sub foreach_yes_namestartchar($) {
foreach_hex_char $_[0], qw(
3a
41 7a
c0 d6 ff
b5c
d7a3
);
}
sub foreach_ch_no_namestartchar($) {
my($do) = @_;
&foreach_ch_no_char;
foreach_hex_char $do, qw(
9 a d 20 21 26 2d 30
3c 3e
7e 7f
80 9f a0 bf
b5e
d7a4
);
foreach_yes_char sub { $do->($_[0]) if ord($_[0]) > 0xd7a4; };
}
*COUNT_ch_no_namestartchar =
memoise sub { count(\&foreach_ch_no_namestartchar) };
*COUNT_yes_namechar = memoise sub { 3 + 2 + 3 + 1 + 1 };
sub foreach_yes_namechar($) {
foreach_hex_char $_[0], qw(
2d 30 3a
41 7a
c0 d6 ff
b5c
d7a3
);
}
sub foreach_ch_no_namechar($) {
my($do) = @_;
&foreach_ch_no_char;
foreach_hex_char $do, qw(
9 a d 20 21 26
3c 3e
7e 7f
80 9f a0 bf
b5e
d7a4
);
foreach_yes_char sub { $do->($_[0]) if ord($_[0]) > 0xd7a4; };
}
*COUNT_ch_no_namechar = memoise sub { count(\&foreach_ch_no_namechar) };
*COUNT_yes_name =
memoise sub { 3*COUNT_yes_namestartchar() + COUNT_yes_namechar()*2 };
sub foreach_yes_name($) {
my($do) = @_;
foreach_yes_namestartchar sub { my($a) = @_;
foreach my $b ("", "a", "a"x40000) {
$do->($a.$b);
}
};
foreach my $b ("a", "a"x40000) {
foreach_yes_namechar sub { my($c) = @_;
$do->($b.$c);
};
}
}
*COUNT_string_no_name = memoise sub {
return 1 + 3*COUNT_ch_no_namestartchar() + COUNT_ch_no_namechar()*2;
};
sub foreach_string_no_name($) {
my($do) = @_;
$do->("");
foreach_ch_no_namestartchar sub { my($a) = @_;
foreach my $b ("", "a", "a"x40000) {
$do->($a.$b);
}
};
foreach my $b ("a", "a"x40000) {
foreach_ch_no_namechar sub { my($c) = @_;
$do->($b.$c);
};
}
}
*COUNT_no_name = memoise sub { COUNT_no_string() + COUNT_string_no_name() };
sub foreach_no_name($) {
&foreach_no_string;
&foreach_string_no_name;
}
*COUNT_yes_encnamestartchar = memoise sub { 4 };
sub foreach_yes_encnamestartchar($) {
foreach_hex_char $_[0], qw(
41 5a
61 7a
);
}
sub foreach_ch_no_encnamestartchar($) {
my($do) = @_;
&foreach_ch_no_char;
foreach_hex_char $do, qw(
9 a d 20 21 26 2c
2d 2e
2f
30 39
3a 40
5b 5e
5f
60
7e 7f
);
foreach_yes_char sub { $do->($_[0]) if ord($_[0]) >= 0x80; };
}
*COUNT_ch_no_encnamestartchar =
memoise sub { count(\&foreach_ch_no_encnamestartchar) };
*COUNT_yes_encnamechar = memoise sub { 9 };
sub foreach_yes_encnamechar($) {
foreach_hex_char $_[0], qw(
2d 2e
30 39
41 5a
5f
61 7a
);
}
sub foreach_ch_no_encnamechar($) {
my($do) = @_;
&foreach_ch_no_char;
foreach_hex_char $do, qw(
9 a d 20 21 26 2c
2f
3a 40
5b 5e
60
7e 7f
);
foreach_yes_char sub { $do->($_[0]) if ord($_[0]) >= 0x80; };
}
*COUNT_ch_no_encnamechar = memoise sub { count(\&foreach_ch_no_encnamechar) };
*COUNT_yes_encname = memoise sub {
return 3*COUNT_yes_encnamestartchar() + COUNT_yes_encnamechar()*2;
};
sub foreach_yes_encname($) {
my($do) = @_;
foreach_yes_encnamestartchar sub { my($a) = @_;
foreach my $b ("", "a", "a"x40000) {
$do->($a.$b);
}
};
foreach my $b ("a", "a"x40000) {
foreach_yes_encnamechar sub { my($c) = @_;
$do->($b.$c);
};
}
}
*COUNT_string_no_encname = memoise sub {
return 1 + 3*COUNT_ch_no_encnamestartchar() +
COUNT_ch_no_encnamechar()*2;
};
sub foreach_string_no_encname($) {
my($do) = @_;
$do->("");
foreach_ch_no_encnamestartchar sub { my($a) = @_;
foreach my $b ("", "a", "a"x40000) {
$do->($a.$b);
}
};
foreach my $b ("a", "a"x40000) {
foreach_ch_no_encnamechar sub { my($c) = @_;
$do->($b.$c);
};
}
}
*COUNT_no_encname =
memoise sub { COUNT_no_string() + COUNT_string_no_encname() };
sub foreach_no_encname($) {
&foreach_no_string;
&foreach_string_no_encname;
}
*COUNT_yes_chardata =
memoise sub { 1 + 3 * COUNT_yes_char() * 2 + COUNT_yes_char() };
sub foreach_yes_chardata($) {
my($do) = @_;
$do->("");
foreach my $a ("", "a", "a"x40000) {
foreach_yes_char sub { my($b) = @_;
$do->($a.$b);
$do->($b.$a);
};
}
foreach_yes_char sub { $do->($_[0] x 40000) };
}
*COUNT_string_no_chardata =
memoise sub { 3 * COUNT_ch_no_char() * 2 + COUNT_ch_no_char() * 2 };
sub foreach_string_no_chardata($) {
my($do) = @_;
foreach my $a ("", "a", "a"x40000) {
foreach_ch_no_char sub { my($b) = @_;
$do->($a.$b);
$do->($b.$a);
};
}
foreach_ch_no_char sub {
$do->("abc".$_[0]."xyz");
$do->($_[0] x 40000);
};
}
*COUNT_no_chardata =
memoise sub { COUNT_no_string() + COUNT_string_no_chardata() };
sub foreach_no_chardata($) {
&foreach_no_string;
&foreach_string_no_chardata;
}
*COUNT_yes_attributes =
memoise sub { 2 + COUNT_yes_name() + COUNT_yes_chardata() };
sub foreach_yes_attributes($) {
my($do) = @_;
$do->({});
$do->({ map { ("a$_" => "z$_") } 0..99 });
foreach_yes_name sub { $do->({ $_[0] => "foo" }) };
foreach_yes_chardata sub { $do->({ foo => $_[0] }) };
}
*COUNT_hash_no_attributes =
memoise sub { COUNT_string_no_name() + COUNT_no_chardata() };
sub foreach_hash_no_attributes($) {
my($do) = @_;
foreach_string_no_name sub { $do->({ $_[0] => "foo" }) };
foreach_no_chardata sub { $do->({ foo => $_[0] }) };
}
*COUNT_no_attributes =
memoise sub { COUNT_no_hash() + COUNT_hash_no_attributes() };
sub foreach_no_attributes($) {
&foreach_no_hash;
&foreach_hash_no_attributes;
}
*COUNT_yes_content_object = memoise sub { 1 };
sub foreach_yes_content_object($) {
my($do) = @_;
$do->($c0);
}
sub foreach_no_content_object($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless
is_strictly_blessed($value, "XML::Easy::Content");
}
}
*COUNT_no_content_object = memoise sub { count(\&foreach_no_content_object) };
*COUNT_yes_content_twine = memoise sub { 3*COUNT_yes_chardata() };
sub foreach_yes_content_twine($) {
my($do) = @_;
foreach_yes_chardata sub {
$do->([ $_[0] ]);
$do->([ $_[0], $e0, "y" ]);
$do->([ "x", $e0, $_[0] ]);
};
}
sub COUNT_no_element();
sub foreach_no_element($);
*COUNT_array_no_content_twine =
memoise sub { 2 + 3*COUNT_no_chardata() + COUNT_no_element() };
sub foreach_array_no_content_twine($) {
my($do) = @_;
$do->($_) foreach [ ], [ "x", $e0 ];
foreach_no_chardata sub {
$do->([ $_[0] ]);
$do->([ $_[0], $e0, "y" ]);
$do->([ "x", $e0, $_[0] ]);
};
foreach_no_element sub { $do->([ "x", $_[0], "y" ]) };
}
*COUNT_no_content_twine =
memoise sub { COUNT_no_array() + COUNT_array_no_content_twine() };
sub foreach_no_content_twine($) {
&foreach_no_array;
&foreach_array_no_content_twine;
}
*COUNT_yes_content =
memoise sub { COUNT_yes_content_object() + COUNT_yes_content_twine() };
sub foreach_yes_content($) {
&foreach_yes_content_object;
&foreach_yes_content_twine;
}
*COUNT_no_content = memoise sub {
return COUNT_no_array_or_content_object() +
COUNT_array_no_content_twine();
};
sub foreach_no_content($) {
&foreach_no_array_or_content_object;
&foreach_array_no_content_twine;
}
*COUNT_yes_element = memoise sub { 1 };
sub foreach_yes_element($) {
my($do) = @_;
$do->($e0);
}
sub foreach_no_element($) {
my($do) = @_;
foreach_interesting_value sub { my($value) = @_;
$do->($value) unless
is_strictly_blessed($value, "XML::Easy::Element");
}
}
*COUNT_no_element = memoise sub { count(\&foreach_no_element) };
1;
|