Skip to content

Commit

Permalink
Simplify sprintf("%e") testing, also test "E"
Browse files Browse the repository at this point in the history
The results of all formats with "#" are the same to the results without
"#", so create those tests programatically.  Also perform the tests for "E"
by uppercasing the format string and the expected result.
  • Loading branch information
lizmat committed Apr 17, 2019
1 parent 0958f70 commit 78d5fe2
Showing 1 changed file with 13 additions and 83 deletions.
96 changes: 13 additions & 83 deletions S32-str/sprintf-e.t
Expand Up @@ -3,12 +3,13 @@ use v6;
#BEGIN %*ENV<PERL6_TEST_DIE_ON_FAIL> = True;
use Test;

# Test combinations of flags for "%e". The @info array is intialized with
# the flags (as a string), the size/precision specification (either a
# Test combinations of flags for "%e" and "%E". The @info array is intialized
# with the flags (as a string), the size/precision specification (either a
# string or a # number), and the expected strings for the values 0, 27.1 and
# -2.71. The flags values will be expanded to all possible permutations to
# ensure that the order of the flags is irrelevant. Each flag permutation is
# combined with the size/permutation value to create a proper format string.
# Each test will be done twice, once for "e" and once for "E".

my ($v0, $v4, $vm) =
0 , 27.1 , -2.71 ;
Expand All @@ -30,22 +31,6 @@ my @info = ( # |----------------|----------------|----------------|
'-+0', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'-0 ', '', " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'-+0 ', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#', '', "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'# ', '', " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#0', '', "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#0 ', '', " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#+', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+ ', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+0', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+0 ', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-', '', "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#-+', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#- ', '', " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#-+ ', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-0', '', "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#-+0', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-0 ', '', " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#-+0 ', '', "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",

# no size, precision 0
'', '.0', "0e+00", "3e+01", "-3e+00",
Expand All @@ -64,22 +49,6 @@ my @info = ( # |----------------|----------------|----------------|
'-+0', '.0', "+0e+00", "+3e+01", "-3e+00",
'-0 ', '.0', " 0e+00", " 3e+01", "-3e+00",
'-+0 ', '.0', "+0e+00", "+3e+01", "-3e+00",
'#', '.0', "0e+00", "3e+01", "-3e+00",
'# ', '.0', " 0e+00", " 3e+01", "-3e+00",
'#0', '.0', "0e+00", "3e+01", "-3e+00",
'#0 ', '.0', " 0e+00", " 3e+01", "-3e+00",
'#+', '.0', "+0e+00", "+3e+01", "-3e+00",
'#+ ', '.0', "+0e+00", "+3e+01", "-3e+00",
'#+0', '.0', "+0e+00", "+3e+01", "-3e+00",
'#+0 ', '.0', "+0e+00", "+3e+01", "-3e+00",
'#-', '.0', "0e+00", "3e+01", "-3e+00",
'#-+', '.0', "+0e+00", "+3e+01", "-3e+00",
'#- ', '.0', " 0e+00", " 3e+01", "-3e+00",
'#-+ ', '.0', "+0e+00", "+3e+01", "-3e+00",
'#-0', '.0', "0e+00", "3e+01", "-3e+00",
'#-+0', '.0', "+0e+00", "+3e+01", "-3e+00",
'#-0 ', '.0', " 0e+00", " 3e+01", "-3e+00",
'#-+0 ', '.0', "+0e+00", "+3e+01", "-3e+00",

# 2 positions, usually doesn't fit
'', 2, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
Expand All @@ -98,22 +67,6 @@ my @info = ( # |----------------|----------------|----------------|
'-0 ', 2, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'-+0', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'-+0 ', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#', 2, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'# ', 2, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#0', 2, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#0 ', 2, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#+', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+ ', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+0', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+0 ', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-', 2, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#-+', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#- ', 2, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#-+ ', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-0', 2, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#-0 ', 2, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#-+0', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-+0 ', 2, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",

# 8 positions, should always fit
'', 8, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
Expand All @@ -132,22 +85,6 @@ my @info = ( # |----------------|----------------|----------------|
'-0 ', 8, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'-+0', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'-+0 ', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#', 8, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'# ', 8, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#0', 8, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#0 ', 8, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#+', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+ ', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+0', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#+0 ', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-', 8, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#-+', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#- ', 8, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#-+ ', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-0', 8, "0.000000e+00", "2.710000e+01", "-2.710000e+00",
'#-0 ', 8, " 0.000000e+00", " 2.710000e+01", "-2.710000e+00",
'#-+0', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",
'#-+0 ', 8, "+0.000000e+00", "+2.710000e+01", "-2.710000e+00",

# 8 positions with precision, precision fits sometimes
'', 8.2, "0.00e+00", "2.71e+01", "-2.71e+00",
Expand All @@ -166,42 +103,35 @@ my @info = ( # |----------------|----------------|----------------|
'-+0', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'-0 ', 8.2, " 0.00e+00", " 2.71e+01", "-2.71e+00",
'-+0 ', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#', 8.2, "0.00e+00", "2.71e+01", "-2.71e+00",
'# ', 8.2, " 0.00e+00", " 2.71e+01", "-2.71e+00",
'#0', 8.2, "0.00e+00", "2.71e+01", "-2.71e+00",
'#0 ', 8.2, " 0.00e+00", " 2.71e+01", "-2.71e+00",
'#+', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#+ ', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#+0', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#+0 ', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#-', 8.2, "0.00e+00", "2.71e+01", "-2.71e+00",
'#-+', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#- ', 8.2, " 0.00e+00", " 2.71e+01", "-2.71e+00",
'#-+ ', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#-0', 8.2, "0.00e+00", "2.71e+01", "-2.71e+00",
'#-+0', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",
'#-0 ', 8.2, " 0.00e+00", " 2.71e+01", "-2.71e+00",
'#-+0 ', 8.2, "+0.00e+00", "+2.71e+01", "-2.71e+00",

).map: -> $flags, $size, $r0, $r4, $rm {
my @flat;
@flat.append('%' ~ $_ ~ $size ~ 'e', $r0, $r4, $rm)
for $flags.comb.permutations>>.join;
@flat.append('%' ~ $_ ~ $size ~ 'e', $r0, $r4, $rm)
for "#$flags".comb.permutations>>.join;
|@flat
}

plan @info/4;

for @info -> $format, $r0, $r4, $rm {
subtest {
plan 3;
plan 6;

is sprintf($format, $v0), $r0,
"sprintf('$format',$v0) eq '$r0'";
is sprintf($format, $v4), $r4,
"sprintf('$format',$v4) eq '$r4'";
is sprintf($format, $vm), $rm,
"sprintf('$format',$vm) eq '$rm'";

is sprintf($format.uc, $v0), $r0.uc,
"sprintf('$format.uc()',$v0) eq '$r0.uc()'";
is sprintf($format.uc, $v4), $r4.uc,
"sprintf('$format.uc()',$v4) eq '$r4.uc()'";
is sprintf($format.uc, $vm), $rm.uc,
"sprintf('$format.uc()',$vm) eq '$rm.uc()'";
}, "Tested '$format'";
}

Expand Down

0 comments on commit 78d5fe2

Please sign in to comment.