File: test_string.rb

package info (click to toggle)
ruby1.8 1.8.7.302-2squeeze5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 26,692 kB
  • ctags: 38,616
  • sloc: ruby: 245,002; ansic: 144,156; yacc: 5,890; sh: 2,677; lisp: 1,626; tcl: 949; makefile: 358; sed: 129; xml: 122; awk: 36; cpp: 28; asm: 25; perl: 18; python: 6
file content (32 lines) | stat: -rw-r--r-- 726 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'test/unit'

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
end