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