Skip to content

Commit 73d6564

Browse files
committed
Perlito5 - another ~200 standard tests
1 parent a9955f1 commit 73d6564

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

t5/op/pow.t

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!./perl -w
2+
# Now they'll be wanting biff! and zap! tests too.
3+
4+
BEGIN {
5+
chdir 't' if -d 't';
6+
@INC = '../lib';
7+
require './test.pl';
8+
}
9+
10+
# This calcualtion ought to be within 0.001 of the right answer.
11+
my $bits_in_uv = int (0.001 + log (~0+1) / log 2);
12+
13+
# 3**30 < 2**48, don't trust things outside that range on a Cray
14+
# Likewise other 3 should not overflow 48 bits if I did my sums right.
15+
my @pow = ([ 3, 30, 1e-14],
16+
[ 4, 32, 0],
17+
[ 5, 20, 1e-14],
18+
[2.5, 10, 1e-14],
19+
[ -2, 69, 0],
20+
[ -3, 30, 1e-14],
21+
);
22+
my $tests;
23+
$tests += $_->[1] foreach @pow;
24+
25+
plan tests => 13 + $bits_in_uv + $tests;
26+
27+
# (-3)**3 gave 27 instead of -27 before change #20167.
28+
# Let's test the other similar edge cases, too.
29+
is((-3)**0, 1, "negative ** 0 = 1");
30+
is((-3)**1, -3, "negative ** 1 = self");
31+
is((-3)**2, 9, "negative ** 2 = positive");
32+
is((-3)**3, -27, "(negative int) ** (odd power) is negative");
33+
34+
# Positives shouldn't be a problem
35+
is(3**0, 1, "positive ** 0 = 1");
36+
is(3**1, 3, "positive ** 1 = self");
37+
is(3**2, 9, "positive ** 2 = positive");
38+
is(3**3, 27, "(positive int) ** (odd power) is positive");
39+
40+
# And test order of operations while we're at it
41+
is(-3**0, -1);
42+
is(-3**1, -3);
43+
is(-3**2, -9);
44+
is(-3**3, -27);
45+
46+
47+
# Ought to be 32, 64, 36 or something like that.
48+
49+
my $remainder = $bits_in_uv & 3;
50+
51+
cmp_ok ($remainder, '==', 0, 'Sanity check bits in UV calculation')
52+
or printf "# ~0 is %d (0x%d) which gives $bits_in_uv bits\n", ~0, ~0;
53+
54+
# These are a lot of brute force tests to see how accurate $m ** $n is.
55+
# Unfortunately rather a lot of perl programs expect 2 ** $n to be integer
56+
# perfect, forgetting that it's a call to floating point pow() which never
57+
# claims to deliver perfection.
58+
foreach my $n (0..$bits_in_uv - 1) {
59+
my $pow = 2 ** $n;
60+
my $int = 1 << $n;
61+
cmp_ok ($pow, '==', $int, "2 ** $n vs 1 << $n");
62+
}
63+
64+
foreach my $pow (@pow) {
65+
my ($base, $max, $range) = @$pow;
66+
my $expect = 1;
67+
foreach my $n (0..$max-1) {
68+
my $got = $base ** $n;
69+
within ($got, $expect, $range, "$base ** $n got[$got] expect[$expect]");
70+
$expect *= $base;
71+
}
72+
}

t5/op/wantarray.t

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!./perl
2+
3+
BEGIN {
4+
chdir 't' if -d 't';
5+
@INC = '../lib';
6+
require './test.pl';
7+
}
8+
9+
use strict;
10+
11+
plan 13;
12+
13+
sub context {
14+
local $::Level = $::Level + 1;
15+
my ( $cona, $testnum ) = @_;
16+
my $conb = (defined wantarray) ? ( wantarray ? 'A' : 'S' ) : 'V';
17+
is $cona, $conb;
18+
}
19+
20+
context('V');
21+
my $a = context('S');
22+
my @a = context('A');
23+
scalar context('S');
24+
$a = scalar context('S');
25+
($a) = context('A');
26+
($a) = scalar context('S');
27+
28+
{
29+
# [ID 20020626.011] incorrect wantarray optimisation
30+
sub simple { wantarray ? 1 : 2 }
31+
sub inline {
32+
my $a = wantarray ? simple() : simple();
33+
$a;
34+
}
35+
my @b = inline();
36+
my $c = inline();
37+
is @b, 1;
38+
is "@b", "2";
39+
is $c, 2;
40+
}
41+
42+
my $q;
43+
44+
my $qcontext = q{
45+
$q = (defined wantarray) ? ( wantarray ? 'A' : 'S' ) : 'V';
46+
};
47+
eval $qcontext;
48+
is $q, 'V';
49+
$a = eval $qcontext;
50+
is $q, 'S';
51+
@a = eval $qcontext;
52+
is $q, 'A';
53+
54+
1;

t5/re/reg_unsafe.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!./perl
2+
3+
BEGIN {
4+
chdir 't' if -d 't';
5+
@INC = '../lib';
6+
7+
}
8+
print "1..1\n";
9+
10+
# there is an equivelent test in t/re/pat.t which does NOT fail
11+
# its not clear why it doesnt fail, so this todo gets its own test
12+
# file until we can work it out.
13+
14+
my $x;
15+
($x='abc')=~/(abc)/g;
16+
$x='123';
17+
18+
print "not " if $1 ne 'abc';
19+
print "ok 1 # TODO safe match vars make /g slow\n";

0 commit comments

Comments
 (0)