279
279
self.assertEqual(sum(map(len, expected_lines)), end_point)
282
class TestEncodeCopyInstruction(tests.TestCase):
284
def assertCopyInstruction(self, expected, offset, length):
285
bytes = groupcompress.encode_copy_instruction(offset, length)
286
if expected != bytes:
287
self.assertEqual([hex(ord(e)) for e in expected],
288
[hex(ord(b)) for b in bytes])
290
def test_encode_no_length(self):
291
self.assertCopyInstruction('\x80', 0, None)
292
self.assertCopyInstruction('\x81\x01', 1, None)
293
self.assertCopyInstruction('\x81\x0a', 10, None)
294
self.assertCopyInstruction('\x81\xff', 255, None)
295
self.assertCopyInstruction('\x82\x01', 256, None)
296
self.assertCopyInstruction('\x83\x01\x01', 257, None)
297
self.assertCopyInstruction('\x8F\xff\xff\xff\xff', 0xFFFFFFFF, None)
298
self.assertCopyInstruction('\x8E\xff\xff\xff', 0xFFFFFF00, None)
299
self.assertCopyInstruction('\x8D\xff\xff\xff', 0xFFFF00FF, None)
300
self.assertCopyInstruction('\x8B\xff\xff\xff', 0xFF00FFFF, None)
301
self.assertCopyInstruction('\x87\xff\xff\xff', 0x00FFFFFF, None)
302
self.assertCopyInstruction('\x8F\x04\x03\x02\x01', 0x01020304, None)
304
def test_encode_no_offset(self):
305
self.assertCopyInstruction('\x90\x01', 0, 1)
306
self.assertCopyInstruction('\x90\x0a', 0, 10)
307
self.assertCopyInstruction('\x90\xff', 0, 255)
308
self.assertCopyInstruction('\xA0\x01', 0, 256)
309
self.assertCopyInstruction('\xB0\x01\x01', 0, 257)
310
self.assertCopyInstruction('\xB0\x01\x01', 0, 257)
311
self.assertCopyInstruction('\xB0\xff\xff', 0, 0xFFFF)
312
# Special case, if copy == 64KiB, then we store exactly 0
313
# Note that this puns with a copy of exactly 0 bytes, but we don't care
314
# about that, as we would never actually copy 0 bytes
315
self.assertCopyInstruction('\x80', 0, 64*1024)
317
def test_encode(self):
318
self.assertCopyInstruction('\x91\x01\x01', 1, 1)
319
self.assertCopyInstruction('\x91\x09\x0a', 9, 10)
320
self.assertCopyInstruction('\x91\xfe\xff', 254, 255)
321
self.assertCopyInstruction('\xA2\x02\x01', 512, 256)
322
self.assertCopyInstruction('\xB3\x02\x01\x01\x01', 258, 257)
323
self.assertCopyInstruction('\xB0\x01\x01', 0, 257)
324
# Special case, if copy == 64KiB, then we store exactly 0
325
# Note that this puns with a copy of exactly 0 bytes, but we don't care
326
# about that, as we would never actually copy 0 bytes
327
self.assertCopyInstruction('\x81\x0a', 10, 64*1024)
330
class TestBase128Int(tests.TestCase):
332
def assertEqualEncode(self, bytes, val):
333
self.assertEqual(bytes, groupcompress.encode_base128_int(val))
335
def assertEqualDecode(self, val, num_decode, bytes):
336
self.assertEqual((val, num_decode),
337
groupcompress.decode_base128_int(bytes))
339
def test_encode(self):
340
self.assertEqualEncode('\x01', 1)
341
self.assertEqualEncode('\x02', 2)
342
self.assertEqualEncode('\x7f', 127)
343
self.assertEqualEncode('\x80\x01', 128)
344
self.assertEqualEncode('\xff\x01', 255)
345
self.assertEqualEncode('\x80\x02', 256)
346
self.assertEqualEncode('\xff\xff\xff\xff\x0f', 0xFFFFFFFF)
348
def test_decode(self):
349
self.assertEqualDecode(1, 1, '\x01')
350
self.assertEqualDecode(2, 1, '\x02')
351
self.assertEqualDecode(127, 1, '\x7f')
352
self.assertEqualDecode(128, 2, '\x80\x01')
353
self.assertEqualDecode(255, 2, '\xff\x01')
354
self.assertEqualDecode(256, 2, '\x80\x02')
355
self.assertEqualDecode(0xFFFFFFFF, 5, '\xff\xff\xff\xff\x0f')
357
def test_decode_with_trailing_bytes(self):
358
self.assertEqualDecode(1, 1, '\x01abcdef')
359
self.assertEqualDecode(127, 1, '\x7f\x01')
360
self.assertEqualDecode(128, 2, '\x80\x01abcdef')
361
self.assertEqualDecode(255, 2, '\xff\x01\xff')
364
282
class TestGroupCompressBlock(tests.TestCase):
366
284
def make_block(self, key_to_text):