Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
any?
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed May 17, 2012
1 parent c568757 commit ccf7cc6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
34 changes: 27 additions & 7 deletions lib/integer/base/eigen.rb
Expand Up @@ -12,11 +12,28 @@ def parse(str, chars)
str = str.to_str.downcase

sign = parse_sign! str
trim_paddings! str, chars.first

abs = parse_abs str, chars
case chars.length
when 1
parse_unary_abs str, chars.first
else
trim_paddings! str, chars.first

abs = parse_abs str, chars

(sign == :-) ? -abs : abs
end
end

def parse_unary_abs(str, char)
chars = str.chars
p chars.to_a
atom = chars.to_a.uniq.first.to_sym

(sign == :-) ? -abs : abs
raise InvalidCharacter, 'contain multiple chars' unless atom.length == 1
raise InvalidCharacter, 'not match the char and atom' unless atom == char

chars.length
end

# @param [#to_int] num
Expand All @@ -40,7 +57,10 @@ def convert_to_string(num, chars)
s.insert 0, '-' if int < 0
}
end


def convert_unary(num, char)
end

private

# @param [Array<#to_sym>] chars
Expand All @@ -49,12 +69,12 @@ def base_chars_for(chars)
chars = chars.map{|c|c.downcase.to_sym}

case
when chars.length < 2
when chars.length < 1
raise TypeError, 'use 2 and more characters'
when chars.dup.uniq!
raise InvalidCharacter, 'dupulicated characters'
when chars.any?{|s|(! s.kind_of?(Symbol)) || (s.length != 1)}
raise InvalidCharacter, 'chars must be Array<Char> (Char: String & length 1)'
when chars.any?{|s|s.length != 1}
raise InvalidCharacter, 'chars must be Array<Char> (Char: length 1)'
when chars.any?{|c|SPECIAL_CHAR =~ c}
raise InvalidCharacter, 'included Special Characters (-, +, space, control-char)'
else
Expand Down
16 changes: 15 additions & 1 deletion test/test_integer-base.rb
Expand Up @@ -72,18 +72,32 @@ def test_binary
end

def test_standards
assert_equal(0, Integer::Base.parse('', Integer::Base::STANDARD_CHARS[1]))

assert_raises Integer::Base::InvalidCharacter do
Integer::Base.parse('', Integer::Base::STANDARD_CHARS[2])
end

assert_equal(1, Integer::Base.parse('0', Integer::Base::STANDARD_CHARS[1]))
assert_equal(2, Integer::Base.parse('00', Integer::Base::STANDARD_CHARS[1]))
assert_equal('11'.to_i(2), Integer::Base.parse('11', Integer::Base::STANDARD_CHARS[2]))
assert_equal('101'.to_i(2), Integer::Base.parse('101', Integer::Base::STANDARD_CHARS[2]))
assert_equal('11'.to_i(27), Integer::Base.parse('11', Integer::Base::STANDARD_CHARS[27]))
assert_equal('101'.to_i(27), Integer::Base.parse('101', Integer::Base::STANDARD_CHARS[27]))
assert_equal('11'.to_i(36), Integer::Base.parse('11', Integer::Base::STANDARD_CHARS[36]))
assert_equal('101'.to_i(36), Integer::Base.parse('101', Integer::Base::STANDARD_CHARS[36]))

assert_raises KeyError do
Integer::Base::STANDARD_CHARS[37]
end
end

def test_implements
assert_raises ArgumentError do
'00'.to_i 1
end
end

def test_upper36
assert_equal(73, '1!'.to_i([*Integer::Base::STANDARD_CHARS[36], '!']))
end
Expand Down

0 comments on commit ccf7cc6

Please sign in to comment.