File: test_string_java_bytes.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (42 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download | duplicates (4)
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
33
34
35
36
37
38
39
40
41
42
require 'java'
require 'test/unit'

class TestStringJavaBytes < Test::Unit::TestCase

  def test_conversions
    # this needed to be modified to clear up the ambiguity surrounding Java's
    # byte being signed; you can only coerce values from -128..127 safely without
    # Java seeing a different value than Ruby. Value can't be lost across the
    # boundary.
    all_byte_values = Array.new(256) {|i| i - 128}

    java_bytes = all_byte_values.to_java(:byte)
    string_from_bytes = String.from_java_bytes(java_bytes)
    assert(string_from_bytes.instance_of?(String))
    assert_equal(string_from_bytes.length, java_bytes.length)
    # different sign for 0..127
    0.upto(127) do |i|
      assert_equal(all_byte_values[i] + 256, string_from_bytes[i])
    end
    # same sign for 128..255
    128.upto(255) do |i|
      assert_equal(all_byte_values[i], string_from_bytes[i])
    end
    
    bytes_from_string = string_from_bytes.to_java_bytes
    assert(bytes_from_string.instance_of?(Java::byte[]))
    assert_equal(bytes_from_string.length, string_from_bytes.length)
    0.upto(255) do |i|
      assert_equal(bytes_from_string[i], java_bytes[i])
    end
    
  end

  def test_exceptions
    assert_raises(TypeError) { String.from_java_bytes(Java::double[1].new) }
    assert_raises(TypeError) { String.from_java_bytes(3.141) }
    # must test underlying call for to_java_bytes
    assert_raises(TypeError) { JavaArrayUtilities.ruby_string_to_bytes(3.141) }
    assert_raises(TypeError) { JavaArrayUtilities.ruby_string_to_bytes( Object.new ) }
  end
end