File: union_spec.rb

package info (click to toggle)
ruby-ffi 1.9.6debian-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,376 kB
  • ctags: 2,126
  • sloc: ansic: 7,471; ruby: 5,585; sh: 24; makefile: 14
file content (67 lines) | stat: -rw-r--r-- 1,972 bytes parent folder | download | duplicates (5)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# 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
    expect(LibTest::TestUnion.members.all? { |m| LibTest::TestUnion.offset_of(m) == 0 }).to 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'
        expect((@u[type[1]] - LibTest.send("union_align_#{k}", @u.to_ptr)).abs).to be < 0.00001
      else
        expect(@u[type[1]]).to eq(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'
        expect((@u[type[1]] - type[2]).abs).to be < 0.00001
      else
        expect(@u[type[1]]).to eq(type[2])
      end
    end
  end

  it 'should return a size equals to the size of the biggest field' do
    expect(LibTest::TestUnion.size).to eq(LibTest.union_size)
  end
end