From 25960f0b5e74e01383bbe46bfca4bf7512c2fac5 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Fri, 11 Feb 2011 09:56:55 +0000 Subject: [PATCH] added base36.rb Ruby library git-svn-id: http://biopieces.googlecode.com/svn/trunk@1265 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/Maasha/lib/base36.rb | 70 ++++++++++++++++++++++++++++ code_ruby/Maasha/test/test_base36.rb | 48 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 code_ruby/Maasha/lib/base36.rb create mode 100755 code_ruby/Maasha/test/test_base36.rb diff --git a/code_ruby/Maasha/lib/base36.rb b/code_ruby/Maasha/lib/base36.rb new file mode 100644 index 0000000..9af7a3f --- /dev/null +++ b/code_ruby/Maasha/lib/base36.rb @@ -0,0 +1,70 @@ +# Copyright (C) 2011 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This software is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Error class for all exceptions to do with Base36. +class Base36Error < StandardError; end + +ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +BASE36 = 36 + +# Class containing methods to encode and decade Base 36. +# Note that the ALPH is [alph + num] and not [num + alph] +# which prevents us from simply using .to_i(36) and .to_s(36). +# +# http://en.wikipedia.org/wiki/Base_36 +class Base36 + # Method that encodes an integer into a base36 string + # that is returned. + def self.encode(num) + raise Base36Error unless num.is_a? Fixnum + + base36 = "" + + while num > 0 + base36 << ALPH[(num % BASE36)] + num /= BASE36 + end + + base36 << ALPH[0] if num == 0 + + base36.reverse + end + + # Method that decodes a base36 string and returns an integer. + def self.decode(base36) + raise Base36Error if base36.empty? + + result = 0 + pos = 0 + + base36.upcase.reverse.each_char do |char| + result += ALPH.index(char) * (BASE36 ** pos) + pos += 1 + end + + return result; + end +end + +__END__ diff --git a/code_ruby/Maasha/test/test_base36.rb b/code_ruby/Maasha/test/test_base36.rb new file mode 100755 index 0000000..c966900 --- /dev/null +++ b/code_ruby/Maasha/test/test_base36.rb @@ -0,0 +1,48 @@ +#!/usr/bin/env ruby +$:.unshift File.join(File.dirname(__FILE__),'..','lib') + +# Copyright (C) 2011 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This software is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +require 'test/unit' +require 'base36' +require 'pp' + +class Base36Test < Test::Unit::TestCase + def test_Base36_encode_with_non_fixnum_value_raises + assert_raise(Base36Error) { Base36.encode("foo") } + end + + def test_Base36_encode_returns_correctly + assert_equal("AWVHI", Base36.encode(1053908)) + end + + def test_Base36_decode_with_empty_value_raises + assert_raise(Base36Error) { Base36.decode("") } + end + + def test_Base36_decode_returns_correctly + assert_equal(1053908, Base36.decode("AWVHI")) + end +end -- 2.39.5