Guest User

Untitled

a guest
Dec 25th, 2010
1,402
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if __name__ == "__main__":
  2.     import random
  3.     import time
  4.  
  5.     xrange = range
  6.     try:
  7.         from StringIO import StringIO
  8.     except ImportError:
  9.         from io import BytesIO as StringIO
  10.  
  11.     # Generates random data for the test
  12.     targetdata = ''.join([chr(random.randint(0, 127)) for n in range(1<<16)])
  13.     chunks = [targetdata[i:i+2048] for i in xrange(0, 1<<17, 2048)]
  14.     for i in xrange(8):
  15.         a, b = (
  16.             random.randrange(0, len(chunks)), random.randrange(0, len(chunks)))
  17.         chunks[a], chunks[b] = chunks[b], chunks[a]
  18.  
  19.     hostdata = ''.join(chunks)
  20.  
  21.     # targetstream: file to be patched
  22.     # hoststream: what the unpatched target needs to become
  23.     # mergedstream: output after patching
  24.  
  25.     # Python 3 bytes compatibility
  26.     mergedstream = StringIO()
  27.     if __builtins__.bytes == str:
  28.         targetstream = StringIO(targetdata)
  29.         hoststream = StringIO(hostdata)
  30.     else:
  31.         targetstream = StringIO(bytes(targetdata, "ascii"))
  32.         hoststream = StringIO(bytes(hostdata, "ascii"))
  33.  
  34.  
  35.     targetchecksums = blockchecksums(targetstream)
  36.     binarypatch = rsyncdelta(hoststream, targetchecksums)
  37.     patchstream(targetstream, mergedstream, binarypatch)
  38.  
  39.     mergedstream.seek(0)
  40.     patcheddata = mergedstream.read()
  41.     if __builtins__.bytes == str:
  42.         assert patcheddata == hostdata
  43.     else:
  44.         assert str(patcheddata, 'ascii') == hostdata
  45.  
  46.     print("Test passed.")
RAW Paste Data