Ejemplo n.º 1
0
def test_alignment_of_longlong():
    ffi = FFI()
    x1 = ffi.alignof('unsigned long long')
    assert x1 in [4, 8]
    ffi.cdef("struct foo_s { unsigned long long x; };")
    lib = verify(ffi, 'test_alignment_of_longlong',
                 "struct foo_s { unsigned long long x; };")
    assert ffi.alignof('unsigned long long') == x1
    assert ffi.alignof('struct foo_s') == x1
Ejemplo n.º 2
0
def test_alignment_of_longlong():
    ffi = FFI()
    x1 = ffi.alignof('unsigned long long')
    assert x1 in [4, 8]
    ffi.cdef("struct foo_s { unsigned long long x; };")
    lib = verify(ffi, 'test_alignment_of_longlong',
                 "struct foo_s { unsigned long long x; };")
    assert ffi.alignof('unsigned long long') == x1
    assert ffi.alignof('struct foo_s') == x1
 def check(self, source, expected_ofs_y, expected_align, expected_size):
     # NOTE: 'expected_*' is the numbers expected from GCC.
     # The numbers expected from MSVC are not explicitly written
     # in this file, and will just be taken from the compiler.
     ffi = FFI()
     ffi.cdef("struct s1 { %s };" % source)
     ctype = ffi.typeof("struct s1")
     # verify the information with gcc
     ffi1 = FFI()
     ffi1.cdef(
         """
         static const int Gofs_y, Galign, Gsize;
         struct s1 *try_with_value(int fieldnum, long long value);
     """
     )
     fnames = [name for name, cfield in ctype.fields if name and cfield.bitsize > 0]
     setters = ["case %d: s.%s = value; break;" % iname for iname in enumerate(fnames)]
     lib = ffi1.verify(
         """
         struct s1 { %s };
         struct sa { char a; struct s1 b; };
         #define Gofs_y  offsetof(struct s1, y)
         #define Galign  offsetof(struct sa, b)
         #define Gsize   sizeof(struct s1)
         struct s1 *try_with_value(int fieldnum, long long value)
         {
             static struct s1 s;
             memset(&s, 0, sizeof(s));
             switch (fieldnum) { %s }
             return &s;
         }
     """
         % (source, " ".join(setters))
     )
     if sys.platform == "win32":
         expected_ofs_y = lib.Gofs_y
         expected_align = lib.Galign
         expected_size = lib.Gsize
     else:
         assert (lib.Gofs_y, lib.Galign, lib.Gsize) == (expected_ofs_y, expected_align, expected_size)
     # the real test follows
     assert ffi.offsetof("struct s1", "y") == expected_ofs_y
     assert ffi.alignof("struct s1") == expected_align
     assert ffi.sizeof("struct s1") == expected_size
     # compare the actual storage of the two
     for name, cfield in ctype.fields:
         if cfield.bitsize < 0 or not name:
             continue
         if int(ffi.cast(cfield.type, -1)) == -1:  # signed
             min_value = -(1 << (cfield.bitsize - 1))
             max_value = (1 << (cfield.bitsize - 1)) - 1
         else:
             min_value = 0
             max_value = (1 << cfield.bitsize) - 1
         for t in [1, 2, 4, 8, 16, 128, 2813, 89728, 981729, -1, -2, -4, -8, -16, -128, -2813, -89728, -981729]:
             if min_value <= t <= max_value:
                 self._fieldcheck(ffi, lib, fnames, name, t)
