1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6}
7
8require './test.pl';
9plan( tests => 28 );
10
11$foo = 'Now is the time for all good men to come to the aid of their country.';
12
13$first = substr($foo,0,index($foo,'the'));
14is($first, "Now is ");
15
16$last = substr($foo,rindex($foo,'the'),100);
17is($last, "their country.");
18
19$last = substr($foo,index($foo,'Now'),2);
20is($last, "No");
21
22$last = substr($foo,rindex($foo,'Now'),2);
23is($last, "No");
24
25$last = substr($foo,index($foo,'.'),100);
26is($last, ".");
27
28$last = substr($foo,rindex($foo,'.'),100);
29is($last, ".");
30
31is(index("ababa","a",-1), 0);
32is(index("ababa","a",0), 0);
33is(index("ababa","a",1), 2);
34is(index("ababa","a",2), 2);
35is(index("ababa","a",3), 4);
36is(index("ababa","a",4), 4);
37is(index("ababa","a",5), -1);
38
39is(rindex("ababa","a",-1), -1);
40is(rindex("ababa","a",0), 0);
41is(rindex("ababa","a",1), 0);
42is(rindex("ababa","a",2), 2);
43is(rindex("ababa","a",3), 2);
44is(rindex("ababa","a",4), 4);
45is(rindex("ababa","a",5), 4);
46
47$a = "foo \x{1234}bar";
48
49is(index($a, "\x{1234}"), 4);
50is(index($a, "bar",    ), 5);
51
52is(rindex($a, "\x{1234}"), 4);
53is(rindex($a, "foo",    ), 0);
54
55{
56    my $needle = "\x{1230}\x{1270}";
57    my @needles = split ( //, $needle );
58    my $haystack = "\x{1228}\x{1228}\x{1230}\x{1270}";
59    foreach ( @needles ) {
60	my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ );
61	my $b = index ( $haystack, $_ );
62	is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8});
63    }
64    $needle = "\x{1270}\x{1230}"; # Transpose them.
65    @needles = split ( //, $needle );
66    foreach ( @needles ) {
67	my $a = index ( "\x{1228}\x{1228}\x{1230}\x{1270}", $_ );
68	my $b = index ( $haystack, $_ );
69	is($a, $b, q{[perl #22375] 'split'/'index' problem for utf8});
70    }
71}
72