Sha256: 4fa8c198c864d185d9c6b0fa3c6ba72fa49d65811811639d8127a3ada7cf47b2

Contents?: true

Size: 1.89 KB

Versions: 39

Compression:

Stored size: 1.89 KB

Contents

#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#

require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))

module LibTest
  Types = {
    's8' => [:char, :c, 1],
    's16' => [:short, :s, 0xff0], 
    's32' => [:int, :i, 0xff00],
    's64' => [:long_long, :j, 0xffff00],
    'long' => [:long, :l, 0xffff],
    'f32' => [:float, :f, 1.0001],
    'f64' => [:double, :d, 1.000000001]
  }
  class TestUnion < FFI::Union
    layout( :a, [:char, 10], 
            :i, :int, 
            :f, :float,
            :d, :double,
            :s, :short,
            :l, :long,
            :j, :long_long,
            :c, :char )
  end
  Types.keys.each do |k| 
    attach_function "union_align_#{k}", [ :pointer ], Types[k][0]
    attach_function "union_make_union_with_#{k}", [ Types[k][0] ], :pointer
  end
  attach_function :union_size, [], :uint
end

describe 'Union' do
  before do
    @u = LibTest::TestUnion.new
  end
  it 'should place all the fields at offset 0' do
    LibTest::TestUnion.members.all? { |m| LibTest::TestUnion.offset_of(m) == 0 }.should be_true
  end
  LibTest::Types.each do |k, type|
    it "should correctly align/write a #{type[0]} value" do
      @u[type[1]] = type[2]
      if k == 'f32' or k == 'f64'
        (@u[type[1]] - LibTest.send("union_align_#{k}", @u.to_ptr)).abs.should < 0.00001
      else
        @u[type[1]].should == LibTest.send("union_align_#{k}", @u.to_ptr)
      end
    end
  end
  LibTest::Types.each do |k, type|
    it "should read a #{type[0]} value from memory" do
      @u = LibTest::TestUnion.new(LibTest.send("union_make_union_with_#{k}", type[2]))
      if k == 'f32' or k == 'f64'
        (@u[type[1]] - type[2]).abs.should < 0.00001
      else
        @u[type[1]].should == type[2]
      end
    end
  end
  it 'should return a size equals to the size of the biggest field' do
    LibTest::TestUnion.size.should == LibTest.union_size
  end
end

Version data entries

39 entries across 37 versions & 8 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/ffi-1.9.3/spec/ffi/union_spec.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/ffi-1.9.3/spec/ffi/union_spec.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/ffi-1.9.3/spec/ffi/union_spec.rb
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/ffi-1.9.3/spec/ffi/union_spec.rb
ffi-1.9.3-x64-mingw32 spec/ffi/union_spec.rb
ffi-1.9.3-x86-mingw32 spec/ffi/union_spec.rb
ffi-1.9.3 spec/ffi/union_spec.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/ffi-1.9.0/spec/ffi/union_spec.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/union_spec.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/ffi-1.9.0/spec/ffi/union_spec.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/union_spec.rb
ffi-1.9.0-x86-mingw32 spec/ffi/union_spec.rb
ffi-1.9.0 spec/ffi/union_spec.rb
vagrant-shell-0.2.6 vendor/bundle/gems/ffi-1.8.1/spec/ffi/union_spec.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/union_spec.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/union_spec.rb
vagrant-shell-0.2.5 vendor/bundle/gems/ffi-1.8.1/spec/ffi/union_spec.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/ffi-1.3.1/spec/ffi/union_spec.rb
ffi-1.8.1-x86-mingw32 spec/ffi/union_spec.rb
ffi-1.8.1 spec/ffi/union_spec.rb