Ejemplo n.º 4
0
 def check(self, source, expected_ofs_y, expected_align, expected_size):
     # NOTE: 'expected_*' is the numbers expected from GCC.
     # The numbers expected from MSVC are not explicitly written
     # in this file, and will just be taken from the compiler.
     ffi = FFI()
     ffi.cdef("struct s1 { %s };" % source)
     ctype = ffi.typeof("struct s1")
     # verify the information with gcc
     ffi1 = FFI()
     ffi1.cdef("""
         static const int Gofs_y, Galign, Gsize;
         struct s1 *try_with_value(int fieldnum, long long value);
     """)
     fnames = [name for name, cfield in ctype.fields
                    if name and cfield.bitsize > 0]
     setters = ['case %d: s.%s = value; break;' % iname
                for iname in enumerate(fnames)]
     lib = ffi1.verify("""
         struct s1 { %s };
         struct sa { char a; struct s1 b; };
         #define Gofs_y  offsetof(struct s1, y)
         #define Galign  offsetof(struct sa, b)
         #define Gsize   sizeof(struct s1)
         struct s1 *try_with_value(int fieldnum, long long value)
         {
             static struct s1 s;
             memset(&s, 0, sizeof(s));
             switch (fieldnum) { %s }
             return &s;
         }
     """ % (source, ' '.join(setters)))
     if sys.platform == 'win32':
         expected_ofs_y = lib.Gofs_y
         expected_align = lib.Galign
         expected_size  = lib.Gsize
     else:
         assert (lib.Gofs_y, lib.Galign, lib.Gsize) == (
             expected_ofs_y, expected_align, expected_size)
     # the real test follows
     assert ffi.offsetof("struct s1", "y") == expected_ofs_y
     assert ffi.alignof("struct s1") == expected_align
     assert ffi.sizeof("struct s1") == expected_size
     # compare the actual storage of the two
     for name, cfield in ctype.fields:
         if cfield.bitsize < 0 or not name:
             continue
         if int(ffi.cast(cfield.type, -1)) == -1:   # signed
             min_value = -(1 << (cfield.bitsize-1))
             max_value = (1 << (cfield.bitsize-1)) - 1
         else:
             min_value = 0
             max_value = (1 << cfield.bitsize) - 1
         for t in [1, 2, 4, 8, 16, 128, 2813, 89728, 981729,
                  -1,-2,-4,-8,-16,-128,-2813,-89728,-981729]:
             if min_value <= t <= max_value:
                 self._fieldcheck(ffi, lib, fnames, name, t)
Ejemplo n.º 5
0
def test_include_5():
    ffi1 = FFI()
    ffi1.cdef("typedef struct { int x[2]; int y; } *mystruct_p;")
    verify(ffi1, "test_include_5_parent",
           "typedef struct { int x[2]; int y; } *mystruct_p;")
    ffi = FFI()
    ffi.include(ffi1)
    ffi.cdef("mystruct_p ff5(mystruct_p);")
    lib = verify(ffi, "test_include_5",
        "typedef struct {int x[2]; int y; } *mystruct_p; //usually #include\n"
        "mystruct_p ff5(mystruct_p p) { p->x[1] += 42; return p; }")
    assert ffi.alignof(ffi.typeof("mystruct_p").item) == 4
    assert ffi1.typeof("mystruct_p") is ffi.typeof("mystruct_p")
    p = ffi.new("mystruct_p", [[5, 10], -17])
    q = lib.ff5(p)
    assert q == p
    assert p.x[0] == 5
    assert p.x[1] == 52
    assert p.y == -17
    assert ffi.alignof(ffi.typeof(p[0])) == 4
Ejemplo n.º 6
0
def test_include_5():
    ffi1 = FFI()
    ffi1.cdef("typedef struct { int x[2]; int y; } *mystruct_p;")
    verify(ffi1, "test_include_5_parent",
           "typedef struct { int x[2]; int y; } *mystruct_p;")
    ffi = FFI()
    ffi.include(ffi1)
    ffi.cdef("mystruct_p ff5(mystruct_p);")
    lib = verify(ffi, "test_include_5",
        "typedef struct {int x[2]; int y; } *mystruct_p; //usually #include\n"
        "mystruct_p ff5(mystruct_p p) { p->x[1] += 42; return p; }")
    assert ffi.alignof(ffi.typeof("mystruct_p").item) == 4
    assert ffi1.typeof("mystruct_p") is ffi.typeof("mystruct_p")
    p = ffi.new("mystruct_p", [[5, 10], -17])
    q = lib.ff5(p)
    assert q == p
    assert p.x[0] == 5
    assert p.x[1] == 52
    assert p.y == -17
    assert ffi.alignof(ffi.typeof(p[0])) == 4
Ejemplo n.º 7
0
def test_alignof():
    ffi = FFI()
    assert ffi.alignof("int[51]") == 4            # unicode literal
Ejemplo n.º 8
0
def test_alignof():
    ffi = FFI()
    assert ffi.alignof("int[51]") == 4  # unicode literal