File: test_lzo.py

package info (click to toggle)
python-lzo 1.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: ansic: 505; python: 200; makefile: 27
file content (151 lines) | stat: -rw-r--r-- 4,989 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env python
##
## vi:ts=4:et
##
##---------------------------------------------------------------------------##
##
## This file is part of the LZO real-time data compression library.
##
## Copyright (C) 1998-2002 Markus Franz Xaver Johannes Oberhumer
## All Rights Reserved.
##
## The LZO library is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of
## the License, or (at your option) any later version.
##
## The LZO library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the LZO library; see the file COPYING.
## If not, write to the Free Software Foundation, Inc.,
## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
## Markus F.X.J. Oberhumer
## <markus@oberhumer.com>
## http://www.oberhumer.com/opensource/lzo/
##
##---------------------------------------------------------------------------##

from __future__ import print_function
import inspect
import pytest
import sys, string

# update sys.path when running in the build directory
from tests.util import get_sys_path
sys.path = get_sys_path()

import lzo


# ***********************************************************************
#  a very simple test driver...
# ***********************************************************************

def print_modinfo():
    #print sys.modules
    mod = sys.modules['lzo']
    #print mod
    d = mod.__dict__
    for k in d.keys():
        print(k, d[k])


def gen(src, level=1):
    a0 = lzo.adler32(src)
    c  = lzo.compress(src, level)
    u1 = lzo.decompress(c)
    a1 = lzo.adler32(u1)
    o  = lzo.optimize(c)
    u2 = lzo.decompress(o)
    a2 = lzo.adler32(u2)
    if src != u1:
        raise lzo.error("internal error 1: %r %r", src, u1)
    if src != u2:
        raise lzo.error("internal error 1: %r %r",  src, u2)
    if a0 != a1 or a0 != a2:
        raise lzo.error("internal error 2")
    print("compressed %6d -> %6d" % (len(src), len(c)))

def gen_all(src):
    all_algos = {"LZO1", "LZO1A", "LZO1B", "LZO1C", "LZO1F", "LZO1X", "LZO1Y", "LZO1Z", "LZO2A"}
    if not src:
        # LZO1 and LZO1A header exception for empty input
        all_algos.remove("LZO1")
        all_algos.remove("LZO1A")
    for algo in all_algos:
        a0 = lzo.adler32(src)
        c  = lzo.compress(src, algorithm=algo)
        u1 = lzo.decompress(c, algorithm=algo)
        a1 = lzo.adler32(u1)
        if src != u1:
            raise lzo.error("internal error 1: %r %r", src, u1)
        if a0 != a1:
            raise lzo.error("internal error 2")
        print(f"compressed using {algo} {len(src): 6} -> {len(c): 6}")


def gen_raw(src, level=1):
    a0 = lzo.adler32(src)
    c  = lzo.compress(src, level, False)
    u1 = lzo.decompress(c, False, len(src))
    a1 = lzo.adler32(u1)
    o  = lzo.optimize(c, False, len(src))
    u2 = lzo.decompress(o, False, len(src))
    a2 = lzo.adler32(u2)
    # make sure it still works when you overstate the output buffer length
    u3 = lzo.decompress(c, False, len(src) + 100)
    if src != u1 or src != u2 or src != u3:
        raise lzo.error("internal error 1")
    if a0 != a1 or a0 != a2:
        raise lzo.error("internal error 2")
    print("compressed %6d -> %6d" % (len(src), len(c)))

def test_version():
    import pkg_resources
    pkg_version = pkg_resources.require("python-lzo")[0].version
    mod_version = lzo.__version__.decode('utf-8')
    assert pkg_version == mod_version, \
        "%r != %r" %(pkg_version, mod_version)

@pytest.mark.parametrize("src, level", [(b"aaaaaaaaaaaaaaaaaaaaaaaa", 1), (b"abcabcabcabcabcabcabcabc", 1), (b"abcabcabcabcabcabcabcabc", 9)])
def test_lzo(src, level):
    gen(src, level)

@pytest.mark.parametrize("src", [b"aaaaaaaaaaaaaaaaaaaaaaaa", b"abcabcabcabcabcabcabcabc"])
def test_lzo_all(src):
    gen_all(src)

@pytest.mark.parametrize("src, level", [(b"aaaaaaaaaaaaaaaaaaaaaaaa", 1), (b"abcabcabcabcabcabcabcabc", 1), (b"abcabcabcabcabcabcabcabc", 9)])
def test_lzo_raw(src, level):
    gen_raw(src, level)


def test_lzo_empty():
    gen(b"")

def test_lzo_empty_all():
    gen_all(b"")

def test_lzo_empty_raw():
    gen_raw(b"")

def test_lzo_big():
    gen(b" " * 131072)

def test_lzo_big_all():
    gen_all(b" " * 131072)

def test_lzo_big_raw():
    gen_raw(b" " * 131072)


if sys.maxsize > 1<<32:
    # This test raises OverflowError on 32-bit Pythons. Compressing
    # this much data requires a 64-bit system.
    def test_lzo_compress_extremely_big():
        b = lzo.compress(bytes(bytearray((1024**3)*2)))