Fix test added in change 23645 with an eval()
[p5sagit/p5-mst-13.2.git] / t / op / index.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 require './test.pl';
9 plan( 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'));
14 is($first, "Now is ");
15
16 $last = substr($foo,rindex($foo,'the'),100);
17 is($last, "their country.");
18
19 $last = substr($foo,index($foo,'Now'),2);
20 is($last, "No");
21
22 $last = substr($foo,rindex($foo,'Now'),2);
23 is($last, "No");
24
25 $last = substr($foo,index($foo,'.'),100);
26 is($last, ".");
27
28 $last = substr($foo,rindex($foo,'.'),100);
29 is($last, ".");
30
31 is(index("ababa","a",-1), 0);
32 is(index("ababa","a",0), 0);
33 is(index("ababa","a",1), 2);
34 is(index("ababa","a",2), 2);
35 is(index("ababa","a",3), 4);
36 is(index("ababa","a",4), 4);
37 is(index("ababa","a",5), -1);
38
39 is(rindex("ababa","a",-1), -1);
40 is(rindex("ababa","a",0), 0);
41 is(rindex("ababa","a",1), 0);
42 is(rindex("ababa","a",2), 2);
43 is(rindex("ababa","a",3), 2);
44 is(rindex("ababa","a",4), 4);
45 is(rindex("ababa","a",5), 4);
46
47 $a = "foo \x{1234}bar";
48
49 is(index($a, "\x{1234}"), 4);
50 is(index($a, "bar",    ), 5);
51
52 is(rindex($a, "\x{1234}"), 4);
53 is(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 }