1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00
libkernaux/pkgs/mruby/test/ntoa.rb

233 lines
6.1 KiB
Ruby
Raw Normal View History

2022-05-30 06:02:16 -04:00
def common_assert(expected, result)
assert_true result.instance_of? String
assert_true result.frozen?
assert_equal expected, result
2022-05-30 06:02:16 -04:00
end
2022-05-30 06:02:16 -04:00
def test_utoa(number, base, expected)
common_assert expected, KernAux.utoa(number, base)
common_assert expected, KernAux.utoa(number, base, nil)
common_assert expected, KernAux.utoa(number, base, '')
2022-02-03 04:42:50 -05:00
end
def test_itoa(number, base, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.itoa(number, base)
common_assert expected, KernAux.itoa(number, base, nil)
common_assert expected, KernAux.itoa(number, base, '')
end
def test_utoax(number, base, prefix, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.utoa(number, base, prefix)
end
def test_itoax(number, base, prefix, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.itoa(number, base, prefix)
2022-02-03 04:42:50 -05:00
end
2022-02-03 04:54:25 -05:00
def test_utoa10(number, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.utoa10(number)
2022-02-03 04:54:25 -05:00
end
def test_itoa10(number, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.itoa10(number)
2022-02-03 04:54:25 -05:00
end
def test_utoa16(number, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.utoa16(number)
2022-02-03 04:54:25 -05:00
end
def test_itoa16(number, expected)
2022-05-30 06:02:16 -04:00
common_assert expected, KernAux.itoa16(number)
2022-02-03 04:54:25 -05:00
end
2022-02-03 03:27:14 -05:00
assert 'KernAux.utoa' do
number = Random.rand(2**32 - 1)
base = 2 + Random.rand(36 - 2)
2022-02-03 04:42:50 -05:00
test_utoa number, base, number.to_s(base)
2022-02-03 03:27:14 -05:00
base = 2 + Random.rand(36 - 2)
2022-02-03 04:47:20 -05:00
test_utoa 0, base, '0'
2022-02-03 03:27:14 -05:00
number = 2**32 - 1
base = 2 + Random.rand(36 - 2)
2022-02-03 04:42:50 -05:00
test_utoa number, base, number.to_s(base)
2022-02-03 03:27:14 -05:00
base = 2 + Random.rand(36 - 2)
assert_raise RangeError, 'can\'t convert negative number to uint64_t' do
2022-02-07 08:48:50 -05:00
KernAux.utoa(-1, base)
2022-02-03 03:27:14 -05:00
end
number = Random.rand(2**32 - 1)
base = -(2 + Random.rand(36 - 2))
2022-02-03 04:42:50 -05:00
test_utoa number, base, number.to_s(-base).upcase
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :b, number.to_s(2)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :B, number.to_s(2)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :o, number.to_s(8)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :O, number.to_s(8)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :d, number.to_s(10)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :D, number.to_s(10)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :h, number.to_s(16)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :x, number.to_s(16)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :H, number.to_s(16).upcase
2022-02-03 03:27:14 -05:00
number = Random.rand(2**32 - 1)
2022-02-03 04:47:20 -05:00
test_utoa number, :X, number.to_s(16).upcase
number = Random.rand(2**32 - 1)
base = 2 + Random.rand(36 - 2)
prefix = 'foo'
test_utoax number, base, prefix, "#{prefix}#{number.abs.to_s(base)}"
number = Random.rand(2**32 - 1)
base = 2 + Random.rand(36 - 2)
prefix = 'a' * 100
test_utoax number, base, prefix, "#{prefix}#{number.abs.to_s(base)}"
assert_raise ArgumentError, 'prefix length 101 is too long' do
number = Random.rand(2**32 - 1)
base = 2 + Random.rand(36 - 2)
prefix = 'a' * 101
KernAux.utoa(number, base, prefix)
end
2022-02-03 03:27:14 -05:00
end
assert 'KernAux.itoa' do
number = Random.rand(2**31 - 1) * [1, -1].sample
base = 2 + Random.rand(36 - 2)
2022-02-03 04:42:50 -05:00
test_itoa number, base, number.to_s(base)
2022-02-03 03:27:14 -05:00
base = 2 + Random.rand(36 - 2)
2022-02-03 04:47:20 -05:00
test_itoa 0, base, '0'
2022-02-03 03:27:14 -05:00
base = 2 + Random.rand(36 - 2)
2022-02-03 04:47:20 -05:00
test_itoa 1, base, '1'
2022-02-03 03:27:14 -05:00
base = 2 + Random.rand(36 - 2)
2022-02-07 08:48:50 -05:00
test_itoa(-1, base, '-1')
2022-02-03 03:27:14 -05:00
number = 2**31 - 1
base = 2 + Random.rand(36 - 2)
2022-02-03 04:42:50 -05:00
test_itoa number, base, number.to_s(base)
2022-02-03 03:27:14 -05:00
number = -(2**31 - 1)
base = 2 + Random.rand(36 - 2)
2022-02-03 04:42:50 -05:00
test_itoa number, base, number.to_s(base)
number = 2**31 - 1
base = -(2 + Random.rand(36 - 2))
test_itoa number, base, number.to_s(-base).upcase
number = -(2**31 - 1)
base = -(2 + Random.rand(36 - 2))
test_itoa number, base, number.to_s(-base).upcase
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :b, number.to_s(2)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :B, number.to_s(2)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :o, number.to_s(8)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :O, number.to_s(8)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :d, number.to_s(10)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :D, number.to_s(10)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :h, number.to_s(16)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :x, number.to_s(16)
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :H, number.to_s(16).upcase
2022-02-03 03:27:14 -05:00
number = Random.rand(2**31 - 1) * [1, -1].sample
2022-02-03 04:47:20 -05:00
test_itoa number, :X, number.to_s(16).upcase
number = Random.rand(2**31 - 1) * [1, -1].sample
base = 2 + Random.rand(36 - 2)
prefix = 'foo'
sign = number < 0 ? '-' : ''
test_itoax number, base, prefix, "#{sign}#{prefix}#{number.abs.to_s(base)}"
number = Random.rand(2**31 - 1) * [1, -1].sample
base = 2 + Random.rand(36 - 2)
prefix = 'a' * 100
sign = number < 0 ? '-' : ''
test_itoax number, base, prefix, "#{sign}#{prefix}#{number.abs.to_s(base)}"
assert_raise ArgumentError, 'prefix length 101 is too long' do
number = Random.rand(2**31 - 1) * [1, -1].sample
base = 2 + Random.rand(36 - 2)
prefix = 'a' * 101
KernAux.itoa(number, base, prefix)
end
2022-02-03 03:27:14 -05:00
end
assert 'KernAux.utoa10' do
2022-02-03 04:54:25 -05:00
test_utoa10 0, '0'
test_utoa10 1, '1'
test_utoa10 123, '123'
test_utoa10 2**32 - 1, (2**32 - 1).to_s
assert_raise RangeError, 'can\'t convert negative number to uint64_t' do
2022-02-07 08:48:50 -05:00
KernAux.utoa10(-1)
2022-02-03 04:54:25 -05:00
end
end
assert 'KernAux.itoa10' do
2022-02-03 04:54:25 -05:00
test_itoa10 0, '0'
test_itoa10 1, '1'
2022-02-07 08:48:50 -05:00
test_itoa10(-1, '-1')
2022-02-03 04:54:25 -05:00
test_itoa10 123, '123'
2022-02-07 08:48:50 -05:00
test_itoa10(-123, '-123')
2022-02-03 04:54:25 -05:00
test_itoa10 2**31 - 1, (2**31 - 1).to_s
2022-02-07 08:48:50 -05:00
test_itoa10(-2**31, (-2**31).to_s)
end
assert 'KernAux.utoa16' do
2022-02-03 04:54:25 -05:00
test_utoa16 0, '0'
test_utoa16 1, '1'
test_utoa16 0x123, '123'
test_utoa16 2**32 - 1, (2**32 - 1).to_s(16)
assert_raise RangeError, 'can\'t convert negative number to uint64_t' do
2022-02-07 08:48:50 -05:00
KernAux.utoa16(-1)
2022-02-03 04:54:25 -05:00
end
end
assert 'KernAux.itoa16' do
2022-02-03 04:54:25 -05:00
test_itoa16 0, '0'
test_itoa16 1, '1'
2022-02-07 08:48:50 -05:00
test_itoa16(-1, '-1')
2022-02-03 04:54:25 -05:00
test_itoa16 0x123, '123'
2022-02-07 08:48:50 -05:00
test_itoa16(-0x123, '-123')
2022-02-03 04:54:25 -05:00
test_itoa16 2**31 - 1, (2**31 - 1).to_s(16)
2022-02-07 08:48:50 -05:00
test_itoa16(-2**31, (-2**31).to_s(16))
end