1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/ruby/test_string.rb
shyouhei e19bd3eaa8 -This line, and those below, will be ignored--
M    ruby_1_8_7/ChangeLog
M    ruby_1_8_7/inits.c
M    ruby_1_8_7/version.h
M    ruby_1_8_7/string.c
M    ruby_1_8_7/st.c
M    ruby_1_8_7/test/ruby/test_string.rb
M    ruby_1_8_7/random.c


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@34151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-12-28 12:47:15 +00:00

45 lines
1 KiB
Ruby

require 'test/unit'
require File.expand_path('envutil', File.dirname(__FILE__))
class TestString < Test::Unit::TestCase
def check_sum(str, bits=16)
sum = 0
str.each_byte {|c| sum += c}
sum = sum & ((1 << bits) - 1) if bits != 0
assert_equal(sum, str.sum(bits))
end
def test_sum
assert_equal(0, "".sum)
assert_equal(294, "abc".sum)
check_sum("abc")
check_sum("\x80")
0.upto(70) {|bits|
check_sum("xyz", bits)
}
end
def test_inspect
original_kcode = $KCODE
$KCODE = 'n'
assert_equal('"\343\201\202"', "\xe3\x81\x82".inspect)
$KCODE = 'u'
assert_equal("\"\343\201\202\"", "\xe3\x81\x82".inspect)
assert_no_match(/\0/, "\xe3\x81".inspect, '[ruby-dev:39550]')
ensure
$KCODE = original_kcode
end
def test_hash_random
str = 'abc'
a = [str.hash.to_s]
cmd = sprintf("%s -e 'print %s.hash'", EnvUtil.rubybin, str.dump)
3.times {
IO.popen(cmd, "rb") {|o|
a << o.read
}
}
assert_not_equal([str.hash.to_s], a.uniq)
end
end