From ccf7cc6bde98d8adeb8b3a1862ba76a3ef7be0a8 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 18 May 2012 08:21:35 +0900 Subject: [PATCH] any? --- lib/integer/base/eigen.rb | 34 +++++++++++++++++++++++++++------- test/test_integer-base.rb | 16 +++++++++++++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/integer/base/eigen.rb b/lib/integer/base/eigen.rb index cc0fe76..b103c84 100644 --- a/lib/integer/base/eigen.rb +++ b/lib/integer/base/eigen.rb @@ -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: String & length 1)' + when chars.any?{|s|s.length != 1} + raise InvalidCharacter, 'chars must be Array (Char: length 1)' when chars.any?{|c|SPECIAL_CHAR =~ c} raise InvalidCharacter, 'included Special Characters (-, +, space, control-char)' else diff --git a/test/test_integer-base.rb b/test/test_integer-base.rb index 4ecf573..273972b 100644 --- a/test/test_integer-base.rb +++ b/test/test_integer-base.rb @@ -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