Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
c39c4c41 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
5 | @INC = '../lib'; | |
6448472a | 6 | require './test.pl'; |
c39c4c41 | 7 | } |
a687059c | 8 | |
e609e586 | 9 | use strict; |
10489e41 | 10 | plan( tests => 111 ); |
a687059c | 11 | |
e3faa678 NC |
12 | run_tests() unless caller; |
13 | ||
14 | sub run_tests { | |
15 | ||
e609e586 | 16 | my $foo = 'Now is the time for all good men to come to the aid of their country.'; |
a687059c | 17 | |
e609e586 | 18 | my $first = substr($foo,0,index($foo,'the')); |
c39c4c41 | 19 | is($first, "Now is "); |
a687059c | 20 | |
e609e586 | 21 | my $last = substr($foo,rindex($foo,'the'),100); |
c39c4c41 | 22 | is($last, "their country."); |
a687059c LW |
23 | |
24 | $last = substr($foo,index($foo,'Now'),2); | |
c39c4c41 | 25 | is($last, "No"); |
a687059c LW |
26 | |
27 | $last = substr($foo,rindex($foo,'Now'),2); | |
c39c4c41 | 28 | is($last, "No"); |
a687059c LW |
29 | |
30 | $last = substr($foo,index($foo,'.'),100); | |
c39c4c41 | 31 | is($last, "."); |
a687059c LW |
32 | |
33 | $last = substr($foo,rindex($foo,'.'),100); | |
c39c4c41 | 34 | is($last, "."); |
d9d8d8de | 35 | |
c39c4c41 JH |
36 | is(index("ababa","a",-1), 0); |
37 | is(index("ababa","a",0), 0); | |
38 | is(index("ababa","a",1), 2); | |
39 | is(index("ababa","a",2), 2); | |
40 | is(index("ababa","a",3), 4); | |
41 | is(index("ababa","a",4), 4); | |
42 | is(index("ababa","a",5), -1); | |
d9d8d8de | 43 | |
c39c4c41 JH |
44 | is(rindex("ababa","a",-1), -1); |
45 | is(rindex("ababa","a",0), 0); | |
46 | is(rindex("ababa","a",1), 0); | |
47 | is(rindex("ababa","a",2), 2); | |
48 | is(rindex("ababa","a",3), 2); | |
49 | is(rindex("ababa","a",4), 4); | |
50 | is(rindex("ababa","a",5), 4); | |
4f593451 | 51 | |
46f1e595 RGS |
52 | # tests for empty search string |
53 | is(index("abc", "", -1), 0); | |
54 | is(index("abc", "", 0), 0); | |
55 | is(index("abc", "", 1), 1); | |
56 | is(index("abc", "", 2), 2); | |
57 | is(index("abc", "", 3), 3); | |
58 | is(index("abc", "", 4), 3); | |
59 | is(rindex("abc", "", -1), 0); | |
60 | is(rindex("abc", "", 0), 0); | |
61 | is(rindex("abc", "", 1), 1); | |
62 | is(rindex("abc", "", 2), 2); | |
63 | is(rindex("abc", "", 3), 3); | |
64 | is(rindex("abc", "", 4), 3); | |
65 | ||
4f593451 JH |
66 | $a = "foo \x{1234}bar"; |
67 | ||
c39c4c41 JH |
68 | is(index($a, "\x{1234}"), 4); |
69 | is(index($a, "bar", ), 5); | |
4f593451 | 70 | |
c39c4c41 JH |
71 | is(rindex($a, "\x{1234}"), 4); |
72 | is(rindex($a, "foo", ), 0); | |
d69d2d9f JH |
73 | |
74 | { | |
d69d2d9f JH |
75 | my $needle = "\x{1230}\x{1270}"; |
76 | my @needles = split ( //, $needle ); | |
77 | my $haystack = "\x{1228}\x{1228}\x{1230}\x{1270}"; | |
78 | foreach ( @needles ) { | |
79 | my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ ); | |
80 | my $b = index ( $haystack, $_ ); | |
c39c4c41 | 81 | is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8}); |
d69d2d9f JH |
82 | } |
83 | $needle = "\x{1270}\x{1230}"; # Transpose them. | |
84 | @needles = split ( //, $needle ); | |
85 | foreach ( @needles ) { | |
86 | my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ ); | |
87 | my $b = index ( $haystack, $_ ); | |
c39c4c41 | 88 | is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8}); |
d69d2d9f JH |
89 | } |
90 | } | |
e609e586 NC |
91 | |
92 | { | |
250d67eb JH |
93 | my $search; |
94 | my $text; | |
95 | if (ord('A') == 193) { | |
96 | $search = "foo \x71 bar"; | |
97 | $text = "a\xb1\xb1a $search $search quux"; | |
98 | } else { | |
99 | $search = "foo \xc9 bar"; | |
100 | $text = "a\xa3\xa3a $search $search quux"; | |
101 | } | |
e609e586 NC |
102 | |
103 | my $text_utf8 = $text; | |
104 | utf8::upgrade($text_utf8); | |
105 | my $search_utf8 = $search; | |
106 | utf8::upgrade($search_utf8); | |
107 | ||
108 | is (index($text, $search), 5); | |
109 | is (rindex($text, $search), 18); | |
110 | is (index($text, $search_utf8), 5); | |
111 | is (rindex($text, $search_utf8), 18); | |
112 | is (index($text_utf8, $search), 5); | |
113 | is (rindex($text_utf8, $search), 18); | |
114 | is (index($text_utf8, $search_utf8), 5); | |
115 | is (rindex($text_utf8, $search_utf8), 18); | |
116 | ||
117 | my $text_octets = $text_utf8; | |
118 | utf8::encode ($text_octets); | |
119 | my $search_octets = $search_utf8; | |
120 | utf8::encode ($search_octets); | |
121 | ||
122 | is (index($text_octets, $search_octets), 7, "index octets, octets") | |
123 | or _diag ($text_octets, $search_octets); | |
124 | is (rindex($text_octets, $search_octets), 21, "rindex octets, octets"); | |
125 | is (index($text_octets, $search_utf8), -1); | |
126 | is (rindex($text_octets, $search_utf8), -1); | |
127 | is (index($text_utf8, $search_octets), -1); | |
128 | is (rindex($text_utf8, $search_octets), -1); | |
129 | ||
130 | is (index($text_octets, $search), -1); | |
131 | is (rindex($text_octets, $search), -1); | |
132 | is (index($text, $search_octets), -1); | |
133 | is (rindex($text, $search_octets), -1); | |
134 | } | |
a2b7337b NC |
135 | |
136 | foreach my $utf8 ('', ', utf-8') { | |
137 | foreach my $arraybase (0, 1, -1, -2) { | |
138 | my $expect_pos = 2 + $arraybase; | |
139 | ||
140 | my $prog = "\$[ = $arraybase; \$big = \"N\\xabN\\xab\"; "; | |
141 | $prog .= '$big .= chr 256; chop $big; ' if $utf8; | |
142 | $prog .= 'print rindex $big, "N", 2 + $['; | |
143 | ||
144 | fresh_perl_is($prog, $expect_pos, {}, "\$[ = $arraybase$utf8"); | |
145 | } | |
146 | } | |
6448472a TS |
147 | |
148 | SKIP: { | |
149 | skip "UTF-EBCDIC is limited to 0x7fffffff", 3 if ord("A") == 193; | |
150 | ||
151 | my $a = "\x{80000000}"; | |
152 | my $s = $a.'defxyz'; | |
153 | is(index($s, 'def'), 1, "0x80000000 is a single character"); | |
154 | ||
155 | my $b = "\x{fffffffd}"; | |
156 | my $t = $b.'pqrxyz'; | |
157 | is(index($t, 'pqr'), 1, "0xfffffffd is a single character"); | |
158 | ||
159 | local ${^UTF8CACHE} = -1; | |
160 | is(index($t, 'xyz'), 4, "0xfffffffd and utf8cache"); | |
161 | } | |
e3faa678 | 162 | |
10489e41 A |
163 | |
164 | # Tests for NUL characters. | |
165 | { | |
166 | my @tests = ( | |
167 | ["", -1, -1, -1], | |
168 | ["foo", -1, -1, -1], | |
169 | ["\0", 0, -1, -1], | |
170 | ["\0\0", 0, 0, -1], | |
171 | ["\0\0\0", 0, 0, 0], | |
172 | ["foo\0", 3, -1, -1], | |
173 | ["foo\0foo\0\0", 3, 7, -1], | |
174 | ); | |
175 | foreach my $l (1 .. 3) { | |
176 | my $q = "\0" x $l; | |
177 | my $i = 0; | |
178 | foreach my $test (@tests) { | |
179 | $i ++; | |
180 | my $str = $$test [0]; | |
181 | my $res = $$test [$l]; | |
182 | ||
183 | { | |
10489e41 A |
184 | is (index ($str, $q), $res, "Find NUL character(s)"); |
185 | } | |
186 | ||
187 | # | |
188 | # Bug #53746 shows a difference between variables and literals, | |
189 | # so test literals as well. | |
190 | # | |
191 | my $test_str = qq {is (index ("$str", "$q"), $res, } . | |
192 | qq {"Find NUL character(s)")}; | |
193 | $test_str =~ s/\0/\\0/g; | |
194 | ||
195 | eval $test_str; | |
196 | die $@ if $@; | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
e3faa678 | 201 | } |