summaryrefslogtreecommitdiff
path: root/test/fiddle/test_c_struct_builder.rb
blob: 187424c92d7cf63266c93eb0fbb5bb293515774c (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
# frozen_string_literal: true
begin
  require_relative 'helper'
  require 'fiddle/struct'
  require 'fiddle/cparser'
rescue LoadError
end

module Fiddle
  class TestCStructBuilder < TestCase
    include Fiddle::CParser

    def test_offsetof
      types, members = parse_struct_signature(['int64_t i','char c'])
      my_struct = Fiddle::CStructBuilder.create(Fiddle::CStruct, types, members)
      assert_equal 0, my_struct.offsetof("i")
      assert_equal Fiddle::SIZEOF_INT64_T, my_struct.offsetof("c")
    end

    def test_offset_with_gap
      types, members = parse_struct_signature(['void *p', 'char c', 'long x'])
      my_struct = Fiddle::CStructBuilder.create(Fiddle::CStruct, types, members)

      assert_equal PackInfo.align(0, ALIGN_VOIDP), my_struct.offsetof("p")
      assert_equal PackInfo.align(SIZEOF_VOIDP, ALIGN_CHAR), my_struct.offsetof("c")
      assert_equal SIZEOF_VOIDP + PackInfo.align(SIZEOF_CHAR, ALIGN_LONG), my_struct.offsetof("x")
    end

    def test_union_offsetof
      types, members = parse_struct_signature(['int64_t i','char c'])
      my_struct = Fiddle::CStructBuilder.create(Fiddle::CUnion, types, members)
      assert_equal 0, my_struct.offsetof("i")
      assert_equal 0, my_struct.offsetof("c")
    end
  end
end if defined?(Fiddle)