[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / test / -ext- / test_printf.rb
blobc2b50285b9adcc486aa5ceb271783d996de5833b
1 # frozen_string_literal: false
2 require 'test/unit'
3 require "-test-/printf"
4 require_relative '../ruby/allpairs'
6 class Test_SPrintf < Test::Unit::TestCase
7   def to_s
8     "#{self.class}:#{object_id}"
9   end
11   def inspect
12     "<#{self.class}:#{object_id}>"
13   end
15   def test_to_str
16     assert_equal("<#{self.class}:#{object_id}>", Bug::Printf.s(self))
17   end
19   def test_inspect
20     assert_equal("{<#{self.class}:#{object_id}>}", Bug::Printf.v(self))
21   end
23   def test_quote
24     assert_equal('[\n]', Bug::Printf.q("\n"))
25     assert_equal('[aaa]', Bug::Printf.q('aaa'))
26     assert_equal('[a a]', Bug::Printf.q('a a'))
27     assert_equal('[]', Bug::Printf.q(''))
28     assert_equal('[]', Bug::Printf.q(:''))
29   end
31   def test_encoding
32     def self.to_s
33       "\u{3042 3044 3046 3048 304a}"
34     end
35     assert_equal("<\u{3042 3044 3046 3048 304a}>", Bug::Printf.s(self))
36   end
38   VS = [
39     #-0x1000000000000000000000000000000000000000000000002,
40     #-0x1000000000000000000000000000000000000000000000001,
41     #-0x1000000000000000000000000000000000000000000000000,
42     #-0xffffffffffffffffffffffffffffffffffffffffffffffff,
43     #-0x1000000000000000000000002,
44     #-0x1000000000000000000000001,
45     #-0x1000000000000000000000000,
46     #-0xffffffffffffffffffffffff,
47     -0x10000000000000002,
48     -0x10000000000000001,
49     -0x10000000000000000,
50     -0xffffffffffffffff,
51     -0x4000000000000002,
52     -0x4000000000000001,
53     -0x4000000000000000,
54     -0x3fffffffffffffff,
55     -0x100000002,
56     -0x100000001,
57     -0x100000000,
58     -0xffffffff,
59     #-0xc717a08d, # 0xc717a08d * 0x524b2245 = 0x4000000000000001
60     -0x80000002,
61     -0x80000001,
62     -0x80000000,
63     -0x7fffffff,
64     #-0x524b2245,
65     -0x40000002,
66     -0x40000001,
67     -0x40000000,
68     -0x3fffffff,
69     #-0x10002,
70     #-0x10001,
71     #-0x10000,
72     #-0xffff,
73     #-0x8101, # 0x8101 * 0x7f01 = 0x40000001
74     #-0x8002,
75     #-0x8001,
76     #-0x8000,
77     #-0x7fff,
78     #-0x7f01,
79     #-65,
80     #-64,
81     #-63,
82     #-62,
83     #-33,
84     #-32,
85     #-31,
86     #-30,
87     -3,
88     -2,
89     -1,
90     0,
91     1,
92     2,
93     3,
94     #30,
95     #31,
96     #32,
97     #33,
98     #62,
99     #63,
100     #64,
101     #65,
102     #0x7f01,
103     #0x7ffe,
104     #0x7fff,
105     #0x8000,
106     #0x8001,
107     #0x8101,
108     #0xfffe,
109     #0xffff,
110     #0x10000,
111     #0x10001,
112     0x3ffffffe,
113     0x3fffffff,
114     0x40000000,
115     0x40000001,
116     #0x524b2245,
117     0x7ffffffe,
118     0x7fffffff,
119     0x80000000,
120     0x80000001,
121     #0xc717a08d,
122     0xfffffffe,
123     0xffffffff,
124     0x100000000,
125     0x100000001,
126     0x3ffffffffffffffe,
127     0x3fffffffffffffff,
128     0x4000000000000000,
129     0x4000000000000001,
130     0xfffffffffffffffe,
131     0xffffffffffffffff,
132     0x10000000000000000,
133     0x10000000000000001,
134     #0xffffffffffffffffffffffff,
135     #0x1000000000000000000000000,
136     #0x1000000000000000000000001,
137     #0xffffffffffffffffffffffffffffffffffffffffffffffff,
138     #0x1000000000000000000000000000000000000000000000000,
139     #0x1000000000000000000000000000000000000000000000001
140   ]
141   VS.reverse!
143   FLAGS = [[nil, ' '], [nil, '#'], [nil, '+'], [nil, '-'], [nil, '0']]
145   def self.assertions_format_integer(format, type, **opts)
146     proc {
147       VS.each {|v|
148         begin
149           r = Bug::Printf.(type, v, **opts)
150         rescue RangeError
151         else
152           e = sprintf format, v
153           assert_equal([e, format], r, "rb_sprintf(#{format.dump}, #{v})")
154         end
155       }
156     }
157   end
159   AllPairs.each(%w[d],
160                 # octal and hexadecimal deal with negative values differently
161                 [nil, 0, 5, 20],
162                 [nil, true, 0], # 8, 20
163                 *FLAGS) {
164     |type, width, prec, sp, hs, pl, mi, zr|
165     precision = ".#{prec unless prec == true}" if prec
166     format = "%#{sp}#{hs}#{pl}#{mi}#{zr}#{width}#{precision}#{type}"
167     define_method("test_format_integer(#{format})",
168                   assertions_format_integer(format, type,
169                                             space: sp, hash: hs,
170                                             plus: pl, minus: mi,
171                                             zero: zr, width: width,
172                                             prec: prec))
173   }
175   def test_string_prec
176     assert_equal("a", Bug::Printf.("s", "a", prec: 3)[0])
177     assert_equal("  a", Bug::Printf.("s", "a", width: 3, prec: 3)[0])
178     assert_equal("a  ", Bug::Printf.("s", "a", minus: true, width: 3, prec: 3)[0])
179   end
181   def test_snprintf_count
182     assert_equal(3, Bug::Printf.sncount("foo"))
183   end