mirror of https://github.com/tailix/libkernaux.git
Ruby: add methods KernAux.supports_*?
This commit is contained in:
parent
a8af451550
commit
635f7b5849
|
@ -65,7 +65,7 @@ module KernAux
|
||||||
|
|
||||||
# @!parse [ruby]
|
# @!parse [ruby]
|
||||||
|
|
||||||
if singleton_class.method_defined? :snprintf1
|
if Version.supports_printf?
|
||||||
##
|
##
|
||||||
# Typical `printf`.
|
# Typical `printf`.
|
||||||
#
|
#
|
||||||
|
@ -127,8 +127,7 @@ module KernAux
|
||||||
# @return [Array<String>] command line arguments
|
# @return [Array<String>] command line arguments
|
||||||
#
|
#
|
||||||
# @raise [CmdlineError] syntax is invalid
|
# @raise [CmdlineError] syntax is invalid
|
||||||
|
##
|
||||||
# @!parse [ruby]
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# @!method utoa(number, base, prefix)
|
# @!method utoa(number, base, prefix)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
# rubocop:disable Lint/EmptyClass
|
# rubocop:disable Lint/EmptyClass
|
||||||
|
|
||||||
if defined? KernAux::File
|
if KernAux::Version.supports_file?
|
||||||
module KernAux
|
module KernAux
|
||||||
##
|
##
|
||||||
# File simulator.
|
# File simulator.
|
||||||
|
|
|
@ -3,4 +3,62 @@
|
||||||
module KernAux
|
module KernAux
|
||||||
# Gem version.
|
# Gem version.
|
||||||
VERSION = '0.3.0'
|
VERSION = '0.3.0'
|
||||||
|
|
||||||
|
##
|
||||||
|
# This module includes functions to determine if specific features are
|
||||||
|
# supported by the library.
|
||||||
|
#
|
||||||
|
module Version
|
||||||
|
def self.supports_cmdline?
|
||||||
|
KernAux.singleton_class.method_defined? :cmdline
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_file?
|
||||||
|
defined? KernAux::File
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_itoa?
|
||||||
|
KernAux.singleton_class.method_defined? :itoa
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_itoa2?
|
||||||
|
KernAux.singleton_class.method_defined? :itoa2
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_itoa8?
|
||||||
|
KernAux.singleton_class.method_defined? :itoa8
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_itoa10?
|
||||||
|
KernAux.singleton_class.method_defined? :itoa10
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_itoa16?
|
||||||
|
KernAux.singleton_class.method_defined? :itoa16
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_printf?
|
||||||
|
KernAux.singleton_class.method_defined? :snprintf1
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_utoa?
|
||||||
|
KernAux.singleton_class.method_defined? :utoa
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_utoa2?
|
||||||
|
KernAux.singleton_class.method_defined? :utoa2
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_utoa8?
|
||||||
|
KernAux.singleton_class.method_defined? :utoa8
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_utoa10?
|
||||||
|
KernAux.singleton_class.method_defined? :utoa10
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.supports_utoa16?
|
||||||
|
KernAux.singleton_class.method_defined? :utoa16
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,94 +2,92 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.cmdline' do
|
KernAux::Version.supports_cmdline? and RSpec.describe KernAux, '.cmdline' do
|
||||||
if described_class.singleton_class.method_defined? :cmdline
|
subject(:cmdline) { described_class.cmdline str }
|
||||||
subject(:cmdline) { described_class.cmdline str }
|
|
||||||
|
|
||||||
let(:str) { 'foo bar\\ baz "car cdr"' }
|
let(:str) { 'foo bar\\ baz "car cdr"' }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of Array }
|
it { is_expected.to be_instance_of Array }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to all be_instance_of String }
|
it { is_expected.to all be_instance_of String }
|
||||||
it { is_expected.to all be_frozen }
|
it { is_expected.to all be_frozen }
|
||||||
it { is_expected.to eq ['foo', 'bar baz', 'car cdr'] }
|
it { is_expected.to eq ['foo', 'bar baz', 'car cdr'] }
|
||||||
|
|
||||||
context 'when str is empty' do
|
context 'when str is empty' do
|
||||||
let(:str) { '' }
|
let(:str) { '' }
|
||||||
|
|
||||||
it { is_expected.to eq [] }
|
it { is_expected.to eq [] }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when str has invalid type' do
|
||||||
|
let(:str) { 123 }
|
||||||
|
|
||||||
|
specify { expect { cmdline }.to raise_error TypeError }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when str has EOL after backslash' do
|
||||||
|
let(:str) { '\\' }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { cmdline }.to \
|
||||||
|
raise_error described_class::CmdlineError, 'EOL after backslash'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when str has invalid type' do
|
context 'when str has EOL after backslash inside quote' do
|
||||||
let(:str) { 123 }
|
let(:str) { '"\\' }
|
||||||
|
|
||||||
specify { expect { cmdline }.to raise_error TypeError }
|
specify do
|
||||||
|
expect { cmdline }.to raise_error \
|
||||||
|
described_class::CmdlineError, 'EOL after backslash inside quote'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when str has EOL after backslash' do
|
context 'when str has unescaped quotation mark' do
|
||||||
let(:str) { '\\' }
|
let(:str) { 'foo"' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect { cmdline }.to \
|
expect { cmdline }.to \
|
||||||
raise_error described_class::CmdlineError, 'EOL after backslash'
|
raise_error described_class::CmdlineError, 'unescaped quotation mark'
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when str has EOL after backslash inside quote' do
|
context 'when str has EOL inside quote' do
|
||||||
let(:str) { '"\\' }
|
let(:str) { '"' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect { cmdline }.to raise_error \
|
expect { cmdline }.to \
|
||||||
described_class::CmdlineError, 'EOL after backslash inside quote'
|
raise_error described_class::CmdlineError, 'EOL inside quote'
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when str has unescaped quotation mark' do
|
context 'when there are not too many args' do
|
||||||
let(:str) { 'foo"' }
|
let(:str) { 'a ' * 256 }
|
||||||
|
|
||||||
specify do
|
it { is_expected.to eq ['a'] * 256 }
|
||||||
expect { cmdline }.to \
|
end
|
||||||
raise_error described_class::CmdlineError, 'unescaped quotation mark'
|
|
||||||
end
|
context 'when there are too many args' do
|
||||||
|
let(:str) { 'a ' * 257 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { cmdline }.to \
|
||||||
|
raise_error described_class::CmdlineError, 'too many args'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when str has EOL inside quote' do
|
context 'when args don\'t cause buffer overflow' do
|
||||||
let(:str) { '"' }
|
let(:str) { 'a' * 4095 }
|
||||||
|
|
||||||
specify do
|
it { is_expected.to eq ['a' * 4095] }
|
||||||
expect { cmdline }.to \
|
end
|
||||||
raise_error described_class::CmdlineError, 'EOL inside quote'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when there are not too many args' do
|
context 'when args cause buffer overflow' do
|
||||||
let(:str) { 'a ' * 256 }
|
let(:str) { 'a' * 4096 }
|
||||||
|
|
||||||
it { is_expected.to eq ['a'] * 256 }
|
specify do
|
||||||
end
|
expect { cmdline }.to \
|
||||||
|
raise_error described_class::CmdlineError, 'buffer overflow'
|
||||||
context 'when there are too many args' do
|
|
||||||
let(:str) { 'a ' * 257 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { cmdline }.to \
|
|
||||||
raise_error described_class::CmdlineError, 'too many args'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when args don\'t cause buffer overflow' do
|
|
||||||
let(:str) { 'a' * 4095 }
|
|
||||||
|
|
||||||
it { is_expected.to eq ['a' * 4095] }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when args cause buffer overflow' do
|
|
||||||
let(:str) { 'a' * 4096 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { cmdline }.to \
|
|
||||||
raise_error described_class::CmdlineError, 'buffer overflow'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,62 +2,60 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.itoa10' do
|
KernAux::Version.supports_itoa10? and RSpec.describe KernAux, '.itoa10' do
|
||||||
if described_class.singleton_class.method_defined? :itoa10
|
subject(:itoa10) { described_class.itoa10 number }
|
||||||
subject(:itoa10) { described_class.itoa10 number }
|
|
||||||
|
|
||||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is 1' do
|
||||||
|
let(:number) { 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is -1' do
|
||||||
|
let(:number) { -1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '-1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is min int64_t' do
|
||||||
|
let(:number) { -2**63 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s }
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is max int64_t' do
|
||||||
let(:number) { 0 }
|
let(:number) { 2**63 - 1 }
|
||||||
|
|
||||||
it { is_expected.to eq '0' }
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is lesser than min uint64_t' do
|
||||||
|
let(:number) { -2**63 - 1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa10 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 1' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 1 }
|
let(:number) { 2**63 }
|
||||||
|
|
||||||
it { is_expected.to eq '1' }
|
specify do
|
||||||
end
|
expect { itoa10 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq '-1' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is min int64_t' do
|
|
||||||
let(:number) { -2**63 }
|
|
||||||
|
|
||||||
it { is_expected.to eq number.to_s }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is max int64_t' do
|
|
||||||
let(:number) { 2**63 - 1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq number.to_s }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is lesser than min uint64_t' do
|
|
||||||
let(:number) { -2**63 - 1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa10 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**63 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa10 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,64 +2,62 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.itoa16' do
|
KernAux::Version.supports_itoa16? and RSpec.describe KernAux, '.itoa16' do
|
||||||
if described_class.singleton_class.method_defined? :itoa16
|
subject(:itoa16) { described_class.itoa16 number }
|
||||||
subject(:itoa16) { described_class.itoa16 number }
|
|
||||||
|
|
||||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||||
|
|
||||||
def sign = number < 0 ? '-' : ''
|
def sign = number < 0 ? '-' : ''
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq "#{sign}0x#{number.abs.to_s(16)}" }
|
it { is_expected.to eq "#{sign}0x#{number.abs.to_s(16)}" }
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is 0' do
|
||||||
let(:number) { 0 }
|
let(:number) { 0 }
|
||||||
|
|
||||||
it { is_expected.to eq '0x0' }
|
it { is_expected.to eq '0x0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is 1' do
|
||||||
|
let(:number) { 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0x1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is -1' do
|
||||||
|
let(:number) { -1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '-0x1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is min int64_t' do
|
||||||
|
let(:number) { -2**63 }
|
||||||
|
|
||||||
|
it { is_expected.to eq "-0x#{number.abs.to_s(16)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max int64_t' do
|
||||||
|
let(:number) { 2**63 - 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is lesser than min uint64_t' do
|
||||||
|
let(:number) { -2**63 - 1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa16 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 1' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 1 }
|
let(:number) { 2**63 }
|
||||||
|
|
||||||
it { is_expected.to eq '0x1' }
|
specify do
|
||||||
end
|
expect { itoa16 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq '-0x1' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is min int64_t' do
|
|
||||||
let(:number) { -2**63 }
|
|
||||||
|
|
||||||
it { is_expected.to eq "-0x#{number.abs.to_s(16)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is max int64_t' do
|
|
||||||
let(:number) { 2**63 - 1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is lesser than min uint64_t' do
|
|
||||||
let(:number) { -2**63 - 1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa16 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**63 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa16 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,64 +2,62 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.itoa2' do
|
KernAux::Version.supports_itoa2? and RSpec.describe KernAux, '.itoa2' do
|
||||||
if described_class.singleton_class.method_defined? :itoa2
|
subject(:itoa2) { described_class.itoa2 number }
|
||||||
subject(:itoa2) { described_class.itoa2 number }
|
|
||||||
|
|
||||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||||
|
|
||||||
def sign = number < 0 ? '-' : ''
|
def sign = number < 0 ? '-' : ''
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq "#{sign}0b#{number.abs.to_s(2)}" }
|
it { is_expected.to eq "#{sign}0b#{number.abs.to_s(2)}" }
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is 0' do
|
||||||
let(:number) { 0 }
|
let(:number) { 0 }
|
||||||
|
|
||||||
it { is_expected.to eq '0b0' }
|
it { is_expected.to eq '0b0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is 1' do
|
||||||
|
let(:number) { 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0b1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is -1' do
|
||||||
|
let(:number) { -1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '-0b1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is min int64_t' do
|
||||||
|
let(:number) { -2**63 }
|
||||||
|
|
||||||
|
it { is_expected.to eq "-0b#{number.abs.to_s(2)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max int64_t' do
|
||||||
|
let(:number) { 2**63 - 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq "0b#{number.to_s(2)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is lesser than min uint64_t' do
|
||||||
|
let(:number) { -2**63 - 1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa2 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 1' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 1 }
|
let(:number) { 2**63 }
|
||||||
|
|
||||||
it { is_expected.to eq '0b1' }
|
specify do
|
||||||
end
|
expect { itoa2 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq '-0b1' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is min int64_t' do
|
|
||||||
let(:number) { -2**63 }
|
|
||||||
|
|
||||||
it { is_expected.to eq "-0b#{number.abs.to_s(2)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is max int64_t' do
|
|
||||||
let(:number) { 2**63 - 1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq "0b#{number.to_s(2)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is lesser than min uint64_t' do
|
|
||||||
let(:number) { -2**63 - 1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa2 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**63 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa2 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,64 +2,62 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.itoa8' do
|
KernAux::Version.supports_itoa8? and RSpec.describe KernAux, '.itoa8' do
|
||||||
if described_class.singleton_class.method_defined? :itoa8
|
subject(:itoa8) { described_class.itoa8 number }
|
||||||
subject(:itoa8) { described_class.itoa8 number }
|
|
||||||
|
|
||||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||||
|
|
||||||
def sign = number < 0 ? '-' : ''
|
def sign = number < 0 ? '-' : ''
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq "#{sign}0o#{number.abs.to_s(8)}" }
|
it { is_expected.to eq "#{sign}0o#{number.abs.to_s(8)}" }
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is 0' do
|
||||||
let(:number) { 0 }
|
let(:number) { 0 }
|
||||||
|
|
||||||
it { is_expected.to eq '0o0' }
|
it { is_expected.to eq '0o0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is 1' do
|
||||||
|
let(:number) { 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0o1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is -1' do
|
||||||
|
let(:number) { -1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '-0o1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is min int64_t' do
|
||||||
|
let(:number) { -2**63 }
|
||||||
|
|
||||||
|
it { is_expected.to eq "-0o#{number.abs.to_s(8)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max int64_t' do
|
||||||
|
let(:number) { 2**63 - 1 }
|
||||||
|
|
||||||
|
it { is_expected.to eq "0o#{number.to_s(8)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is lesser than min uint64_t' do
|
||||||
|
let(:number) { -2**63 - 1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa8 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 1' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 1 }
|
let(:number) { 2**63 }
|
||||||
|
|
||||||
it { is_expected.to eq '0o1' }
|
specify do
|
||||||
end
|
expect { itoa8 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq '-0o1' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is min int64_t' do
|
|
||||||
let(:number) { -2**63 }
|
|
||||||
|
|
||||||
it { is_expected.to eq "-0o#{number.abs.to_s(8)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is max int64_t' do
|
|
||||||
let(:number) { 2**63 - 1 }
|
|
||||||
|
|
||||||
it { is_expected.to eq "0o#{number.to_s(8)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is lesser than min uint64_t' do
|
|
||||||
let(:number) { -2**63 - 1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa8 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**63 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa8 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,237 +2,235 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.itoa' do
|
KernAux::Version.supports_itoa? and RSpec.describe KernAux, '.itoa' do
|
||||||
if described_class.singleton_class.method_defined? :itoa
|
subject(:itoa) { described_class.itoa number, base, prefix }
|
||||||
subject(:itoa) { described_class.itoa number, base, prefix }
|
|
||||||
|
|
||||||
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
let(:number) { rand((-2**63)..(2**63 - 1)) }
|
||||||
let(:base) { rand 2..36 }
|
let(:base) { rand 2..36 }
|
||||||
let(:prefix) { [nil, ''].sample }
|
let(:prefix) { [nil, ''].sample }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s base }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq '0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is 1' do
|
||||||
|
let(:number) { 1 }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq '1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is -1' do
|
||||||
|
let(:number) { -1 }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq '-1' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is min int64_t' do
|
||||||
|
let(:number) { -2**63 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq number.to_s base }
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is max int64_t' do
|
||||||
let(:number) { 0 }
|
let(:number) { 2**63 - 1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq '0' }
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is lesser than min uint64_t' do
|
||||||
|
let(:number) { -2**63 - 1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 1' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 1 }
|
let(:number) { 2**63 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
specify do
|
||||||
it { is_expected.to be_frozen }
|
expect { itoa }.to raise_error \
|
||||||
it { is_expected.to eq '1' }
|
RangeError, 'bignum too big to convert into `long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is -1' do
|
context 'when base is negative' do
|
||||||
let(:number) { -1 }
|
let(:base) { -rand(2..36) }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq '-1' }
|
it { is_expected.to eq number.to_s(-base).upcase }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :b' do
|
||||||
|
let(:base) { :b }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :B:' do
|
||||||
|
let(:base) { :B }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :o' do
|
||||||
|
let(:base) { :o }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 8 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :O' do
|
||||||
|
let(:base) { :O }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 8 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :d' do
|
||||||
|
let(:base) { :d }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :D' do
|
||||||
|
let(:base) { :D }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :h' do
|
||||||
|
let(:base) { :h }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 16 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :x' do
|
||||||
|
let(:base) { :x }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 16 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :H' do
|
||||||
|
let(:base) { :H }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s(16).upcase }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :X' do
|
||||||
|
let(:base) { :X }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s(16).upcase }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is an invalid symbol' do
|
||||||
|
let(:base) { :foo }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa }.to \
|
||||||
|
raise_error described_class::InvalidNtoaBaseError, 'invalid base'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is min int64_t' do
|
context 'when no prefix is given' do
|
||||||
let(:number) { -2**63 }
|
subject(:itoa) { described_class.itoa number, base }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq number.to_s base }
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is nil' do
|
||||||
|
let(:prefix) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is empty' do
|
||||||
|
let(:prefix) { '' }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is present' do
|
||||||
|
let(:prefix) { 'foo' }
|
||||||
|
|
||||||
|
def sign = number < 0 ? '-' : ''
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq "#{sign}foo#{number.abs.to_s(base)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is not a string' do
|
||||||
|
let(:prefix) { 123 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { itoa }.to raise_error(
|
||||||
|
TypeError,
|
||||||
|
"no implicit conversion of #{prefix.class} into String",
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is max int64_t' do
|
context 'when prefix has max length' do
|
||||||
let(:number) { 2**63 - 1 }
|
let(:prefix) { 'a' * 100 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
def sign = number < 0 ? '-' : ''
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is lesser than min uint64_t' do
|
it { is_expected.to be_instance_of String }
|
||||||
let(:number) { -2**63 - 1 }
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq "#{sign}#{prefix}#{number.abs.to_s(base)}" }
|
||||||
|
end
|
||||||
|
|
||||||
specify do
|
context 'when prefix is too long' do
|
||||||
expect { itoa }.to raise_error \
|
let(:prefix) { 'a' * 101 }
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
specify do
|
||||||
let(:number) { 2**63 }
|
expect { itoa }.to raise_error(
|
||||||
|
described_class::TooLongNtoaPrefixError,
|
||||||
specify do
|
"prefix length #{prefix.length} is too long",
|
||||||
expect { itoa }.to raise_error \
|
)
|
||||||
RangeError, 'bignum too big to convert into `long long\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is negative' do
|
|
||||||
let(:base) { -rand(2..36) }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s(-base).upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :b' do
|
|
||||||
let(:base) { :b }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 2 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :B:' do
|
|
||||||
let(:base) { :B }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 2 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :o' do
|
|
||||||
let(:base) { :o }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 8 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :O' do
|
|
||||||
let(:base) { :O }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 8 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :d' do
|
|
||||||
let(:base) { :d }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :D' do
|
|
||||||
let(:base) { :D }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :h' do
|
|
||||||
let(:base) { :h }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 16 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :x' do
|
|
||||||
let(:base) { :x }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 16 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :H' do
|
|
||||||
let(:base) { :H }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s(16).upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :X' do
|
|
||||||
let(:base) { :X }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s(16).upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is an invalid symbol' do
|
|
||||||
let(:base) { :foo }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa }.to \
|
|
||||||
raise_error described_class::InvalidNtoaBaseError, 'invalid base'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when no prefix is given' do
|
|
||||||
subject(:itoa) { described_class.itoa number, base }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is nil' do
|
|
||||||
let(:prefix) { nil }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is empty' do
|
|
||||||
let(:prefix) { '' }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is present' do
|
|
||||||
let(:prefix) { 'foo' }
|
|
||||||
|
|
||||||
def sign = number < 0 ? '-' : ''
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "#{sign}foo#{number.abs.to_s(base)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is not a string' do
|
|
||||||
let(:prefix) { 123 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa }.to raise_error(
|
|
||||||
TypeError,
|
|
||||||
"no implicit conversion of #{prefix.class} into String",
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix has max length' do
|
|
||||||
let(:prefix) { 'a' * 100 }
|
|
||||||
|
|
||||||
def sign = number < 0 ? '-' : ''
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "#{sign}#{prefix}#{number.abs.to_s(base)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is too long' do
|
|
||||||
let(:prefix) { 'a' * 101 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { itoa }.to raise_error(
|
|
||||||
described_class::TooLongNtoaPrefixError,
|
|
||||||
"prefix length #{prefix.length} is too long",
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,44 +2,42 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.utoa10' do
|
KernAux::Version.supports_utoa10? and RSpec.describe KernAux, '.utoa10' do
|
||||||
if described_class.singleton_class.method_defined? :utoa10
|
subject(:utoa10) { described_class.utoa10 number }
|
||||||
subject(:utoa10) { described_class.utoa10 number }
|
|
||||||
|
|
||||||
let(:number) { rand 0..(2**64 - 1) }
|
let(:number) { rand 0..(2**64 - 1) }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max uint64_t' do
|
||||||
|
let(:number) { 2**64 - 1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s }
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is -1' do
|
||||||
let(:number) { 0 }
|
let(:number) { -1 }
|
||||||
|
|
||||||
it { is_expected.to eq '0' }
|
specify do
|
||||||
|
expect { utoa10 }.to \
|
||||||
|
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is max uint64_t' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 2**64 - 1 }
|
let(:number) { 2**64 }
|
||||||
|
|
||||||
it { is_expected.to eq number.to_s }
|
specify do
|
||||||
end
|
expect { utoa10 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa10 }.to \
|
|
||||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**64 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa10 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,44 +2,42 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.utoa16' do
|
KernAux::Version.supports_utoa16? and RSpec.describe KernAux, '.utoa16' do
|
||||||
if described_class.singleton_class.method_defined? :utoa16
|
subject(:utoa16) { described_class.utoa16 number }
|
||||||
subject(:utoa16) { described_class.utoa16 number }
|
|
||||||
|
|
||||||
let(:number) { rand 0..(2**64 - 1) }
|
let(:number) { rand 0..(2**64 - 1) }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0x0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max uint64_t' do
|
||||||
|
let(:number) { 2**64 - 1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is -1' do
|
||||||
let(:number) { 0 }
|
let(:number) { -1 }
|
||||||
|
|
||||||
it { is_expected.to eq '0x0' }
|
specify do
|
||||||
|
expect { utoa16 }.to \
|
||||||
|
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is max uint64_t' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 2**64 - 1 }
|
let(:number) { 2**64 }
|
||||||
|
|
||||||
it { is_expected.to eq "0x#{number.to_s(16)}" }
|
specify do
|
||||||
end
|
expect { utoa16 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa16 }.to \
|
|
||||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**64 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa16 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,44 +2,42 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.utoa2' do
|
KernAux::Version.supports_utoa2? and RSpec.describe KernAux, '.utoa2' do
|
||||||
if described_class.singleton_class.method_defined? :utoa2
|
subject(:utoa2) { described_class.utoa2 number }
|
||||||
subject(:utoa2) { described_class.utoa2 number }
|
|
||||||
|
|
||||||
let(:number) { rand 0..(2**64 - 1) }
|
let(:number) { rand 0..(2**64 - 1) }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq "0b#{number.to_s(2)}" }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0b0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max uint64_t' do
|
||||||
|
let(:number) { 2**64 - 1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "0b#{number.to_s(2)}" }
|
it { is_expected.to eq "0b#{number.to_s(2)}" }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is -1' do
|
||||||
let(:number) { 0 }
|
let(:number) { -1 }
|
||||||
|
|
||||||
it { is_expected.to eq '0b0' }
|
specify do
|
||||||
|
expect { utoa2 }.to \
|
||||||
|
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is max uint64_t' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 2**64 - 1 }
|
let(:number) { 2**64 }
|
||||||
|
|
||||||
it { is_expected.to eq "0b#{number.to_s(2)}" }
|
specify do
|
||||||
end
|
expect { utoa2 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa2 }.to \
|
|
||||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**64 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa2 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,44 +2,42 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.utoa8' do
|
KernAux::Version.supports_utoa8? and RSpec.describe KernAux, '.utoa8' do
|
||||||
if described_class.singleton_class.method_defined? :utoa8
|
subject(:utoa8) { described_class.utoa8 number }
|
||||||
subject(:utoa8) { described_class.utoa8 number }
|
|
||||||
|
|
||||||
let(:number) { rand 0..(2**64 - 1) }
|
let(:number) { rand 0..(2**64 - 1) }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq "0o#{number.to_s(8)}" }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to eq '0o0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max uint64_t' do
|
||||||
|
let(:number) { 2**64 - 1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "0o#{number.to_s(8)}" }
|
it { is_expected.to eq "0o#{number.to_s(8)}" }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is -1' do
|
||||||
let(:number) { 0 }
|
let(:number) { -1 }
|
||||||
|
|
||||||
it { is_expected.to eq '0o0' }
|
specify do
|
||||||
|
expect { utoa8 }.to \
|
||||||
|
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is max uint64_t' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 2**64 - 1 }
|
let(:number) { 2**64 }
|
||||||
|
|
||||||
it { is_expected.to eq "0o#{number.to_s(8)}" }
|
specify do
|
||||||
end
|
expect { utoa8 }.to raise_error \
|
||||||
|
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||||
context 'when number is -1' do
|
|
||||||
let(:number) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa8 }.to \
|
|
||||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
|
||||||
let(:number) { 2**64 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa8 }.to raise_error \
|
|
||||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,209 +2,207 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.utoa' do
|
KernAux::Version.supports_utoa? and RSpec.describe KernAux, '.utoa' do
|
||||||
if described_class.singleton_class.method_defined? :utoa
|
subject(:utoa) { described_class.utoa number, base, prefix }
|
||||||
subject(:utoa) { described_class.utoa number, base, prefix }
|
|
||||||
|
|
||||||
let(:number) { rand 0..(2**64 - 1) }
|
let(:number) { rand 0..(2**64 - 1) }
|
||||||
let(:base) { rand 2..36 }
|
let(:base) { rand 2..36 }
|
||||||
let(:prefix) { [nil, ''].sample }
|
let(:prefix) { [nil, ''].sample }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s base }
|
||||||
|
|
||||||
|
context 'when number is 0' do
|
||||||
|
let(:number) { 0 }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq '0' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when number is max uint64_t' do
|
||||||
|
let(:number) { 2**64 - 1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq number.to_s base }
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is 0' do
|
context 'when number is -1' do
|
||||||
let(:number) { 0 }
|
let(:number) { -1 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
specify do
|
||||||
it { is_expected.to be_frozen }
|
expect { utoa }.to \
|
||||||
it { is_expected.to eq '0' }
|
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is max uint64_t' do
|
context 'when number is greater than max uint64_t' do
|
||||||
let(:number) { 2**64 - 1 }
|
let(:number) { 2**64 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
specify do
|
||||||
it { is_expected.to be_frozen }
|
expect { utoa }.to raise_error \
|
||||||
it { is_expected.to eq number.to_s base }
|
RangeError, 'bignum too big to convert into `unsigned long long\''
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is -1' do
|
context 'when base is negative' do
|
||||||
let(:number) { -1 }
|
let(:base) { -rand(2..36) }
|
||||||
|
|
||||||
specify do
|
it { is_expected.to be_instance_of String }
|
||||||
expect { utoa }.to \
|
it { is_expected.to be_frozen }
|
||||||
raise_error RangeError, 'can\'t convert negative number to uint64_t'
|
it { is_expected.to eq number.to_s(-base).upcase }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when base is :b' do
|
||||||
|
let(:base) { :b }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :B:' do
|
||||||
|
let(:base) { :B }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :o' do
|
||||||
|
let(:base) { :o }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 8 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :O' do
|
||||||
|
let(:base) { :O }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 8 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :d' do
|
||||||
|
let(:base) { :d }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :D' do
|
||||||
|
let(:base) { :D }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :h' do
|
||||||
|
let(:base) { :h }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 16 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :x' do
|
||||||
|
let(:base) { :x }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s 16 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :H' do
|
||||||
|
let(:base) { :H }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s(16).upcase }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is :X' do
|
||||||
|
let(:base) { :X }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s(16).upcase }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when base is an invalid symbol' do
|
||||||
|
let(:base) { :foo }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { utoa }.to \
|
||||||
|
raise_error described_class::InvalidNtoaBaseError, 'invalid base'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when number is greater than max uint64_t' do
|
context 'when no prefix is given' do
|
||||||
let(:number) { 2**64 }
|
subject(:utoa) { described_class.utoa number, base }
|
||||||
|
|
||||||
specify do
|
it { is_expected.to be_instance_of String }
|
||||||
expect { utoa }.to raise_error \
|
it { is_expected.to be_frozen }
|
||||||
RangeError, 'bignum too big to convert into `unsigned long long\''
|
it { is_expected.to eq number.to_s base }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when prefix is nil' do
|
||||||
|
let(:prefix) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is empty' do
|
||||||
|
let(:prefix) { '' }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq number.to_s base }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is present' do
|
||||||
|
let(:prefix) { 'foo' }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of String }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to eq "#{prefix}#{number.to_s(base)}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when prefix is not a string' do
|
||||||
|
let(:prefix) { 123 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { utoa }.to raise_error(
|
||||||
|
TypeError,
|
||||||
|
"no implicit conversion of #{prefix.class} into String",
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when base is negative' do
|
context 'when prefix has max length' do
|
||||||
let(:base) { -rand(2..36) }
|
let(:prefix) { 'a' * 100 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq number.to_s(-base).upcase }
|
it { is_expected.to eq "#{prefix}#{number.to_s(base)}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when base is :b' do
|
context 'when prefix is too long' do
|
||||||
let(:base) { :b }
|
let(:prefix) { 'a' * 101 }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
specify do
|
||||||
it { is_expected.to be_frozen }
|
expect { utoa }.to raise_error(
|
||||||
it { is_expected.to eq number.to_s 2 }
|
described_class::TooLongNtoaPrefixError,
|
||||||
end
|
"prefix length #{prefix.length} is too long",
|
||||||
|
)
|
||||||
context 'when base is :B:' do
|
|
||||||
let(:base) { :B }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 2 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :o' do
|
|
||||||
let(:base) { :o }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 8 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :O' do
|
|
||||||
let(:base) { :O }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 8 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :d' do
|
|
||||||
let(:base) { :d }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :D' do
|
|
||||||
let(:base) { :D }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :h' do
|
|
||||||
let(:base) { :h }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 16 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :x' do
|
|
||||||
let(:base) { :x }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s 16 }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :H' do
|
|
||||||
let(:base) { :H }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s(16).upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is :X' do
|
|
||||||
let(:base) { :X }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s(16).upcase }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when base is an invalid symbol' do
|
|
||||||
let(:base) { :foo }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa }.to \
|
|
||||||
raise_error described_class::InvalidNtoaBaseError, 'invalid base'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when no prefix is given' do
|
|
||||||
subject(:utoa) { described_class.utoa number, base }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is nil' do
|
|
||||||
let(:prefix) { nil }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is empty' do
|
|
||||||
let(:prefix) { '' }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq number.to_s base }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is present' do
|
|
||||||
let(:prefix) { 'foo' }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "#{prefix}#{number.to_s(base)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is not a string' do
|
|
||||||
let(:prefix) { 123 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa }.to raise_error(
|
|
||||||
TypeError,
|
|
||||||
"no implicit conversion of #{prefix.class} into String",
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix has max length' do
|
|
||||||
let(:prefix) { 'a' * 100 }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to eq "#{prefix}#{number.to_s(base)}" }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when prefix is too long' do
|
|
||||||
let(:prefix) { 'a' * 101 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { utoa }.to raise_error(
|
|
||||||
described_class::TooLongNtoaPrefixError,
|
|
||||||
"prefix length #{prefix.length} is too long",
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,280 +2,278 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.snprintf1' do
|
KernAux::Version.supports_printf? and RSpec.describe KernAux, '.snprintf1' do
|
||||||
if described_class.singleton_class.method_defined? :snprintf1
|
let(:size) { 10_000 }
|
||||||
let(:size) { 10_000 }
|
|
||||||
|
context 'with 0 arguments' do
|
||||||
|
specify do
|
||||||
|
expect { described_class.snprintf1 }.to \
|
||||||
|
raise_error ArgumentError, 'expected 2, 3, 4 or 5 args'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with 1 argument' do
|
||||||
|
specify do
|
||||||
|
expect { described_class.snprintf1 size }.to \
|
||||||
|
raise_error ArgumentError, 'expected 2, 3, 4 or 5 args'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with 6 arguments' do
|
||||||
|
specify do
|
||||||
|
expect do
|
||||||
|
described_class.snprintf1 size, '%*.*s', 20, 10, 'foo', 'bar'
|
||||||
|
end.to \
|
||||||
|
raise_error ArgumentError, 'expected 2, 3, 4 or 5 args'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with 2 arguments' do
|
||||||
|
subject(:snprintf1) { described_class.snprintf1 size, format }
|
||||||
|
|
||||||
|
let(:format) { '%%' }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of Array }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to all be_frozen }
|
||||||
|
|
||||||
|
specify { expect(snprintf1.size).to equal 2 }
|
||||||
|
specify { expect(snprintf1[0]).to be_instance_of String }
|
||||||
|
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
||||||
|
specify { expect(snprintf1[0]).to eq '%' }
|
||||||
|
specify { expect(snprintf1[1]).to eq 1 }
|
||||||
|
|
||||||
|
context 'with leading and trailing spaces' do
|
||||||
|
let(:format) { ' %% ' }
|
||||||
|
|
||||||
|
specify { expect(snprintf1[0]).to eq ' % ' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with "%s" format' do
|
||||||
|
let(:format) { '%s' }
|
||||||
|
|
||||||
|
specify { expect(snprintf1[0]).to eq '' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when size has invalid type' do
|
||||||
|
let(:size) { '10000' }
|
||||||
|
|
||||||
|
specify { expect { snprintf1 }.to raise_error TypeError }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when size is negative' do
|
||||||
|
let(:size) { -1 }
|
||||||
|
|
||||||
context 'with 0 arguments' do
|
|
||||||
specify do
|
specify do
|
||||||
expect { described_class.snprintf1 }.to \
|
expect { snprintf1 }.to \
|
||||||
raise_error ArgumentError, 'expected 2, 3, 4 or 5 args'
|
raise_error RangeError, 'expected non-negative size'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with 1 argument' do
|
context 'when format doesn\'t include "%" char' do
|
||||||
|
let(:format) { 'foo' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect { described_class.snprintf1 size }.to \
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
raise_error ArgumentError, 'expected 2, 3, 4 or 5 args'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with 6 arguments' do
|
context 'when format includes more than two "%" chars' do
|
||||||
|
let(:format) { '%%%' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect do
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
described_class.snprintf1 size, '%*.*s', 20, 10, 'foo', 'bar'
|
|
||||||
end.to \
|
|
||||||
raise_error ArgumentError, 'expected 2, 3, 4 or 5 args'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with 2 arguments' do
|
context 'with 3 arguments' do
|
||||||
subject(:snprintf1) { described_class.snprintf1 size, format }
|
subject(:snprintf1) { described_class.snprintf1 size, format, arg }
|
||||||
|
|
||||||
|
let(:format) { '%s' }
|
||||||
|
let(:arg) { 'Hello, World!' }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of Array }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to all be_frozen }
|
||||||
|
|
||||||
|
specify { expect(snprintf1.size).to equal 2 }
|
||||||
|
specify { expect(snprintf1[0]).to be_instance_of String }
|
||||||
|
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
||||||
|
specify { expect(snprintf1[0]).to eq arg }
|
||||||
|
specify { expect(snprintf1[1]).to eq arg.size }
|
||||||
|
|
||||||
|
context 'with leading and trailing spaces' do
|
||||||
|
let(:format) { ' %s ' }
|
||||||
|
|
||||||
|
specify { expect(snprintf1[0]).to eq " #{arg} " }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with "%%" format' do
|
||||||
let(:format) { '%%' }
|
let(:format) { '%%' }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of Array }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to all be_frozen }
|
|
||||||
|
|
||||||
specify { expect(snprintf1.size).to equal 2 }
|
|
||||||
specify { expect(snprintf1[0]).to be_instance_of String }
|
|
||||||
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
|
||||||
specify { expect(snprintf1[0]).to eq '%' }
|
specify { expect(snprintf1[0]).to eq '%' }
|
||||||
specify { expect(snprintf1[1]).to eq 1 }
|
|
||||||
|
|
||||||
context 'with leading and trailing spaces' do
|
|
||||||
let(:format) { ' %% ' }
|
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq ' % ' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with "%s" format' do
|
|
||||||
let(:format) { '%s' }
|
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq '' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when size has invalid type' do
|
|
||||||
let(:size) { '10000' }
|
|
||||||
|
|
||||||
specify { expect { snprintf1 }.to raise_error TypeError }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when size is negative' do
|
|
||||||
let(:size) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to \
|
|
||||||
raise_error RangeError, 'expected non-negative size'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when format doesn\'t include "%" char' do
|
|
||||||
let(:format) { 'foo' }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when format includes more than two "%" chars' do
|
|
||||||
let(:format) { '%%%' }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with 3 arguments' do
|
context 'when size has invalid type' do
|
||||||
subject(:snprintf1) { described_class.snprintf1 size, format, arg }
|
let(:size) { '10000' }
|
||||||
|
|
||||||
let(:format) { '%s' }
|
specify { expect { snprintf1 }.to raise_error TypeError }
|
||||||
let(:arg) { 'Hello, World!' }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of Array }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to all be_frozen }
|
|
||||||
|
|
||||||
specify { expect(snprintf1.size).to equal 2 }
|
|
||||||
specify { expect(snprintf1[0]).to be_instance_of String }
|
|
||||||
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
|
||||||
specify { expect(snprintf1[0]).to eq arg }
|
|
||||||
specify { expect(snprintf1[1]).to eq arg.size }
|
|
||||||
|
|
||||||
context 'with leading and trailing spaces' do
|
|
||||||
let(:format) { ' %s ' }
|
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq " #{arg} " }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with "%%" format' do
|
|
||||||
let(:format) { '%%' }
|
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq '%' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when size has invalid type' do
|
|
||||||
let(:size) { '10000' }
|
|
||||||
|
|
||||||
specify { expect { snprintf1 }.to raise_error TypeError }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when size is negative' do
|
|
||||||
let(:size) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to \
|
|
||||||
raise_error RangeError, 'expected non-negative size'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when format doesn\'t include "%" char' do
|
|
||||||
let(:format) { 'foo' }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when format includes more than two "%" chars' do
|
|
||||||
let(:format) { '%%%' }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with 4 arguments' do
|
context 'when size is negative' do
|
||||||
subject(:snprintf1) { described_class.snprintf1 size, format, width, arg }
|
let(:size) { -1 }
|
||||||
|
|
||||||
let(:format) { '%*s' }
|
|
||||||
let(:width) { 20 }
|
|
||||||
let(:arg) { 'Hello, World!' }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of Array }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to all be_frozen }
|
|
||||||
|
|
||||||
specify { expect(snprintf1.size).to equal 2 }
|
|
||||||
specify { expect(snprintf1[0]).to be_instance_of String }
|
|
||||||
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
|
||||||
specify { expect(snprintf1[0]).to eq arg.rjust(width, ' ') }
|
|
||||||
specify { expect(snprintf1[1]).to eq width }
|
|
||||||
|
|
||||||
context 'with leading and trailing spaces' do
|
|
||||||
let(:format) { ' %*s ' }
|
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq " #{arg.rjust(width, ' ')} " }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with "%*%" format' do
|
|
||||||
let(:format) { '%*%' }
|
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq '%' }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when size has invalid type' do
|
|
||||||
let(:size) { '10000' }
|
|
||||||
|
|
||||||
specify { expect { snprintf1 }.to raise_error TypeError }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when size is negative' do
|
|
||||||
let(:size) { -1 }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to \
|
|
||||||
raise_error RangeError, 'expected non-negative size'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when format doesn\'t include "%" char' do
|
|
||||||
let(:format) { 'foo' }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when format includes more than two "%" chars' do
|
|
||||||
let(:format) { '%%%' }
|
|
||||||
|
|
||||||
specify do
|
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with 5 arguments' do
|
|
||||||
subject :snprintf1 do
|
|
||||||
described_class.snprintf1 size, format, width, precision, arg
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:format) { '%*.*f' }
|
|
||||||
let(:width) { 20 }
|
|
||||||
let(:precision) { 3 }
|
|
||||||
let(:arg) { 123.456789 }
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of Array }
|
|
||||||
it { is_expected.to be_frozen }
|
|
||||||
it { is_expected.to all be_frozen }
|
|
||||||
|
|
||||||
specify { expect(snprintf1.size).to equal 2 }
|
|
||||||
specify { expect(snprintf1[0]).to be_instance_of String }
|
|
||||||
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
|
||||||
specify { expect(snprintf1[1]).to eq width }
|
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect(snprintf1[0]).to eq arg.round(precision).to_s.rjust(width, ' ')
|
expect { snprintf1 }.to \
|
||||||
|
raise_error RangeError, 'expected non-negative size'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with leading and trailing spaces' do
|
context 'when format doesn\'t include "%" char' do
|
||||||
let(:format) { ' %*.*f ' }
|
let(:format) { 'foo' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect(snprintf1[0]).to \
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
eq " #{arg.round(precision).to_s.rjust(width, ' ')} "
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with "%*.*%" format' do
|
context 'when format includes more than two "%" chars' do
|
||||||
let(:format) { '%*.*%' }
|
let(:format) { '%%%' }
|
||||||
|
|
||||||
specify { expect(snprintf1[0]).to eq '%' }
|
specify do
|
||||||
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when size has invalid type' do
|
context 'with 4 arguments' do
|
||||||
let(:size) { '10000' }
|
subject(:snprintf1) { described_class.snprintf1 size, format, width, arg }
|
||||||
|
|
||||||
specify { expect { snprintf1 }.to raise_error TypeError }
|
let(:format) { '%*s' }
|
||||||
|
let(:width) { 20 }
|
||||||
|
let(:arg) { 'Hello, World!' }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of Array }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to all be_frozen }
|
||||||
|
|
||||||
|
specify { expect(snprintf1.size).to equal 2 }
|
||||||
|
specify { expect(snprintf1[0]).to be_instance_of String }
|
||||||
|
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
||||||
|
specify { expect(snprintf1[0]).to eq arg.rjust(width, ' ') }
|
||||||
|
specify { expect(snprintf1[1]).to eq width }
|
||||||
|
|
||||||
|
context 'with leading and trailing spaces' do
|
||||||
|
let(:format) { ' %*s ' }
|
||||||
|
|
||||||
|
specify { expect(snprintf1[0]).to eq " #{arg.rjust(width, ' ')} " }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with "%*%" format' do
|
||||||
|
let(:format) { '%*%' }
|
||||||
|
|
||||||
|
specify { expect(snprintf1[0]).to eq '%' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when size has invalid type' do
|
||||||
|
let(:size) { '10000' }
|
||||||
|
|
||||||
|
specify { expect { snprintf1 }.to raise_error TypeError }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when size is negative' do
|
||||||
|
let(:size) { -1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { snprintf1 }.to \
|
||||||
|
raise_error RangeError, 'expected non-negative size'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when size is negative' do
|
context 'when format doesn\'t include "%" char' do
|
||||||
let(:size) { -1 }
|
let(:format) { 'foo' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect { snprintf1 }.to \
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
raise_error RangeError, 'expected non-negative size'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when format doesn\'t include "%" char' do
|
context 'when format includes more than two "%" chars' do
|
||||||
let(:format) { 'foo' }
|
let(:format) { '%%%' }
|
||||||
|
|
||||||
specify do
|
specify do
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when format includes more than two "%" chars' do
|
context 'with 5 arguments' do
|
||||||
let(:format) { '%%%' }
|
subject :snprintf1 do
|
||||||
|
described_class.snprintf1 size, format, width, precision, arg
|
||||||
|
end
|
||||||
|
|
||||||
specify do
|
let(:format) { '%*.*f' }
|
||||||
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
let(:width) { 20 }
|
||||||
end
|
let(:precision) { 3 }
|
||||||
|
let(:arg) { 123.456789 }
|
||||||
|
|
||||||
|
it { is_expected.to be_instance_of Array }
|
||||||
|
it { is_expected.to be_frozen }
|
||||||
|
it { is_expected.to all be_frozen }
|
||||||
|
|
||||||
|
specify { expect(snprintf1.size).to equal 2 }
|
||||||
|
specify { expect(snprintf1[0]).to be_instance_of String }
|
||||||
|
specify { expect(snprintf1[1]).to be_instance_of Integer }
|
||||||
|
specify { expect(snprintf1[1]).to eq width }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect(snprintf1[0]).to eq arg.round(precision).to_s.rjust(width, ' ')
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with leading and trailing spaces' do
|
||||||
|
let(:format) { ' %*.*f ' }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect(snprintf1[0]).to \
|
||||||
|
eq " #{arg.round(precision).to_s.rjust(width, ' ')} "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with "%*.*%" format' do
|
||||||
|
let(:format) { '%*.*%' }
|
||||||
|
|
||||||
|
specify { expect(snprintf1[0]).to eq '%' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when size has invalid type' do
|
||||||
|
let(:size) { '10000' }
|
||||||
|
|
||||||
|
specify { expect { snprintf1 }.to raise_error TypeError }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when size is negative' do
|
||||||
|
let(:size) { -1 }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { snprintf1 }.to \
|
||||||
|
raise_error RangeError, 'expected non-negative size'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when format doesn\'t include "%" char' do
|
||||||
|
let(:format) { 'foo' }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when format includes more than two "%" chars' do
|
||||||
|
let(:format) { '%%%' }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
expect { snprintf1 }.to raise_error ArgumentError, 'invalid format'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,27 +2,25 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.sprintf1' do
|
KernAux::Version.supports_printf? and RSpec.describe KernAux, '.sprintf1' do
|
||||||
if described_class.singleton_class.method_defined? :sprintf1
|
context 'with 1 argument' do
|
||||||
context 'with 1 argument' do
|
subject(:sprintf1) { described_class.sprintf1 format }
|
||||||
subject(:sprintf1) { described_class.sprintf1 format }
|
|
||||||
|
|
||||||
let(:format) { '%%' }
|
let(:format) { '%%' }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq described_class.snprintf1(1000, format).first }
|
it { is_expected.to eq described_class.snprintf1(1000, format).first }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with 2 argument' do
|
context 'with 2 argument' do
|
||||||
subject(:sprintf1) { described_class.sprintf1 format, arg }
|
subject(:sprintf1) { described_class.sprintf1 format, arg }
|
||||||
|
|
||||||
let(:format) { '%s' }
|
let(:format) { '%s' }
|
||||||
let(:arg) { 'Hello, World!' }
|
let(:arg) { 'Hello, World!' }
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq described_class.snprintf1(100, format, arg).first }
|
it { is_expected.to eq described_class.snprintf1(100, format, arg).first }
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,52 +2,50 @@
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
RSpec.describe KernAux, '.sprintf' do
|
KernAux::Version.supports_printf? and RSpec.describe KernAux, '.sprintf' do
|
||||||
if described_class.singleton_class.method_defined? :sprintf
|
subject :sprintf do
|
||||||
subject :sprintf do
|
described_class.sprintf 'Hello, ', ['%s', 'World'], '!'
|
||||||
described_class.sprintf 'Hello, ', ['%s', 'World'], '!'
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it { is_expected.to be_instance_of String }
|
it { is_expected.to be_instance_of String }
|
||||||
it { is_expected.to be_frozen }
|
it { is_expected.to be_frozen }
|
||||||
it { is_expected.to eq 'Hello, World!' }
|
it { is_expected.to eq 'Hello, World!' }
|
||||||
|
|
||||||
context 'for empty string value' do
|
context 'for empty string value' do
|
||||||
subject(:sprintf) { described_class.sprintf ['Hello testing%s'] }
|
subject(:sprintf) { described_class.sprintf ['Hello testing%s'] }
|
||||||
|
|
||||||
it { is_expected.to eq 'Hello testing' }
|
it { is_expected.to eq 'Hello testing' }
|
||||||
end
|
end
|
||||||
|
|
||||||
[
|
[
|
||||||
['', 'using regular tests'],
|
['', 'using regular tests'],
|
||||||
['_orig', 'using original tests'],
|
['_orig', 'using original tests'],
|
||||||
].each do |(suffix, description)|
|
].each do |(suffix, description)|
|
||||||
context description do
|
context description do
|
||||||
printf_yml = File.expand_path(
|
printf_yml = File.expand_path(
|
||||||
"../../../../../../common/printf#{suffix}.yml",
|
"../../../../../../common/printf#{suffix}.yml",
|
||||||
__dir__,
|
__dir__,
|
||||||
)
|
)
|
||||||
|
|
||||||
YAML.safe_load_file(printf_yml).each do |test|
|
YAML.safe_load_file(printf_yml).each do |test|
|
||||||
expected = test['result']
|
expected = test['result']
|
||||||
|
|
||||||
args = test['args'].map do |arg|
|
args = test['args'].map do |arg|
|
||||||
if arg.is_a? String
|
if arg.is_a? String
|
||||||
arg
|
arg
|
||||||
else
|
else
|
||||||
arg.map do |item|
|
arg.map do |item|
|
||||||
if item.is_a? Array
|
if item.is_a? Array
|
||||||
item[0]
|
item[0]
|
||||||
else
|
else
|
||||||
item
|
item
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "transforms #{args.inspect} to #{expected.inspect}" do
|
it "transforms #{args.inspect} to #{expected.inspect}" do
|
||||||
expect(described_class.sprintf(*args)).to eq expected
|
expect(described_class.sprintf(*args)).to eq expected
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue