bijective 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1e54e88636e6e344cb2c2562a744f0fc5b8206d0
4
+ data.tar.gz: 0e9ce56aeb1c7bde458a5dc9e372fceed0435d2e
5
+ SHA512:
6
+ metadata.gz: 2bbed7df489380bc49533cd3fa0edbb9928de0f4d04600137d85e53ca81320dca0380fa745241cbd7568ad302d94a423096ca36dbf9bb5d9a8d358b394ee89ec
7
+ data.tar.gz: 731c2e262747af55a7f3ee199a067c1c8a2f759941e823f6f24e07db9a02cbb5cf7d6d06ceec5158f08569d305d5859524757cf2c4ecf6ddcc8840905deecb4d
@@ -0,0 +1 @@
1
+ README.html
data/License ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Dennis Hall, http://dennishall.de
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # bijective
2
+
3
+ ## Install
4
+ gem install bijective
5
+
6
+ ## Usage
7
+ - Require the gem
8
+ - Generate a new sequence
9
+ - Create a new instance with generated sequence
10
+ - Encode or decode
11
+
12
+ ## License
13
+ See License file.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'bijective/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'bijective'
7
+ s.version = Bijective::VERSION
8
+ s.date = '2013-07-05'
9
+ s.summary = "bijective decodes and encoes via a generated character sequence"
10
+ s.description = "bijective can be used to decode strings and encode numbers"
11
+ s.authors = ["Dennis Hall"]
12
+ s.email = 'dennis@dennishall.de'
13
+ s.homepage = 'http://rubygems.org/gems/bijective'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,35 @@
1
+ require 'bijective/generator'
2
+ require 'bijective/initialization_error'
3
+
4
+ module Bijective
5
+ class Instance
6
+ def initialize sequence
7
+ dublicate = sequence.split(//).uniq.join
8
+
9
+ if dublicate.length != sequence.length
10
+ raise(InitializationError, 'Sequence string must contain only unique charaters.')
11
+ end
12
+
13
+ @sequence = sequence
14
+ @base = @sequence.length
15
+ end
16
+
17
+ # returns string
18
+ def encode i
19
+ return @sequence[0] if i == 0
20
+ s = ''
21
+ while i > 0
22
+ s << @sequence[i.modulo(@base)]
23
+ i /= @base
24
+ end
25
+ s.reverse
26
+ end
27
+
28
+ # returns integer
29
+ def decode s
30
+ i = 0
31
+ s.each_char { |c| i = i * @base + @sequence.index(c) }
32
+ i
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module Bijective
2
+ class Generator
3
+ def self.generate_sequence
4
+ upcase_letters = ('A'..'Z').to_a
5
+ downcase_letters = ('a'..'z').to_a
6
+ numbers = (0..9).to_a
7
+ (upcase_letters + downcase_letters + numbers).shuffle.join
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Bijective
2
+ class InitializationError < Exception
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Bijective
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require 'bijective'
3
+ require 'bijective/generator'
4
+ require 'bijective/initialization_error'
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class BijectiveTest < Test::Unit::TestCase
4
+ def test_encode
5
+ set_sequence
6
+ h = { '7' => 0,
7
+ 'o' => 10,
8
+ '1U' => 100,
9
+ 'pl' => 1000,
10
+ '3Cv' => 10000,
11
+ 'J7g' => 100000 }
12
+ h.each { |k, v| assert_equal(k, @bijective.encode(v)) }
13
+ end
14
+
15
+ def test_decode
16
+ set_sequence
17
+ h = { 0 => '7',
18
+ 10 => 'o',
19
+ 100 => '1U',
20
+ 1000 => 'pl',
21
+ 10000 => '3Cv',
22
+ 100000 => 'J7g' }
23
+ h.each { |k, v| assert_equal(k, @bijective.decode(v)) }
24
+ end
25
+
26
+ private
27
+
28
+ def set_sequence
29
+ sequence = '713z6iE2lKokr5TLpHvwtQ9mZRJWcNbduBnqeCUfxDYGSXPVhj4asFyIgM8A0O'
30
+ @bijective = Bijective::Instance.new(sequence)
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class GeneratorTest < Test::Unit::TestCase
4
+ def test_generate_sequence
5
+ sequence = Bijective::Generator.generate_sequence
6
+ assert_equal(62, sequence.split(//).uniq.join.length)
7
+
8
+ sequence2 = Bijective::Generator.generate_sequence
9
+ assert_not_equal(sequence, sequence2)
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class InitializationErrorTest < Test::Unit::TestCase
4
+ def test_initialization_error
5
+ assert_raise Bijective::InitializationError do
6
+ bijective = Bijective::Instance.new('StringWithDuplicatedCharacters')
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bijective
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Hall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: bijective can be used to decode strings and encode numbers
14
+ email: dennis@dennishall.de
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - .gitignore
20
+ - License
21
+ - README.md
22
+ - Rakefile
23
+ - bijective.gemspec
24
+ - lib/bijective.rb
25
+ - lib/bijective/generator.rb
26
+ - lib/bijective/initialization_error.rb
27
+ - lib/bijective/version.rb
28
+ - test/helper.rb
29
+ - test/test_bijective.rb
30
+ - test/test_generator.rb
31
+ - test/test_initialization_error.rb
32
+ homepage: http://rubygems.org/gems/bijective
33
+ licenses: []
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.0.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: bijective decodes and encoes via a generated character sequence
55
+ test_files:
56
+ - test/helper.rb
57
+ - test/test_bijective.rb
58
+ - test/test_generator.rb
59
+ - test/test_initialization_error.rb