This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
re-implement OPpASSIGN_COMMON mechanism
[perl5.git] / t / op / index.t
CommitLineData
a687059c
LW
1#!./perl
2
c39c4c41
JH
3BEGIN {
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 10use strict;
15c41403 11plan( tests => 122 );
a687059c 12
e3faa678
NC
13run_tests() unless caller;
14
15sub run_tests {
16
e609e586 17my $foo = 'Now is the time for all good men to come to the aid of their country.';
a687059c 18
e609e586 19my $first = substr($foo,0,index($foo,'the'));
c39c4c41 20is($first, "Now is ");
a687059c 21
e609e586 22my $last = substr($foo,rindex($foo,'the'),100);
c39c4c41 23is($last, "their country.");
a687059c
LW
24
25$last = substr($foo,index($foo,'Now'),2);
c39c4c41 26is($last, "No");
a687059c
LW
27
28$last = substr($foo,rindex($foo,'Now'),2);
c39c4c41 29is($last, "No");
a687059c
LW
30
31$last = substr($foo,index($foo,'.'),100);
c39c4c41 32is($last, ".");
a687059c
LW
33
34$last = substr($foo,rindex($foo,'.'),100);
c39c4c41 35is($last, ".");
d9d8d8de 36
c39c4c41
JH
37is(index("ababa","a",-1), 0);
38is(index("ababa","a",0), 0);
39is(index("ababa","a",1), 2);
40is(index("ababa","a",2), 2);
41is(index("ababa","a",3), 4);
42is(index("ababa","a",4), 4);
43is(index("ababa","a",5), -1);
d9d8d8de 44
c39c4c41
JH
45is(rindex("ababa","a",-1), -1);
46is(rindex("ababa","a",0), 0);
47is(rindex("ababa","a",1), 0);
48is(rindex("ababa","a",2), 2);
49is(rindex("ababa","a",3), 2);
50is(rindex("ababa","a",4), 4);
51is(rindex("ababa","a",5), 4);
4f593451 52
46f1e595
RGS
53# tests for empty search string
54is(index("abc", "", -1), 0);
55is(index("abc", "", 0), 0);
56is(index("abc", "", 1), 1);
57is(index("abc", "", 2), 2);
58is(index("abc", "", 3), 3);
59is(index("abc", "", 4), 3);
60is(rindex("abc", "", -1), 0);
61is(rindex("abc", "", 0), 0);
62is(rindex("abc", "", 1), 1);
63is(rindex("abc", "", 2), 2);
64is(rindex("abc", "", 3), 3);
65is(rindex("abc", "", 4), 3);
66
4f593451
JH
67$a = "foo \x{1234}bar";
68
c39c4c41
JH
69is(index($a, "\x{1234}"), 4);
70is(index($a, "bar", ), 5);
4f593451 71
c39c4c41
JH
72is(rindex($a, "\x{1234}"), 4);
73is(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 132SKIP: {
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
195use constant {PVBM => 'galumphing', PVBM2 => 'bang'};
196
197sub index_it {
198 is(index('galumphing', PVBM), 0,
199 "index isn't confused by format compilation");
200}
201
202index_it();
203is($^A, '', '$^A is empty');
204formline PVBM;
205is($^A, 'galumphing', "formline isn't confused by index compilation");
206index_it();
207
208$^A = '';
209# must not do index here before formline.
210is($^A, '', '$^A is empty');
211formline PVBM2;
212is($^A, 'bang', "formline isn't confused by index compilation");
213is(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
222use constant riffraff => \our $referent;
223index "foo", riffraff;
224is ref riffraff, 'SCALAR', 'index does not flatten ref constants';
225
226package o { use overload '""' => sub { "foo" } }
227bless \our $referent, o::;
228is index("foo", riffraff), 0,
229 'index respects changes in ref stringification';
230
310f4fdb
FC
231use constant quire => ${qr/(?{})/}; # A REGEXP, not a reference to one
232index "foo", quire;
233eval ' "" =~ quire ';
234is $@, "", 'regexp constants containing code blocks are not flattened';
235
236use constant bang => $! = 8;
237index "foo", bang;
238cmp_ok bang, '==', 8, 'dualvar constants are not flattened';
239
240use 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}
250is u, undef, 'undef constant is still undef';
251
11609d9c
FC
252is index('the main road', __PACKAGE__), 4,
253 '[perl #119169] __PACKAGE__ as 2nd argument';
254
8aafe2e9 255} # end of sub run_tests
15c41403
JR
256
257utf8::upgrade my $substr = "\x{a3}a";
258
259is index($substr, 'a'), 1, 'index reply reflects characters not octets';