base_convert 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +27 -25
- data/lib/base_convert.rb +2 -2
- data/lib/base_convert/{config.rb → configuration.rb} +5 -5
- data/lib/base_convert/from_to.rb +35 -0
- data/lib/base_convert/functions.rb +4 -4
- data/lib/base_convert/helpers.rb +6 -6
- data/lib/base_convert/number.rb +4 -4
- data/lib/base_convert/version.rb +2 -2
- metadata +10 -38
- data/History.txt +0 -3
- data/TODO.txt +0 -3
- data/base_convert.gemspec +0 -55
- data/lib/base_convert/base_convert.rb +0 -35
- data/test/test_base_convert.rb +0 -56
- data/test/test_functions.rb +0 -34
- data/test/test_helpers.rb +0 -99
- data/test/test_number.rb +0 -21
- data/test/test_original.rb +0 -47
- data/test/test_original2.rb +0 -66
- data/test/test_trivial.rb +0 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cdec0c0e2e6f861cdf6a3b1d76c0c0236d96fa2
|
4
|
+
data.tar.gz: 92dac030b2644afc8713a7e2c528d22c704a1a0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5abcd99a6357e1b8cafd89c6085b905bbbd9f29739f1d906e16fae5d6c9bad598a4c714cf12bea1d0c088df75022538c3bb9a281899f07f36c199991e155c301
|
7
|
+
data.tar.gz: e5fa9736608216d2e3ad1c88ecbec19fb9bd8592b8175a7a0ee2993a0573fe5a37832f62f30ab5a7609397e8293030b686757621edd1c83a1cb62bf8b90def2e
|
data/README.rdoc
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
= base_convert
|
2
2
|
|
3
|
+
{<img src="https://badge.fury.io/rb/base_convert.svg" alt="Gem Version" />}[http://badge.fury.io/rb/base_convert]
|
4
|
+
|
3
5
|
github :: https://www.github.com/carlosjhr64/base_convert
|
4
6
|
rubygems :: https://rubygems.org/gems/base_convert
|
5
7
|
|
@@ -17,36 +19,36 @@ See also Non-decimal_radices/Convert[http://rosettacode.org/wiki/Non-decimal_rad
|
|
17
19
|
== SYNOPSIS:
|
18
20
|
|
19
21
|
require 'base_convert'
|
20
|
-
include
|
21
|
-
h2o =
|
22
|
-
o2h =
|
23
|
-
|
24
|
-
|
22
|
+
include BaseConvert
|
23
|
+
h2o = FromTo.new(16, 8)
|
24
|
+
o2h = FromTo.new(8, 16)
|
25
|
+
a = h2o.convert('FFFF') #=> "177777"
|
26
|
+
b = o2h.convert('177777') #=> "FFFF"
|
25
27
|
|
26
28
|
== BUT WAIT, THERE'S MORE:
|
27
29
|
|
28
30
|
Using `irb` to demonstrate the features.
|
29
|
-
The components are scoped under `
|
31
|
+
The components are scoped under `BaseConvert`:
|
30
32
|
|
31
33
|
# irb
|
32
34
|
# Welcome to IRB...
|
33
35
|
require 'base_convert' #=> true
|
34
|
-
include
|
36
|
+
include BaseConvert #=> Object
|
35
37
|
|
36
38
|
`base_convert` provides three ways to convert a string representation of a number.
|
37
39
|
The first is functional. One can extend(import) the functions that do the conversions.
|
38
40
|
The conversion functions are `to_integer` and `to_base`.
|
39
41
|
For example, the octal number "7777":
|
40
42
|
|
41
|
-
extend
|
42
|
-
digits =
|
43
|
+
extend Functions #=> main
|
44
|
+
digits = '01234567'
|
43
45
|
base = digits.length #=> 8
|
44
46
|
to_integer('7777', base, digits) #=> 4095
|
45
47
|
to_base(4095, base, digits) #=> "7777"
|
46
48
|
|
47
49
|
You can work with arbitrary digits:
|
48
50
|
|
49
|
-
digits =
|
51
|
+
digits = ')!@#$%^&'
|
50
52
|
base = digits.length #=> 8
|
51
53
|
to_integer('&&&&', base, digits) #=> 4095
|
52
54
|
to_base(4095, base, digits) #=> "&&&&"
|
@@ -55,52 +57,52 @@ For convenience, `base_convert` provides two sets of digits.
|
|
55
57
|
`WORD` are the ASCII word characters except underscore[_].
|
56
58
|
`QGRAPH` are the ASCII graph characters except quotes['"].
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
to_base(4095, 8,
|
61
|
-
to_base(4095, 8,
|
60
|
+
Configuration::WORD #=> "01...AB...ab...yz"
|
61
|
+
Configuration::QGRAPH #=> "!#...01...AB...ab...}~"
|
62
|
+
to_base(4095, 8, Configuration::WORD) #=> "7777"
|
63
|
+
to_base(4095, 8, Configuration::QGRAPH) #=> "****"
|
62
64
|
|
63
|
-
The second way to convert is via a conversion object of `
|
65
|
+
The second way to convert is via a conversion object of `BaseConvert::FromTo`.
|
64
66
|
For example, to convert from hexadecimal to octal, and back:
|
65
67
|
|
66
|
-
h2o =
|
67
|
-
o2h =
|
68
|
+
h2o = FromTo.new(16, 8)
|
69
|
+
o2h = FromTo.new(8, 16)
|
68
70
|
h2o.convert('FFFF') #=> "177777"
|
69
71
|
o2h.convert('177777') #=> "FFFF"
|
70
72
|
|
71
73
|
You can access the conversion alphabets via
|
72
|
-
the accessors
|
74
|
+
the accessors FromTo#from_digits and FromTo#to_digits.
|
73
75
|
By default, WORD is used for bases upto 62.
|
74
76
|
For bigger bases upto 91, QGRAPH is used.
|
75
77
|
You can have bigger bases, but
|
76
78
|
you will need to come up with a bigger alphabet, perhaps
|
77
79
|
by adding greek letters.
|
78
80
|
Note that when there's no ambiguity while using WORD,
|
79
|
-
|
81
|
+
FromTo will upcase the string.
|
80
82
|
|
81
83
|
h2o.convert('FFFF') #=> "177777"
|
82
84
|
h2o.convert('ffff') #=> "177777"
|
83
85
|
|
84
|
-
|
85
|
-
|
86
|
+
FromTo also makes available the intermediary methods that works with integers,
|
87
|
+
FromTo#base2integer and FromTo#integer2Base:
|
86
88
|
|
87
89
|
h2o.base2integer('FFFF') #=> 65535
|
88
90
|
h2o.integer2base(65535) #=> "177777"
|
89
91
|
|
90
92
|
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, `
|
93
|
+
The third way to convert is via the subclass of String, `BaseConvert::Number`:
|
92
94
|
|
93
|
-
hexadecimal = Number.new('FFFF', 16,
|
95
|
+
hexadecimal = Number.new('FFFF', 16, Configuration::WORD) #=> "FFFF"
|
94
96
|
# WORD is the default alphabet
|
95
97
|
hexadecimal = Number.new('FFFF', 16) #=> "FFFF"
|
96
98
|
hexadecimal.to_integer #=> 65535
|
97
|
-
#
|
99
|
+
# The default base is 10.
|
98
100
|
decimal = Number.new('65535') # "65535"
|
99
101
|
decimal.to_integer #=> 65535
|
100
102
|
decimal.to_base(16) #=> "FFFF"
|
101
103
|
decimal.to_base(8) #=> "177777"
|
102
104
|
# You can also specify the digits to use
|
103
|
-
digits =
|
105
|
+
digits = ')!@#$%^&'
|
104
106
|
decimal.to_base(8, digits) #=> "!&&&&&"
|
105
107
|
|
106
108
|
== INSTALL:
|
data/lib/base_convert.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'base_convert/version'
|
2
|
-
require 'base_convert/
|
2
|
+
require 'base_convert/configuration'
|
3
3
|
require 'base_convert/functions'
|
4
4
|
require 'base_convert/helpers'
|
5
5
|
require 'base_convert/number'
|
6
|
-
require 'base_convert/
|
6
|
+
require 'base_convert/from_to'
|
7
7
|
#`ruby`
|
@@ -1,10 +1,10 @@
|
|
1
|
-
module
|
2
|
-
module
|
1
|
+
module BaseConvert
|
2
|
+
module Configuration
|
3
3
|
|
4
|
-
QGRAPH = 0.upto(255).map{|i| i.chr}.select{|c| c=~/[[:graph:]]/ && c=~/[^`'"]/}
|
4
|
+
QGRAPH = 0.upto(255).map{|i| i.chr}.select{|c| c=~/[[:graph:]]/ && c=~/[^`'"]/}.join.freeze
|
5
5
|
|
6
|
-
WORD = 0.upto(255).map{|i| i.chr}.select{|c| c=~/\w/ && c=~/[^_]/} # 0..9 a..z A..Z
|
7
|
-
INDEXa = WORD.
|
6
|
+
WORD = 0.upto(255).map{|i| i.chr}.select{|c| c=~/\w/ && c=~/[^_]/}.join.freeze # 0..9 a..z A..Z
|
7
|
+
INDEXa = WORD.index('a').freeze
|
8
8
|
|
9
9
|
BASE = {
|
10
10
|
:word => WORD.length,
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module BaseConvert
|
2
|
+
class FromTo
|
3
|
+
include Configuration
|
4
|
+
extend Functions
|
5
|
+
extend Helpers
|
6
|
+
|
7
|
+
attr_accessor :from_digits, :to_digits
|
8
|
+
def initialize(basefrom, baseto=basefrom)
|
9
|
+
@basefrom = FromTo.base(basefrom)
|
10
|
+
@baseto = FromTo.base(baseto)
|
11
|
+
@from_digits = FromTo.digits(basefrom)
|
12
|
+
@to_digits = FromTo.digits(baseto)
|
13
|
+
|
14
|
+
FromTo.validate(@baseto, @to_digits)
|
15
|
+
FromTo.validate(@basefrom, @from_digits)
|
16
|
+
end
|
17
|
+
|
18
|
+
def base2integer(string)
|
19
|
+
string = string.upcase if FromTo.upcase?(@basefrom, @from_digits) # covenience
|
20
|
+
FromTo.validate_string(string, @basefrom, @from_digits)
|
21
|
+
FromTo.to_integer(string, @basefrom, @from_digits)
|
22
|
+
end
|
23
|
+
alias base2dec base2integer
|
24
|
+
|
25
|
+
def integer2base(integer)
|
26
|
+
FromTo.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
|
@@ -1,18 +1,18 @@
|
|
1
1
|
# http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
|
2
|
-
module
|
3
|
-
module
|
2
|
+
module BaseConvert
|
3
|
+
module Functions
|
4
4
|
|
5
5
|
def to_integer(string, base, digits)
|
6
6
|
integer = 0
|
7
7
|
string.each_char do |c|
|
8
|
-
index = digits.
|
8
|
+
index = digits.index(c)
|
9
9
|
integer = integer * base + index
|
10
10
|
end
|
11
11
|
integer
|
12
12
|
end
|
13
13
|
|
14
14
|
def to_base(integer, base, digits)
|
15
|
-
return digits
|
15
|
+
return digits[0] if integer == 0
|
16
16
|
string = ''
|
17
17
|
while integer > 0
|
18
18
|
integer, index = integer.divmod(base)
|
data/lib/base_convert/helpers.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
# http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
|
2
|
-
module
|
3
|
-
module
|
4
|
-
include
|
2
|
+
module BaseConvert
|
3
|
+
module Helpers
|
4
|
+
include Configuration
|
5
5
|
|
6
6
|
def upcase?(base, digits)
|
7
7
|
base <= INDEXa and digits.equal?(WORD)
|
8
8
|
end
|
9
9
|
|
10
10
|
def validate(base, digits)
|
11
|
-
raise 'base is not an integer'
|
12
|
-
raise 'digits not an
|
11
|
+
raise 'base is not an integer' unless base.kind_of?(Integer)
|
12
|
+
raise 'digits not an String' unless digits.kind_of?(String)
|
13
13
|
raise 'base not between 2 and digits.length' unless base.between?(2, digits.length)
|
14
14
|
end
|
15
15
|
|
16
16
|
def validate_string(string, base, digits)
|
17
17
|
string.chars.uniq.each do |c|
|
18
|
-
raise 'String has invalid character' unless (i=digits.
|
18
|
+
raise 'String has invalid character' unless (i=digits.index(c)) and (i<base)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
data/lib/base_convert/number.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module BaseConvert
|
2
2
|
class Number < String
|
3
|
-
include
|
4
|
-
extend
|
5
|
-
extend
|
3
|
+
include Configuration
|
4
|
+
extend Functions
|
5
|
+
extend Helpers
|
6
6
|
|
7
7
|
def initialize(counter, base=10, digits=Number.digits(base), validate=true)
|
8
8
|
super(counter.to_s)
|
data/lib/base_convert/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = '
|
1
|
+
module BaseConvert
|
2
|
+
VERSION = '2.0.0'
|
3
3
|
end
|
metadata
CHANGED
@@ -1,35 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base_convert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- carlosjhr64
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
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
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
33
13
|
description: |
|
34
14
|
base_convert - Number base conversion.
|
35
15
|
|
@@ -43,24 +23,14 @@ extensions: []
|
|
43
23
|
extra_rdoc_files:
|
44
24
|
- README.rdoc
|
45
25
|
files:
|
46
|
-
- History.txt
|
47
26
|
- README.rdoc
|
48
|
-
- TODO.txt
|
49
|
-
- base_convert.gemspec
|
50
27
|
- lib/base_convert.rb
|
51
|
-
- lib/base_convert/
|
52
|
-
- lib/base_convert/
|
28
|
+
- lib/base_convert/configuration.rb
|
29
|
+
- lib/base_convert/from_to.rb
|
53
30
|
- lib/base_convert/functions.rb
|
54
31
|
- lib/base_convert/helpers.rb
|
55
32
|
- lib/base_convert/number.rb
|
56
33
|
- 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
|
64
34
|
homepage: https://github.com/carlosjhr64/base_convert
|
65
35
|
licenses:
|
66
36
|
- MIT
|
@@ -82,10 +52,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
52
|
- !ruby/object:Gem::Version
|
83
53
|
version: '0'
|
84
54
|
requirements:
|
85
|
-
- 'ruby: ruby 2.1.
|
55
|
+
- 'ruby: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]'
|
56
|
+
- 'join: join (GNU coreutils) 8.21'
|
86
57
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
58
|
+
rubygems_version: 2.4.1
|
88
59
|
signing_key:
|
89
60
|
specification_version: 4
|
90
61
|
summary: base_convert - Number base conversion.
|
91
62
|
test_files: []
|
63
|
+
has_rdoc:
|
data/History.txt
DELETED
data/TODO.txt
DELETED
data/base_convert.gemspec
DELETED
@@ -1,55 +0,0 @@
|
|
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
|
@@ -1,35 +0,0 @@
|
|
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
|
data/test/test_base_convert.rb
DELETED
@@ -1,56 +0,0 @@
|
|
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
|
data/test/test_functions.rb
DELETED
@@ -1,34 +0,0 @@
|
|
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
|
data/test/test_helpers.rb
DELETED
@@ -1,99 +0,0 @@
|
|
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
|
data/test/test_number.rb
DELETED
@@ -1,21 +0,0 @@
|
|
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
|
data/test/test_original.rb
DELETED
@@ -1,47 +0,0 @@
|
|
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
|
data/test/test_original2.rb
DELETED
@@ -1,66 +0,0 @@
|
|
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
|
data/test/test_trivial.rb
DELETED
@@ -1,48 +0,0 @@
|
|
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
|