def test_setitem(self): buf = MutableStringBuffer(4) buf.setitem(0, 'A') buf.setitem(1, 'B') buf.setitem(2, 'C') buf.setitem(3, 'D') assert buf.finish() == 'ABCD'
def test_unaligned(self): # to force a non-aligned 'i' expected = struct.pack('=BBi', 0xAB, 0xCD, 0x1234) # wbuf = MutableStringBuffer(len(expected)) wbuf.setitem(0, chr(0xAB)) wbuf.setitem(1, chr(0xCD)) fake_fmtiter = self.mypack_into('i', wbuf, 0x1234, advance=2) assert fake_fmtiter.pos == wbuf.getlength() got = wbuf.finish() assert got == expected
def test_subbuffer(self): # to force a non-aligned 'i' expected = struct.pack('=BBi', 0xAB, 0xCD, 0x1234) size = len(expected) # wbuf = MutableStringBuffer(size) wsubbuf = SubBuffer(wbuf, 2, size - 4) wbuf.setitem(0, chr(0xAB)) wbuf.setitem(1, chr(0xCD)) fake_fmtiter = self.mypack_into('i', wsubbuf, 0x1234) assert fake_fmtiter.pos == wbuf.getlength( ) - 2 # -2 since it's a SubBuffer got = wbuf.finish() assert got == expected