Skip to content
This repository was archived by the owner on Apr 23, 2022. It is now read-only.

Commit b50e780

Browse files
committed
more tests and tweak _xdelta3.c
1 parent 969c33e commit b50e780

File tree

6 files changed

+56
-27
lines changed

6 files changed

+56
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
env/
3+
env35/
34
*.py[cod]
45
*.egg-info/
56
build/

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Fast delta encoding in python using xdelta3.
88
Requirements
99
------------
1010

11-
* **Python 3.5 or 3.6** - it's 2017: you should be using python 3.6 by now anyway.
11+
* **Python 3.5 or 3.6** - it's 2017, you should be using python 3.6 by now anyway.
1212
* **linux** - compilation only tested on ubuntu, might work on other platform.
1313

1414
Installation
@@ -38,8 +38,8 @@ How fast?
3838

3939
*xdelta3-python* is a thin wrapper around `xdelta 3.1.1 <https://github.com/jmacd/xdelta/>`_
4040
which is a highly optimised c library for delta calculation and compression.
41-
It can encode a delta and decode it again for 5 small changes in a 5.4M character string
42-
(the complete works of shakespeare) in around 30ms. Boom.
41+
It can encode a delta and decode it again for 5 small changes in a 5.5 million character string
42+
(the complete works of shakespeare) in around 10ms (or 30ms with the highest compression level). Boom.
4343
See `performance.py <https://github.com/samuelcolvin/xdelta3-python/blob/master/performance.py>`_.
4444

4545
.. |BuildStatus| image:: https://travis-ci.org/samuelcolvin/xdelta3-python.svg?branch=master

performance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
v2 = Path('shakespeare_changed.txt').read_bytes()
1818

1919
times = []
20-
for i in range(10):
20+
for i in range(50):
2121
start = time()
22-
delta = xdelta3.encode(v1, v2)
22+
delta = xdelta3.encode(v1, v2, xdelta3.Flags.COMPLEVEL_1)
2323
v22 = xdelta3.decode(v1, delta)
2424
time_taken = (time() - start) * 1000
2525
times.append(time_taken)

tests/test_main.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1+
import base64
2+
import os
3+
import string
4+
15
import pytest
26

37
import xdelta3
48

59
value_one = b'this is a short string to test with. It is suitable for delta encoding.'
610
value_two = b'this is a different short string to test with. It is suitable for delta encoding.'
11+
expected_delta = b'\xd6\xc3\xc4\x00\x00\x01G\x00\x14Q\x00\t\x04\x02different\x1a\n\x13>\x00\t'
712

813

914
def test_encode_decode():
1015
delta = xdelta3.encode(value_one, value_two)
16+
assert delta == expected_delta
1117
value_two2 = xdelta3.decode(value_one, delta)
1218
assert value_two == value_two2
1319

1420

15-
def test_encode():
16-
result = xdelta3.encode(value_one, value_two)
17-
assert result == b'\xd6\xc3\xc4\x00\x00\x01G\x00\x14Q\x00\t\x04\x02different\x1a\n\x13>\x00\t'
21+
def test_long_random():
22+
v1 = base64.b32encode(os.urandom(1000))
23+
v2 = b'x' + v1 + b'x'
24+
delta = xdelta3.encode(v1, v2)
25+
v22 = xdelta3.decode(v1, delta)
26+
assert v2 == v22
27+
28+
29+
def test_decode_error():
30+
with pytest.raises(xdelta3.XDeltaError) as exc_info:
31+
xdelta3.decode(expected_delta, value_one)
32+
assert exc_info.value.args[0] == 'Error occur executing xdelta3: XD3_INVALID_INPUT'
1833

1934

2035
def test_no_delta():
@@ -23,6 +38,20 @@ def test_no_delta():
2338
assert exc_info.value.args[0] == 'No delta found shorter than the input value'
2439

2540

41+
def test_different_compression():
42+
all_ascii = (string.ascii_letters + string.digits).encode()
43+
v1 = all_ascii * 1000
44+
v2 = all_ascii * 900 + string.ascii_letters.encode() * 100 + all_ascii * 100
45+
delta_a = xdelta3.encode(v1, v2, xdelta3.Flags.COMPLEVEL_9)
46+
v2_a = xdelta3.decode(v1, delta_a)
47+
assert v2 == v2_a
48+
49+
delta_b = xdelta3.encode(v1, v2, xdelta3.Flags.COMPLEVEL_1)
50+
v2_b = xdelta3.decode(v1, delta_b)
51+
assert v2 == v2_b
52+
assert len(delta_a) < len(delta_b)
53+
54+
2655
def test_readme():
2756
value_one = b'wonderful string to demonstrate xdelta3, much of these two strings is the same.'
2857
value_two = b'different string to demonstrate xdelta3, much of these two strings is the same.'

xdelta3/_xdelta3.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ static PyObject * xdelta3_execute(PyObject *self, PyObject *args)
1919
source_size = (size_t)source_len;
2020
input_size = (size_t)input_len;
2121

22-
2322
if (action == 0) {
2423
// if the output would be longer than the input itself, there's no point using delta encoding
2524
output_alloc = input_size;
@@ -59,30 +58,30 @@ static PyObject * xdelta3_execute(PyObject *self, PyObject *args)
5958

6059
static PyMethodDef xdelta3_methods[] = {
6160
{"execute", xdelta3_execute, METH_VARARGS, "xdelta3 encode or decode"},
62-
{NULL, NULL, 0, NULL} /* Sentinel */
61+
{NULL, NULL, 0, NULL}
6362
};
6463

6564
static struct PyModuleDef xdelta3_module = {
66-
PyModuleDef_HEAD_INIT,
67-
"xdelta3", /* name of module */
68-
"xdelta3", /* module documentation, may be NULL */
69-
0, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
70-
xdelta3_methods
65+
PyModuleDef_HEAD_INIT,
66+
"_xdelta3",
67+
NULL,
68+
0,
69+
xdelta3_methods
7170
};
7271

7372
PyMODINIT_FUNC PyInit__xdelta3(void) {
74-
PyObject *m;
73+
PyObject *m;
7574

76-
m = PyModule_Create(&xdelta3_module);
77-
if (m == NULL)
78-
return NULL;
75+
m = PyModule_Create(&xdelta3_module);
76+
if (m == NULL)
77+
return NULL;
7978

80-
XDeltaError = PyErr_NewException("xdelta3.XDeltaError", NULL, NULL);
81-
Py_INCREF(XDeltaError);
82-
PyModule_AddObject(m, "XDeltaError", XDeltaError);
79+
XDeltaError = PyErr_NewException("xdelta3.XDeltaError", NULL, NULL);
80+
Py_INCREF(XDeltaError);
81+
PyModule_AddObject(m, "XDeltaError", XDeltaError);
8382

84-
NoDeltaFound = PyErr_NewException("xdelta3.NoDeltaFound", NULL, NULL);
85-
Py_INCREF(NoDeltaFound);
86-
PyModule_AddObject(m, "NoDeltaFound", NoDeltaFound);
87-
return m;
83+
NoDeltaFound = PyErr_NewException("xdelta3.NoDeltaFound", NULL, NULL);
84+
Py_INCREF(NoDeltaFound);
85+
PyModule_AddObject(m, "NoDeltaFound", NoDeltaFound);
86+
return m;
8887
}

xdelta3/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__all__ = ['VERSION']
44

5-
VERSION = StrictVersion('0.0.4')
5+
VERSION = StrictVersion('0.0.5')

0 commit comments

Comments
 (0)