1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
require 'test/unit'
class TestSprintf < Test::Unit::TestCase
def test_positional
assert_equal(" 00001", sprintf("%*1$.*2$3$d", 10, 5, 1))
end
def test_binary
assert_equal("0", sprintf("%b", 0))
assert_equal("1", sprintf("%b", 1))
assert_equal("10", sprintf("%b", 2))
assert_equal("..1", sprintf("%b", -1))
assert_equal(" 0", sprintf("%4b", 0))
assert_equal(" 1", sprintf("%4b", 1))
assert_equal(" 10", sprintf("%4b", 2))
assert_equal(" ..1", sprintf("%4b", -1))
assert_equal("0000", sprintf("%04b", 0))
assert_equal("0001", sprintf("%04b", 1))
assert_equal("0010", sprintf("%04b", 2))
assert_equal("1111", sprintf("%04b", -1))
assert_equal("0000", sprintf("%.4b", 0))
assert_equal("0001", sprintf("%.4b", 1))
assert_equal("0010", sprintf("%.4b", 2))
assert_equal("1111", sprintf("%.4b", -1))
assert_equal(" 0000", sprintf("%6.4b", 0))
assert_equal(" 0001", sprintf("%6.4b", 1))
assert_equal(" 0010", sprintf("%6.4b", 2))
assert_equal(" 1111", sprintf("%6.4b", -1))
assert_equal(" 0b0", sprintf("%#4b", 0))
assert_equal(" 0b1", sprintf("%#4b", 1))
assert_equal("0b10", sprintf("%#4b", 2))
assert_equal("0b..1", sprintf("%#4b", -1))
assert_equal("0b00", sprintf("%#04b", 0))
assert_equal("0b01", sprintf("%#04b", 1))
assert_equal("0b10", sprintf("%#04b", 2))
assert_equal("0b11", sprintf("%#04b", -1))
assert_equal("0b0000", sprintf("%#.4b", 0))
assert_equal("0b0001", sprintf("%#.4b", 1))
assert_equal("0b0010", sprintf("%#.4b", 2))
assert_equal("0b1111", sprintf("%#.4b", -1))
assert_equal("0b0000", sprintf("%#6.4b", 0))
assert_equal("0b0001", sprintf("%#6.4b", 1))
assert_equal("0b0010", sprintf("%#6.4b", 2))
assert_equal("0b1111", sprintf("%#6.4b", -1))
assert_equal("+0", sprintf("%+b", 0))
assert_equal("+1", sprintf("%+b", 1))
assert_equal("+10", sprintf("%+b", 2))
assert_equal("-1", sprintf("%+b", -1))
assert_equal(" +0", sprintf("%+4b", 0))
assert_equal(" +1", sprintf("%+4b", 1))
assert_equal(" +10", sprintf("%+4b", 2))
assert_equal(" -1", sprintf("%+4b", -1))
assert_equal("+000", sprintf("%+04b", 0))
assert_equal("+001", sprintf("%+04b", 1))
assert_equal("+010", sprintf("%+04b", 2))
assert_equal("-001", sprintf("%+04b", -1))
assert_equal("+0000", sprintf("%+.4b", 0))
assert_equal("+0001", sprintf("%+.4b", 1))
assert_equal("+0010", sprintf("%+.4b", 2))
assert_equal("-0001", sprintf("%+.4b", -1))
assert_equal(" +0000", sprintf("%+6.4b", 0))
assert_equal(" +0001", sprintf("%+6.4b", 1))
assert_equal(" +0010", sprintf("%+6.4b", 2))
assert_equal(" -0001", sprintf("%+6.4b", -1))
end
def test_nan
nan = 0.0 / 0.0
assert_equal("NaN", sprintf("%f", nan))
assert_equal("NaN", sprintf("%-f", nan))
assert_equal("+NaN", sprintf("%+f", nan))
assert_equal(" NaN", sprintf("%8f", nan))
assert_equal("NaN ", sprintf("%-8f", nan))
assert_equal(" +NaN", sprintf("%+8f", nan))
assert_equal("00000NaN", sprintf("%08f", nan))
assert_equal("NaN ", sprintf("%-08f", nan))
assert_equal("+0000NaN", sprintf("%+08f", nan))
assert_equal(" NaN", sprintf("% 8f", nan))
assert_equal(" NaN ", sprintf("%- 8f", nan))
assert_equal(" +NaN", sprintf("%+ 8f", nan))
assert_equal(" 0000NaN", sprintf("% 08f", nan))
assert_equal(" NaN ", sprintf("%- 08f", nan))
assert_equal("+0000NaN", sprintf("%+ 08f", nan))
end
def test_inf
inf = 1.0 / 0.0
assert_equal("Inf", sprintf("%f", inf))
assert_equal("Inf", sprintf("%-f", inf))
assert_equal("+Inf", sprintf("%+f", inf))
assert_equal(" Inf", sprintf("%8f", inf))
assert_equal("Inf ", sprintf("%-8f", inf))
assert_equal(" +Inf", sprintf("%+8f", inf))
assert_equal("00000Inf", sprintf("%08f", inf))
assert_equal("Inf ", sprintf("%-08f", inf))
assert_equal("+0000Inf", sprintf("%+08f", inf))
assert_equal(" Inf", sprintf("% 8f", inf))
assert_equal(" Inf ", sprintf("%- 8f", inf))
assert_equal(" +Inf", sprintf("%+ 8f", inf))
assert_equal(" 0000Inf", sprintf("% 08f", inf))
assert_equal(" Inf ", sprintf("%- 08f", inf))
assert_equal("+0000Inf", sprintf("%+ 08f", inf))
assert_equal("-Inf", sprintf("%f", -inf))
assert_equal("-Inf", sprintf("%-f", -inf))
assert_equal("-Inf", sprintf("%+f", -inf))
assert_equal(" -Inf", sprintf("%8f", -inf))
assert_equal("-Inf ", sprintf("%-8f", -inf))
assert_equal(" -Inf", sprintf("%+8f", -inf))
assert_equal("-0000Inf", sprintf("%08f", -inf))
assert_equal("-Inf ", sprintf("%-08f", -inf))
assert_equal("-0000Inf", sprintf("%+08f", -inf))
assert_equal(" -Inf", sprintf("% 8f", -inf))
assert_equal("-Inf ", sprintf("%- 8f", -inf))
assert_equal(" -Inf", sprintf("%+ 8f", -inf))
assert_equal("-0000Inf", sprintf("% 08f", -inf))
assert_equal("-Inf ", sprintf("%- 08f", -inf))
assert_equal("-0000Inf", sprintf("%+ 08f", -inf))
assert_equal('..f00000000',
sprintf("%x", -2**32), '[ruby-dev:32351]')
assert_equal("..101111111111111111111111111111111",
sprintf("%b", -2147483649), '[ruby-dev:32365]')
end
def test_invalid
# Star precision before star width:
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%.**d", 5, 10, 1)}
# Precision before flags and width:
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%.5+05d", 5)}
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%.5 5d", 5)}
# Overriding a star width with a numeric one:
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%*1s", 5, 1)}
# Width before flags:
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%5+0d", 1)}
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%5 0d", 1)}
# Specifying width multiple times:
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%50+30+20+10+5d", 5)}
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%50 30 20 10 5d", 5)}
# Specifying the precision multiple times with negative star arguments:
assert_raise(ArgumentError, "[ruby-core:11570]") {sprintf("%.*.*.*.*f", -1, -1, -1, 5, 1)}
# Null bytes after percent signs are removed:
assert_equal("%\0x hello", sprintf("%\0x hello"), "[ruby-core:11571]")
assert_raise(ArgumentError, "[ruby-core:11573]") {sprintf("%.25555555555555555555555555555555555555s", "hello")}
end
def test_float
assert_equal("36893488147419111424",
sprintf("%20.0f", 36893488147419107329.0))
end
end
|