From: martinahansen Date: Mon, 7 Mar 2011 09:46:22 +0000 (+0000) Subject: added bits.rb and test_bits.rb X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=cd99566b458ea72aa29bf0a98e2259f8f73da98f;p=biopieces.git added bits.rb and test_bits.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1280 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/bits.rb b/code_ruby/Maasha/lib/bits.rb new file mode 100644 index 0000000..f6e52c8 --- /dev/null +++ b/code_ruby/Maasha/lib/bits.rb @@ -0,0 +1,76 @@ +# Copyright (C) 2007-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). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Monkey patching Class String to add bitwise operators. +# Behaviour matching Perl's: +# http://perldoc.perl.org/perlop.html#Bitwise-String-Operators +class String + # Method that performs bitwise AND operation where bits + # are copied if they exists in BOTH operands. If the operand + # sizes are different, the & operator methods acts as though + # the longer operand were truncated to the length of the shorter. + def &(str) + new = "" + + (0 ... [self.length, str.length].min).each do |i| + new << (self[i].ord & str[i].ord) + end + + new + end + + # Method that performs bitwise OR operation where bits + # are copied if they exists in EITHER operands. If the operand + # sizes differ, the shorter operand is extended with the terminal + # part of the longer operand. + def |(str) + new = "" + + min = [self.length, str.length].min + + (0 ... min).each do |i| + new << (self[i].ord | str[i].ord) + end + + if self.length > str.length + new << self[min ... self.length] + elsif self.length < str.length + new << str[min ... str.length] + end + + new + end + + # Method that performs bitwise XOR operation where bits + # are copied if they exists in ONE BUT NOT BOTH operands. + def ^(str) + new = "" + + (0 ... [self.length, str.length].min).each do |i| + new << (self[i].ord ^ str[i].ord) + end + + new + end +end diff --git a/code_ruby/Maasha/test/test_bits.rb b/code_ruby/Maasha/test/test_bits.rb new file mode 100755 index 0000000..f9d5824 --- /dev/null +++ b/code_ruby/Maasha/test/test_bits.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require 'bits' +require 'test/unit' +require 'pp' + +class TestBits < Test::Unit::TestCase + def test_Bits_AND_with_equal_length_returns_correctly + assert_equal("ABCD", "abcd" & "____") + end + + def test_Bits_AND_with_unequal_length_returns_correctly + assert_equal("JAPH\n", "japh\nJunk" & '_____') + assert_equal("JAPH\n", '_____' & "japh\nJunk") + end + + def test_Bits_OR_with_equal_length_returns_correctly + assert_equal("abcd", "ab " | " cd") + end + + def test_Bits_OR_with_unequal_length_returns_correctly + assert_equal("japh\n", "JA" | " ph\n") + assert_equal("japh\n", " ph\n" | "JA") + end + + def test_Bits_XOR_with_equal_length_returns_correctly + assert_equal("ABCD", "ab " ^ " cd") + end + + def test_Bits_XOR_with_unequal_length_returns_correctly + assert_equal("JAPH", "j p \n" ^ " a h") + assert_equal("JAPH", " a h" ^ "j p \n") + end +end