Permalink
Browse files

any?

  • Loading branch information...
1 parent c568757 commit ccf7cc6bde98d8adeb8b3a1862ba76a3ef7be0a8 @kachick committed May 17, 2012
Showing with 42 additions and 8 deletions.
  1. +27 −7 lib/integer/base/eigen.rb
  2. +15 −1 test/test_integer-base.rb
View
@@ -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
@@ -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
@@ -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
View
@@ -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

0 comments on commit ccf7cc6

Please sign in to comment.