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