Debian Codegraph @

gendata/gen-head

raw
1 #!perl
2 use strict;
3 use warnings;
4
5 # functions for gen-* scripts : testcount(), testhead() and testnew().
6
7 require './dumpstr'; # for string()
8
9 sub testcount ($$;$) {
10 my $r_ok = shift; # reference to the counter
11 my $test = shift; # give a test script or increase by
12 my $mult = shift; # multiplier (tests in a loop)
13
14 my $numeric = $test =~ /^\d+\z/;
15 my $num_ok = $test =~ s/ok\(/ok\(/g;
16 $$r_ok += $numeric ? $test : ($mult ? $mult : 1) * $num_ok;
17 # if $test is real, ok() in the script will be counted.
18 # if $numeric, only increase in $r_ok.
19
20 my $retval;
21 $retval .= "{\n" if $mult;
22 $retval .= $test if !$numeric;
23 $retval .= "}\n" if $mult;
24 $retval .= "\n# $$r_ok\n\n";
25 return $retval;
26 }
27
28 sub testhead ($$) {
29 my $loc = shift; # no Unicode::Collate::Locale->new() if $loc is false
30 my $ok = shift;
31 my $lc = lc $loc;
32 my $uc = uc $loc;
33 my $uf = ucfirst $lc;
34
35 my $head = <<'HEADER';
36 BEGIN {
37 if ($ENV{PERL_CORE}) {
38 chdir('t') if -d 't';
39 @INC = $^O eq 'MacOS' ? qw(::lib) : qw(../lib);
40 }
41 }
42 HEADER
43
44 my $locale = <<"LOCALE";
45 use strict;
46 use warnings;
47 BEGIN { \$| = 1; print "1..$ok\\n"; }
48 my \$count = 0;
49 sub ok (\$;\$) {
50 my \$p = my \$r = shift;
51 if (\@_) {
52 my \$x = shift;
53 \$p = !defined \$x ? !defined \$r : !defined \$r ? 0 : \$r eq \$x;
54 }
55 print \$p ? "ok" : "not ok", ' ', ++\$count, "\\n";
56 }
57
58 use Unicode::Collate;
59
60 ok(1);
61
62 sub _pack_U { Unicode::Collate::pack_U(\@_) }
63 sub _unpack_U { Unicode::Collate::unpack_U(\@_) }
64
65 #########################
66 LOCALE
67
68 $locale =~ s/(use Unicode::Collate)/${1}::Locale/ if $loc;
69
70 my $retval = "\n$head\n$locale";
71 $retval .= "\n".testnew($uf, $uc) if $loc;
72 return $retval;
73 }
74
75 sub testnew ($$) {
76 my $obj = shift;
77 my $loc = shift;
78 my $ret = lc $loc;
79 return <<"NEW_OK";
80 my \$obj$obj = Unicode::Collate::Locale->
81 new(locale => '$loc', normalization => undef);
82
83 ok(\$obj$obj->getlocale, '$ret');
84
85 NEW_OK
86 }
87
88 sub testprim ($$) {
89 my $objn = shift; # object identifier following "\$obj"
90 my $list = shift; # arrayref of codepoints in hexadecimal
91 my $pre = pack('U', hex shift @$list);
92 my $retv = '';
93 for my $h (@$list) {
94 my $cur = pack('U', hex $h);
95 my $spre = string($pre);
96 my $scur = string($cur);
97 $retv .= qq|ok(\$obj$objn->lt($spre, $scur));\n|;
98 $pre = $cur;
99 }
100 return $retv;
101 }
102
103 1;