require 'logger' require 'test/unit/autorunner' class String def bytes bytes = [] self.each_byte { |b| bytes << b } bytes end def hexify result = '' each_byte do |b| result << "0x#{b.hexify} " end result.chop end end class Array def stringify res = '' self.each { |b| res << b } res end def hexify map { |b| "0x#{b && b.hexify}" }.join( ' ' ) end def binarize map { |b| "%#{b && b.binarize}" }.join( ' ' ) end end class Fixnum def bytes bytes = [] val = to_i while val > 0 bytes.insert 0, ( val & 0xFF ) val >>= 8 end bytes end def hexify result = '' val = to_i while val > 0 b = val & 0xF result = b.to_s( 16 ) + result val >>= 4 end padded result, 2 end def binarize result = '' val = to_i while val > 0 b = val & 1 result = b.to_s( 2 ) + result val >>= 1 end padded result, 8 end def padded( val, length_multiplier ) padded_length = ( val.length / length_multiplier ).ceil * length_multiplier padded_length = length_multiplier if padded_length == 0 pad_size = padded_length - val.length '0' * pad_size + val end end def hexify( data ) result = '' data.each_byte do |b| nibble = ( b >> 4 ) & 15 result << ( 0x30 + nibble ) if nibble < 10 result << ( 0x41 + nibble - 10 ) if nibble >= 10 nibble = ( b >> 0 ) & 15 result << ( 0x30 + nibble ) if nibble < 10 result << ( 0x41 + nibble - 10 ) if nibble >= 10 end result end def get_resource( basedir, relative_filename ) basedir = File.dirname basedir unless File.directory? basedir filename = File.join( basedir, relative_filename ) raise "Resource not found: #{filename}" unless File.exists? filename return filename end def open_resource( basedir, relative_filename ) File.new get_resource( basedir, relative_filename ), 'rb' end def load_resource( basedir, relative_filename ) File.open( get_resource( basedir, relative_filename ), 'rb' ) do |file| return file.read end end class NullLogger < Logger def initialize( *args ) super STDOUT @level = WARN end end NULL_LOGGER = NullLogger.new args = [ '--runner=console', '--verbose=v' ] args.concat ARGV success = Test::Unit::AutoRunner.run( true, File.dirname( $0 ), args ) exit 10 unless success