Change 28404 broke the construct s/foo/<<BAR/e. So, try to be more
[p5sagit/p5-mst-13.2.git] / t / op / index.t
CommitLineData
a687059c 1#!./perl
2
c39c4c41 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
a687059c 7
e609e586 8use strict;
c39c4c41 9require './test.pl';
a2b7337b 10plan( tests => 66 );
a687059c 11
e609e586 12my $foo = 'Now is the time for all good men to come to the aid of their country.';
a687059c 13
e609e586 14my $first = substr($foo,0,index($foo,'the'));
c39c4c41 15is($first, "Now is ");
a687059c 16
e609e586 17my $last = substr($foo,rindex($foo,'the'),100);
c39c4c41 18is($last, "their country.");
a687059c 19
20$last = substr($foo,index($foo,'Now'),2);
c39c4c41 21is($last, "No");
a687059c 22
23$last = substr($foo,rindex($foo,'Now'),2);
c39c4c41 24is($last, "No");
a687059c 25
26$last = substr($foo,index($foo,'.'),100);
c39c4c41 27is($last, ".");
a687059c 28
29$last = substr($foo,rindex($foo,'.'),100);
c39c4c41 30is($last, ".");
d9d8d8de 31
c39c4c41 32is(index("ababa","a",-1), 0);
33is(index("ababa","a",0), 0);
34is(index("ababa","a",1), 2);
35is(index("ababa","a",2), 2);
36is(index("ababa","a",3), 4);
37is(index("ababa","a",4), 4);
38is(index("ababa","a",5), -1);
d9d8d8de 39
c39c4c41 40is(rindex("ababa","a",-1), -1);
41is(rindex("ababa","a",0), 0);
42is(rindex("ababa","a",1), 0);
43is(rindex("ababa","a",2), 2);
44is(rindex("ababa","a",3), 2);
45is(rindex("ababa","a",4), 4);
46is(rindex("ababa","a",5), 4);
4f593451 47
46f1e595 48# tests for empty search string
49is(index("abc", "", -1), 0);
50is(index("abc", "", 0), 0);
51is(index("abc", "", 1), 1);
52is(index("abc", "", 2), 2);
53is(index("abc", "", 3), 3);
54is(index("abc", "", 4), 3);
55is(rindex("abc", "", -1), 0);
56is(rindex("abc", "", 0), 0);
57is(rindex("abc", "", 1), 1);
58is(rindex("abc", "", 2), 2);
59is(rindex("abc", "", 3), 3);
60is(rindex("abc", "", 4), 3);
61
4f593451 62$a = "foo \x{1234}bar";
63
c39c4c41 64is(index($a, "\x{1234}"), 4);
65is(index($a, "bar", ), 5);
4f593451 66
c39c4c41 67is(rindex($a, "\x{1234}"), 4);
68is(rindex($a, "foo", ), 0);
d69d2d9f 69
70{
d69d2d9f 71 my $needle = "\x{1230}\x{1270}";
72 my @needles = split ( //, $needle );
73 my $haystack = "\x{1228}\x{1228}\x{1230}\x{1270}";
74 foreach ( @needles ) {
75 my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ );
76 my $b = index ( $haystack, $_ );
c39c4c41 77 is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8});
d69d2d9f 78 }
79 $needle = "\x{1270}\x{1230}"; # Transpose them.
80 @needles = split ( //, $needle );
81 foreach ( @needles ) {
82 my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ );
83 my $b = index ( $haystack, $_ );
c39c4c41 84 is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8});
d69d2d9f 85 }
86}
e609e586 87
88{
89 my $search = "foo \xc9 bar";
90 my $text = "a\xa3\xa3a $search $search quux";
91
92 my $text_utf8 = $text;
93 utf8::upgrade($text_utf8);
94 my $search_utf8 = $search;
95 utf8::upgrade($search_utf8);
96
97 is (index($text, $search), 5);
98 is (rindex($text, $search), 18);
99 is (index($text, $search_utf8), 5);
100 is (rindex($text, $search_utf8), 18);
101 is (index($text_utf8, $search), 5);
102 is (rindex($text_utf8, $search), 18);
103 is (index($text_utf8, $search_utf8), 5);
104 is (rindex($text_utf8, $search_utf8), 18);
105
106 my $text_octets = $text_utf8;
107 utf8::encode ($text_octets);
108 my $search_octets = $search_utf8;
109 utf8::encode ($search_octets);
110
111 is (index($text_octets, $search_octets), 7, "index octets, octets")
112 or _diag ($text_octets, $search_octets);
113 is (rindex($text_octets, $search_octets), 21, "rindex octets, octets");
114 is (index($text_octets, $search_utf8), -1);
115 is (rindex($text_octets, $search_utf8), -1);
116 is (index($text_utf8, $search_octets), -1);
117 is (rindex($text_utf8, $search_octets), -1);
118
119 is (index($text_octets, $search), -1);
120 is (rindex($text_octets, $search), -1);
121 is (index($text, $search_octets), -1);
122 is (rindex($text, $search_octets), -1);
123}
a2b7337b 124
125foreach my $utf8 ('', ', utf-8') {
126 foreach my $arraybase (0, 1, -1, -2) {
127 my $expect_pos = 2 + $arraybase;
128
129 my $prog = "\$[ = $arraybase; \$big = \"N\\xabN\\xab\"; ";
130 $prog .= '$big .= chr 256; chop $big; ' if $utf8;
131 $prog .= 'print rindex $big, "N", 2 + $[';
132
133 fresh_perl_is($prog, $expect_pos, {}, "\$[ = $arraybase$utf8");
134 }
135}