This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
applied patch, tweak for threads awareness
[perl5.git] / t / op / array.t
1 #!./perl
2
3 # $RCSfile: array.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:37 $
4
5 print "1..40\n";
6
7 @ary = (1,2,3,4,5);
8 if (join('',@ary) eq '12345') {print "ok 1\n";} else {print "not ok 1\n";}
9
10 $tmp = $ary[$#ary]; --$#ary;
11 if ($tmp == 5) {print "ok 2\n";} else {print "not ok 2\n";}
12 if ($#ary == 3) {print "ok 3\n";} else {print "not ok 3\n";}
13 if (join('',@ary) eq '1234') {print "ok 4\n";} else {print "not ok 4\n";}
14
15 $[ = 1;
16 @ary = (1,2,3,4,5);
17 if (join('',@ary) eq '12345') {print "ok 5\n";} else {print "not ok 5\n";}
18
19 $tmp = $ary[$#ary]; --$#ary;
20 if ($tmp == 5) {print "ok 6\n";} else {print "not ok 6\n";}
21 if ($#ary == 4) {print "ok 7\n";} else {print "not ok 7\n";}
22 if (join('',@ary) eq '1234') {print "ok 8\n";} else {print "not ok 8\n";}
23
24 if ($ary[5] eq '') {print "ok 9\n";} else {print "not ok 9\n";}
25
26 $#ary += 1;     # see if element 5 gone for good
27 if ($#ary == 5) {print "ok 10\n";} else {print "not ok 10\n";}
28 if (defined $ary[5]) {print "not ok 11\n";} else {print "ok 11\n";}
29
30 $[ = 0;
31 @foo = ();
32 $r = join(',', $#foo, @foo);
33 if ($r eq "-1") {print "ok 12\n";} else {print "not ok 12 $r\n";}
34 $foo[0] = '0';
35 $r = join(',', $#foo, @foo);
36 if ($r eq "0,0") {print "ok 13\n";} else {print "not ok 13 $r\n";}
37 $foo[2] = '2';
38 $r = join(',', $#foo, @foo);
39 if ($r eq "2,0,,2") {print "ok 14\n";} else {print "not ok 14 $r\n";}
40 @bar = ();
41 $bar[0] = '0';
42 $bar[1] = '1';
43 $r = join(',', $#bar, @bar);
44 if ($r eq "1,0,1") {print "ok 15\n";} else {print "not ok 15 $r\n";}
45 @bar = ();
46 $r = join(',', $#bar, @bar);
47 if ($r eq "-1") {print "ok 16\n";} else {print "not ok 16 $r\n";}
48 $bar[0] = '0';
49 $r = join(',', $#bar, @bar);
50 if ($r eq "0,0") {print "ok 17\n";} else {print "not ok 17 $r\n";}
51 $bar[2] = '2';
52 $r = join(',', $#bar, @bar);
53 if ($r eq "2,0,,2") {print "ok 18\n";} else {print "not ok 18 $r\n";}
54 reset 'b';
55 @bar = ();
56 $bar[0] = '0';
57 $r = join(',', $#bar, @bar);
58 if ($r eq "0,0") {print "ok 19\n";} else {print "not ok 19 $r\n";}
59 $bar[2] = '2';
60 $r = join(',', $#bar, @bar);
61 if ($r eq "2,0,,2") {print "ok 20\n";} else {print "not ok 20 $r\n";}
62
63 $foo = 'now is the time';
64 if (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)) {
65     if ($F1 eq 'now' && $F2 eq 'is' && $Etc eq 'the time') {
66         print "ok 21\n";
67     }
68     else {
69         print "not ok 21\n";
70     }
71 }
72 else {
73     print "not ok 21\n";
74 }
75
76 $foo = 'lskjdf';
77 if ($cnt = (($F1,$F2,$Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))) {
78     print "not ok 22 $cnt $F1:$F2:$Etc\n";
79 }
80 else {
81     print "ok 22\n";
82 }
83
84 %foo = ('blurfl','dyick','foo','bar','etc.','etc.');
85 %bar = %foo;
86 print $bar{'foo'} eq 'bar' ? "ok 23\n" : "not ok 23\n";
87 %bar = ();
88 print $bar{'foo'} eq '' ? "ok 24\n" : "not ok 24\n";
89 (%bar,$a,$b) = (%foo,'how','now');
90 print $bar{'foo'} eq 'bar' ? "ok 25\n" : "not ok 25\n";
91 print $bar{'how'} eq 'now' ? "ok 26\n" : "not ok 26\n";
92 @bar{keys %foo} = values %foo;
93 print $bar{'foo'} eq 'bar' ? "ok 27\n" : "not ok 27\n";
94 print $bar{'how'} eq 'now' ? "ok 28\n" : "not ok 28\n";
95
96 @foo = grep(/e/,split(' ','now is the time for all good men to come to'));
97 print join(' ',@foo) eq 'the time men come' ? "ok 29\n" : "not ok 29\n";
98
99 @foo = grep(!/e/,split(' ','now is the time for all good men to come to'));
100 print join(' ',@foo) eq 'now is for all good to to' ? "ok 30\n" : "not ok 30\n";
101
102 $foo = join('',('a','b','c','d','e','f')[0..5]);
103 print $foo eq 'abcdef' ? "ok 31\n" : "not ok 31\n";
104
105 $foo = join('',('a','b','c','d','e','f')[0..1]);
106 print $foo eq 'ab' ? "ok 32\n" : "not ok 32\n";
107
108 $foo = join('',('a','b','c','d','e','f')[6]);
109 print $foo eq '' ? "ok 33\n" : "not ok 33\n";
110
111 @foo = ('a','b','c','d','e','f')[0,2,4];
112 @bar = ('a','b','c','d','e','f')[1,3,5];
113 $foo = join('',(@foo,@bar)[0..5]);
114 print $foo eq 'acebdf' ? "ok 34\n" : "not ok 34\n";
115
116 $foo = ('a','b','c','d','e','f')[0,2,4];
117 print $foo eq 'e' ? "ok 35\n" : "not ok 35\n";
118
119 $foo = ('a','b','c','d','e','f')[1];
120 print $foo eq 'b' ? "ok 36\n" : "not ok 36\n";
121
122 # Test pseudo-hashes and %FIELDS. Real programs would "use fields..."
123 # but we assign to %FIELDS manually since the real module tests come later.
124
125 BEGIN {
126     %Base::WithFields::FIELDS = (foo => 1, bar => 2, baz => 3, __MAX__ => 3);
127     %OtherBase::WithFields::FIELDS = (one => 1, two => 2, __MAX__ => 2);
128 }
129 {
130     package Base::WithoutFields;
131 }
132 @ISA = qw(Base::WithoutFields Base::WithFields);
133 @k = sort keys %FIELDS;
134 print "not " unless "@k" eq "__MAX__ bar baz foo";
135 print "ok 37\n";
136 eval {
137     @ISA = 'OtherBase::WithFields';
138 };
139 print "not " unless $@ =~ /Inherited %FIELDS can't override existing %FIELDS/;
140 print "ok 38\n";
141 undef %FIELDS;
142 eval {
143     @ISA = qw(Base::WithFields OtherBase::WithFields);
144 };
145 print "not " unless $@ =~ /Can't multiply inherit %FIELDS/;
146 print "ok 39\n";
147
148 @foo = ( 'foo', 'bar', 'burbl');
149 push(foo, 'blah');
150 print $#foo == 3 ? "ok 40\n" : "not ok 40\n";