to_decimal 0.0.1
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 +7 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +27 -0
- data/LICENCE.txt +20 -0
- data/README.md +124 -0
- data/Rakefile +25 -0
- data/lib/to_decimal.rb +2 -0
- data/lib/to_decimal/to_decimal_class.rb +60 -0
- data/lib/to_decimal/wrong_base_input_error.rb +1 -0
- data/test/to_decimal_test.rb +254 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f859738206a03fb0a00035e44e2a662802ff26bff56df8d39ccb09b16cacd615
|
4
|
+
data.tar.gz: ced07eabf62e00f554914d5c4f865063c2924972d912f7f617c0736d3e0ad340
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a467ba70eb07499cad887f9db131173f104bad319845c332dde768431cfb59672b85c87c599a436ded343520676c588b413ad88c922d55e208a64939d637af9
|
7
|
+
data.tar.gz: e439bb489045de5973dfa690fb632e4b4df58b420dc454a90c59e78663ddd3de3657387b2ca3e3091deb80cda933f0e2c47b89e0576c15bdbed8377b4f4ad275
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
ansi (1.5.0)
|
5
|
+
builder (3.2.3)
|
6
|
+
minitest (5.11.3)
|
7
|
+
minitest-reporters (1.1.19)
|
8
|
+
ansi
|
9
|
+
builder
|
10
|
+
minitest (>= 5.0)
|
11
|
+
ruby-progressbar
|
12
|
+
rake (12.3.0)
|
13
|
+
ruby-progressbar (1.9.0)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
minitest
|
20
|
+
minitest-reporters (~> 1.1)
|
21
|
+
rake
|
22
|
+
|
23
|
+
RUBY VERSION
|
24
|
+
ruby 2.5.0p0
|
25
|
+
|
26
|
+
BUNDLED WITH
|
27
|
+
1.16.1
|
data/LICENCE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
to_decimal Copyright 2017 Laurent Guinotte
|
2
|
+
|
3
|
+
***MIT Licence***
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# to_decimal
|
2
|
+
|
3
|
+
A simple gem to convert an integer expressed in bases
|
4
|
+
ranging from 2 to 10 into a decimal integer.
|
5
|
+
|
6
|
+
Ruby comes with useful built-in methods to convert integers and string
|
7
|
+
representation of integers to another base (`String#to_i(base=10)` and
|
8
|
+
`Ìnteger#to_s(base=10)`).
|
9
|
+
|
10
|
+
But I didn't found a simple and straightforward way to convert an integer
|
11
|
+
form a given base to an integer of base 10, nor in the core API, nor in the
|
12
|
+
Standard Library.
|
13
|
+
|
14
|
+
I found the following two gems :
|
15
|
+
- [bases](https://github.com/whatyouhide/bases) ;
|
16
|
+
- [radix](https://github.com/rubyworks/radix) gems ;
|
17
|
+
|
18
|
+
They are very comprehensive, and go both behind the base 36, which is the limit
|
19
|
+
of Ruby.
|
20
|
+
|
21
|
+
But they are a little too heavy for my purpose. I just wanted something like :
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
ToDecimal.to_d(12, base: 8) # => 10
|
25
|
+
|
26
|
+
# or
|
27
|
+
|
28
|
+
base8.to_d(12) # => 10
|
29
|
+
|
30
|
+
# or even
|
31
|
+
|
32
|
+
base8[12] # => 10
|
33
|
+
```
|
34
|
+
|
35
|
+
And, if any conversion is needed back to the original base, we can just use
|
36
|
+
the buit-in Ruby methods on the return value of the method.
|
37
|
+
|
38
|
+
So I decided to create this little gem for that. If something similar already
|
39
|
+
exists, I would be happy to know.
|
40
|
+
|
41
|
+
|
42
|
+
# Installation
|
43
|
+
```shell
|
44
|
+
$ gem install to_decimal
|
45
|
+
$ bundle install
|
46
|
+
```
|
47
|
+
|
48
|
+
# Test
|
49
|
+
|
50
|
+
This project is tested under minitest.
|
51
|
+
|
52
|
+
`rake` or `rake test`
|
53
|
+
|
54
|
+
# Usage
|
55
|
+
|
56
|
+
### Instance methods
|
57
|
+
|
58
|
+
Instantiate a new `ToDecimal` object :
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
base10 = ToDecimal.new
|
62
|
+
# => a new object with default base set to 10
|
63
|
+
|
64
|
+
# or
|
65
|
+
base8 = ToDecimal.new(8)
|
66
|
+
# or
|
67
|
+
base8 = ToDecimal.new('8')
|
68
|
+
# => a new object with base set to 8
|
69
|
+
```
|
70
|
+
|
71
|
+
The base attribute can be updated simply :
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
base8.base = 3
|
75
|
+
|
76
|
+
#or
|
77
|
+
|
78
|
+
base8.base = '3'
|
79
|
+
```
|
80
|
+
|
81
|
+
To convert a number of a given base to a base 10 integer, use the `#to_d(input)`
|
82
|
+
method (alias `#to_decimal`):
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
base8.to_d(12) # => 10
|
86
|
+
|
87
|
+
# alias
|
88
|
+
|
89
|
+
convertor[12] # => 10
|
90
|
+
```
|
91
|
+
An error wil be raised if the input integer is not of the given base.
|
92
|
+
|
93
|
+
### Class method
|
94
|
+
|
95
|
+
Instead of creating a new object, you can also call a class method `#to_d` on
|
96
|
+
the ToDecimal class itself :
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
ToDecimal.to_d(1231, base: 4)
|
100
|
+
# => 109
|
101
|
+
```
|
102
|
+
|
103
|
+
# Contribute
|
104
|
+
|
105
|
+
If you think this small gem could be improved in any way, don't hesitate,
|
106
|
+
I would be happy to learn. And if you want to contribute, I would be happy too !
|
107
|
+
|
108
|
+
- Fork it ;
|
109
|
+
- Create your own branch (`git checkout -b my-new-feature`) ;
|
110
|
+
- Make your feature addition or bug fix ;
|
111
|
+
- Add tests for it ;
|
112
|
+
- Commit on your own branch ;
|
113
|
+
- Push to the branch (`git push origin my-new-feature`) ;
|
114
|
+
- Create a new pull request ;
|
115
|
+
|
116
|
+
# Author
|
117
|
+
|
118
|
+
Laurent Guinotte
|
119
|
+
|
120
|
+
|
121
|
+
# Licence
|
122
|
+
|
123
|
+
to_decimal is released under the MIT Licence. See LICENCE.txt
|
124
|
+
for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'find'
|
3
|
+
|
4
|
+
desc 'Run tests'
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.libs << 'test'
|
8
|
+
t.test_files = FileList['test/**/*_test.rb']
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Run tests'
|
12
|
+
task :default => :test
|
13
|
+
|
14
|
+
desc 'Display inventory of all files'
|
15
|
+
task :inventory do
|
16
|
+
Find.find('.') do |name|
|
17
|
+
next if name.include?('/.') # Excludes files and directories with . names
|
18
|
+
puts name if File.file?(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Run rubocop'
|
23
|
+
task :rub do
|
24
|
+
sh 'rubocop'
|
25
|
+
end
|
data/lib/to_decimal.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
class ToDecimal
|
2
|
+
attr_reader :input, :base
|
3
|
+
|
4
|
+
def self.to_d(digit, base: 10)
|
5
|
+
self.new(base).to_d(digit)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(base=10)
|
9
|
+
@base = format_and_validate(base)
|
10
|
+
end
|
11
|
+
|
12
|
+
def base=(new_base)
|
13
|
+
@base = format_and_validate(new_base)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_d(input)
|
17
|
+
formatted_input = format(input).digits
|
18
|
+
validate_input(formatted_input)
|
19
|
+
decimal = 0
|
20
|
+
formatted_input.each_with_index do |digit, power|
|
21
|
+
decimal += digit * (base**power)
|
22
|
+
end
|
23
|
+
decimal
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method :[], :to_d
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def format(input)
|
31
|
+
if input.is_a?(String) || input.is_a?(Integer)
|
32
|
+
if input.is_a?(String)
|
33
|
+
input.to_i
|
34
|
+
else
|
35
|
+
input
|
36
|
+
end
|
37
|
+
else
|
38
|
+
raise ArgumentError, "Integer or String representation of an Integer\
|
39
|
+
expected as input or base"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def validate_base(base)
|
44
|
+
raise ArgumentError, "Base must be an Integer" unless base.is_a?(Integer)
|
45
|
+
raise ArgumentError, "Base must be 1..10" unless base > 0 && base <= 10
|
46
|
+
end
|
47
|
+
|
48
|
+
def format_and_validate(base)
|
49
|
+
formatted_base = format(base)
|
50
|
+
validate_base(formatted_base)
|
51
|
+
formatted_base
|
52
|
+
end
|
53
|
+
|
54
|
+
def validate_input(formatted_input)
|
55
|
+
if formatted_input.any? { |digit| digit >= base }
|
56
|
+
raise WrongBaseInputError, "A number of base #{base} cannot have a digit\
|
57
|
+
greater or equal to #{base}. Check your input: #{formatted_input.reverse.join}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class WrongBaseInputError < ArgumentError; end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/reporters'
|
3
|
+
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
|
4
|
+
|
5
|
+
require_relative '../lib/to_decimal'
|
6
|
+
|
7
|
+
# ========== Data for the tests ================================================
|
8
|
+
|
9
|
+
TEST_VALUES_BASE_2 = [[101, 2, 5], [100_000_000, 2, 256]]
|
10
|
+
TEST_VALUES_BASE_3 = [[1122111, 3, 1201], [12, 3, 5]]
|
11
|
+
TEST_VALUES_BASE_4 = [[11001, 4, 321], [322, 4, 58]]
|
12
|
+
TEST_VALUES_BASE_5 = [[14, 5, 9], [10011302, 5, 78952]]
|
13
|
+
TEST_VALUES_BASE_6 = [[13, 6, 9], [1405304, 6, 78952]]
|
14
|
+
TEST_VALUES_BASE_7 = [[12, 7, 9], [446116, 7, 78952]]
|
15
|
+
TEST_VALUES_BASE_8 = [[11, 8, 9], [232150, 8, 78952]]
|
16
|
+
TEST_VALUES_BASE_9 = [[10, 9, 9], [130264, 9, 78952]]
|
17
|
+
TEST_VALUES_BASE_10 = [[9, 10, 9], [78952, 10, 78952]]
|
18
|
+
|
19
|
+
# ========== Test helper methods ===============================================
|
20
|
+
|
21
|
+
def execute_test_of_to_d(test_values)
|
22
|
+
test_values.each do |data_set|
|
23
|
+
@convertor.base = data_set[1]
|
24
|
+
assert_equal data_set[2], @convertor.to_d(data_set[0])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def execute_test_of_to_d_class_method(test_values)
|
29
|
+
test_values.each do |data_set|
|
30
|
+
assert_equal data_set[2], ToDecimal.to_d(data_set[0], base: data_set[1])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class ToDecimalConvertorTest < Minitest::Test
|
35
|
+
# ======== Setup =============================================================
|
36
|
+
|
37
|
+
def setup
|
38
|
+
@convertor = ToDecimal.new
|
39
|
+
end
|
40
|
+
|
41
|
+
# ======== Testing initialization process ====================================
|
42
|
+
|
43
|
+
def test_initialization_without_argument_has_default_base
|
44
|
+
assert_equal 10, @convertor.base
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_initialization_with_arguments_has_base
|
48
|
+
@new_convertor = ToDecimal.new(8)
|
49
|
+
assert_equal 8, @new_convertor.base
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_initialization_accepts_also_string_as_base
|
53
|
+
@new_convertor = ToDecimal.new('4')
|
54
|
+
assert_equal 4, @new_convertor.base
|
55
|
+
end
|
56
|
+
|
57
|
+
# ======== Testing accessing and modifying attributes ========================
|
58
|
+
|
59
|
+
def test_base_can_be_updated
|
60
|
+
@convertor.base = 2
|
61
|
+
assert_equal(2, @convertor.base)
|
62
|
+
@convertor.base = 8
|
63
|
+
assert_equal(8, @convertor.base)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_base_can_also_be_updated_with_a_string
|
67
|
+
@convertor.base = '4'
|
68
|
+
assert_equal 4, @convertor.base
|
69
|
+
end
|
70
|
+
|
71
|
+
# ========= Testing ArgumentError is raised with invalid base attribute ======
|
72
|
+
|
73
|
+
def test_raises_an_error_if_base_arg_is_not_an_integer_or_a_string
|
74
|
+
assert_raises(ArgumentError) { @convertor.base = [3] }
|
75
|
+
assert_raises(ArgumentError) { @convertor.base = :sym }
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_raises_an_error_if_base_arg_is_greater_than_10
|
79
|
+
assert_raises(ArgumentError) { @convertor.base = 12 }
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_raises_an_error_if_base_arg_is_smaller_than_1
|
83
|
+
assert_raises(ArgumentError) { @convertor.base = 0 }
|
84
|
+
assert_raises(ArgumentError) { @convertor.base = -4 }
|
85
|
+
end
|
86
|
+
|
87
|
+
# ======== Testing error is raises if input is not of the correct base =======
|
88
|
+
|
89
|
+
def test_to_d_raises_error_if_input_is_not_from_base_2
|
90
|
+
@convertor.base = 2
|
91
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(2) }
|
92
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_to_d_raises_error_if_input_is_not_from_base_3
|
96
|
+
@convertor.base = 3
|
97
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(3) }
|
98
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_to_d_raises_error_if_input_is_not_from_base_4
|
102
|
+
@convertor.base = 4
|
103
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(4) }
|
104
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_to_d_raises_error_if_input_is_not_from_base_5
|
108
|
+
@convertor.base = 5
|
109
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(5) }
|
110
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_to_d_raises_error_if_input_is_not_from_base_6
|
114
|
+
@convertor.base = 6
|
115
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(6) }
|
116
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_to_d_raises_error_if_input_is_not_from_base_7
|
120
|
+
@convertor.base = 7
|
121
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(7) }
|
122
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_to_d_raises_error_if_input_is_not_from_base_8
|
126
|
+
@convertor.base = 8
|
127
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(8) }
|
128
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_to_d_raises_error_if_input_is_not_from_base_9
|
132
|
+
@convertor.base = 9
|
133
|
+
assert_raises(WrongBaseInputError) { @convertor.to_d(9) }
|
134
|
+
end
|
135
|
+
|
136
|
+
# ========= Testing return values of to_d instance method ====================
|
137
|
+
|
138
|
+
def test_to_d_returns_decimal_value_of_number_of_base_2
|
139
|
+
execute_test_of_to_d(TEST_VALUES_BASE_2)
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_to_d_returns_decimal_value_of_number_of_base_3
|
143
|
+
execute_test_of_to_d(TEST_VALUES_BASE_3)
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_to_d_returns_decimal_value_of_number_of_base_4
|
147
|
+
execute_test_of_to_d(TEST_VALUES_BASE_4)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_to_d_returns_decimal_value_of_number_of_base_5
|
151
|
+
execute_test_of_to_d(TEST_VALUES_BASE_5)
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_to_d_returns_decimal_value_of_number_of_base_6
|
155
|
+
execute_test_of_to_d(TEST_VALUES_BASE_6)
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_to_d_returns_decimal_value_of_number_of_base_7
|
159
|
+
execute_test_of_to_d(TEST_VALUES_BASE_7)
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_to_d_returns_decimal_value_of_number_of_base_8
|
163
|
+
execute_test_of_to_d(TEST_VALUES_BASE_8)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_to_d_returns_decimal_value_of_number_of_base_9
|
167
|
+
execute_test_of_to_d(TEST_VALUES_BASE_9)
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_to_d_returns_decimal_value_of_number_of_base_10
|
171
|
+
execute_test_of_to_d(TEST_VALUES_BASE_10)
|
172
|
+
end
|
173
|
+
|
174
|
+
# ========= Testing aliased to_decimal instance method =======================
|
175
|
+
|
176
|
+
def test_square_brackets_is_alias_for_to_d
|
177
|
+
convertor = ToDecimal.new(10)
|
178
|
+
assert_equal(200, convertor[200])
|
179
|
+
end
|
180
|
+
|
181
|
+
# ========= Testing to_d class method accepts both string and integer args ===
|
182
|
+
|
183
|
+
def test_to_d_class_method_accepts_integers_for_both_parameters
|
184
|
+
assert_silent { ToDecimal.to_d(101, base: 2) }
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_to_d_class_method_accepts_strings_for_both_parameters
|
188
|
+
assert_silent { ToDecimal.to_d('101', base: '2') }
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_to_d_class_method_accepts_mixed_strings_and_integers_as_args
|
192
|
+
assert_silent { ToDecimal.to_d('101', base: 2) }
|
193
|
+
assert_silent { ToDecimal.to_d(101, base: '2') }
|
194
|
+
end
|
195
|
+
|
196
|
+
# ======== Testing to_d class method raises error with invalid parameters ====
|
197
|
+
|
198
|
+
def test_to_d_class_method_raises_error_if_first_arg_is_not_string_or_integer
|
199
|
+
assert_raises(ArgumentError) { ToDecimal.to_d(:sym, base: 5) }
|
200
|
+
assert_raises(ArgumentError) { ToDecimal.to_d([4], base: 5) }
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_to_d_class_method_raises_error_if_base_arg_is_not_an_integer_or_str
|
204
|
+
assert_raises(ArgumentError) { ToDecimal.to_d('222', base: [5]) }
|
205
|
+
assert_raises(ArgumentError) { ToDecimal.to_d(45, base: :sym) }
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_to_d_class_method_raises_an_error_if_base_arg_is_greater_than_10
|
209
|
+
assert_raises(ArgumentError) { ToDecimal.to_d(456, base: 12) }
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_to_d_class_method_raises_an_error_if_base_arg_is_smaller_than_1
|
213
|
+
assert_raises(ArgumentError) { ToDecimal.to_d(456, base: 0) }
|
214
|
+
assert_raises(ArgumentError) { ToDecimal.to_d(456, base: -8) }
|
215
|
+
end
|
216
|
+
|
217
|
+
# ======== Testing return values of to_d class method ========================
|
218
|
+
|
219
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_2
|
220
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_2)
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_3
|
224
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_3)
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_4
|
228
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_4)
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_5
|
232
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_5)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_6
|
236
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_6)
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_7
|
240
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_7)
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_8
|
244
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_8)
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_9
|
248
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_9)
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_to_d_class_method_returns_decimal_value_of_number_of_base_10
|
252
|
+
execute_test_of_to_d_class_method(TEST_VALUES_BASE_10)
|
253
|
+
end
|
254
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_decimal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Laurent Guinotte
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.1'
|
69
|
+
description: |
|
70
|
+
Provides a simple way to convert an integer expressed in bases
|
71
|
+
2 up to 10 to a decimal integer
|
72
|
+
email: laurentguinotte@icloud.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENCE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/to_decimal.rb
|
83
|
+
- lib/to_decimal/to_decimal_class.rb
|
84
|
+
- lib/to_decimal/wrong_base_input_error.rb
|
85
|
+
- test/to_decimal_test.rb
|
86
|
+
homepage: https://github.com/loranger32/to_decimal
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 2.5.0
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.7.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: A simple integer converter from bases 2..10 to decimal integers
|
110
|
+
test_files: []
|