Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Add more evil sprintf() tests #17

Closed
wants to merge 1 commit into from

2 participants

Konrad Borowski Duke Leto
Konrad Borowski
Collaborator
xfix commented March 29, 2012

I've decided to write some evil tests for sprintf() function. I'm not sure if they're valid because spec says nearly nothing about sprintf() aside of format used by it (and "is mostly identical to the C library sprintf function", even if it has vectors in grammar which only Perl 5 implementation has), so I'm making pull request.

Duke Leto
Collaborator
leto commented March 29, 2012

+1. The spec needs clarity on exactly what is and is not a valid sprintf format string as well as how errors will be thrown.

Konrad Borowski xfix closed this December 01, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 29, 2012
Konrad Borowski More evil sprintf() tests 3cb0da8
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 34 additions and 8 deletions. Show diff stats Hide diff stats

  1. 42  S32-str/sprintf.t
42  S32-str/sprintf.t
@@ -2,7 +2,7 @@ use v6;
2 2
 
3 3
 use Test;
4 4
 
5  
-plan 43;
  5
+plan 63;
6 6
 
7 7
 # L<S32::Str/Str/"identical to" "C library sprintf">
8 8
 
@@ -12,25 +12,40 @@ is sprintf("%03d",      3),       "003",    "sprintf() works with one arg";
12 12
 is sprintf("%03d %02d", 3, 1),    "003 01", "sprintf() works with two args";
13 13
 is sprintf("%d %d %d",  3,1,4),   "3 1 4",  "sprintf() works with three args";
14 14
 is sprintf("%d%d%d%d",  3,1,4,1), "3141",   "sprintf() works with four args";
  15
+is sprintf('%2$d %1$d', 42, 24),  '24 42',  'numbered parameters';
15 16
 
16  
-ok(eval('sprintf("%b",1)'),                  'eval of sprintf() with %b');
  17
+ok(eval('sprintf("%b",1)'),                 'eval of sprintf() with %b');
17 18
 
18  
-is sprintf("%04b",3),             '0011',   '0-padded sprintf() with %b';
19  
-is sprintf("%4b",3),              '  11',   '" "-padded sprintf() with %b';
20  
-is sprintf("%b",30),              '11110',  'longer string, no padding';
21  
-is sprintf("%2b",30),             '11110',  'padding specified, not needed';
22  
-is sprintf("%03b",7),             '111',    '0 padding, longer string';
23  
-is sprintf("%b %b",3,3),          '11 11',  'two args %b';
  19
+is sprintf("%04b", 3),            '0011',   '0-padded sprintf() with %b';
  20
+is sprintf("%4b", 3),             '  11',   '" "-padded sprintf() with %b';
  21
+is sprintf('%-4b', 3),            '11  ',   'left padding';
  22
+is sprintf("%b", 30),             '11110',  'longer string, no padding';
  23
+is sprintf("%2b", 30),            '11110',  'padding specified, not needed';
  24
+is sprintf("%03b", 7),            '111',    '0 padding, longer string';
  25
+is sprintf("%b %b", 3, 3),        '11 11',  'two args %b';
24 26
 
25 27
 is sprintf('%c', 97),             'a',      '%c test';
26 28
 is sprintf('%s', 'string'),       'string', '%s test';
  29
+is sprintf('%2s', 's'),           ' s',     '%s length';
  30
+is sprintf('%1$s', '%1$s'),       '%1$s',   "%s shouldn't be recursive";
  31
+is sprintf('%3.2s', 'string'),    ' st',    '%s precission';
  32
+is sprintf('%s', [1, 3]),         '1 3',    'array to string';
  33
+is sprintf('%d', [1, 3]),         '2',      'array to digit';
27 34
 
28 35
 is sprintf('%d', 12),             '12',     'simple %d';
29 36
 is sprintf('%d', -22),            '-22',    'negative %d';
30 37
 is sprintf('%04d', 32),           '0032',   '0-padded %d';
31 38
 is sprintf('%04d', -42),          '-042',   '0-padded negative %d';
  39
+is sprintf('% d', 1),             ' 1',     'space-padded digit';
  40
+is sprintf('% d', -1),            '-1',     'space-padding negative digits';
32 41
 is sprintf('%i', -22),            '-22',    'negative %i';
33 42
 is sprintf('%04i', -42),          '-042',   '0-padded negative %i';
  43
+is sprintf('%d', 123.789),        '123',    "%d format shouldn't round up";
  44
+
  45
+is sprintf('%.0f', 0),            '0',      '%f modifier';
  46
+is sprintf('%*.*f', 4, 1, 3),     ' 3.0',   '* operator';
  47
+
  48
+is sprintf('%e', 0.1234567E-101), '1.234567e-102', 'very small numbers';
34 49
 
35 50
 is sprintf('%u', 12),             '12',     'simple %u';
36 51
 is sprintf('%u', 22.01),          '22',     'decimal %u';
@@ -50,10 +65,19 @@ is sprintf('%03x', 42.6),         '02a',    '0-padded decimal %x';
50 65
 is sprintf('%X', 12),             'C',      'simple %X';
51 66
 is sprintf('%03X', 42.6),         '02A',    '0-padded decimal %X';
52 67
 
  68
+is sprintf('%hd', 2 ** 16 + 1),   '1',      'type argument';
  69
+
  70
+is sprintf('%vd', "A\x100"),      '65.256', 'vector flag';
  71
+is sprintf('%vc', 'Str'),         'S.t.r',  'vector characters';
  72
+is sprintf('%#v.0o', "\0\x1"),    '0.01',   'octal prefixes';
  73
+is sprintf('%*vc!', ' ', 'STR'),  'S T R!', 'custom vector separators';
  74
+
53 75
 # L<S32::Str/"Str"/"The special directive, %n does not work in Perl 6">
54 76
 dies_ok(sub {my $x = sprintf('%n', 1234)}, '%n dies (Perl 5 compatibility)');   #OK not used
55 77
 #?rakudo todo "%p doesn't yet throw exception - but should it, or just Failure?"
56 78
 dies_ok(sub {my $x = sprintf('%p', 1234)}, '%p dies (Perl 5 compatibility)');   #OK not used
  79
+# Abstract parameter number
  80
+dies_ok(sub {my $x = sprintf('%*2147483647$v2d', 0)}, 'Wrong parameter number');#OK not used
57 81
 
58 82
 is sprintf('%s', NaN),              NaN,    'sprintf %s handles NaN';
59 83
 is sprintf('%s', -NaN),             NaN,    'sprintf %s handles NaN';
@@ -65,6 +89,8 @@ is sprintf('%s', -Inf),            -Inf,    'sprintf %s handles Inf';
65 89
 is sprintf('%d %1$x %1$o', 12),    '12 c 14',  'positional argument specifier $';
66 90
 }
67 91
 
  92
+dies_ok {sprintf}, 'missing sprintf value';
  93
+
68 94
 # RT #74610
69 95
 dies_ok {sprintf "%s"}, 'missing sprintf string argument';
70 96
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.