1#! /usr/bin/env python
2##
3## vi:ts=4:et
4##
5##---------------------------------------------------------------------------##
6##
7## This file is part of the LZO real-time data compression library.
8##
9## Copyright (C) 1998-2002 Markus Franz Xaver Johannes Oberhumer
10## All Rights Reserved.
11##
12## The LZO library is free software; you can redistribute it and/or
13## modify it under the terms of the GNU General Public License as
14## published by the Free Software Foundation; either version 2 of
15## the License, or (at your option) any later version.
16##
17## The LZO library is distributed in the hope that it will be useful,
18## but WITHOUT ANY WARRANTY; without even the implied warranty of
19## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20## GNU General Public License for more details.
21##
22## You should have received a copy of the GNU General Public License
23## along with the LZO library; see the file COPYING.
24## If not, write to the Free Software Foundation, Inc.,
25## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26##
27## Markus F.X.J. Oberhumer
28## <markus@oberhumer.com>
29## http://www.oberhumer.com/opensource/lzo/
30##
31##---------------------------------------------------------------------------##
32
33from __future__ import print_function
34
35import sys, string
36
37# update sys.path when running in the build directory
38from tests.util import get_sys_path
39sys.path = get_sys_path()
40
41import lzo
42
43
44# ***********************************************************************
45#  a very simple test driver...
46# ***********************************************************************
47
48def print_modinfo():
49    #print sys.modules
50    mod = sys.modules['lzo']
51    #print mod
52    d = mod.__dict__
53    for k in d.keys():
54        print(k, d[k])
55
56
57def gen(src, level=1):
58    a0 = lzo.adler32(src)
59    c =  lzo.compress(src, level)
60    u1 = lzo.decompress(c)
61    a1 = lzo.adler32(u1)
62    o =  lzo.optimize(c)
63    u2 = lzo.decompress(o)
64    a2 = lzo.adler32(u2)
65    if src != u1:
66        raise lzo.error("internal error 1: %r %r", src, u1)
67    if src != u2:
68        raise lzo.error("internal error 1: %r %r",  src, u2)
69    if a0 != a1 or a0 != a2:
70        raise lzo.error("internal error 2")
71    print("compressed %6d -> %6d" % (len(src), len(c)))
72
73
74def gen_raw(src, level=1):
75    a0 = lzo.adler32(src)
76    c =  lzo.compress(src, level, False)
77    u1 = lzo.decompress(c, False, len(src))
78    a1 = lzo.adler32(u1)
79    o =  lzo.optimize(c, False, len(src))
80    u2 = lzo.decompress(o, False, len(src))
81    a2 = lzo.adler32(u2)
82    # make sure it still works when you overstate the output buffer length
83    u3 = lzo.decompress(c, False, len(src) + 100)
84    if src != u1 or src != u2 or src != u3:
85        raise lzo.error("internal error 1")
86    if a0 != a1 or a0 != a2:
87        raise lzo.error("internal error 2")
88    print("compressed %6d -> %6d" % (len(src), len(c)))
89
90def test_version():
91    import pkg_resources
92    pkg_version = pkg_resources.require("python-lzo")[0].version
93    mod_version = lzo.__version__.decode('utf-8')
94    assert pkg_version == mod_version, \
95        "%r != %r" %(pkg_version, mod_version)
96
97def test_lzo():
98    yield gen, b"aaaaaaaaaaaaaaaaaaaaaaaa"
99    yield gen, b"abcabcabcabcabcabcabcabc"
100    yield gen, b"abcabcabcabcabcabcabcabc", 9
101
102
103def test_lzo_raw():
104    yield gen_raw, b"aaaaaaaaaaaaaaaaaaaaaaaa"
105    yield gen_raw, b"abcabcabcabcabcabcabcabc"
106    yield gen_raw, b"abcabcabcabcabcabcabcabc", 9
107
108
109def test_lzo_empty():
110    yield gen, b""
111    yield gen_raw, b""
112
113
114def test_lzo_big():
115    gen(b" " * 131072)
116
117
118def test_lzo_raw_big():
119    gen_raw(b" " * 131072)
120
121
122if sys.maxsize > 1<<32:
123    # This test raises OverflowError on 32-bit Pythons. Compressing
124    # this much data requires a 64-bit system.
125    def test_lzo_compress_extremely_big():
126        b = lzo.compress(bytes(bytearray((1024**3)*2)))
127