require 'test/unit'
require 'fox'

include Fox

class TC_FXMemoryStream < Test::Unit::TestCase

  private
  
  def assert_closed(stream)
    stream.open(nil, FXStreamSave) == false
  end

  public

  def test_open
    s1 = FXMemoryStream.new
    assert(s1.open(nil, FXStreamLoad))
    assert_equal(1, s1.space)
    s1.close

    s2 = FXMemoryStream.new
    assert(s2.open(nil, FXStreamSave))
    assert_equal(1, s2.space)
    s2.close

    s3 = FXMemoryStream.new
    assert(s3.open("foo", FXStreamLoad))
    assert_equal(3, s3.space)
    s3.close

    s4 = FXMemoryStream.new
    assert(s4.open("foo", FXStreamSave))
    assert_equal(3, s4.space)
    s4.close
  end

  def test_open_s
    s1 = FXMemoryStream.open(nil, FXStreamLoad)
    assert_equal(1, s1.space)
    s1.close

    s2 = FXMemoryStream.open(nil, FXStreamSave)
    assert_equal(1, s2.space)
    s2.close

    s3 = FXMemoryStream.open("foo", FXStreamLoad)
    assert_equal(3, s3.space)
    s3.close

    s4 = FXMemoryStream.open("foo", FXStreamSave)
    assert_equal(3, s4.space)
    s4.close
  end

  def test_open_s_with_block
    stream = nil
    FXMemoryStream.open("foo", FXStreamLoad) do |s|
      stream = s
    end
    assert_closed(stream)
  end

  def test_setSpace
    FXMemoryStream.open(nil, FXStreamSave) do |stream|
      stream.space = 5000
      assert_equal(5000, stream.space)
    end
  end

  def test_takeBuffer_empty
    FXMemoryStream.open(nil, FXStreamSave) do |stream|
      buffer = stream.takeBuffer
      assert_equal("\0", buffer)
    end
  end

  def test_giveBuffer
    FXMemoryStream.open(nil, FXStreamLoad) do |stream|
      assert_equal(1, stream.space)
      stream.giveBuffer("foo")
      assert_equal(3, stream.space)
    end
  end
end
