base_convert 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbccf02603e1f029d1acfa79abfff94b93f315c6
4
- data.tar.gz: 44227572331e4c2203d3012f32377ea4bde2e573
3
+ metadata.gz: e38bf64a72b3127b158c244a87a65176db358c21
4
+ data.tar.gz: 1dc82bdddee617c3fe44e2c125a33b15641a346f
5
5
  SHA512:
6
- metadata.gz: 07e12a56d9fd457e70417ab65124d4b9fba0c45ecd922ec6ebdf8ec3856da9d10be56f69d11cf364dd3e9aeb4a57d150824cda3d5383df874115af2d4400b4ac
7
- data.tar.gz: 89dd3ea459891e0bc478b1f808e680e1b6d761c4b4bfd4e7e7e0e6440f889139de4383e503510fd23ba817eee6bbe7cf42abddf753cf270278773a099fc4fad8
6
+ metadata.gz: 44c73f5bb7180fbecaaba4cc1936a339019ddbc367a76a30e815cbee9a0ce13d1235e2b86d5bca6e65fc3b3f3c1a1fba7a21e4dcad5eceb806a2b4e14e705e15
7
+ data.tar.gz: 11c95ca5dd520a33e7c61da20c29c42e8adcb80da99030efcdfeb8d16cc7b63fa972dff21510a1105dbf4eafc911dfcfaf42eaf33569b88f4445c62caf9c2595
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ 0.0.0 September 24, 2011 (5.5 KB) yanked
2
+ 0.0.1 September 24, 2011 (5.5 KB) yanked
3
+ 0.0.2 February 12, 2014 (6.5 KB)
data/README.rdoc ADDED
@@ -0,0 +1,133 @@
1
+ = base_convert
2
+
3
+ github :: https://www.github.com/carlosjhr64/base_convert
4
+ rubygems :: https://rubygems.org/gems/base_convert
5
+
6
+ == DESCRIPTION:
7
+
8
+ base_convert - Number base conversion.
9
+
10
+ Converts positive integers to different bases:
11
+ Binary, octal, hexadecimal, decimal, or any arbitrary base.
12
+ "Out of the box" handling of up to base 91.
13
+ Allows for arbitrary choice of alphabet(digits).
14
+
15
+ See also Non-decimal_radices/Convert[http://rosettacode.org/wiki/Non-decimal_radices/Convert].
16
+
17
+ == SYNOPSIS:
18
+
19
+ require 'base_convert'
20
+ include BASE_CONVERT
21
+ h2o = BaseConvert.new(16, 8)
22
+ o2h = BaseConvert.new(8, 16)
23
+ puts h2o.convert('FFFF') #=> "177777"
24
+ puts o2h.convert('177777') #=> "FFFF"
25
+
26
+ == BUT WAIT, THERE'S MORE:
27
+
28
+ Using `irb` to demonstrate the features.
29
+ The components are scoped under `BASE_CONVERT`:
30
+
31
+ # irb
32
+ # Welcome to IRB...
33
+ require 'base_convert' #=> true
34
+ include BASE_CONVERT #=> Object
35
+
36
+ `base_convert` provides three ways to convert a string representation of a number.
37
+ The first is functional. One can extend(import) the functions that do the conversions.
38
+ The conversion functions are `to_integer` and `to_base`.
39
+ For example, the octal number "7777":
40
+
41
+ extend FUNCTIONS #=> main
42
+ digits = ['0','1','2','3','4','5','6','7']
43
+ base = digits.length #=> 8
44
+ to_integer('7777', base, digits) #=> 4095
45
+ to_base(4095, base, digits) #=> "7777"
46
+
47
+ You can work with arbitrary digits:
48
+
49
+ digits = [')','!','@','#','$','%','^','&']
50
+ base = digits.length #=> 8
51
+ to_integer('&&&&', base, digits) #=> 4095
52
+ to_base(4095, base, digits) #=> "&&&&"
53
+
54
+ For convenience, `base_convert` provides two sets of digits.
55
+ `WORD` are the ASCII word characters except underscore[_].
56
+ `QGRAPH` are the ASCII graph characters except quotes['"].
57
+
58
+ CONFIG::WORD #=> ["0","1",... "A","B",... "a","b",... "y","z"]
59
+ CONFIG::QGRAPH #=> ["!", "#",... "0","1",... "A","B",... "a","b",... "}", "~"]
60
+ to_base(4095, 8, CONFIG::WORD) #=> "7777"
61
+ to_base(4095, 8, CONFIG::QGRAPH) #=> "****"
62
+
63
+ The second way to convert is via a conversion object of `BASE_CONVERT::BaseConvert`.
64
+ For example, to convert from hexadecimal to octal, and back:
65
+
66
+ h2o = BaseConvert.new(16, 8)
67
+ o2h = BaseConvert.new(8, 16)
68
+ h2o.convert('FFFF') #=> "177777"
69
+ o2h.convert('177777') #=> "FFFF"
70
+
71
+ You can access the conversion alphabets via
72
+ the accessors BaseConvert#from_digits and BaseConvert#to_digits.
73
+ By default, WORD is used for bases upto 62.
74
+ For bigger bases upto 91, QGRAPH is used.
75
+ You can have bigger bases, but
76
+ you will need to come up with a bigger alphabet, perhaps
77
+ by adding greek letters.
78
+ Note that when there's no ambiguity while using WORD,
79
+ BaseConvert will upcase the string.
80
+
81
+ h2o.convert('FFFF') #=> "177777"
82
+ h2o.convert('ffff') #=> "177777"
83
+
84
+ BaseConvert also make avalable the intermediary methods that works with integers,
85
+ BaseConvert#base2integer and BaseConvert#integer2Base:
86
+
87
+ h2o.base2integer('FFFF') #=> 65535
88
+ h2o.integer2base(65535) #=> "177777"
89
+
90
+ The above replaces #base2dec and #dec2base which are now aliases and are deprecated.
91
+ The third way to convert is via the subclass of String, `BASE_CONVERT::Number`:
92
+
93
+ hexadecimal = Number.new('FFFF', 16, CONFIG::WORD) #=> "FFFF"
94
+ # WORD is the default alphabet
95
+ hexadecimal = Number.new('FFFF', 16) #=> "FFFF"
96
+ hexadecimal.to_integer #=> 65535
97
+ # Base 10 the default
98
+ decimal = Number.new('65535') # "65535"
99
+ decimal.to_integer #=> 65535
100
+ decimal.to_base(16) #=> "FFFF"
101
+ decimal.to_base(8) #=> "177777"
102
+ # You can also specify the digits to use
103
+ digits = [')','!','@','#','$','%','^','&']
104
+ decimal.to_base(8, digits) #=> "!&&&&&"
105
+
106
+ == INSTALL:
107
+
108
+ $ sudo gem install base_convert
109
+
110
+ == LICENSE:
111
+
112
+ (The MIT License)
113
+
114
+ Copyright (c) 2014 CarlosJHR64
115
+
116
+ Permission is hereby granted, free of charge, to any person obtaining
117
+ a copy of this software and associated documentation files (the
118
+ 'Software'), to deal in the Software without restriction, including
119
+ without limitation the rights to use, copy, modify, merge, publish,
120
+ distribute, sublicense, and/or sell copies of the Software, and to
121
+ permit persons to whom the Software is furnished to do so, subject to
122
+ the following conditions:
123
+
124
+ The above copyright notice and this permission notice shall be
125
+ included in all copies or substantial portions of the Software.
126
+
127
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
128
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
129
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
130
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
131
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
132
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
133
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/TODO.txt ADDED
@@ -0,0 +1,3 @@
1
+ == File List
2
+
3
+
@@ -0,0 +1,55 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.name = 'base_convert'
4
+ s.version = '1.0.0'
5
+
6
+ s.homepage = 'https://github.com/carlosjhr64/base_convert'
7
+
8
+ s.author = 'CarlosJHR64'
9
+ s.email = 'carlosjhr64@gmail.com'
10
+
11
+ s.date = '2014-02-13'
12
+ s.licenses = ['MIT']
13
+
14
+ s.description = <<DESCRIPTION
15
+ base_convert - Number base conversion.
16
+
17
+ Converts positive integers to different bases:
18
+ Binary, octal, hexadecimal, decimal, or any arbitrary base.
19
+ "Out of the box" handling of up to base 91.
20
+ Allows for arbitrary choice of alphabet(digits).
21
+ DESCRIPTION
22
+
23
+ s.summary = <<SUMMARY
24
+ base_convert - Number base conversion.
25
+ SUMMARY
26
+
27
+ s.extra_rdoc_files = ['README.rdoc']
28
+ s.rdoc_options = ["--main", "README.rdoc"]
29
+
30
+ s.require_paths = ["lib"]
31
+ s.files = %w(
32
+ History.txt
33
+ README.rdoc
34
+ TODO.txt
35
+ base_convert.gemspec
36
+ lib/base_convert.rb
37
+ lib/base_convert/base_convert.rb
38
+ lib/base_convert/config.rb
39
+ lib/base_convert/functions.rb
40
+ lib/base_convert/helpers.rb
41
+ lib/base_convert/number.rb
42
+ lib/base_convert/version.rb
43
+ test/test_base_convert.rb
44
+ test/test_functions.rb
45
+ test/test_helpers.rb
46
+ test/test_number.rb
47
+ test/test_original.rb
48
+ test/test_original2.rb
49
+ test/test_trivial.rb
50
+ )
51
+
52
+ s.add_development_dependency 'test-unit', '~> 2.5', '>= 2.5.5'
53
+ s.requirements << 'ruby: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]'
54
+
55
+ end
data/lib/base_convert.rb CHANGED
@@ -1,58 +1,7 @@
1
- # http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
2
- class BaseConvert
3
- DIGITS = 0.upto(255).map{|i| i.chr}.select{|c| c=~/\w/ && c=~/[^_]/} # 0..9 a..z A..Z
4
- QGRAPH = 0.upto(255).map{|i| i.chr}.select{|c| c=~/[[:graph:]]/ && c=~/[^`'"]/}
5
-
6
- BASE = {
7
- :word => DIGITS.length,
8
- :qgraph => QGRAPH.length,
9
- :hexadecimal => 16,
10
- :hex => 16,
11
- :decimal => 10,
12
- :dec => 10,
13
- :octal => 8,
14
- :oct => 8,
15
- :binary => 2,
16
- }
17
-
18
- attr_accessor :from_digits, :to_digits
19
- def initialize(basefrom,baseto=nil)
20
- baseto = basefrom if baseto.nil?
21
- @basefrom = BASE[basefrom] || basefrom
22
- @baseto = BASE[baseto] || baseto
23
- dl = DIGITS.length
24
- @from_digits = ((basefrom == :qgraph) || (@basefrom > dl))? QGRAPH : DIGITS
25
- @to_digits = ((baseto == :qgraph) || (@baseto > dl))? QGRAPH : DIGITS
26
- end
27
-
28
- def convert(str)
29
- if str.class == String
30
- dec = base2dec(str)
31
- return (@baseto == @basefrom)? dec : dec2base(dec)
32
- end
33
- dec2base(str)
34
- end
35
-
36
- def base2dec(str)
37
- str = str.upcase if @basefrom < 37
38
- raise ArgumentError, "base is invalid" unless @basefrom.between?(2, @from_digits.length)
39
- res = 0
40
- str.to_s.each_char do |c|
41
- idx = @from_digits[0,@basefrom].find_index(c)
42
- idx.nil? and raise ArgumentError, "invalid base-#{@basefrom} digit: #{c}"
43
- res = res * @basefrom + idx
44
- end
45
- res
46
- end
47
-
48
- def dec2base(n)
49
- return @to_digits.first if n == 0
50
- raise ArgumentError, "base is invalid" unless @baseto.between?(2, @to_digits.length)
51
- res = []
52
- while n > 0
53
- n, r = n.divmod(@baseto)
54
- res.unshift(@to_digits[r])
55
- end
56
- res.join("")
57
- end
58
- end
1
+ require 'base_convert/version'
2
+ require 'base_convert/config'
3
+ require 'base_convert/functions'
4
+ require 'base_convert/helpers'
5
+ require 'base_convert/number'
6
+ require 'base_convert/base_convert'
7
+ #`ruby`
@@ -0,0 +1,35 @@
1
+ module BASE_CONVERT
2
+ class BaseConvert
3
+ include CONFIG
4
+ extend FUNCTIONS
5
+ extend HELPERS
6
+
7
+ attr_accessor :from_digits, :to_digits
8
+ def initialize(basefrom, baseto=basefrom)
9
+ @basefrom = BaseConvert.base(basefrom)
10
+ @baseto = BaseConvert.base(baseto)
11
+ @from_digits = BaseConvert.digits(basefrom)
12
+ @to_digits = BaseConvert.digits(baseto)
13
+
14
+ BaseConvert.validate(@baseto, @to_digits)
15
+ BaseConvert.validate(@basefrom, @from_digits)
16
+ end
17
+
18
+ def base2integer(string)
19
+ string = string.upcase if BaseConvert.upcase?(@basefrom, @from_digits) # covenience
20
+ BaseConvert.validate_string(string, @basefrom, @from_digits)
21
+ BaseConvert.to_integer(string, @basefrom, @from_digits)
22
+ end
23
+ alias base2dec base2integer
24
+
25
+ def integer2base(integer)
26
+ BaseConvert.to_base(integer, @baseto, @to_digits)
27
+ end
28
+ alias dec2base integer2base
29
+
30
+ def convert(string)
31
+ integer2base base2integer string.to_s
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ module BASE_CONVERT
2
+ module CONFIG
3
+
4
+ QGRAPH = 0.upto(255).map{|i| i.chr}.select{|c| c=~/[[:graph:]]/ && c=~/[^`'"]/}
5
+
6
+ WORD = 0.upto(255).map{|i| i.chr}.select{|c| c=~/\w/ && c=~/[^_]/} # 0..9 a..z A..Z
7
+ INDEXa = WORD.find_index('a')
8
+
9
+ BASE = {
10
+ :word => WORD.length,
11
+ :qgraph => QGRAPH.length,
12
+ :hexadecimal => 16,
13
+ :hex => 16,
14
+ :decimal => 10,
15
+ :dec => 10,
16
+ :octal => 8,
17
+ :oct => 8,
18
+ :binary => 2,
19
+ }
20
+
21
+ DIGITS = {
22
+ :word => WORD,
23
+ :qgraph => QGRAPH,
24
+ }
25
+
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ # http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
2
+ module BASE_CONVERT
3
+ module FUNCTIONS
4
+
5
+ def to_integer(string, base, digits)
6
+ integer = 0
7
+ string.each_char do |c|
8
+ index = digits.find_index(c)
9
+ integer = integer * base + index
10
+ end
11
+ integer
12
+ end
13
+
14
+ def to_base(integer, base, digits)
15
+ return digits.first if integer == 0
16
+ string = ''
17
+ while integer > 0
18
+ integer, index = integer.divmod(base)
19
+ string = string.insert(0, digits[index])
20
+ end
21
+ string
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ # http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
2
+ module BASE_CONVERT
3
+ module HELPERS
4
+ include CONFIG
5
+
6
+ def upcase?(base, digits)
7
+ base <= INDEXa and digits.equal?(WORD)
8
+ end
9
+
10
+ def validate(base, digits)
11
+ raise 'base is not an integer' unless base.kind_of?(Integer)
12
+ raise 'digits not an Array' unless digits.kind_of?(Array)
13
+ raise 'base not between 2 and digits.length' unless base.between?(2, digits.length)
14
+ end
15
+
16
+ def validate_string(string, base, digits)
17
+ string.chars.uniq.each do |c|
18
+ raise 'String has invalid character' unless (i=digits.find_index(c)) and (i<base)
19
+ end
20
+ end
21
+
22
+ def digits(base)
23
+ if base.class == Symbol
24
+ if digits = DIGITS[base]
25
+ return digits
26
+ end
27
+ base = BASE[base]
28
+ end
29
+ (base > WORD.length)? QGRAPH : WORD
30
+ end
31
+
32
+ def base(base)
33
+ (base.class == Symbol)? BASE[base]: base
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module BASE_CONVERT
2
+ class Number < String
3
+ include CONFIG
4
+ extend FUNCTIONS
5
+ extend HELPERS
6
+
7
+ def initialize(counter, base=10, digits=Number.digits(base), validate=true)
8
+ super(counter.to_s)
9
+ @base = Number.base(base)
10
+ @digits = digits
11
+ if validate
12
+ self.upcase! if Number.upcase?(@base, @digits)
13
+ Number.validate(@base, @digits)
14
+ Number.validate_string(self, @base, @digits)
15
+ end
16
+ end
17
+
18
+ def to_integer
19
+ Number.to_integer(self, @base, @digits)
20
+ end
21
+
22
+ def to_base(base, digits=Number.digits(base), validate=true)
23
+ base = Number.base(base)
24
+ Number.validate(base, digits) if validate
25
+ integer = self.to_integer
26
+ string = Number.to_base(integer, base, digits)
27
+ Number.new(string, base, digits, false) # no need to validate
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module BASE_CONVERT
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,56 @@
1
+ require 'test/unit'
2
+ require 'base_convert'
3
+
4
+ class TestBaseConvert < Test::Unit::TestCase
5
+ include BASE_CONVERT
6
+ include CONFIG
7
+
8
+ def test_001_new
9
+ wl = WORD.length
10
+
11
+ base = BaseConvert.new(10, 16)
12
+ assert base.from_digits.equal?(WORD)
13
+ assert base.to_digits.equal?(WORD)
14
+
15
+ base = BaseConvert.new(70, 8)
16
+ assert base.from_digits.equal?(QGRAPH)
17
+ assert base.to_digits.equal?(WORD)
18
+
19
+ base = BaseConvert.new(wl, 90)
20
+ assert base.from_digits.equal?(WORD)
21
+ assert base.to_digits.equal?(QGRAPH)
22
+
23
+ base = BaseConvert.new(wl+1, wl+2)
24
+ assert base.from_digits.equal?(QGRAPH)
25
+ assert base.to_digits.equal?(QGRAPH)
26
+
27
+ base = BaseConvert.new(:word, :qgraph)
28
+ assert base.from_digits.equal?(WORD)
29
+ assert base.to_digits.equal?(QGRAPH)
30
+
31
+ base = BaseConvert.new(:qgraph, :word)
32
+ assert base.from_digits.equal?(QGRAPH)
33
+ assert base.to_digits.equal?(WORD)
34
+
35
+ a = base.base2integer('z')
36
+ assert_equal(Fixnum, a.class)
37
+ assert_equal(86, a)
38
+
39
+ a = base.integer2base(16)
40
+ assert_equal(String, a.class)
41
+ assert_equal('G', a)
42
+
43
+ a = base.convert('z')
44
+ assert_equal(String, a.class)
45
+ base = BaseConvert.new(:word, :qgraph)
46
+ a = base.convert(a)
47
+ assert_equal('z', a)
48
+
49
+ base = BaseConvert.new(:hex, :oct)
50
+ assert_equal('7777', base.convert('FFF'))
51
+ assert_equal('7777', base.convert('fff')) # Tests the upcase convenience
52
+
53
+ base = BaseConvert.new(:oct, :hex)
54
+ assert_equal('FFF', base.convert('7777'))
55
+ end
56
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'base_convert/config'
3
+ require 'base_convert/functions'
4
+
5
+ class TestFunctions < Test::Unit::TestCase
6
+ include BASE_CONVERT
7
+ include CONFIG
8
+ extend FUNCTIONS
9
+
10
+ HEX = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
11
+
12
+ def test_001_to_integer
13
+ a = TestFunctions.to_integer('0', 16, HEX)
14
+ assert_equal(0, a)
15
+ a = TestFunctions.to_integer('a', 16, HEX)
16
+ assert_equal(10, a)
17
+ a = TestFunctions.to_integer('f', 16, HEX)
18
+ assert_equal(15, a)
19
+ a = TestFunctions.to_integer('abcdef', 16, HEX)
20
+ assert_equal(11259375, a)
21
+ end
22
+
23
+ def test_002_to_base
24
+ a = TestFunctions.to_base(0, 16, WORD)
25
+ assert_equal('0', a)
26
+ a = TestFunctions.to_base(10, 16, WORD)
27
+ assert_equal('A', a)
28
+ a = TestFunctions.to_base(15, 16, WORD)
29
+ assert_equal('F', a)
30
+ a = TestFunctions.to_base(11259375, 16, WORD)
31
+ assert_equal('ABCDEF', a)
32
+ end
33
+
34
+ end
@@ -0,0 +1,99 @@
1
+ require 'test/unit'
2
+ require 'base_convert/config'
3
+ require 'base_convert/helpers'
4
+
5
+ class TestHelpers < Test::Unit::TestCase
6
+ include BASE_CONVERT
7
+ include CONFIG
8
+ extend HELPERS
9
+
10
+ HEX = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
11
+
12
+ def test_003_upcase
13
+ assert(TestHelpers.upcase?(36, WORD))
14
+ refute(TestHelpers.upcase?(36, HEX))
15
+ refute(TestHelpers.upcase?(37, WORD))
16
+ end
17
+
18
+ def test_004_validate
19
+ assert_raises(RuntimeError){ TestHelpers.validate(5.0, WORD) }
20
+ assert_nothing_raised(Exception){ TestHelpers.validate(5, WORD) }
21
+
22
+ assert_raises(RuntimeError){ TestHelpers.validate(5, '0123456789abdef') }
23
+ assert_nothing_raised(Exception){ TestHelpers.validate(5, ['0', '1', '2', '3', '4']) }
24
+
25
+ assert_raises(RuntimeError){ TestHelpers.validate(5, ['0', '1', '2', '3']) }
26
+ assert_nothing_raised(Exception){ TestHelpers.validate(5, ['0', '1', '2', '3', '4', '5', '6']) }
27
+
28
+ assert_raises(RuntimeError){ TestHelpers.validate(1, ['0', '1']) }
29
+ assert_nothing_raised(Exception){ TestHelpers.validate(2, ['0', '1']) }
30
+ end
31
+
32
+ def test_005_validate_string
33
+ assert_raises(RuntimeError){ TestHelpers.validate_string('xyz', 3, ['a','b','c']) }
34
+ assert_nothing_raised(Exception){ TestHelpers.validate_string('xyz', 3, ['x','y','z']) }
35
+ assert_nothing_raised(Exception){ TestHelpers.validate_string('xyz', 3, ['x','y','z','a','b','c']) }
36
+ assert_raises(RuntimeError){ TestHelpers.validate_string('abc', 3, ['x','y','z','a','b','c']) }
37
+ assert_nothing_raised(Exception){ TestHelpers.validate_string('abc', 6, ['x','y','z','a','b','c']) }
38
+ end
39
+
40
+ def test_006_digits
41
+ wl = WORD.length
42
+
43
+ a = TestHelpers.digits(wl)
44
+ assert(a.equal?(WORD))
45
+
46
+ a = TestHelpers.digits(wl+1)
47
+ assert(a.equal?(QGRAPH))
48
+
49
+ a = TestHelpers.digits(:word)
50
+ assert(a.equal?(WORD))
51
+
52
+ a = TestHelpers.digits(:qgraph)
53
+ assert(a.equal?(QGRAPH))
54
+
55
+ a = TestHelpers.digits(:hex)
56
+ assert(a.equal?(WORD))
57
+
58
+ DIGITS[:my_digits] = ['X','Y','Z']
59
+ a = TestHelpers.digits(:my_digits)
60
+ assert(a.equal?(DIGITS[:my_digits]))
61
+
62
+ BASE[:my_base] = wl
63
+ a = TestHelpers.digits(:my_base)
64
+ assert(a.equal?(WORD))
65
+
66
+ BASE[:my_base] = wl+1
67
+ a = TestHelpers.digits(:my_base)
68
+ assert(a.equal?(QGRAPH))
69
+ end
70
+
71
+ def test_007_base
72
+ wl = WORD.length
73
+ ql = QGRAPH.length
74
+
75
+ a = TestHelpers.base(:word)
76
+ assert_equal(wl, a)
77
+
78
+ a = TestHelpers.base(:qgraph)
79
+ assert_equal(ql, a)
80
+
81
+ a = TestHelpers.base(:hex)
82
+ assert_equal(16, a)
83
+ a = TestHelpers.base(:hexadecimal)
84
+ assert_equal(16, a)
85
+
86
+ a = TestHelpers.base(:decimal)
87
+ assert_equal(10, a)
88
+ a = TestHelpers.base(:dec)
89
+ assert_equal(10, a)
90
+
91
+ a = TestHelpers.base(:octal)
92
+ assert_equal(8, a)
93
+ a = TestHelpers.base(:oct)
94
+ assert_equal(8, a)
95
+
96
+ a = TestHelpers.base(:binary)
97
+ assert_equal(2, a)
98
+ end
99
+ end
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+ require 'base_convert'
3
+
4
+ class TestNumber < Test::Unit::TestCase
5
+ include BASE_CONVERT
6
+ include CONFIG
7
+
8
+ def test_001_new
9
+ a = Number.new(100)
10
+ assert_equal '100', a
11
+ a = Number.new('100')
12
+ assert_equal '100', a
13
+ assert_equal 100, a.to_integer
14
+
15
+ assert_raises(RuntimeError){ Number.new('FFF') }
16
+ a = Number.new('FFF', 16)
17
+ assert_equal 'FFF', a
18
+ assert_equal '7777', a.to_base(8)
19
+ assert_equal 'FFF', a.to_base(8).to_base(16)
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'base_convert'
3
+ require 'digest'
4
+
5
+ class TestOriginal < Test::Unit::TestCase
6
+ include BASE_CONVERT
7
+
8
+ def test_001_to_s_n
9
+ # test against number.to_s(n)
10
+ 2.upto(36) do |n|
11
+ # Converts to base n.
12
+ base = BaseConvert.new(n)
13
+ 100.times do
14
+ number = rand(10000)
15
+ string = number.to_s(n)
16
+ # BaseConvert uses caps
17
+ assert_equal(string.upcase, base.integer2base(number))
18
+ # and it will automatically upcase
19
+ assert_equal(number, base.base2integer(string))
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_002_hello_world
25
+ hexdigest = Digest::SHA256.hexdigest('Hello World!') # and verified below
26
+ assert_equal '7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069', hexdigest
27
+
28
+ word = BaseConvert.new(:hex, :word).convert(hexdigest)
29
+ assert_equal 'UEjKBW7lvEq1KjyCxAYQRJqmvffQEbwLNvzs8Ggdy4P', word
30
+
31
+ assert_equal hexdigest.upcase, BaseConvert.new(:word, :hex).convert(word)
32
+
33
+ qstring = '$<c6PCX_mugC58xfO5JOp+V4|<jHekI^_WE$?d?9'
34
+ assert_equal qstring, BaseConvert.new(:hex, :qgraph).convert(hexdigest)
35
+ assert_equal hexdigest.upcase, BaseConvert.new(:qgraph, :hex).convert(qstring)
36
+ end
37
+
38
+ def test_003_hundred_million
39
+ assert_equal '6laZE', BaseConvert.new(10, 62).convert( 100_000_000 )
40
+ assert_equal ')8]3H', BaseConvert.new(63).integer2base( 100_000_000 )
41
+
42
+ base = BaseConvert.new(62)
43
+ base.to_digits = BaseConvert::QGRAPH
44
+ assert_equal ')RGF1', base.integer2base( 100_000_000 )
45
+ end
46
+
47
+ end
@@ -0,0 +1,66 @@
1
+ require 'test/unit'
2
+ require 'base_convert'
3
+ require 'digest'
4
+
5
+ # Using my original tests on BASE_CONVERT::Number
6
+ class TestOriginal2 < Test::Unit::TestCase
7
+ include BASE_CONVERT
8
+
9
+ def test_001_to_s_n
10
+ # test against number.to_s(base)
11
+ 2.upto(36) do |base|
12
+ 100.times do
13
+ decimal = rand(10000)
14
+
15
+ decimal0 = decimal.to_s
16
+ decimal1 = Number.new(decimal)
17
+ assert_equal(decimal0, decimal1)
18
+
19
+ base0 = decimal.to_s(base)
20
+ base1 = decimal1.to_base(base)
21
+
22
+ # BaseConvert uses caps
23
+ assert_equal(base0.upcase, base1)
24
+ assert_equal(decimal1, base1.to_base(10))
25
+ # and it will automatically upcase
26
+ assert_equal(decimal1, Number.new(base0, base).to_base(10))
27
+ end
28
+ end
29
+ end
30
+
31
+ def test_002_hello_world
32
+ hexdigest = Number.new(Digest::SHA256.hexdigest('Hello World!'), :hex)
33
+ # and verified below (BaseConvert uses uppecase for hexadeximals.
34
+ assert_equal '7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069'.upcase, hexdigest
35
+
36
+ #word = BaseConvert.new(:hex, :word).convert(hexdigest)
37
+ word = hexdigest.to_base(:word)
38
+ assert_equal 'UEjKBW7lvEq1KjyCxAYQRJqmvffQEbwLNvzs8Ggdy4P', word
39
+
40
+ #assert_equal hexdigest.upcase, BaseConvert.new(:word, :hex).convert(word)
41
+ assert_equal hexdigest, word.to_base(:hex)
42
+
43
+ qstring = Number.new('$<c6PCX_mugC58xfO5JOp+V4|<jHekI^_WE$?d?9', :qgraph)
44
+ # and verified below
45
+ assert_equal '$<c6PCX_mugC58xfO5JOp+V4|<jHekI^_WE$?d?9', qstring
46
+ #assert_equal qstring, BaseConvert.new(:hex, :qgraph).convert(hexdigest)
47
+ assert_equal qstring, hexdigest.to_base(:qgraph)
48
+
49
+ assert_equal hexdigest, qstring.to_base(:hex)
50
+ end
51
+
52
+ def test_003_hundred_million
53
+ hudred_million = Number.new(100_000_000)
54
+ assert_equal '100000000', hudred_million
55
+ #assert_equal '6laZE', BaseConvert.new(10, 62).convert( 100_000_000 )
56
+ assert_equal '6laZE', hudred_million.to_base(62)
57
+ #assert_equal ')8]3H', BaseConvert.new(63).integer2base( 100_000_000 )
58
+ assert_equal ')8]3H', hudred_million.to_base(63)
59
+
60
+ #base = BaseConvert.new(62)
61
+ #base.to_digits = BaseConvert::QGRAPH
62
+ #assert_equal ')RGF1', base.integer2base( 100_000_000 )
63
+ assert_equal ')RGF1', hudred_million.to_base(62, BaseConvert::QGRAPH)
64
+ end
65
+
66
+ end
@@ -0,0 +1,48 @@
1
+ require 'test/unit'
2
+ require 'base_convert'
3
+
4
+ class TestTrivial < Test::Unit::TestCase
5
+
6
+ def test_001_version
7
+ assert_not_nil BASE_CONVERT::VERSION=~/^\d+\.\d+\.\d+$/
8
+ end
9
+
10
+ def test_002_qgraph
11
+ a = BASE_CONVERT::CONFIG::QGRAPH
12
+ assert_equal(91, a.length)
13
+ refute(a.include?('"'))
14
+ refute(a.include?("'"))
15
+ assert_equal('!', a.first)
16
+ end
17
+
18
+ def test_003_word
19
+ a = BASE_CONVERT::CONFIG::WORD
20
+ assert_equal(62, a.length)
21
+ refute(a.include?('_'))
22
+ assert_equal('0', a.first)
23
+ assert_equal(36, BASE_CONVERT::CONFIG::INDEXa)
24
+ assert_equal('a', a[36])
25
+ end
26
+
27
+ def test_004_base
28
+ a = BASE_CONVERT::CONFIG::BASE
29
+ assert_equal(Hash, a.class)
30
+ assert_equal(62, a[:word])
31
+ assert_equal(91, a[:qgraph])
32
+ assert_equal(16, a[:hex])
33
+ assert_equal(16, a[:hexadecimal])
34
+ assert_equal(10, a[:decimal])
35
+ assert_equal(10, a[:dec])
36
+ assert_equal(8, a[:octal])
37
+ assert_equal(8, a[:oct])
38
+ assert_equal(2, a[:binary])
39
+ end
40
+
41
+ def test_005_digits
42
+ a = BASE_CONVERT::CONFIG::DIGITS
43
+ assert_equal(Hash, a.class)
44
+ assert(a[:word].equal?(BASE_CONVERT::CONFIG::WORD))
45
+ assert(a[:qgraph].equal?(BASE_CONVERT::CONFIG::QGRAPH))
46
+ end
47
+
48
+ end
metadata CHANGED
@@ -1,29 +1,74 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base_convert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - carlosjhr64@gmail.com
7
+ - CarlosJHR64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-12 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Got the code originally from http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
14
- which I then converted to a class.
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: test-unit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.5.5
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.5.5
33
+ description: |
34
+ base_convert - Number base conversion.
35
+
36
+ Converts positive integers to different bases:
37
+ Binary, octal, hexadecimal, decimal, or any arbitrary base.
38
+ "Out of the box" handling of up to base 91.
39
+ Allows for arbitrary choice of alphabet(digits).
15
40
  email: carlosjhr64@gmail.com
16
41
  executables: []
17
42
  extensions: []
18
- extra_rdoc_files: []
43
+ extra_rdoc_files:
44
+ - README.rdoc
19
45
  files:
20
- - "./README.txt"
21
- - "./lib/base_convert.rb"
46
+ - History.txt
47
+ - README.rdoc
48
+ - TODO.txt
49
+ - base_convert.gemspec
50
+ - lib/base_convert.rb
51
+ - lib/base_convert/base_convert.rb
52
+ - lib/base_convert/config.rb
53
+ - lib/base_convert/functions.rb
54
+ - lib/base_convert/helpers.rb
55
+ - lib/base_convert/number.rb
56
+ - lib/base_convert/version.rb
57
+ - test/test_base_convert.rb
58
+ - test/test_functions.rb
59
+ - test/test_helpers.rb
60
+ - test/test_number.rb
61
+ - test/test_original.rb
62
+ - test/test_original2.rb
63
+ - test/test_trivial.rb
22
64
  homepage: https://github.com/carlosjhr64/base_convert
23
- licenses: []
65
+ licenses:
66
+ - MIT
24
67
  metadata: {}
25
68
  post_install_message:
26
- rdoc_options: []
69
+ rdoc_options:
70
+ - "--main"
71
+ - README.rdoc
27
72
  require_paths:
28
73
  - lib
29
74
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -36,10 +81,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
81
  - - ">="
37
82
  - !ruby/object:Gem::Version
38
83
  version: '0'
39
- requirements: []
84
+ requirements:
85
+ - 'ruby: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]'
40
86
  rubyforge_project:
41
87
  rubygems_version: 2.2.0
42
88
  signing_key:
43
89
  specification_version: 4
44
- summary: BaseConvert
90
+ summary: base_convert - Number base conversion.
45
91
  test_files: []
data/README.txt DELETED
@@ -1,84 +0,0 @@
1
- # BaseConvert
2
- # DESCRIPTION:
3
- # Got the code originally from
4
- # http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
5
- # which I then converted to a class.
6
- # SINOPSIS:
7
-
8
- def ok(a,b)
9
- raise "bad! /#{a}/ == /#{b}/" unless a == b
10
- end
11
-
12
- # ruby -I ./lib README.txt
13
- if RUBY_VERSION =~ /^1.9.2/ then
14
- require 'base_convert'
15
-
16
- # Verify it works up to base 36 with number.to_s(n)
17
- 2.upto(36) do |n|
18
- base = BaseConvert.new(n)
19
- 100.times do
20
- number = rand(10000)
21
- string = number.to_s(n)
22
- ok( base.convert(string), number )
23
- ok( base.convert(number), string.upcase ) # BaseConvert uses caps
24
- end
25
- end
26
-
27
- # BaseConvert is case insensitive up to base 36, but
28
- # returns string values with capital leters.
29
- # At base 37 and up, it's case sensitive with lower case letter being of higher order.
30
- # It primary use is to reduce digest strings.
31
- # For example:
32
- require 'digest'
33
- # This hexadecimal:
34
- ok( "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069",
35
- hexdigest = Digest::SHA256.hexdigest('Hello World!') )
36
- # Translatates to this word(\w except '_') string
37
- ok( string = 'UEjKBW7lvEq1KjyCxAYQRJqmvffQEbwLNvzs8Ggdy4P',
38
- BaseConvert.new(:hexadecimal,:word).convert(hexdigest) )
39
- # and we can covert it back:
40
- ok( BaseConvert.new(:word,:hexadecimal).convert(string),
41
- hexdigest.upcase )
42
- # Again, note the BaseConvert returns the hexdigest string capitalized.
43
- #There is also a quotable string([[:graph:]] except [`'"]):
44
- ok( qstring = '$<c6PCX_mugC58xfO5JOp+V4|<jHekI^_WE$?d?9',
45
- BaseConvert.new(:hexadecimal,:qgraph).convert(hexdigest))
46
- # And you can convert it back
47
- ok( BaseConvert.new(:qgraph,:hexadecimal).convert(qstring),
48
- hexdigest.upcase)
49
- # Note the length
50
- ok( 64, hexdigest.length)
51
- ok( 43, string.length)
52
- ok( 40, qstring.length)
53
-
54
- # BaseConvert has the following primary form:
55
- # # converting from basefrom to baseto
56
- # base = BaseConvert.new( basefrom, baseto )
57
- # # covert basefrom_string in basefrom to baseto_string in baseto
58
- # baseto_string = base.convert( basefrom_string )
59
- # # convert basefrom_string to it's decimal number representation
60
- # decimal_number = base.base2dec( basefrom_string )
61
- # # convert a decimal_number to it's baseto string representation
62
- # baseto_string = base.dec2base( decimal_number )
63
- # BaseConvert has the following heuristic forms:
64
- # # converting to and from _base_
65
- # base = BaseConvert.new( _base_ )
66
- # base_string = base.convert( decimal_number )
67
- # decimal_number = base.convert( base_string )
68
- # BaseConvert has the following mneumonic keys that can be use for base:
69
- # :binary => 2
70
- # :oct,:octal => 8,
71
- # :dec,:decimal => 10,
72
- # :hex,:hexadecimal => 16
73
- # BaseConvert's base can go up to 62 with :word(\w except '_') and 91 with :qgraph([:graph] except [`'"].
74
- # By default, :word is used...
75
- ok( '6laZE', BaseConvert.new(62).convert( 100_000_000 ))
76
- # ...until a transition to :qgraph is required.
77
- ok( ')8]3H', BaseConvert.new(63).convert( 100_000_000 ))
78
- # But you can specify the digit dictionary. At this point you've read and know the code...
79
- base = BaseConvert.new(62)
80
- base.to_digits = BaseConvert::QGRAPH
81
- ok( ')RGF1', base.convert( 100_000_000 ))
82
- end
83
- # The End.
84
- # <i>carlosjhr64</i>@<i>gmail<i/>.<i>com</i>