source: nikanabo/current/xdelta/diy/xdelta3-test.py@ 870

Last change on this file since 870 was 185, checked in by geyser, 18 years ago
File size: 4.4 KB
RevLine 
[185]1#!/usr/bin/python2.4
2# xdelta 3 - delta compression tools and library
3# Copyright (C) 2003, 2006, 2007. Joshua P. MacDonald
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19import xdelta3
20
21# the test data section is expected to be len('target')
22source = 'source source input0 source source'
23target = 'source source target source source'
24
25#
26#
27
28print 'encode: basic ...'
29result, patch = xdelta3.xd3_encode_memory(target, source, 50)
30
31assert result == 0
32assert len(patch) < len(source)
33
34print 'encode: adler32 ...'
35result, patch_adler32 = xdelta3.xd3_encode_memory(target, source, 50,
36 xdelta3.XD3_ADLER32)
37
38assert result == 0
39assert len(patch_adler32) < len(source)
40assert len(patch_adler32) > len(patch)
41
42print 'encode: secondary ...'
43result, patch_djw = xdelta3.xd3_encode_memory(target, source, 50,
44 xdelta3.XD3_SEC_DJW)
45
46assert result == 0
47# secondary compression doesn't help
48assert len(patch_djw) > len(patch)
49
50print 'encode: exact ...'
51result, ignore = xdelta3.xd3_encode_memory(target, source, len(patch))
52
53assert result == 0
54assert len(ignore) < len(source)
55
56print 'encode: out of space ...'
57result, ignore = xdelta3.xd3_encode_memory(target, source, len(patch) - 1)
58
59assert result == 28
60assert ignore == None
61
62print 'encode: zero space ...'
63result, ignore = xdelta3.xd3_encode_memory(target, source, 0)
64
65assert result == 28
66assert ignore == None
67
68print 'encode: no source ...'
69result, zdata = xdelta3.xd3_encode_memory(target, None, 50)
70
71assert result == 0
72assert len(zdata) > len(patch)
73
74print 'encode: no input ...'
75result, ignore = xdelta3.xd3_encode_memory(None, None, 50)
76
77assert result != 0
78
79print 'decode: basic ...'
80result, target1 = xdelta3.xd3_decode_memory(patch, source, len(target))
81
82assert result == 0
83assert len(target1) == len(target)
84assert target1 == target
85
86print 'decode: out of space ...'
87result, ignore = xdelta3.xd3_decode_memory(patch, source, len(target) - 1)
88
89assert result == 28
90assert ignore == None
91
92print 'decode: zero space ...'
93result, ignore = xdelta3.xd3_decode_memory(patch, source, 0)
94
95assert result == 28
96assert ignore == None
97
98print 'decode: single byte error ...'
99# a few expected single-byte errors, e.g., unused address cache bits, see
100# xdelta3-test.h's single-bit error tests
101extra_count = 4
102noverify_count = 0
103for corrupt_pos in range(len(patch_adler32)):
104 input = ''.join([j == corrupt_pos and '\xff' or patch_adler32[j]
105 for j in range(len(patch_adler32))])
106
107 result, ignore = xdelta3.xd3_decode_memory(input, source, len(target), 0)
108 assert result == -17712
109 assert ignore == None
110
111 # without adler32 verification, the error may be in the data section which
112 # in this case is 6 bytes 'target'
113 result, corrupt = xdelta3.xd3_decode_memory(input, source, len(target),
114 xdelta3.XD3_ADLER32_NOVER)
115 if result == 0:
116 noverify_count = noverify_count + 1
117 #print "got %s" % corrupt
118 #end
119#end
120assert noverify_count == len('target') + extra_count
121
122print 'decode: no source ...'
123result, target2 = xdelta3.xd3_decode_memory(zdata, None, len(target))
124
125assert result == 0
126assert target == target2
127
128# Test compression level setting via flags. assumes a 9 byte checksum
129# and that level 9 steps 2, level 1 steps 15:
130# 01234567890123456789012345678901
131# level 1 only indexes 2 checksums "abcdefghi" and "ABCDEFGHI"
132# outputs 43 vs. 23 bytes
133print 'encode: compression level ...'
134
135source = '_la_la_abcdefghi_la_la_ABCDEFGHI'
136target = 'la_la_ABCDEFGH__la_la_abcdefgh__'
137
138result1, level1 = xdelta3.xd3_encode_memory(target, source, 50, xdelta3.XD3_COMPLEVEL_1)
139result9, level9 = xdelta3.xd3_encode_memory(target, source, 50, xdelta3.XD3_COMPLEVEL_9)
140
141assert result1 == 0 and result9 == 0
142assert len(level1) > len(level9)
143
144#
145#
146
147print 'PASS'
Note: See TracBrowser for help on using the repository browser.