Upgrade to CPAN 1.58, from Andreas König.
[p5sagit/p5-mst-13.2.git] / t / op / tr.t
CommitLineData
c8e3bb4c 1# tr.t
2
f05dd7cc 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
f05dd7cc 6}
a5095b95 7
d897a58d 8print "1..27\n";
c8e3bb4c 9
10$_ = "abcdefghijklmnopqrstuvwxyz";
11
12tr/a-z/A-Z/;
13
14print "not " unless $_ eq "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
15print "ok 1\n";
16
17tr/A-Z/a-z/;
18
19print "not " unless $_ eq "abcdefghijklmnopqrstuvwxyz";
20print "ok 2\n";
21
22tr/b-y/B-Y/;
23
24print "not " unless $_ eq "aBCDEFGHIJKLMNOPQRSTUVWXYz";
25print "ok 3\n";
26
27# In EBCDIC 'I' is \xc9 and 'J' is \0xd1, 'i' is \x89 and 'j' is \x91.
28# Yes, discontinuities. Regardless, the \xca in the below should stay
29# untouched (and not became \x8a).
5e037136 30{
31 no utf8;
32 $_ = "I\xcaJ";
c8e3bb4c 33
5e037136 34 tr/I-J/i-j/;
c8e3bb4c 35
5e037136 36 print "not " unless $_ eq "i\xcaj";
37 print "ok 4\n";
38}
c8e3bb4c 39#
2de7b02f 40
41# make sure that tr cancels IOK and NOK
42($x = 12) =~ tr/1/3/;
43(my $y = 12) =~ tr/1/3/;
44($f = 1.5) =~ tr/1/3/;
45(my $g = 1.5) =~ tr/1/3/;
46print "not " unless $x + $y + $f + $g == 71;
47print "ok 5\n";
48
49# make sure tr is harmless if not updating - see [ID 20000511.005]
50$_ = 'fred';
51/([a-z]{2})/;
52$1 =~ tr/A-Z//;
53s/^(\s*)f/$1F/;
54print "not " if $_ ne 'Fred';
55print "ok 6\n";
56
57# check tr handles UTF8 correctly
58($x = 256.65.258) =~ tr/a/b/;
59print "not " if $x ne 256.65.258 or length $x != 3;
60print "ok 7\n";
61$x =~ tr/A/B/;
67a17885 62if (ord("\t") == 9) { # ASCII
63 print "not " if $x ne 256.66.258 or length $x != 3;
64}
65else {
66 print "not " if $x ne 256.65.258 or length $x != 3;
67}
2de7b02f 68print "ok 8\n";
036b4402 69
70{
67a17885 71if (ord("\t") == 9) { # ASCII
72 use utf8;
73}
036b4402 74
75# 9 - changing UTF8 characters in a UTF8 string, same length.
76$l = chr(300); $r = chr(400);
77$x = 200.300.400;
78$x =~ tr/\x{12c}/\x{190}/;
79printf "not (%vd) ", $x if $x ne 200.400.400 or length $x != 3;
80print "ok 9\n";
81
82# 10 - changing UTF8 characters in UTF8 string, more bytes.
83$x = 200.300.400;
84$x =~ tr/\x{12c}/\x{be8}/;
85printf "not (%vd) ", $x if $x ne 200.3048.400 or length $x != 3;
86print "ok 10\n";
87
88# 11 - introducing UTF8 characters to non-UTF8 string.
89$x = 100.125.60;
90$x =~ tr/\x{64}/\x{190}/;
91printf "not (%vd) ", $x if $x ne 400.125.60 or length $x != 3;
92print "ok 11\n";
93
94# 12 - removing UTF8 characters from UTF8 string
95$x = 400.125.60;
96$x =~ tr/\x{190}/\x{64}/;
97printf "not (%vd) ", $x if $x ne 100.125.60 or length $x != 3;
98print "ok 12\n";
99
100# 13 - counting UTF8 chars in UTF8 string
101$x = 400.125.60.400;
102$y = $x =~ tr/\x{190}/\x{190}/;
103print "not " if $y != 2;
104print "ok 13\n";
105
106# 14 - counting non-UTF8 chars in UTF8 string
107$x = 60.400.125.60.400;
108$y = $x =~ tr/\x{3c}/\x{3c}/;
109print "not " if $y != 2;
110print "ok 14\n";
111
112# 15 - counting UTF8 chars in non-UTF8 string
113$x = 200.125.60;
114$y = $x =~ tr/\x{190}/\x{190}/;
115print "not " if $y != 0;
116print "ok 15\n";
117}
c2e66d9e 118
119# 16: test brokenness with tr/a-z-9//;
120$_ = "abcdefghijklmnopqrstuvwxyz";
121eval "tr/a-z-9/ /";
122print (($@ =~ /^Ambiguous range in transliteration operator/)
123 ? '' : 'not ', "ok 16\n");
124
125# 17-19: Make sure leading and trailing hyphens still work
126$_ = "car-rot9";
127tr/-a-m/./;
128print (($_ eq '..r.rot9') ? '' : 'not ', "ok 17\n");
129
130$_ = "car-rot9";
131tr/a-m-/./;
132print (($_ eq '..r.rot9') ? '' : 'not ', "ok 18\n");
133
134$_ = "car-rot9";
135tr/-a-m-/./;
136print (($_ eq '..r.rot9') ? '' : 'not ', "ok 19\n");
137
138$_ = "abcdefghijklmnop";
139tr/ae-hn/./;
140print (($_ eq '.bcd....ijklm.op') ? '' : 'not ', "ok 20\n");
141
142$_ = "abcdefghijklmnop";
143tr/a-cf-kn-p/./;
144print (($_ eq '...de......lm...') ? '' : 'not ', "ok 21\n");
145
146$_ = "abcdefghijklmnop";
147tr/a-ceg-ikm-o/./;
148print (($_ eq '...d.f...j.l...p') ? '' : 'not ', "ok 22\n");
149
150# 23: Test reversed range check
151# 20000705 MJD
152eval "tr/m-d/ /";
153print (($@ =~ /^Invalid \[\] range "m-d" in transliteration operator/)
154 ? '' : 'not ', "ok 23\n");
155
d897a58d 156# 24: test cannot update if read-only
157eval '$1 =~ tr/x/y/';
158print (($@ =~ /^Modification of a read-only value attempted/) ? '' : 'not ',
159 "ok 24\n");
160
161# 25: test can count read-only
162'abcdef' =~ /(bcd)/;
163print (( eval '$1 =~ tr/abcd//' == 3) ? '' : 'not ', "ok 25\n");
164
165# 26: test lhs OK if not updating
166print ((eval '"123" =~ tr/12//' == 2) ? '' : 'not ', "ok 26\n");
167
168# 27: test lhs bad if updating
169eval '"123" =~ tr/1/1/';
170print (($@ =~ m|^Can't modify constant item in transliteration \(tr///\)|)
171 ? '' : 'not ', "ok 27\n");
172