Commits

David Schneider committed 0e209e1

more tests for _rawffi structures using bitfields

Comments (0)

Files changed (1)

pypy/module/_rawffi/test/test__rawffi.py

             free(x1->next);
             free(x1);
         }
-        
+
         const char *static_str = "xxxxxx";
         long static_int = 42;
         double static_double = 42.42;
         long double static_longdouble = 42.42;
-        
+
         unsigned short add_shorts(short one, short two)
         {
            return one + two;
             inp.y = inp.x * 100;
             return inp;
         }
-        
+
         '''))
         symbols = """get_char char_check get_raw_pointer
                      add_shorts
         eci = ExternalCompilationInfo(export_symbols=symbols)
         return str(platform.compile([c_file], eci, 'x', standalone=False))
     prepare_c_example = staticmethod(prepare_c_example)
-    
+
     def setup_class(cls):
         space = cls.space
         from rpython.rlib.clibffi import get_libc_name
         arg.free()
         assert t.tm_year == 70
         assert t.tm_sec == 1
-        assert t.tm_min == 2      
+        assert t.tm_min == 2
         x.free()
 
     def test_nested_structures(self):
         free_double_struct = lib.ptr("free_double_struct", ['P'], None)
         free_double_struct(res)
 
+    def test_structure_bitfields_char(self):
+        import _rawffi
+        X = _rawffi.Structure([('A', 'B', 1),
+                               ('B', 'B', 6),
+                               ('C', 'B', 1)])
+        x = X()
+        x.A = 0xf
+        x.B = 0xff
+        x.C = 0xf
+        assert x.A == 1
+        assert x.B == 63
+        assert x.C == 1
+        x.free()
+
     def test_structure_bitfields(self):
         import _rawffi
         X = _rawffi.Structure([('A', 'I', 1),
         assert (y.a, y.b, y.c) == (-1, -7, 0)
         y.free()
 
+    def test_structure_bitfields_longlong(self):
+        import _rawffi
+        Z = _rawffi.Structure([('a', 'Q', 1),
+                               ('b', 'Q', 62),
+                               ('c', 'Q', 1)])
+        z = Z()
+        z.a, z.b, z.c = 7, 0x1000000000000001, 7
+        assert (z.a, z.b, z.c) == (1, 0x1000000000000001, 1)
+        z.free()
+
+    def test_structure_ulonglong_bitfields(self):
+        import _rawffi
+        X = _rawffi.Structure([('A', 'Q', 1),
+                               ('B', 'Q', 62),
+                               ('C', 'Q', 1)])
+        x = X()
+        x.A, x.B, x.C = 7, 0x1000000000000001, 7
+        assert x.A == 1
+        assert x.B == 0x1000000000000001
+        assert x.C == 1
+        x.free()
+
+    def test_structure_longlong_bitfields(self):
+        import _rawffi
+        Y = _rawffi.Structure([('a', 'q', 1),
+                               ('b', 'q', 61),
+                               ('c', 'q', 1)])
+        y = Y()
+        y.a, y.b, y.c = 0, -7, 0
+        assert (y.a, y.b, y.c) == (0, -7, 0)
+        y.free()
+
+    def test_structure_ulonglong_bitfields(self):
+        import _rawffi
+        X = _rawffi.Structure([('A', 'Q', 1),
+                               ('B', 'Q', 62),
+                               ('C', 'Q', 1)])
+        x = X()
+        x.A, x.B, x.C = 7, 0x1000000000000001, 7
+        assert x.A == 1
+        assert x.B == 0x1000000000000001
+        assert x.C == 1
+        x.free()
+
+    def test_structure_longlong_bitfields(self):
+        import _rawffi
+        Y = _rawffi.Structure([('a', 'q', 1),
+                               ('b', 'q', 61),
+                               ('c', 'q', 1)])
+        y = Y()
+        y.a, y.b, y.c = 0, -7, 0
+        assert (y.a, y.b, y.c) == (0, -7, 0)
+        y.free()
+
     def test_invalid_bitfields(self):
         import _rawffi
         raises(TypeError, _rawffi.Structure, [('A', 'c', 1)])
         res = pass_ll(arg1)
         assert res[0] == 1<<42
         arg1.free()
-    
+
     def test_callback(self):
         import _rawffi
         import struct
         assert res.y == 33
         assert s2h.x == 7
         assert s2h.y == 11
-        
+
         s2h.free()
 
     def test_ret_struct_containing_array(self):
 
 class AppTestAutoFree:
     spaceconfig = dict(usemodules=['_rawffi', 'struct'])
-    
+
     def setup_class(cls):
         cls.w_sizes_and_alignments = cls.space.wrap(dict(
             [(k, (v.c_size, v.c_alignment)) for k,v in TYPEMAP.iteritems()]))