summaryrefslogtreecommitdiff
path: root/test/-ext-/num2int/test_num2int.rb
blob: 1ab1390920453460a691d7d59fa1db63af89e78d (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# frozen_string_literal: false
require 'test/unit'
require '-test-/num2int'
require '-test-/integer'
require 'rbconfig/sizeof'

class TestNum2int < Test::Unit::TestCase
  l = RbConfig::LIMITS

  SHRT_MIN = l["SHRT_MIN"]
  SHRT_MAX = l["SHRT_MAX"]
  USHRT_MAX = l["USHRT_MAX"]

  INT_MIN = l["INT_MIN"]
  INT_MAX = l["INT_MAX"]
  UINT_MAX = l["UINT_MAX"]

  LONG_MAX = l["LONG_MAX"]
  LONG_MIN = l["LONG_MIN"]
  ULONG_MAX = l["ULONG_MAX"]

  LLONG_MAX = l["LLONG_MAX"]
  LLONG_MIN = l["LLONG_MIN"]
  ULLONG_MAX = l["ULLONG_MAX"]

  FIXNUM_MAX = l["FIXNUM_MAX"]
  FIXNUM_MIN = l["FIXNUM_MIN"]

  def fix2big(n)
    10000000000000000000000000000.coerce(n)[0]
  end

  def assert_num2i_success_internal(exp, func, arg)
    mesg = "#{func}(#{arg.inspect})"
    out = nil
    assert_nothing_raised(mesg) {
      out = Num2int.send(func, arg)
    }
    assert_equal(exp, out, mesg)
  end

  def assert_num2i_success(type, num, result=num)
    func = "NUM2#{type}".upcase
    assert_num2i_success_internal(result.to_s, func, num)
    assert_num2i_success_internal(result.to_s, func, fix2big(num))
    assert_num2i_success_internal(result.to_s, func, Rational(num, 1))
    if num.to_f.to_i == num
      assert_num2i_success_internal(result.to_s, func, num.to_f)
    end
    # The conversion functions such as NUM2INT uses (conceptually) to_int.
    if (arg = num.to_f + 0.5) != num.to_f && arg.to_int == num
      assert_num2i_success_internal(result.to_s, func, arg)
    end
    if (arg = num.to_f - 0.5) != num.to_f && arg.to_int == num
      assert_num2i_success_internal(result.to_s, func, arg)
    end
    if (arg = num + Rational(1,2)) && arg.to_int == num
      assert_num2i_success_internal(result.to_s, func, arg)
    end
    if (arg = num - Rational(1,2)) && arg.to_int == num
      assert_num2i_success_internal(result.to_s, func, arg)
    end
  end

  def assert_num2i_error_internal(func, arg)
    assert_raise(RangeError, "#{func}(#{arg.inspect})") {
      Num2int.send(func, arg)
    }
  end

  def assert_num2i_error(type, num)
    func = "NUM2#{type}".upcase
    assert_num2i_error_internal(func, num)
    assert_num2i_error_internal(func, fix2big(num))
    assert_num2i_error_internal(func, Rational(num, 1))
    if num.to_f.to_i == num
      assert_num2i_error_internal(func, num.to_f)
    end
    # The conversion functions such as NUM2INT uses (conceptually) to_int.
    if (arg = num.to_f + 0.5) != num.to_f && arg.to_int == num
      assert_num2i_error_internal(func, arg)
    end
    if (arg = num.to_f - 0.5) != num.to_f && arg.to_int == num
      assert_num2i_error_internal(func, arg)
    end
    if (arg = num + Rational(1,2)) && arg.to_int == num
      assert_num2i_error_internal(func, arg)
    end
    if (arg = num - Rational(1,2)) && arg.to_int == num
      assert_num2i_error_internal(func, arg)
    end
  end

  def assert_fix2i_success_internal(exp, func, arg)
    mesg = "#{func}(#{arg.inspect})"
    out = nil
    assert_nothing_raised(mesg) {
      out = Num2int.send(func, arg)
    }
    assert_equal(exp, out, mesg)
  end

  def assert_fix2i_success(type, num, result=num)
    return unless Bug::Integer.fixnum?(num)
    func = "FIX2#{type}".upcase
    assert_fix2i_success_internal(result.to_s, func, num)
  end

  def assert_fix2i_error_internal(func, arg)
    assert_raise(RangeError, "#{func}(#{arg.inspect})") {
      Num2int.send(func, arg)
    }
  end

  def assert_fix2i_error(type, num)
    return unless Bug::Integer.fixnum?(num)
    func = "FIX2#{type}".upcase
    assert_num2i_error_internal(func, num)
  end

  def test_num2short
    assert_num2i_success(:short, SHRT_MIN)
    assert_num2i_success(:short, SHRT_MIN+1)
    assert_num2i_success(:short, SHRT_MAX)
    assert_num2i_error(:short, SHRT_MIN-1)
    assert_num2i_error(:short, SHRT_MAX+1)
  end

  def test_num2ushort
    assert_num2i_success(:ushort, 0)
    assert_num2i_success(:ushort, USHRT_MAX)
    assert_num2i_success(:ushort, -1, USHRT_MAX)
    assert_num2i_success(:ushort, SHRT_MIN, SHRT_MAX+1)
    assert_num2i_success(:ushort, SHRT_MIN+1, SHRT_MAX+2)
    assert_num2i_error(:ushort, SHRT_MIN-1)
    assert_num2i_error(:ushort, USHRT_MAX+1)
  end

  def test_num2int
    assert_num2i_success(:int, INT_MIN)
    assert_num2i_success(:int, INT_MIN+1)
    assert_num2i_success(:int, INT_MAX)
    assert_num2i_error(:int, INT_MIN-1)
    assert_num2i_error(:int, INT_MAX+1)
  end

  def test_num2uint
    assert_num2i_success(:uint, 0)
    assert_num2i_success(:uint, UINT_MAX)
    assert_num2i_success(:uint, -1, UINT_MAX)
    assert_num2i_success(:uint, INT_MIN, INT_MAX+1)
    assert_num2i_success(:uint, INT_MIN+1, INT_MAX+2)
    assert_num2i_error(:uint, INT_MIN-1)
    assert_num2i_error(:uint, UINT_MAX+1)
  end

  def test_num2long
    assert_num2i_success(:long, LONG_MIN)
    assert_num2i_success(:long, LONG_MIN+1)
    assert_num2i_success(:long, LONG_MAX)
    assert_num2i_error(:long, LONG_MIN-1)
    assert_num2i_error(:long, LONG_MAX+1)
    assert_num2i_success(:long, FIXNUM_MIN)
    assert_num2i_success(:long, FIXNUM_MIN+1)
    assert_num2i_success(:long, FIXNUM_MIN-1)
    assert_num2i_success(:long, FIXNUM_MAX)
    assert_num2i_success(:long, FIXNUM_MAX+1)
  end

  def test_num2ulong
    assert_num2i_success(:ulong, 0)
    assert_num2i_success(:ulong, ULONG_MAX)
    assert_num2i_success(:ulong, -1, ULONG_MAX)
    assert_num2i_success(:ulong, LONG_MIN, LONG_MAX+1)
    assert_num2i_success(:ulong, LONG_MIN+1, LONG_MAX+2)
    assert_num2i_error(:ulong, LONG_MIN-1)
    assert_num2i_error(:ulong, ULONG_MAX+1)
    assert_num2i_success(:ulong, FIXNUM_MIN, ULONG_MAX-FIXNUM_MAX)
    assert_num2i_success(:ulong, FIXNUM_MIN+1, ULONG_MAX-FIXNUM_MAX+1)
    assert_num2i_success(:ulong, FIXNUM_MIN-1, ULONG_MAX-FIXNUM_MAX-1)
    assert_num2i_success(:ulong, FIXNUM_MAX, FIXNUM_MAX)
    assert_num2i_success(:ulong, FIXNUM_MAX+1, FIXNUM_MAX+1)
  end

  def test_num2ll
    assert_num2i_success(:ll, LLONG_MIN)
    assert_num2i_success(:ll, LLONG_MIN+1)
    assert_num2i_success(:ll, LLONG_MAX)
    assert_num2i_error(:ll, LLONG_MIN-1)
    assert_num2i_error(:ll, LLONG_MAX+1)
    assert_num2i_success(:ll, FIXNUM_MIN)
    assert_num2i_success(:ll, FIXNUM_MIN+1)
    assert_num2i_success(:ll, FIXNUM_MIN-1)
    assert_num2i_success(:ll, FIXNUM_MAX)
    assert_num2i_success(:ll, FIXNUM_MAX+1)
  end if defined?(Num2int.NUM2LL)

  def test_num2ull
    assert_num2i_success(:ull, 0)
    assert_num2i_success(:ull, ULLONG_MAX)
    assert_num2i_success(:ull, -1, ULLONG_MAX)
    assert_num2i_success(:ull, LLONG_MIN, LLONG_MAX+1)
    assert_num2i_success(:ull, LLONG_MIN+1, LLONG_MAX+2)
    assert_num2i_error(:ull, LLONG_MIN-1)
    assert_num2i_error(:ull, ULLONG_MAX+1)
    assert_num2i_success(:ull, FIXNUM_MIN, ULLONG_MAX-FIXNUM_MAX)
    assert_num2i_success(:ull, FIXNUM_MIN+1, ULLONG_MAX-FIXNUM_MAX+1)
    assert_num2i_success(:ull, FIXNUM_MIN-1, ULLONG_MAX-FIXNUM_MAX-1)
    assert_num2i_success(:ull, FIXNUM_MAX)
    assert_num2i_success(:ull, FIXNUM_MAX+1)
  end if defined?(Num2int.NUM2ULL)

  def test_fix2short
    assert_fix2i_success(:short, 0)
    assert_fix2i_success(:short, SHRT_MAX)
    assert_fix2i_success(:short, SHRT_MIN)
    assert_fix2i_success(:short, SHRT_MIN+1)
    assert_fix2i_error(:short, SHRT_MAX+1)
    assert_fix2i_error(:short, SHRT_MIN-1)
    assert_fix2i_error(:short, FIXNUM_MAX)
    assert_fix2i_error(:short, FIXNUM_MIN)
    assert_fix2i_error(:short, FIXNUM_MIN+1)
  end

  def test_fix2int
    assert_fix2i_success(:int, 0)
    assert_fix2i_success(:int, INT_MAX)
    assert_fix2i_success(:int, INT_MIN)
    assert_fix2i_success(:int, INT_MIN+1)
    assert_fix2i_error(:int, INT_MAX+1)
    assert_fix2i_error(:int, INT_MIN-1)
    assert_fix2i_error(:int, FIXNUM_MAX) if INT_MAX < FIXNUM_MAX
    assert_fix2i_error(:int, FIXNUM_MIN) if FIXNUM_MIN < INT_MIN
    assert_fix2i_error(:int, FIXNUM_MIN+1) if FIXNUM_MIN+1 < INT_MIN
  end

  def test_fix2uint
    assert_fix2i_success(:uint, 0)
    assert_fix2i_success(:uint, UINT_MAX)
    assert_fix2i_success(:uint, INT_MAX)
    assert_fix2i_success(:uint, INT_MIN, INT_MAX+1)
    assert_fix2i_success(:uint, INT_MIN+1, INT_MAX+2)
    assert_fix2i_error(:uint, UINT_MAX+1)
    assert_fix2i_error(:uint, INT_MIN-1)
    assert_fix2i_error(:uint, FIXNUM_MAX) if UINT_MAX < FIXNUM_MAX
    assert_fix2i_error(:uint, FIXNUM_MIN) if FIXNUM_MIN < INT_MIN
    assert_fix2i_error(:uint, FIXNUM_MIN+1) if FIXNUM_MIN+1 < INT_MIN
  end

  def test_fix2long
    assert_fix2i_success(:long, 0)
    assert_fix2i_success(:long, FIXNUM_MAX)
    assert_fix2i_success(:long, FIXNUM_MIN)
    assert_fix2i_success(:long, FIXNUM_MIN+1)
  end

  def test_fix2ulong
    assert_fix2i_success(:ulong, 0)
    assert_fix2i_success(:ulong, FIXNUM_MAX)
    assert_fix2i_success(:ulong, -1, ULONG_MAX)
  end

end