Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
c39c4c41 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
b21ea4ec | 5 | require './test.pl'; |
43ece5b1 | 6 | set_up_inc('../lib'); |
b21ea4ec | 7 | require './charset_tools.pl'; |
c39c4c41 | 8 | } |
a687059c | 9 | |
e609e586 | 10 | use strict; |
15c41403 | 11 | plan( tests => 122 ); |
a687059c | 12 | |
e3faa678 NC |
13 | run_tests() unless caller; |
14 | ||
15 | sub run_tests { | |
16 | ||
e609e586 | 17 | my $foo = 'Now is the time for all good men to come to the aid of their country.'; |
a687059c | 18 | |
e609e586 | 19 | my $first = substr($foo,0,index($foo,'the')); |
c39c4c41 | 20 | is($first, "Now is "); |
a687059c | 21 | |
e609e586 | 22 | my $last = substr($foo,rindex($foo,'the'),100); |
c39c4c41 | 23 | is($last, "their country."); |
a687059c LW |
24 | |
25 | $last = substr($foo,index($foo,'Now'),2); | |
c39c4c41 | 26 | is($last, "No"); |
a687059c LW |
27 | |
28 | $last = substr($foo,rindex($foo,'Now'),2); | |
c39c4c41 | 29 | is($last, "No"); |
a687059c LW |
30 | |
31 | $last = substr($foo,index($foo,'.'),100); | |
c39c4c41 | 32 | is($last, "."); |
a687059c LW |
33 | |
34 | $last = substr($foo,rindex($foo,'.'),100); | |
c39c4c41 | 35 | is($last, "."); |
d9d8d8de | 36 | |
c39c4c41 JH |
37 | is(index("ababa","a",-1), 0); |
38 | is(index("ababa","a",0), 0); | |
39 | is(index("ababa","a",1), 2); | |
40 | is(index("ababa","a",2), 2); | |
41 | is(index("ababa","a",3), 4); | |
42 | is(index("ababa","a",4), 4); | |
43 | is(index("ababa","a",5), -1); | |
d9d8d8de | 44 | |
c39c4c41 JH |
45 | is(rindex("ababa","a",-1), -1); |
46 | is(rindex("ababa","a",0), 0); | |
47 | is(rindex("ababa","a",1), 0); | |
48 | is(rindex("ababa","a",2), 2); | |
49 | is(rindex("ababa","a",3), 2); | |
50 | is(rindex("ababa","a",4), 4); | |
51 | is(rindex("ababa","a",5), 4); | |
4f593451 | 52 | |
46f1e595 RGS |
53 | # tests for empty search string |
54 | is(index("abc", "", -1), 0); | |
55 | is(index("abc", "", 0), 0); | |
56 | is(index("abc", "", 1), 1); | |
57 | is(index("abc", "", 2), 2); | |
58 | is(index("abc", "", 3), 3); | |
59 | is(index("abc", "", 4), 3); | |
60 | is(rindex("abc", "", -1), 0); | |
61 | is(rindex("abc", "", 0), 0); | |
62 | is(rindex("abc", "", 1), 1); | |
63 | is(rindex("abc", "", 2), 2); | |
64 | is(rindex("abc", "", 3), 3); | |
65 | is(rindex("abc", "", 4), 3); | |
66 | ||
4f593451 JH |
67 | $a = "foo \x{1234}bar"; |
68 | ||
c39c4c41 JH |
69 | is(index($a, "\x{1234}"), 4); |
70 | is(index($a, "bar", ), 5); | |
4f593451 | 71 | |
c39c4c41 JH |
72 | is(rindex($a, "\x{1234}"), 4); |
73 | is(rindex($a, "foo", ), 0); | |
d69d2d9f JH |
74 | |
75 | { | |
d69d2d9f JH |
76 | my $needle = "\x{1230}\x{1270}"; |
77 | my @needles = split ( //, $needle ); | |
78 | my $haystack = "\x{1228}\x{1228}\x{1230}\x{1270}"; | |
79 | foreach ( @needles ) { | |
80 | my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ ); | |
81 | my $b = index ( $haystack, $_ ); | |
c39c4c41 | 82 | is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8}); |
d69d2d9f JH |
83 | } |
84 | $needle = "\x{1270}\x{1230}"; # Transpose them. | |
85 | @needles = split ( //, $needle ); | |
86 | foreach ( @needles ) { | |
87 | my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ ); | |
88 | my $b = index ( $haystack, $_ ); | |
c39c4c41 | 89 | is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8}); |
d69d2d9f JH |
90 | } |
91 | } | |
e609e586 NC |
92 | |
93 | { | |
250d67eb JH |
94 | my $search; |
95 | my $text; | |
d1cef54a KW |
96 | $search = "foo " . uni_to_native("\xc9") . " bar"; |
97 | $text = "a" . uni_to_native("\xa3\xa3") . "a $search $search quux"; | |
e609e586 NC |
98 | |
99 | my $text_utf8 = $text; | |
100 | utf8::upgrade($text_utf8); | |
101 | my $search_utf8 = $search; | |
102 | utf8::upgrade($search_utf8); | |
103 | ||
104 | is (index($text, $search), 5); | |
105 | is (rindex($text, $search), 18); | |
106 | is (index($text, $search_utf8), 5); | |
107 | is (rindex($text, $search_utf8), 18); | |
108 | is (index($text_utf8, $search), 5); | |
109 | is (rindex($text_utf8, $search), 18); | |
110 | is (index($text_utf8, $search_utf8), 5); | |
111 | is (rindex($text_utf8, $search_utf8), 18); | |
112 | ||
113 | my $text_octets = $text_utf8; | |
114 | utf8::encode ($text_octets); | |
115 | my $search_octets = $search_utf8; | |
116 | utf8::encode ($search_octets); | |
117 | ||
118 | is (index($text_octets, $search_octets), 7, "index octets, octets") | |
119 | or _diag ($text_octets, $search_octets); | |
120 | is (rindex($text_octets, $search_octets), 21, "rindex octets, octets"); | |
121 | is (index($text_octets, $search_utf8), -1); | |
122 | is (rindex($text_octets, $search_utf8), -1); | |
123 | is (index($text_utf8, $search_octets), -1); | |
124 | is (rindex($text_utf8, $search_octets), -1); | |
125 | ||
126 | is (index($text_octets, $search), -1); | |
127 | is (rindex($text_octets, $search), -1); | |
128 | is (index($text, $search_octets), -1); | |
129 | is (rindex($text, $search_octets), -1); | |
130 | } | |
a2b7337b | 131 | |
6448472a | 132 | SKIP: { |
f87af711 | 133 | skip "UTF-EBCDIC is limited to 0x7fffffff", 3 if $::IS_EBCDIC; |
6448472a | 134 | |
f87af711 | 135 | my $a = eval q{"\x{80000000}"}; |
6448472a TS |
136 | my $s = $a.'defxyz'; |
137 | is(index($s, 'def'), 1, "0x80000000 is a single character"); | |
138 | ||
f87af711 | 139 | my $b = eval q{"\x{fffffffd}"}; |
6448472a TS |
140 | my $t = $b.'pqrxyz'; |
141 | is(index($t, 'pqr'), 1, "0xfffffffd is a single character"); | |
142 | ||
143 | local ${^UTF8CACHE} = -1; | |
144 | is(index($t, 'xyz'), 4, "0xfffffffd and utf8cache"); | |
145 | } | |
e3faa678 | 146 | |
10489e41 A |
147 | |
148 | # Tests for NUL characters. | |
149 | { | |
150 | my @tests = ( | |
151 | ["", -1, -1, -1], | |
152 | ["foo", -1, -1, -1], | |
153 | ["\0", 0, -1, -1], | |
154 | ["\0\0", 0, 0, -1], | |
155 | ["\0\0\0", 0, 0, 0], | |
156 | ["foo\0", 3, -1, -1], | |
157 | ["foo\0foo\0\0", 3, 7, -1], | |
158 | ); | |
159 | foreach my $l (1 .. 3) { | |
160 | my $q = "\0" x $l; | |
161 | my $i = 0; | |
162 | foreach my $test (@tests) { | |
163 | $i ++; | |
164 | my $str = $$test [0]; | |
165 | my $res = $$test [$l]; | |
166 | ||
167 | { | |
10489e41 A |
168 | is (index ($str, $q), $res, "Find NUL character(s)"); |
169 | } | |
170 | ||
171 | # | |
172 | # Bug #53746 shows a difference between variables and literals, | |
173 | # so test literals as well. | |
174 | # | |
175 | my $test_str = qq {is (index ("$str", "$q"), $res, } . | |
176 | qq {"Find NUL character(s)")}; | |
177 | $test_str =~ s/\0/\\0/g; | |
178 | ||
179 | eval $test_str; | |
180 | die $@ if $@; | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
3e2d3818 NC |
185 | { |
186 | # RT#75898 | |
187 | is(eval { utf8::upgrade($_ = " "); index $_, " ", 72 }, -1, | |
188 | 'UTF-8 cache handles offset beyond the end of the string'); | |
189 | $_ = "\x{100}BC"; | |
190 | is(index($_, "C", 4), -1, | |
191 | 'UTF-8 cache handles offset beyond the end of the string'); | |
192 | } | |
193 | ||
74e0ddf7 NC |
194 | # RT #89218 |
195 | use constant {PVBM => 'galumphing', PVBM2 => 'bang'}; | |
196 | ||
197 | sub index_it { | |
198 | is(index('galumphing', PVBM), 0, | |
199 | "index isn't confused by format compilation"); | |
200 | } | |
201 | ||
202 | index_it(); | |
203 | is($^A, '', '$^A is empty'); | |
204 | formline PVBM; | |
205 | is($^A, 'galumphing', "formline isn't confused by index compilation"); | |
206 | index_it(); | |
207 | ||
208 | $^A = ''; | |
209 | # must not do index here before formline. | |
210 | is($^A, '', '$^A is empty'); | |
211 | formline PVBM2; | |
212 | is($^A, 'bang', "formline isn't confused by index compilation"); | |
213 | is(index('bang', PVBM2), 0, "index isn't confused by format compilation"); | |
214 | ||
9402563a NC |
215 | { |
216 | use constant perl => "rules"; | |
217 | is(index("perl rules", perl), 5, 'first index of a constant works'); | |
218 | is(index("rules 1 & 2", perl), 0, 'second index of the same constant works'); | |
219 | } | |
220 | ||
948d2370 FC |
221 | # PVBM compilation should not flatten ref constants |
222 | use constant riffraff => \our $referent; | |
223 | index "foo", riffraff; | |
224 | is ref riffraff, 'SCALAR', 'index does not flatten ref constants'; | |
225 | ||
226 | package o { use overload '""' => sub { "foo" } } | |
227 | bless \our $referent, o::; | |
228 | is index("foo", riffraff), 0, | |
229 | 'index respects changes in ref stringification'; | |
230 | ||
310f4fdb FC |
231 | use constant quire => ${qr/(?{})/}; # A REGEXP, not a reference to one |
232 | index "foo", quire; | |
233 | eval ' "" =~ quire '; | |
234 | is $@, "", 'regexp constants containing code blocks are not flattened'; | |
235 | ||
236 | use constant bang => $! = 8; | |
237 | index "foo", bang; | |
238 | cmp_ok bang, '==', 8, 'dualvar constants are not flattened'; | |
239 | ||
240 | use constant u => undef; | |
241 | { | |
242 | my $w; | |
243 | local $SIG{__WARN__} = sub { $w .= shift }; | |
244 | eval ' | |
245 | use warnings; | |
246 | sub { () = index "foo", u; } | |
247 | '; | |
248 | is $w, undef, 'no warnings from compiling index($foo, undef_constant)'; | |
249 | } | |
250 | is u, undef, 'undef constant is still undef'; | |
251 | ||
11609d9c FC |
252 | is index('the main road', __PACKAGE__), 4, |
253 | '[perl #119169] __PACKAGE__ as 2nd argument'; | |
254 | ||
8aafe2e9 | 255 | } # end of sub run_tests |
15c41403 JR |
256 | |
257 | utf8::upgrade my $substr = "\x{a3}a"; | |
258 | ||
259 | is index($substr, 'a'), 1, 'index reply reflects characters not octets'; |