summaryrefslogtreecommitdiff
path: root/spec/ffi/type_spec.rb
blob: e880c8326158e83700b5b82592cd4ba28e7081d4 (plain)
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
68
69
70
71
72
73
74
75
76
#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#

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

describe FFI::Type do
  it 'has a memsize function', skip: RUBY_ENGINE != "ruby" do
    base_size = ObjectSpace.memsize_of(Object.new)

    size = ObjectSpace.memsize_of(FFI::Type.new(42))
    expect(size).to be > base_size
    base_size = size

    converter = Module.new do
      extend FFI::DataConverter

      def self.native_type
        @native_type_called = true
        FFI::Type::INT32
      end

      def self.to_native(val, ctx)
        @to_native_called = true
        ToNativeMap[val]
      end

      def self.from_native(val, ctx)
        @from_native_called = true
        FromNativeMap[val]
      end
    end

    size = ObjectSpace.memsize_of(FFI::Type::Mapped.new(converter))
    expect(size).to be > base_size
    base_size = size

    # Builtin types are larger as they also have a name and own ffi_type
    size = ObjectSpace.memsize_of(FFI::Type::Builtin::CHAR)
    expect(size).to be > base_size
  end

  it "should be shareable with Ractor", :ractor do
    expect(Ractor.shareable?(FFI::Type.new(5))).to eq(true)
  end

  describe :Builtin do
    it "should be shareable with Ractor", :ractor do
      expect(Ractor.shareable?(FFI::Type::INT32)).to eq(true)
    end
  end

  describe :Mapped do
    it "should be shareable with Ractor", :ractor do
      converter = Module.new do
        extend FFI::DataConverter

        def self.native_type
          FFI::Type::INT32
        end

        def self.to_native(val, ctx)
          ToNativeMap[val]
        end

        def self.from_native(val, ctx)
          FromNativeMap[val]
        end
      end
      expect(Ractor.shareable?(converter)).to eq(true)
      type = FFI::Type::Mapped.new(converter)
      expect(Ractor.shareable?(type)).to eq(true)
    end
  end
end