def testDuplicate(self): b = Buffer(1024) c = b.duplicate() self.assertEqual(b.position, c.position) self.assertEqual(b.limit, c.limit) self.assertEqual(b.capacity, c.capacity) b.write_bytes('test') #check that position is independend from c self.assertNotEqual(b.position, c.position) self.assertEqual(b.limit, c.limit) self.assertEqual(b.capacity, c.capacity) #but data buffer should be shared with c, so c should be able to read it self.assertEquals('test', c.read_bytes(4)) #if we delete b, c should still be able to reference the buffer, (it will keep b around #until it itself is released del b c.clear() self.assertEquals('test', c.read_bytes(4)) del c #this releases buffer b as well, we cannot test this, but this should not crash :-)
def testString1(self): S = 'Henk\0Punt' b = Buffer(1024) self.assertEqual(0, b.position) self.assertEqual(1024, b.limit) b.write_bytes(S) self.assertEqual(9, b.position) self.assertEqual(1024, b.limit) b.flip() self.assertEqual(0, b.position) self.assertEqual(9, b.limit)
def testBytes(self): #test that we can write all bytes into buffer #and get them back without any encoding/decoding stuff going on #a string with all possible bytes B = ''.join([chr(i) for i in range(256)]) b = Buffer(1024) b.write_bytes(B) b.flip() self.assertEqual(256, b.limit) x = b.read_bytes(256) self.assertEquals(B, x)
def testReadBytesUntil(self): b = Buffer(1024) b.write_bytes('hello world!') b.flip() self.assertEquals('hello', b.read_bytes_until(ord(' '))) self.assertEquals('world', b.read_bytes_until(ord('!'))) b = Buffer(1024) b.write_bytes('hello world!\nTest\n\nPiet\nKlaas\n') b.flip() self.assertEquals('hello world!', b.read_bytes_until(10)) self.assertEquals('Test', b.read_bytes_until(10)) self.assertEquals('', b.read_bytes_until(10)) self.assertEquals('Piet', b.read_bytes_until(10)) self.assertEquals('Klaas', b.read_bytes_until(10)) try: b.read_bytes_until(10) self.fail("expected buffer underflow") except BufferUnderflowError: pass
def testReadLine(self): b = Buffer(1024) b.clear() b.write_bytes('hello world!\n') b.flip() self.assertEquals('hello world!', b.read_line()) self.assertEquals(b.position, b.limit) b.clear() b.write_bytes('hello world!\r\n') b.flip() self.assertEquals('hello world!', b.read_line()) self.assertEquals(b.position, b.limit) b.clear() b.write_bytes('hello world!\r') b.flip() try: b.read_line() self.fail('expected BufferUnderFlow') except BufferUnderflowError: pass b.clear() b.write_bytes('hello world!\n') b.flip() self.assertEquals('hello world!\n', b.read_line(True)) self.assertEquals(b.position, b.limit) b.clear() b.write_bytes('hello world!\r\n') b.flip() self.assertEquals('hello world!\r\n', b.read_line(True)) self.assertEquals(b.position, b.limit) b.clear() b.write_bytes('hello world!\r') b.flip() try: b.read_line(True) self.fail('expected BufferUnderFlow') except BufferUnderflowError: pass b.clear() b.write_bytes('line1\nline2\r\nline3\nline4\r\n') b.flip() self.assertEquals('line1', b.read_line()) self.assertEquals('line2', b.read_line()) self.assertEquals('line3', b.read_line()) self.assertEquals('line4', b.read_line()) self.assertEquals(b.position, b.limit) b.clear() b.write_bytes('line1\nline2\r\nline3\nline4\r\n') b.flip() self.assertEquals('line1\n', b.read_line(True)) self.assertEquals('line2\r\n', b.read_line(True)) self.assertEquals('line3\n', b.read_line(True)) self.assertEquals('line4\r\n', b.read_line(True)) self.assertEquals(b.position, b.limit)