1require_relative 'base'
2
3class TestMkmf
4  class TestSizeof < TestMkmf
5    def setup
6      super
7      @sizeof_short = config_value("SIZEOF_SHORT").to_i
8      @sizeof_int = config_value("SIZEOF_INT").to_i
9      @sizeof_long = config_value("SIZEOF_LONG").to_i
10      @sizeof_long_long = config_value("SIZEOF_LONG_LONG").to_i
11      @sizeof___int64 = config_value("SIZEOF___INT64").to_i
12    end
13
14    def test_sizeof_builtin
15      %w[char short int long float double void*].each do |type|
16        assert_kind_of(Integer, mkmf {check_sizeof(type)}, MKMFLOG)
17      end
18      assert_operator(@sizeof_short, :<=, @sizeof_int)
19      assert_operator(@sizeof_int, :<=, @sizeof_long)
20      assert_operator(@sizeof_long, :<=, @sizeof_long_long) unless @sizeof_long_long.zero?
21      assert_equal(8, @sizeof___int64) unless @sizeof___int64.zero?
22    end
23
24    def test_sizeof_struct
25      open("confdefs.h", "w") {|f|
26        f.puts "typedef struct {char x;} test1_t;"
27      }
28      assert_equal(1, mkmf {check_sizeof("test1_t", "confdefs.h")}, MKMFLOG)
29
30      open("confdefs.h", "w") {|f|
31        f.puts "typedef struct {char x, y;} test1_t;"
32      }
33      assert_equal(2, mkmf {check_sizeof("test1_t", "confdefs.h")}, MKMFLOG)
34
35      open("confdefs.h", "w") {|f|
36        f.puts "typedef struct {int x;} test1_t;"
37      }
38      assert_equal(@sizeof_int, mkmf {check_sizeof("test1_t", "confdefs.h")}, MKMFLOG)
39      open("confdefs.h", "w") {|f|
40        f.puts "typedef struct {int x, y;} test1_t;"
41      }
42      assert_equal(2 * @sizeof_int, mkmf {check_sizeof("test1_t", "confdefs.h")}, MKMFLOG)
43    ensure
44      File.unlink("confdefs.h")
45    end
46  end
47end
48