Integrate LZ4 library, compress the ramdisk
All checks were successful
Build documentation / build-and-deploy (push) Successful in 1m53s

This commit is contained in:
2026-03-07 02:54:26 +01:00
parent eaec32975a
commit b9e8a8bf1d
234 changed files with 52373 additions and 13 deletions

28
lz4/tests/.gitignore vendored Normal file
View File

@@ -0,0 +1,28 @@
# build artefacts
datagen
frametest
frametest32
fullbench
fullbench32
fullbench-lib
fullbench-dll
fuzzer
fuzzer32
fasttest
roundTripTest
checkTag
checkFrame
decompress-partial
decompress-partial-usingDict
abiTest
freestanding
# test artefacts
tmp*
versionsTest
abiTests
lz4_all.c
# local tests
afl

83
lz4/tests/COPYING Normal file
View File

@@ -0,0 +1,83 @@
Full name:
GNU General Public License v2.0 or later
Short identifier:
GPL-2.0-or-later
Text:
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations.
Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and modification follow.
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License.
c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program.
In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License.
3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable.
If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.
5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License.
7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice.
This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation.
10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

401
lz4/tests/Makefile Normal file
View File

@@ -0,0 +1,401 @@
# ##########################################################################
# LZ4 programs - Makefile
# Copyright (C) Yann Collet 2011-2020
#
# GPL v2 License
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# You can contact the author at :
# - LZ4 homepage : http://www.lz4.org
# - LZ4 source repository : https://github.com/lz4/lz4
# ##########################################################################
# fuzzer : Test tool, to check lz4 integrity on target platform
# frametest : Test tool, to check lz4frame integrity on target platform
# fullbench : Precisely measure speed for each LZ4 function variant
# datagen : generates synthetic data samples for tests & benchmarks
# ##########################################################################
LIBDIR := ../lib
PRGDIR := ../programs
VERSIONSTESTDIR := versionsTest
PYTHON ?= python3
DEBUGLEVEL?= 1
DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL)
USERCFLAGS:= -O3 $(CFLAGS) # appended for higher priority
WFLAGS = -Wall -Wextra -Wundef -Wcast-qual -Wcast-align -Wshadow \
-Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes \
-Wpointer-arith -Wstrict-aliasing=1
CFLAGS = $(WFLAGS) $(DEBUGFLAGS) $(USERCFLAGS)
CPPFLAGS += -I$(LIBDIR) -I$(PRGDIR) -DXXH_NAMESPACE=LZ4_
ALLFLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
include ../build/make/lz4defs.make
LZ4 := $(PRGDIR)/lz4$(EXT)
C_SRCDIRS = $(LIBDIR) $(PRGDIR) .
include ../build/make/multiconf.make
# Default test parameters
TEST_FILES := COPYING
FUZZER_TIME := -T90s
NB_LOOPS ?= -i1
.PHONY: default
default: all
.PHONY: all
all: fullbench fuzzer frametest roundTripTest datagen checkFrame decompress-partial
.PHONY: all32
all32: CFLAGS+=-m32
all32: all
.PHONY: lz4
lz4: MAKEFLAGS=
lz4:
$(MAKE) -C $(PRGDIR) $@ CFLAGS="$(CFLAGS)"
.PHONY: lib liblz4.pc
lib liblz4.pc: MAKEFLAGS=
lib liblz4.pc:
CFLAGS="$(CFLAGS)" $(MAKE) -C $(LIBDIR) $@
lz4c unlz4 lz4cat: lz4
$(LN_SF) $(LZ4) $(PRGDIR)/$@
.PHONY: lz4c32
lz4c32: MAKEFLAGS=
lz4c32: # create a 32-bits version for 32/64 interop tests
$(MAKE) -C $(PRGDIR) $@ CFLAGS="-m32 $(CFLAGS)"
# *.o objects are from library
%.o : $(LIBDIR)/%.c $(LIBDIR)/%.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
fullbench : DEBUGLEVEL=0
fullbench : CPPFLAGS += -DNDEBUG
$(eval $(call c_program,fullbench,fullbench.o lz4.o lz4hc.o lz4frame.o xxhash.o))
.PHONY: $(LIBDIR)/liblz4.a
$(LIBDIR)/liblz4.a: MAKEFLAGS=
$(LIBDIR)/liblz4.a:
$(MAKE) -C $(LIBDIR) liblz4.a
CLEAN += fullbench-lib
fullbench-lib : DEBUGLEVEL=0
fullbench-lib : CPPFLAGS += -DNDEBUG
fullbench-lib: fullbench.c $(LIBDIR)/liblz4.a
$(CC) $(ALLFLAGS) $^ -o $@$(EXT)
# Note: Windows only
ifeq ($(WINBASED),yes)
CLEAN += fullbench-dll
fullbench-dll : DEBUGLEVEL=0
fullbench-dll : CPPFLAGS += -DNDEBUG
fullbench-dll : MAKEFLAGS=
fullbench-dll: fullbench.c $(LIBDIR)/xxhash.c
$(MAKE) -C $(LIBDIR) liblz4
$(CC) $(ALLFLAGS) $^ -o $@$(EXT) -DLZ4_DLL_IMPORT=1 $(LIBDIR)/dll/$(LIBLZ4).dll
endif
# test LZ4_USER_MEMORY_FUNCTIONS
.PHONY: fullbench-wmalloc
fullbench-wmalloc: CPPFLAGS += -DLZ4_USER_MEMORY_FUNCTIONS
fullbench-wmalloc: fullbench
fuzzer:
$(eval $(call c_program_shared_o,fuzzer,lz4.o lz4hc.o xxhash.o fuzzer.o))
frametest:
$(eval $(call c_program_shared_o,frametest,lz4frame.o lz4file.o lz4.o lz4hc.o xxhash.o frametest.o))
roundTripTest:
$(eval $(call c_program_shared_o,roundTripTest,lz4.o lz4hc.o xxhash.o roundTripTest.o))
datagen: CPPFLAGS+=-DNDEBUG
$(eval $(call c_program,datagen,datagen.o lorem.o loremOut.o datagencli.o))
checkFrame:
$(eval $(call c_program_shared_o,checkFrame,lz4.o lz4hc.o xxhash.o lz4frame.o checkFrame.o))
decompress-partial:
$(eval $(call c_program_shared_o,decompress-partial,lz4.o decompress-partial.o))
decompress-partial-usingDict:
$(eval $(call c_program_shared_o,decompress-partial-usingDict,lz4.o decompress-partial-usingDict.o))
.PHONY: clean
clean: clean_cache
@$(MAKE) -C $(LIBDIR) $@ > $(VOID)
@$(MAKE) -C $(PRGDIR) $@ > $(VOID)
@$(RM) $(CLEAN) core *.o *.test
@$(RM) -r $(VERSIONSTESTDIR) tmp*
@echo Cleaning completed
.PHONY: versionsTest
versionsTest:
$(PYTHON) test-lz4-versions.py
.PHONY: listTest
listTest: lz4
QEMU_SYS=$(QEMU_SYS) $(PYTHON) test-lz4-list.py
# Note: requires liblz4 installed
CLEAN += abiTest
abiTest: LDLIBS += -llz4
.PHONY: abiTests
abiTests:
$(PYTHON) test-lz4-abi.py
checkTag:
$(eval $(call c_program_shared_o,checkTag,checkTag.o))
#-----------------------------------------------------------------------------
# validated only for Linux, OSX, BSD, Hurd and Solaris targets
#-----------------------------------------------------------------------------
ifeq ($(POSIX_ENV),Yes)
MD5 ?= $(if $(filter Darwin,$(shell $(UNAME))),md5 -r,md5sum)
GREP?= grep
CAT ?= cat
DATAGEN?=./datagen
PATH:=../programs:$(shell pwd):$(PATH)
.PHONY: list
list:
$(GREP) '^[^#[:space:]].*:' Makefile | sed 's/:.*//' | sort | uniq
ifneq ($(TARGETSEARCH),NO)
ALL_TARGETS := $(shell make list TARGETSEARCH=NO)
endif
TEST_TARGETS := $(filter test%,$(ALL_TARGETS))
.PHONY: $(TEST_TARGETS) # all targets starting by `test` are now .PHONY
test_targets:
@echo TEST_TARGETS = $(TEST_TARGETS)
.PHONY: check
check: test-lz4-essentials
test: test-lz4 test-lz4c test-frametest test-fullbench test-fuzzer test-amalgamation listTest test-decompress-partial
test32: CFLAGS+=-m32
test32: test
test-amalgamation: lz4_all.o
lz4_all.o: CFLAGS += -Werror -std=c90
CLEAN += lz4_all.c
lz4_all.c: $(LIBDIR)/lz4.c $(LIBDIR)/lz4hc.c $(LIBDIR)/lz4frame.c
$(CAT) $^ > $@
test-install: lz4 lib liblz4.pc
lz4_root=.. ./test_install.sh
test-compile-with-lz4-memory-usage:
$(MAKE) clean; CFLAGS=-O0 CPPFLAGS=-D'LZ4_MEMORY_USAGE=LZ4_MEMORY_USAGE_MIN' $(MAKE) all
$(MAKE) clean; CFLAGS=-O0 CPPFLAGS=-D'LZ4_MEMORY_USAGE=LZ4_MEMORY_USAGE_MAX' $(MAKE) all
# Rules regarding Temporary test files :
# Each test must use its own unique set of names during execution.
# Each temporary test file must begin by an FPREFIX.
# Each FPREFIX must be unique for each test.
# All FPREFIX must start with `tmp`, for `make clean`
# All tests must clean their temporary test files on successful completion,
# and only their test files : do not employ sweeping statements such `rm tmp*` or `rm *.lz4`
test-lz4-sparse: lz4 datagen
@echo "\n ---- test sparse file support ----"
./test-lz4-sparse.sh
test-lz4-contentSize: lz4 datagen
@echo "\n ---- test original size support ----"
./test-lz4-contentSize.sh
test-lz4-frame-concatenation: lz4 datagen
@echo "\n ---- test frame concatenation ----"
./test-lz4-frame-concatenation.sh
test-lz4-multiple: lz4 datagen
@echo "\n ---- test multiple files ----"
./test-lz4-multiple.sh
test-lz4-multiple-legacy: lz4 datagen
@echo "\n ---- test multiple files (Legacy format) ----"
./test-lz4-multiple-legacy.sh
test-lz4-skippable: lz4
@echo "\n ---- test lz4 with skippable frames ----"
./test-lz4-skippable.sh
test-datagen-invalid-input: datagen
@echo "\n ---- test datagen fails correctly on invalid input ----"
$(DATAGEN) -g1234l && exit 1 || true
test-lz4-basic: lz4 datagen unlz4 lz4cat
@echo "\n ---- test lz4 basic compression/decompression ----"
./test-lz4-basic.sh
test-lz4-dict: lz4 datagen
@echo "\n ---- test lz4 compression/decompression with dictionary ----"
./test-lz4-dict.sh
test-lz4hc-hugefile: lz4 datagen
@echo "\n ---- test HC compression/decompression of huge files ----"
./test-lz4hc-hugefile.sh
test-lz4-fast-hugefile: lz4 datagen
@echo "\n ---- test huge files compression/decompression ----"
./test-lz4-fast-hugefile.sh
test-lz4-hugefile: test-lz4-fast-hugefile test-lz4hc-hugefile
test-lz4-testmode: lz4 datagen
@echo "\n ---- bench mode ----"
./test-lz4-testmode.sh
test-lz4-opt-parser: lz4 datagen
@echo "\n ---- test opt-parser ----"
./test-lz4-opt-parser.sh
test-lz4-essentials : lz4 datagen test-datagen-invalid-input test-lz4-basic test-lz4-multiple \
test-lz4-multiple-legacy test-lz4-frame-concatenation \
test-lz4-testmode test-lz4-contentSize test-lz4-dict
test-lz4: lz4 datagen test-lz4-essentials test-lz4-opt-parser \
test-lz4-sparse test-lz4-hugefile test-lz4-dict \
test-lz4-skippable
test-lz4c: LZ4C := $(basename $(LZ4))c$(suffix $(LZ4))
test-lz4c: lz4c datagen
@echo "\n ---- test lz4c variant ----"
$(DATAGEN) -g256MB | $(LZ4C) -l -v | $(LZ4C) -t
test-lz4c32: CFLAGS+=-m32
test-lz4c32: test-lz4
test-interop-32-64: lz4 lz4c32 datagen
@echo "\n ---- test interoperability 32-bits -vs- 64 bits ----"
$(DATAGEN) -g16KB | $(LZ4)c32 -9 | $(LZ4) -t
$(DATAGEN) -P10 | $(LZ4) -9B4 | $(LZ4)c32 -t
$(DATAGEN) | $(LZ4)c32 | $(LZ4) -t
$(DATAGEN) -g1M | $(LZ4) -3B5 | $(LZ4)c32 -t
$(DATAGEN) -g256MB | $(LZ4)c32 -vqB4D | $(LZ4) -qt
$(DATAGEN) -g1G -P90 | $(LZ4) | $(LZ4)c32 -t
$(DATAGEN) -g6GB | $(LZ4)c32 -vq9BD | $(LZ4) -qt
test-lz4c32-basic: lz4c32 datagen
@echo "\n ---- test lz4c32 32-bits version ----"
$(DATAGEN) -g16KB | $(LZ4)c32 -9 | $(LZ4)c32 -t
$(DATAGEN) | $(LZ4)c32 | $(LZ4)c32 -t
$(DATAGEN) -g256MB | $(LZ4)c32 -vqB4D | $(LZ4)c32 -qt
$(DATAGEN) -g6GB | $(LZ4)c32 -vqB5D | $(LZ4)c32 -qt
test-platform:
@echo "\n ---- test lz4 $(QEMU_SYS) platform ----"
$(QEMU_SYS) $(DATAGEN) -g16KB | $(QEMU_SYS) $(LZ4) -9 | $(QEMU_SYS) $(LZ4) -t
$(QEMU_SYS) $(DATAGEN) | $(QEMU_SYS) $(LZ4) | $(QEMU_SYS) $(LZ4) -t
$(QEMU_SYS) $(DATAGEN) -g256MB | $(QEMU_SYS) $(LZ4) -vqB4D | $(QEMU_SYS) $(LZ4) -qt
ifneq ($(QEMU_SYS),qemu-arm-static)
$(QEMU_SYS) $(DATAGEN) -g3GB | $(QEMU_SYS) $(LZ4) -vqB5D | $(QEMU_SYS) $(LZ4) -qt
endif
test-fullbench: fullbench
./fullbench --no-prompt $(NB_LOOPS) $(TEST_FILES)
test-fullbench32: CFLAGS += -m32
test-fullbench32: test-fullbench
test-fuzzer: fuzzer
./fuzzer $(FUZZER_TIME)
test-fuzzer32: CFLAGS += -m32
test-fuzzer32: test-fuzzer
test-frametest: frametest
./frametest -v $(FUZZER_TIME)
test-frametest32: CFLAGS += -m32
test-frametest32: test-frametest
VALGRIND = valgrind --leak-check=yes --error-exitcode=1
test-mem: FPREFIX = tmp-tvm
test-mem: lz4 datagen fuzzer frametest fullbench
@echo "\n ---- valgrind tests : memory analyzer ----"
$(VALGRIND) $(DATAGEN) -g50M > $(VOID)
$(DATAGEN) -g16KB > $(FPREFIX)dg16K
$(VALGRIND) $(LZ4) -9 -BD -f $(FPREFIX)dg16K $(VOID)
$(DATAGEN) -g16KB -s2 > $(FPREFIX)dg16K2
$(DATAGEN) -g16KB -s3 > $(FPREFIX)dg16K3
$(VALGRIND) $(LZ4) --force --multiple $(FPREFIX)dg16K $(FPREFIX)dg16K2 $(FPREFIX)dg16K3
$(DATAGEN) -g7MB > $(FPREFIX)dg7M
$(VALGRIND) $(LZ4) -9 -B5D -f $(FPREFIX)dg7M $(FPREFIX)dg16K2
$(VALGRIND) $(LZ4) -t $(FPREFIX)dg16K2
$(VALGRIND) $(LZ4) -bi1 $(FPREFIX)dg7M
$(VALGRIND) ./fullbench -i1 $(FPREFIX)dg7M $(FPREFIX)dg16K2
$(VALGRIND) $(LZ4) -B4D -f -vq $(FPREFIX)dg7M $(VOID)
$(VALGRIND) $(LZ4) --list -m $(FPREFIX)*.lz4
$(VALGRIND) $(LZ4) --list -m -v $(FPREFIX)*.lz4
$(RM) $(FPREFIX)*
$(VALGRIND) ./fuzzer -i64 -t1
$(VALGRIND) ./frametest -i256
test-mem32: lz4c32 datagen
# unfortunately, valgrind doesn't seem to work with non-native binary...
test-decompress-partial : decompress-partial decompress-partial-usingDict
@echo "\n ---- test decompress-partial ----"
./decompress-partial$(EXT)
@echo "\n ---- test decompress-partial-usingDict ----"
./decompress-partial-usingDict$(EXT)
#-----------------------------------------------------------------------------
# freestanding test only for Linux x86_64
#-----------------------------------------------------------------------------
UNAME_S ?= $(if $(filter Windows_NT,$(OS)),Windows,$(shell uname -s))
UNAME_M ?= $(if $(filter Windows_NT,$(OS)),Unknown,$(shell uname -m))
FREESTANDING_CFLAGS := -ffreestanding -nostdlib
ifneq ($(UNAME_S), Linux)
FREESTANDING_CFLAGS :=
endif
ifneq ($(UNAME_M), x86_64)
FREESTANDING_CFLAGS :=
endif
CLEAN += freestanding
freestanding: freestanding.c
$(CC) $(FREESTANDING_CFLAGS) $^ -o $@$(EXT)
test-freestanding: freestanding
@echo "\n ---- test freestanding ----"
ifeq ($(FREESTANDING_CFLAGS),)
@echo "\n (skip)"
else
./freestanding$(EXT)
-strace ./freestanding$(EXT)
-ltrace ./freestanding$(EXT)
endif
endif

71
lz4/tests/README.md Normal file
View File

@@ -0,0 +1,71 @@
Programs and scripts for automated testing of LZ4
=======================================================
This directory contains the following programs and scripts:
- `datagen` : Synthetic and parametrable data generator, for tests
- `frametest` : Test tool that checks lz4frame integrity on target platform
- `fullbench` : Precisely measure speed for each lz4 inner functions
- `fuzzer` : Test tool, to check lz4 integrity on target platform
- `test-lz4-speed.py` : script for testing lz4 speed difference between commits
- `test-lz4-versions.py` : compatibility test between lz4 versions stored on Github
#### `test-lz4-versions.py` - script for testing lz4 interoperability between versions
This script creates `versionsTest` directory to which lz4 repository is cloned.
Then all tagged (released) versions of lz4 are compiled.
In the following step interoperability between lz4 versions is checked.
#### `test-lz4-speed.py` - script for testing lz4 speed difference between commits
This script creates `speedTest` directory to which lz4 repository is cloned.
Then it compiles all branches of lz4 and performs a speed benchmark for a given list of files (the `testFileNames` parameter).
After `sleepTime` (an optional parameter, default 300 seconds) seconds the script checks repository for new commits.
If a new commit is found it is compiled and a speed benchmark for this commit is performed.
The results of the speed benchmark are compared to the previous results.
If compression or decompression speed for one of lz4 levels is lower than `lowerLimit` (an optional parameter, default 0.98) the speed benchmark is restarted.
If second results are also lower than `lowerLimit` the warning e-mail is sent to recipients from the list (the `emails` parameter).
Additional remarks:
- To be sure that speed results are accurate the script should be run on a "stable" target system with no other jobs running in parallel
- Using the script with virtual machines can lead to large variations of speed results
- The speed benchmark is not performed until computers' load average is lower than `maxLoadAvg` (an optional parameter, default 0.75)
- The script sends e-mails using `mutt`; if `mutt` is not available it sends e-mails without attachments using `mail`; if both are not available it only prints a warning
The example usage with two test files, one e-mail address, and with an additional message:
```
./test-lz4-speed.py "silesia.tar calgary.tar" "email@gmail.com" --message "tested on my laptop" --sleepTime 60
```
To run the script in background please use:
```
nohup ./test-lz4-speed.py testFileNames emails &
```
The full list of parameters:
```
positional arguments:
testFileNames file names list for speed benchmark
emails list of e-mail addresses to send warnings
optional arguments:
-h, --help show this help message and exit
--message MESSAGE attach an additional message to e-mail
--lowerLimit LOWERLIMIT
send email if speed is lower than given limit
--maxLoadAvg MAXLOADAVG
maximum load average to start testing
--lastCLevel LASTCLEVEL
last compression level for testing
--sleepTime SLEEPTIME
frequency of repository checking in seconds
```
#### License
All files in this directory are licensed under GPL-v2.
See [COPYING](COPYING) for details.
The text of the license is also included at the top of each source file.

224
lz4/tests/abiTest.c Normal file
View File

@@ -0,0 +1,224 @@
/*
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree),
* meaning you may select, at your option, one of the above-listed licenses.
*/
/*
* abiTest :
* ensure ABI stability expectations are not broken by a new version
**/
/*===========================================
* Dependencies
*==========================================*/
#include <stddef.h> /* size_t */
#include <stdlib.h> /* malloc, free, exit */
#include <stdio.h> /* fprintf */
#include <string.h> /* strcmp */
#include <assert.h>
#include <sys/types.h> /* stat */
#include <sys/stat.h> /* stat */
#include "xxhash.h"
#include "lz4.h"
#include "lz4frame.h"
/*===========================================
* Macros
*==========================================*/
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define MSG(...) fprintf(stderr, __VA_ARGS__)
#define CONTROL_MSG(c, ...) { \
if ((c)) { \
MSG(__VA_ARGS__); \
MSG(" \n"); \
abort(); \
} \
}
static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize)
{
const char* const ip1 = (const char*)buff1;
const char* const ip2 = (const char*)buff2;
size_t pos;
for (pos=0; pos<buffSize; pos++)
if (ip1[pos]!=ip2[pos])
break;
return pos;
}
LZ4_stream_t LZ4_cState;
LZ4_streamDecode_t LZ4_dState;
/** roundTripTest() :
* Compresses `srcBuff` into `compressedBuff`,
* then decompresses `compressedBuff` into `resultBuff`.
* If clevel==0, compression level is derived from srcBuff's content head bytes.
* This function abort() if it detects any round-trip error.
* Therefore, if it returns, round trip is considered successfully validated.
* Note : `compressedBuffCapacity` should be `>= LZ4_compressBound(srcSize)`
* for compression to be guaranteed to work */
static void roundTripTest(void* resultBuff, size_t resultBuffCapacity,
void* compressedBuff, size_t compressedBuffCapacity,
const void* srcBuff, size_t srcSize)
{
int const acceleration = 1;
// Note : can't use LZ4_initStream(), because it's only present since v1.9.0
memset(&LZ4_cState, 0, sizeof(LZ4_cState));
{ int const cSize = LZ4_compress_fast_continue(&LZ4_cState, (const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, acceleration);
CONTROL_MSG(cSize == 0, "Compression error !");
{ int const dInit = LZ4_setStreamDecode(&LZ4_dState, NULL, 0);
CONTROL_MSG(dInit == 0, "LZ4_setStreamDecode error !");
}
{ int const dSize = LZ4_decompress_safe_continue (&LZ4_dState, (const char*)compressedBuff, (char*)resultBuff, cSize, (int)resultBuffCapacity);
CONTROL_MSG(dSize < 0, "Decompression detected an error !");
CONTROL_MSG(dSize != (int)srcSize, "Decompression corruption error : wrong decompressed size !");
} }
/* check potential content corruption error */
assert(resultBuffCapacity >= srcSize);
{ size_t const errorPos = checkBuffers(srcBuff, resultBuff, srcSize);
CONTROL_MSG(errorPos != srcSize,
"Silent decoding corruption, at pos %u !!!",
(unsigned)errorPos);
}
}
static void roundTripCheck(void* srcBuff, size_t srcSize)
{
size_t const cBuffSize = LZ4_COMPRESSBOUND(srcSize);
void* const cBuff = malloc(cBuffSize);
void* const rBuff = malloc(cBuffSize);
if (!cBuff || !rBuff) {
fprintf(stderr, "not enough memory ! \n");
free(srcBuff);
exit(1);
}
roundTripTest(rBuff, cBuffSize,
cBuff, cBuffSize,
srcBuff, srcSize);
free(rBuff);
free(cBuff);
}
static size_t getFileSize(const char* infilename)
{
int r;
#if defined(_MSC_VER)
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
#endif
return (size_t)statbuf.st_size;
}
static int isDirectory(const char* infilename)
{
int r;
#if defined(_MSC_VER)
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
if (!r && S_ISDIR(statbuf.st_mode)) return 1;
#endif
return 0;
}
/** loadFile() :
* requirement : `buffer` size >= `fileSize` */
static void loadFile(void* buffer, const char* fileName, size_t fileSize)
{
FILE* const f = fopen(fileName, "rb");
if (isDirectory(fileName)) {
MSG("Ignoring %s directory \n", fileName);
free(buffer);
fclose(f);
exit(2);
}
if (f==NULL) {
MSG("Impossible to open %s \n", fileName);
free(buffer);
exit(3);
}
{ size_t const readSize = fread(buffer, 1, fileSize, f);
if (readSize != fileSize) {
MSG("Error reading %s \n", fileName);
free(buffer);
fclose(f);
exit(5);
} }
fclose(f);
}
static void fileCheck(const char* fileName)
{
size_t const fileSize = getFileSize(fileName);
void* const buffer = malloc(fileSize + !fileSize /* avoid 0 */);
if (!buffer) {
MSG("not enough memory \n");
exit(4);
}
loadFile(buffer, fileName, fileSize);
roundTripCheck(buffer, fileSize);
free (buffer);
}
int bad_usage(const char* exeName)
{
MSG(" \n");
MSG("bad usage: \n");
MSG(" \n");
MSG("%s [Options] fileName \n", exeName);
MSG(" \n");
MSG("Options: \n");
MSG("-# : use #=[0-9] compression level (default:0 == random) \n");
return 1;
}
int main(int argCount, const char** argv)
{
const char* const exeName = argv[0];
int argNb = 1;
// Note : LZ4_VERSION_STRING requires >= v1.7.3+
MSG("abiTest, built binary based on API %s \n", LZ4_VERSION_STRING);
// Note : LZ4_versionString() requires >= v1.7.5+
MSG("currently linked to dll %s \n", LZ4_versionString());
assert(argCount >= 1);
if (argCount < 2) return bad_usage(exeName);
if (argNb >= argCount) return bad_usage(exeName);
fileCheck(argv[argNb]);
MSG("no pb detected \n");
return 0;
}

312
lz4/tests/checkFrame.c Normal file
View File

@@ -0,0 +1,312 @@
/*
checkFrame - verify frame headers
Copyright (C) Yann Collet 2014-2020
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 homepage : http://www.lz4.org
- LZ4 source repository : https://github.com/lz4/lz4
*/
/*-************************************
* Compiler options
**************************************/
#ifdef _MSC_VER /* Visual Studio */
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
#endif
/*-************************************
* Includes
**************************************/
#include "util.h" /* U32 */
#include <stdlib.h> /* malloc, free */
#include <stdio.h> /* fprintf */
#include <string.h> /* strcmp */
#include <time.h> /* clock_t, clock(), CLOCKS_PER_SEC */
#include <assert.h>
#include "lz4frame.h" /* include multiple times to test correctness/safety */
#include "lz4frame.h"
#define LZ4F_STATIC_LINKING_ONLY
#include "lz4frame.h"
#include "lz4frame.h"
#include "lz4.h" /* LZ4_VERSION_STRING */
#define XXH_STATIC_LINKING_ONLY
#include "xxhash.h" /* XXH64 */
/*-************************************
* Constants
**************************************/
#define KB *(1U<<10)
#define MB *(1U<<20)
#define GB *(1U<<30)
/*-************************************
* Macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
/**************************************
* Exceptions
***************************************/
#ifndef DEBUG
# define DEBUG 0
#endif
#define DEBUGOUTPUT(...) do { if (DEBUG) DISPLAY(__VA_ARGS__); } while (0)
#define EXM_THROW(error, ...) \
do { \
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
DISPLAYLEVEL(1, "Error %i : ", error); \
DISPLAYLEVEL(1, __VA_ARGS__); \
DISPLAYLEVEL(1, " \n"); \
return(error); \
} while (0)
/*-***************************************
* Local Parameters
*****************************************/
static U32 no_prompt = 0;
static U32 displayLevel = 2;
static U32 use_pause = 0;
/*-*******************************************************
* Fuzzer functions
*********************************************************/
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
typedef struct {
void* srcBuffer;
size_t srcBufferSize;
void* dstBuffer;
size_t dstBufferSize;
LZ4F_decompressionContext_t ctx;
} cRess_t;
static int createCResources(cRess_t* ress)
{
ress->srcBufferSize = 4 MB;
ress->srcBuffer = malloc(ress->srcBufferSize);
ress->dstBufferSize = 4 MB;
ress->dstBuffer = malloc(ress->dstBufferSize);
if (!ress->srcBuffer || !ress->dstBuffer) {
free(ress->srcBuffer);
free(ress->dstBuffer);
EXM_THROW(20, "Allocation error : not enough memory");
}
if (LZ4F_isError( LZ4F_createDecompressionContext(&(ress->ctx), LZ4F_VERSION) )) {
free(ress->srcBuffer);
free(ress->dstBuffer);
EXM_THROW(21, "Unable to create decompression context");
}
return 0;
}
static void freeCResources(cRess_t ress)
{
free(ress.srcBuffer);
free(ress.dstBuffer);
(void) LZ4F_freeDecompressionContext(ress.ctx);
}
int frameCheck(cRess_t ress, FILE* const srcFile, unsigned bsid, size_t blockSize)
{
LZ4F_errorCode_t nextToLoad = 0;
size_t curblocksize = 0;
int partialBlock = 0;
/* Main Loop */
for (;;) {
size_t readSize;
size_t pos = 0;
size_t decodedBytes = ress.dstBufferSize;
size_t remaining;
LZ4F_frameInfo_t frameInfo;
/* Read input */
readSize = fread(ress.srcBuffer, 1, ress.srcBufferSize, srcFile);
if (!readSize) break; /* reached end of file or stream */
while (pos < readSize) { /* still to read */
/* Decode Input (at least partially) */
if (!nextToLoad) {
/* LZ4F_decompress returned 0 : starting new frame */
curblocksize = 0;
remaining = readSize - pos;
nextToLoad = LZ4F_getFrameInfo(ress.ctx, &frameInfo, (char*)(ress.srcBuffer)+pos, &remaining);
if (LZ4F_isError(nextToLoad))
EXM_THROW(22, "Error getting frame info: %s",
LZ4F_getErrorName(nextToLoad));
if (frameInfo.blockSizeID != (LZ4F_blockSizeID_t) bsid)
EXM_THROW(23, "Block size ID %u != expected %u",
frameInfo.blockSizeID, bsid);
pos += remaining;
/* nextToLoad should be block header size */
remaining = nextToLoad;
decodedBytes = ress.dstBufferSize;
nextToLoad = LZ4F_decompress(ress.ctx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, NULL);
if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
pos += remaining;
}
decodedBytes = ress.dstBufferSize;
/* nextToLoad should be just enough to cover the next block */
if (nextToLoad > (readSize - pos)) {
/* block is not fully contained in current buffer */
partialBlock = 1;
remaining = readSize - pos;
} else {
if (partialBlock) {
partialBlock = 0;
}
remaining = nextToLoad;
}
nextToLoad = LZ4F_decompress(ress.ctx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, NULL);
if (LZ4F_isError(nextToLoad)) EXM_THROW(24, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
curblocksize += decodedBytes;
pos += remaining;
if (!partialBlock) {
/* detect small block due to end of frame; the final 4-byte frame checksum could be left in the buffer */
if ((curblocksize != 0) && (nextToLoad > 4)) {
if (curblocksize != blockSize)
EXM_THROW(25, "Block size %u != expected %u, pos %u\n",
(unsigned)curblocksize, (unsigned)blockSize, (unsigned)pos);
}
curblocksize = 0;
}
}
}
/* can be out because readSize == 0, which could be an fread() error */
if (ferror(srcFile)) EXM_THROW(26, "Read error");
if (nextToLoad!=0) EXM_THROW(27, "Unfinished stream");
return 0;
}
int FUZ_usage(const char* programName)
{
DISPLAY( "Usage :\n");
DISPLAY( " %s [args] filename\n", programName);
DISPLAY( "\n");
DISPLAY( "Arguments :\n");
DISPLAY( " -b# : expected blocksizeID [4-7] (required)\n");
DISPLAY( " -B# : expected blocksize [32-4194304] (required)\n");
DISPLAY( " -v : verbose\n");
DISPLAY( " -h : display help and exit\n");
return 0;
}
int main(int argc, const char** argv)
{
int argNb;
unsigned bsid=0;
size_t blockSize=0;
const char* const programName = argv[0];
/* Check command line */
for (argNb=1; argNb<argc; argNb++) {
const char* argument = argv[argNb];
if(!argument) continue; /* Protection if argument empty */
/* Decode command (note : aggregated short commands are allowed) */
if (argument[0]=='-') {
if (!strcmp(argument, "--no-prompt")) {
no_prompt=1;
displayLevel=1;
continue;
}
argument++;
while (*argument!=0) {
switch(*argument)
{
case 'h':
return FUZ_usage(programName);
case 'v':
argument++;
displayLevel++;
break;
case 'q':
argument++;
displayLevel--;
break;
case 'p': /* pause at the end */
argument++;
use_pause = 1;
break;
case 'b':
argument++;
bsid=0;
while ((*argument>='0') && (*argument<='9')) {
bsid *= 10;
bsid += (unsigned)(*argument - '0');
argument++;
}
break;
case 'B':
argument++;
blockSize=0;
while ((*argument>='0') && (*argument<='9')) {
blockSize *= 10;
blockSize += (size_t)(*argument - '0');
argument++;
}
break;
default:
;
return FUZ_usage(programName);
}
}
} else {
int err;
FILE *srcFile;
cRess_t ress;
if (bsid == 0 || blockSize == 0)
return FUZ_usage(programName);
DISPLAY("Starting frame checker (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING);
err = createCResources(&ress);
if (err) return (err);
srcFile = fopen(argument, "rb");
if ( srcFile==NULL ) {
freeCResources(ress);
EXM_THROW(1, "%s: %s \n", argument, strerror(errno));
}
assert (srcFile != NULL);
err = frameCheck(ress, srcFile, bsid, blockSize);
freeCResources(ress);
fclose(srcFile);
return (err);
}
}
return 0;
}

79
lz4/tests/checkTag.c Normal file
View File

@@ -0,0 +1,79 @@
/*
checkTag.c - Version validation tool for LZ4
Copyright (C) Yann Collet 2018-2020
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 homepage : http://www.lz4.org
- LZ4 source repo : https://github.com/lz4/lz4
*/
/* checkTag command :
* $ ./checkTag tag
* checkTag validates tags of following format : v[0-9].[0-9].[0-9]{any}
* The tag is then compared to LZ4 version number.
* They are compatible if first 3 digits are identical.
* Anything beyond that is free, and doesn't impact validation.
* Example : tag v1.8.1.2 is compatible with version 1.8.1
* When tag and version are not compatible, program exits with error code 1.
* When they are compatible, it exists with a code 0.
* checkTag is intended to be used in automated testing environment.
*/
#include <stdio.h> /* printf */
#include <string.h> /* strlen, strncmp */
#include "lz4.h" /* LZ4_VERSION_STRING */
/* validate() :
* @return 1 if tag is compatible, 0 if not.
*/
static int validate(const char* const tag)
{
size_t const tagLength = strlen(tag);
size_t const verLength = strlen(LZ4_VERSION_STRING);
if (tagLength < 2) return 0;
if (tag[0] != 'v') return 0;
if (tagLength <= verLength) return 0;
if (strncmp(LZ4_VERSION_STRING, tag+1, verLength)) return 0;
return 1;
}
int main(int argc, const char** argv)
{
const char* const exeName = argv[0];
const char* const tag = argv[1];
if (argc!=2) {
printf("incorrect usage : %s tag \n", exeName);
return 2;
}
printf("Version : %s \n", LZ4_VERSION_STRING);
printf("Tag : %s \n", tag);
if (validate(tag)) {
printf("OK : tag is compatible with lz4 version \n");
return 0;
}
printf("!! error : tag and versions are not compatible !! \n");
return 1;
}

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env sh
set -e
# written as a script shell, because pipe management in python is horrible
ldd $1 | grep liblz4

90
lz4/tests/check_stdvars.sh Executable file
View File

@@ -0,0 +1,90 @@
#!/usr/bin/env sh
set -e # exit on first command error
###############################################################################
# check_stdvars.sh verify that user-supplied CC/CFLAGS/CPPFLAGS/LDFLAGS/
# LDLIBS reach every compile- and link-command emitted by a recursive Make.
#
# Usage: tests/check_stdvars.sh build.log
# where build.log is produced with:
# make -nr V=1 \
# CC='cc -DCC_TEST' \
# CFLAGS='-DCFLAGS_TEST' \
# CPPFLAGS='-DCPPFLAGS_TEST' \
# LDFLAGS='-DLDFLAGS_TEST' \
# LDLIBS='-DLDLIBS_TEST' \
# > build.log
###############################################################################
log_file=${1:?need build-log file}
# marker flags injected by the Makefile test target
CC_TAG='-DCC_TEST'
CF_TAG='-DCFLAGS_TEST'
CP_TAG='-DCPPFLAGS_TEST'
LD_TAG='-DLDFLAGS_TEST'
LL_TAG='-DLDLIBS_TEST'
compile_seen=0 ; compile_ok=0
link_seen=0 ; link_ok=0
fail=0
report() {
echo "$1:" >&2
echo " $2" >&2
fail=1
}
# read log line-by-line
while IFS= read -r line
do
# skip blank lines, comments, or progress "echo …" lines
[ -z "$line" ] && continue
case "$line" in
\#*) continue ;;
' echo '*) continue ;; # leading whitespace + echo
'echo '*) continue ;; # echo with no leading whitespace
esac
# classify
step=other
case "$line" in
*' -c '*) step=compile ;; # compile if -c appears
*' cc '*|cc\ *|*' gcc '*|gcc\ *|*' clang '*|clang\ *|*' g++ '*|g++\ *|*' clang++ '*|clang++\ *)
step=link ;; # compiler without -c ⇒ link
esac
[ "$step" = other ] && continue
if [ "$step" = compile ]; then
compile_seen=$((compile_seen + 1))
ok=1
case "$line" in *"$CC_TAG"*) : ;; *) ok=0 ;; esac
case "$line" in *"$CF_TAG"*) : ;; *) ok=0 ;; esac
case "$line" in *"$CP_TAG"*) : ;; *) ok=0 ;; esac
if [ "$ok" -eq 1 ]; then
compile_ok=$((compile_ok + 1))
else
report "compile cmd missing marker(s)" "$line"
fi
else # link
link_seen=$((link_seen + 1))
ok=1
case "$line" in *"$LD_TAG"*) : ;; *) ok=0 ;; esac
case "$line" in *"$LL_TAG"*) : ;; *) ok=0 ;; esac
if [ "$ok" -eq 1 ]; then
link_ok=$((link_ok + 1))
else
report "link cmd missing marker(s)" "$line"
fi
fi
done < "$log_file"
if [ "$fail" -ne 0 ]; then
echo "standard-variable propagation test FAILED" >&2
echo " compile: $compile_ok / $compile_seen OK" >&2
echo " link : $link_ok / $link_seen OK" >&2
exit 1
fi
echo "$compile_seen compile + $link_seen link commands: all markers present"
exit 0

View File

@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.5...4.0.2)
project(cmake_install_test LANGUAGES C)
# Find LZ4 installation in Config mode
find_package(lz4 CONFIG REQUIRED)
# Verify that the universal target always exists
if (NOT TARGET LZ4::lz4)
message(FATAL_ERROR "LZ4::lz4 target does not exist!")
endif()
# Verify that the location property is set
get_property(LZ4_LOCATION TARGET LZ4::lz4_shared PROPERTY LOCATION)
if (NOT LZ4_LOCATION)
message(FATAL_ERROR "Missing LOCATION property for LZ4::lz4_shared!")
endif()
# Verify that the include directory property is set
get_property(LZ4_SHARED_INCLUDE_DIRECTORIES TARGET LZ4::lz4_shared PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
if (NOT LZ4_SHARED_INCLUDE_DIRECTORIES)
message(FATAL_ERROR "Missing INTERFACE_INCLUDE_DIRECTORIES property for LZ4::lz4_shared!")
endif()
# Add a test that builds against the installation
set(LZ4_TESTS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
add_executable(decompress-partial "${LZ4_TESTS_SOURCE_DIR}/decompress-partial.c")
target_link_libraries(decompress-partial LZ4::lz4_shared)
# Add a test that builds against the lz4::lz4 target
set(LZ4_TESTS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
add_executable(decompress-partial-lz4 "${LZ4_TESTS_SOURCE_DIR}/decompress-partial.c")
target_link_libraries(decompress-partial-lz4 lz4::lz4)

188
lz4/tests/datagen.c Normal file
View File

@@ -0,0 +1,188 @@
/*
datagen.c - compressible data generator test tool
Copyright (C) Yann Collet 2012-2024
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 source repository : https://github.com/lz4/lz4
- Public forum : https://groups.google.com/forum/#!forum/lz4c
*/
/**************************************
* Includes
**************************************/
#include "platform.h" /* Compiler options, SET_BINARY_MODE */
#include "util.h" /* U32 */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* FILE, fwrite */
#include <string.h> /* memcpy */
#include <assert.h>
/**************************************
* Constants
**************************************/
#define KB *(1 <<10)
#define PRIME1 2654435761U
#define PRIME2 2246822519U
/**************************************
* Local types
**************************************/
#define LTLOG 13
#define LTSIZE (1<<LTLOG)
#define LTMASK (LTSIZE-1)
typedef BYTE litDistribTable[LTSIZE];
/*********************************************************
* Local Functions
*********************************************************/
#define MIN(a,b) ( (a) < (b) ? (a) :(b) )
#define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r)))
static unsigned int RDG_rand(U32* src)
{
U32 rand32 = *src;
rand32 *= PRIME1;
rand32 ^= PRIME2;
rand32 = RDG_rotl32(rand32, 13);
*src = rand32;
return rand32;
}
static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
{
BYTE const firstChar = ld <= 0.0 ? 0 : '(';
BYTE const lastChar = ld <= 0.0 ? 255 : '}';
BYTE character = ld <= 0.0 ? 0 : '0';
U32 u = 0;
while (u<LTSIZE) {
U32 const weight = (U32)((double)(LTSIZE - u) * ld) + 1;
U32 const end = MIN(u+weight, LTSIZE);
while (u < end) {
assert(u<LTSIZE); /* try to ease static analyzer. u < end <= LTSIZE */
lt[u++] = character;
}
character++;
if (character > lastChar) character = firstChar;
}
}
static BYTE RDG_genChar(U32* seed, const litDistribTable lt)
{
U32 id = RDG_rand(seed) & LTMASK;
return (lt[id]);
}
#define RDG_DICTSIZE (32 KB)
#define RDG_RAND15BITS ((RDG_rand(seed) >> 3) & 32767)
#define RDG_RANDLENGTH ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15)
void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned* seedPtr)
{
BYTE* buffPtr = (BYTE*)buffer;
const U32 matchProba32 = (U32)(32768 * matchProba);
size_t pos = prefixSize;
U32* seed = seedPtr;
/* special case */
while (matchProba >= 1.0) {
size_t size0 = RDG_rand(seed) & 3;
size0 = (size_t)1 << (16 + size0 * 2);
size0 += RDG_rand(seed) & (size0-1); /* because size0 is power of 2*/
if (buffSize < pos + size0) {
memset(buffPtr+pos, 0, buffSize-pos);
return;
}
memset(buffPtr+pos, 0, size0);
pos += size0;
buffPtr[pos-1] = RDG_genChar(seed, lt);
}
/* init */
if (pos==0) {
buffPtr[0] = RDG_genChar(seed, lt);
pos=1;
}
/* Generate compressible data */
while (pos < buffSize) {
/* Select : Literal (char) or Match (within 32K) */
if (RDG_RAND15BITS < matchProba32) {
/* Copy (within 32K) */
size_t match;
size_t d;
size_t length = RDG_RANDLENGTH + 4;
U32 offset = RDG_RAND15BITS + 1;
if (offset > pos) offset = (U32)pos;
match = pos - offset;
d = pos + length;
if (d > buffSize) d = buffSize;
while (pos < d) buffPtr[pos++] = buffPtr[match++];
} else {
/* Literal (noise) */
size_t d;
size_t length = RDG_RANDLENGTH;
d = pos + length;
if (d > buffSize) d = buffSize;
while (pos < d) buffPtr[pos++] = RDG_genChar(seed, lt);
}
}
}
void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba, unsigned seed)
{
litDistribTable lt;
if (litProba==0.0) litProba = matchProba / 4.5;
RDG_fillLiteralDistrib(lt, litProba);
RDG_genBlock(buffer, size, 0, matchProba, lt, &seed);
}
#define RDG_BLOCKSIZE (128 KB)
void RDG_genOut(unsigned long long size, double matchProba, double litProba, unsigned seed)
{
BYTE buff[RDG_DICTSIZE + RDG_BLOCKSIZE];
U64 total = 0;
size_t genBlockSize = RDG_BLOCKSIZE;
litDistribTable lt;
/* init */
if (litProba==0.0) litProba = matchProba / 4.5;
RDG_fillLiteralDistrib(lt, litProba);
SET_BINARY_MODE(stdout);
/* Generate dict */
RDG_genBlock(buff, RDG_DICTSIZE, 0, matchProba, lt, &seed);
/* Generate compressible data */
while (total < size) {
RDG_genBlock(buff, RDG_DICTSIZE+RDG_BLOCKSIZE, RDG_DICTSIZE, matchProba, lt, &seed);
if (size-total < RDG_BLOCKSIZE) genBlockSize = (size_t)(size-total);
total += genBlockSize;
fwrite(buff, 1, genBlockSize, stdout); /* should check potential write error */
/* update dict */
memcpy(buff, buff + RDG_BLOCKSIZE, RDG_DICTSIZE);
}
}

42
lz4/tests/datagen.h Normal file
View File

@@ -0,0 +1,42 @@
/*
datagen.h - compressible data generator header
Copyright (C) Yann Collet 2012-2024
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 source repository : https://github.com/lz4/lz4
- Public forum : https://groups.google.com/forum/#!forum/lz4c
*/
#include <stddef.h> /* size_t */
/* RDG_genOut():
* Generate @size bytes of compressible data into stdout.
* Compressibility can be controlled using @matchProba.
* @LitProba is optional, and affect variability of bytes. If @litProba==0.0, default value is used.
* Generated data can be influenced using @seed.
* If @matchProba, @litProba and @seed are equal, the function always generates the same content.
*/
void RDG_genOut(unsigned long long size, double matchProba, double litProba, unsigned seed);
/*
* RDG_genBuffer():
* Same as RDG_genOut, but generates data into provided @buffer
*/
void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba, unsigned seed);

156
lz4/tests/datagencli.c Normal file
View File

@@ -0,0 +1,156 @@
/*
datagencli.c
compressible data command line generator
Copyright (C) Yann Collet 2012-2020
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 source repository : https://github.com/lz4/lz4
- Public forum : https://groups.google.com/forum/#!forum/lz4c
*/
/**************************************
* Includes
**************************************/
#include "util.h" /* U32 */
#include <stdio.h> /* fprintf, stderr */
#include "datagen.h" /* RDG_generate */
#include "loremOut.h" /* LOREM_genOut */
#include "lz4.h" /* LZ4_VERSION_STRING */
/**************************************
* Compiler specific
**************************************/
#ifdef _MSC_VER /* Visual Studio */
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
# define strtoull _strtoui64 /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtoui64-wcstoui64-strtoui64-l-wcstoui64-l */
#endif
/**************************************
* Constants
**************************************/
#define KB *(1 <<10)
#define MB *(1 <<20)
#define GB *(1U<<30)
#define SIZE_DEFAULT (64 KB)
#define SEED_DEFAULT 0
#define COMPRESSIBILITY_NOT_SET 9999
/**************************************
* Macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) do { if (displayLevel>=(l)) DISPLAY(__VA_ARGS__); } while (0)
static unsigned displayLevel = 2;
/*********************************************************
* Command line
*********************************************************/
static int usage(char* programName)
{
DISPLAY( "Compressible data generator\n");
DISPLAY( "Usage :\n");
DISPLAY( " %s [size] [args]\n", programName);
DISPLAY( "\n");
DISPLAY( "Arguments :\n");
DISPLAY( " -g# : generate # data (default:%i)\n", SIZE_DEFAULT);
DISPLAY( " -s# : Select seed (default:%i)\n", SEED_DEFAULT);
DISPLAY( " -P# : Select compressibility in %% (range [0-100])\n");
DISPLAY( " -h : display help and exit\n");
DISPLAY( "Special values :\n");
DISPLAY( " -P0 : generate incompressible noise\n");
DISPLAY( " -P100 : generate sparse files\n");
return 0;
}
int main(int argc, char** argv)
{
int argNb;
unsigned long long proba = COMPRESSIBILITY_NOT_SET;
double litProba = 0.0;
U64 size = SIZE_DEFAULT;
unsigned seed = SEED_DEFAULT;
char* programName;
/* Check command line */
programName = argv[0];
for(argNb=1; argNb<argc; argNb++) {
char* argument = argv[argNb];
if(!argument) continue; /* Protection if argument empty */
/* Handle commands. Aggregated commands are allowed */
if (*argument=='-') {
argument++;
while (*argument!=0) {
switch(*argument) {
case 'h':
return usage(programName);
case 'g':
argument++;
size = strtoull(argument, &argument, 10);
if (*argument=='K') { size <<= 10; argument++; }
if (*argument=='M') { size <<= 20; argument++; }
if (*argument=='G') { size <<= 30; argument++; }
if (*argument=='B') { argument++; }
break;
case 's':
argument++;
seed = (unsigned)strtoul(argument, &argument, 10);
break;
case 'P':
argument++;
proba = strtoull(argument, &argument, 10);
break;
case 'L': /* hidden argument : Literal distribution probability */
argument++;
litProba = (double) strtoull(argument, &argument, 10);
if (litProba>100.) litProba=100.;
litProba /= 100.;
break;
case 'v':
displayLevel = 4;
argument++;
break;
default:
usage(programName);
return 1; /* Return error if usage is unexpected */
}
}
} /* if (*argument=='-') */
} /* for(argNb=1; argNb<argc; argNb++) */
DISPLAYLEVEL(4, "Data Generator %s \n", LZ4_VERSION_STRING);
DISPLAYLEVEL(3, "Seed = %u \n", seed);
if (proba != COMPRESSIBILITY_NOT_SET) {
DISPLAYLEVEL(3, "Compressibility : %i%%\n", (int)proba);
RDG_genOut(size, (double)proba / 100., litProba, seed);
} else {
LOREM_genOut(size, seed);
}
DISPLAYLEVEL(1, "\n");
return 0;
}

View File

@@ -0,0 +1,103 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "lz4.h"
const char source[] =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\n"
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\n"
"veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\n"
"commodo consequat. Duis aute irure dolor in reprehenderit in voluptate\n"
"velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat\n"
"cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id\n"
"est laborum.\n"
"\n"
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium\n"
"doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore\n"
"veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim\n"
"ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia\n"
"consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque\n"
"porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,\n"
"adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore\n"
"et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis\n"
"nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid\n"
"ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea\n"
"voluptate velit esse quam nihil molestiae consequatur, vel illum qui\n"
"dolorem eum fugiat quo voluptas nulla pariatur?\n";
#define BUFFER_SIZE 2048
int main(void)
{
int srcLen = (int)strlen(source);
size_t const smallSize = 1024;
size_t const largeSize = 64 * 1024 - 1;
char cmpBuffer[BUFFER_SIZE];
char* const buffer = (char*)malloc(BUFFER_SIZE + largeSize);
char* outBuffer = buffer + largeSize;
char* const dict = (char*)malloc(largeSize);
char* const largeDict = dict;
char* const smallDict = dict + largeSize - smallSize;
int i;
int cmpSize;
printf("starting test decompress-partial-usingDict : \n");
assert(buffer != NULL);
assert(dict != NULL);
cmpSize = LZ4_compress_default(source, cmpBuffer, srcLen, BUFFER_SIZE);
for (i = cmpSize; i < cmpSize + 10; ++i) {
int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, NULL, 0);
if ( (result < 0)
|| (result != srcLen)
|| memcmp(source, outBuffer, (size_t)srcLen) ) {
printf("test decompress-partial-usingDict with no dict error \n");
return -1;
}
}
for (i = cmpSize; i < cmpSize + 10; ++i) {
int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, outBuffer - smallSize, smallSize);
if ( (result < 0)
|| (result != srcLen)
|| memcmp(source, outBuffer, (size_t)srcLen) ) {
printf("test decompress-partial-usingDict with small prefix error \n");
return -1;
}
}
for (i = cmpSize; i < cmpSize + 10; ++i) {
int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, buffer, largeSize);
if ( (result < 0)
|| (result != srcLen)
|| memcmp(source, outBuffer, (size_t)srcLen) ) {
printf("test decompress-partial-usingDict with large prefix error \n");
return -1;
}
}
for (i = cmpSize; i < cmpSize + 10; ++i) {
int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, smallDict, smallSize);
if ( (result < 0)
|| (result != srcLen)
|| memcmp(source, outBuffer, (size_t)srcLen) ) {
printf("test decompress-partial-usingDict with small external dict error \n");
return -1;
}
}
for (i = cmpSize; i < cmpSize + 10; ++i) {
int result = LZ4_decompress_safe_partial_usingDict(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE, largeDict, largeSize);
if ( (result < 0)
|| (result != srcLen)
|| memcmp(source, outBuffer, (size_t)srcLen) ) {
printf("test decompress-partial-usingDict with large external dict error \n");
return -1;
}
}
printf("test decompress-partial-usingDict OK \n");
return 0;
}

View File

@@ -0,0 +1,49 @@
#include "stdio.h"
#include "string.h"
#include "lz4.h"
const char source[] =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\n"
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\n"
"veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\n"
"commodo consequat. Duis aute irure dolor in reprehenderit in voluptate\n"
"velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat\n"
"cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id\n"
"est laborum.\n"
"\n"
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium\n"
"doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore\n"
"veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim\n"
"ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia\n"
"consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque\n"
"porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,\n"
"adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore\n"
"et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis\n"
"nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid\n"
"ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea\n"
"voluptate velit esse quam nihil molestiae consequatur, vel illum qui\n"
"dolorem eum fugiat quo voluptas nulla pariatur?\n";
#define BUFFER_SIZE 2048
int main(void)
{
int srcLen = (int)strlen(source);
char cmpBuffer[BUFFER_SIZE];
char outBuffer[BUFFER_SIZE];
int cmpSize;
int i;
cmpSize = LZ4_compress_default(source, cmpBuffer, srcLen, BUFFER_SIZE);
for (i = cmpSize; i < cmpSize + 10; ++i) {
int result = LZ4_decompress_safe_partial(cmpBuffer, outBuffer, i, srcLen, BUFFER_SIZE);
if ((result < 0) || (result != srcLen) || memcmp(source, outBuffer, srcLen)) {
printf("test decompress-partial error \n");
return -1;
}
}
printf("test decompress-partial OK \n");
return 0;
}

1679
lz4/tests/frametest.c Normal file

File diff suppressed because it is too large Load Diff

238
lz4/tests/freestanding.c Normal file
View File

@@ -0,0 +1,238 @@
// Basic test for LZ4_FREESTANDING
// $ gcc -ffreestanding -nostdlib freestanding.c && ./a.out || echo $?
// $ strace ./a.out
// execve("./a.out", ["./a.out"], 0x7fffaf5fa580 /* 22 vars */) = 0
// brk(NULL) = 0x56536f4fe000
// arch_prctl(0x3001 /* ARCH_??? */, 0x7fffc9e74950) = -1 EINVAL (Invalid argument)
// mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd5c9c2b000
// access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
// arch_prctl(ARCH_SET_FS, 0x7fd5c9c2bc40) = 0
// set_tid_address(0x7fd5c9c2bf10) = 381
// set_robust_list(0x7fd5c9c2bf20, 24) = 0
// rseq(0x7fd5c9c2c5e0, 0x20, 0, 0x53053053) = 0
// mprotect(0x56536ea63000, 4096, PROT_READ) = 0
// exit(0) = ?
// +++ exited with 0 +++
// $ ltrace ./a.out
// +++ exited (status 0) +++
#include <stddef.h>
#include <stdint.h>
#if defined(__cplusplus)
# define EXTERN_C extern "C"
#else
# define EXTERN_C
#endif
#if !defined(__x86_64__) || !defined(__linux__)
int main(int argc, char** argv) { return 0; }
#else
static void MY_exit(int exitCode);
static void MY_abort(void);
EXTERN_C void *memmove(void *dst, const void *src, size_t n);
EXTERN_C void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n);
EXTERN_C void *memset(void *s, int c, size_t n);
EXTERN_C int memcmp(const void *s1, const void *s2, size_t n);
// LZ4/HC basic freestanding setup
#define LZ4_FREESTANDING 1
#define LZ4_memmove(dst, src, size) memmove((dst),(src),(size))
#define LZ4_memcpy(dst, src, size) memcpy((dst),(src),(size))
#define LZ4_memset(p,v,s) memset((p),(v),(s))
#include "../lib/lz4.c"
#include "../lib/lz4hc.c"
// Test for LZ4
static void test_lz4(const uint8_t* srcData, int srcSize) {
// Compress
static uint8_t compressBuffer[1024 * 1024];
const int compressedSize = LZ4_compress_default(
(const char*) srcData,
(char*) compressBuffer,
srcSize,
sizeof(compressBuffer)
);
if (compressedSize <= 0) {
MY_exit(__LINE__);
}
// Decompress
static uint8_t decompressBuffer[1024 * 1024];
const int decompressedSize = LZ4_decompress_safe(
(const char*) compressBuffer,
(char*) decompressBuffer,
compressedSize,
sizeof(decompressBuffer)
);
if (decompressedSize <= 0) {
MY_exit(__LINE__);
}
// Verify
if (decompressedSize != srcSize) {
MY_exit(__LINE__);
}
if (memcmp(srcData, decompressBuffer, srcSize) != 0) {
MY_exit(__LINE__);
}
}
// Test for LZ4HC
static void test_lz4hc(const uint8_t* srcData, int srcSize) {
// Compress
static uint8_t compressBuffer[1024 * 1024];
const int compressedSize = LZ4_compress_HC(
(const char*) srcData,
(char*) compressBuffer,
srcSize,
sizeof(compressBuffer),
LZ4HC_CLEVEL_DEFAULT
);
if (compressedSize <= 0) {
MY_exit(__LINE__);
}
// Decompress
static uint8_t decompressBuffer[1024 * 1024];
const int decompressedSize = LZ4_decompress_safe(
(const char*) compressBuffer,
(char*) decompressBuffer,
compressedSize,
sizeof(decompressBuffer)
);
if (decompressedSize <= 0) {
MY_exit(__LINE__);
}
// Verify
if (decompressedSize != srcSize) {
MY_exit(__LINE__);
}
if (memcmp(srcData, decompressBuffer, srcSize) != 0) {
MY_exit(__LINE__);
}
}
static void test(void) {
// First 256 bytes of lz4/README.md
static const uint8_t README_md[] = {
0x4c, 0x5a, 0x34, 0x20, 0x2d, 0x20, 0x45, 0x78, 0x74, 0x72, 0x65, 0x6d, 0x65, 0x6c, 0x79, 0x20,
0x66, 0x61, 0x73, 0x74, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e,
0x0a, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d,
0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d,
0x3d, 0x0a, 0x0a, 0x4c, 0x5a, 0x34, 0x20, 0x69, 0x73, 0x20, 0x6c, 0x6f, 0x73, 0x73, 0x6c, 0x65,
0x73, 0x73, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x61,
0x6c, 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2c, 0x0a, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64,
0x69, 0x6e, 0x67, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20,
0x73, 0x70, 0x65, 0x65, 0x64, 0x20, 0x3e, 0x20, 0x35, 0x30, 0x30, 0x20, 0x4d, 0x42, 0x2f, 0x73,
0x20, 0x70, 0x65, 0x72, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x2c, 0x0a, 0x73, 0x63, 0x61, 0x6c, 0x61,
0x62, 0x6c, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x2d, 0x63,
0x6f, 0x72, 0x65, 0x73, 0x20, 0x43, 0x50, 0x55, 0x2e, 0x0a, 0x49, 0x74, 0x20, 0x66, 0x65, 0x61,
0x74, 0x75, 0x72, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x65, 0x78, 0x74, 0x72, 0x65, 0x6d, 0x65,
0x6c, 0x79, 0x20, 0x66, 0x61, 0x73, 0x74, 0x20, 0x64, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x2c,
0x0a, 0x77, 0x69, 0x74, 0x68, 0x20, 0x73, 0x70, 0x65, 0x65, 0x64, 0x20, 0x69, 0x6e, 0x20, 0x6d,
0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x20, 0x47, 0x42, 0x2f, 0x73, 0x20, 0x70, 0x65, 0x72,
};
static const uint8_t* srcData = README_md;
static const int srcSize = (int) sizeof(README_md);
test_lz4 (srcData, srcSize);
test_lz4hc(srcData, srcSize);
}
// low level syscall
#define SYS_exit (60)
static __inline long os_syscall1(long n, long a1) {
register long rax __asm__ ("rax") = n;
register long rdi __asm__ ("rdi") = a1;
__asm__ __volatile__ ("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
return rax;
}
static void MY_exit(int exitCode) {
(void) os_syscall1(SYS_exit, exitCode);
__builtin_unreachable(); // suppress "warning: 'noreturn' function does return"
}
static void MY_abort(void) {
MY_exit(-1);
}
// https://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/baselib---assert-fail-1.html
void __assert_fail(const char * assertion, const char * file, unsigned int line, const char * function) {
MY_abort();
}
// GCC requires memcpy, memmove, memset and memcmp.
// https://gcc.gnu.org/onlinedocs/gcc/Standards.html
// > GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp.
EXTERN_C void *memmove(void *dst, const void *src, size_t n) {
uint8_t* d = (uint8_t*) dst;
const uint8_t* s = (const uint8_t*) src;
if (d > s) {
d += n;
s += n;
while (n--) {
*--d = *--s;
}
} else {
while (n--) {
*d++ = *s++;
}
}
return dst;
}
EXTERN_C void *memcpy(void * __restrict__ dst, const void * __restrict__ src, size_t n) {
return memmove(dst, src, n);
}
EXTERN_C void *memset(void *s, int c, size_t n) {
uint8_t* p = (uint8_t*) s;
while (n--) {
*p++ = (uint8_t) c;
}
return s;
}
EXTERN_C int memcmp(const void *s1, const void *s2, size_t n) {
const uint8_t* p1 = (const uint8_t*) s1;
const uint8_t* p2 = (const uint8_t*) s2;
while (n--) {
const uint8_t c1 = *p1++;
const uint8_t c2 = *p2++;
if (c1 < c2) {
return -1;
} else if (c1 > c2) {
return 1;
}
}
return 0;
}
//
EXTERN_C void __attribute__((force_align_arg_pointer)) _start(void) {
test();
MY_exit(0);
}
int main(int argc, char** argv) {
test();
MY_exit(0);
return 0;
}
#endif

998
lz4/tests/fullbench.c Normal file
View File

@@ -0,0 +1,998 @@
/*
bench.c - Demo program to benchmark open-source compression algorithm
Copyright (C) Yann Collet 2012-2020
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 source repository : https://github.com/lz4/lz4
- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
*/
#if defined(_MSC_VER) || defined(_WIN32)
/* S_ISREG & gettimeofday() are not supported by MSVC */
# define BMK_LEGACY_TIMER 1
#endif
/**************************************
* Includes
**************************************/
#include "platform.h" /* _CRT_SECURE_NO_WARNINGS, Large Files support */
#include "util.h" /* U32, UTIL_getFileSize */
#include <stdlib.h> /* malloc, free */
#include <stdio.h> /* fprintf, fopen, ftello */
#include <sys/types.h> /* stat64 */
#include <sys/stat.h> /* stat64 */
#include <string.h> /* strcmp */
#include <time.h> /* clock_t, clock(), CLOCKS_PER_SEC */
#define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */
#include "lz4.h"
#include "lz4hc.h"
#include "lz4frame.h"
#include "xxhash.h"
/**************************************
* Constants
**************************************/
#define PROGRAM_DESCRIPTION "LZ4 speed analyzer"
#define AUTHOR "Yann Collet"
#define WELCOME_MESSAGE "*** %s v%s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, LZ4_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR
#define NBLOOPS 6
#define TIMELOOP (CLOCKS_PER_SEC * 19 / 10)
#define KB *(1 <<10)
#define MB *(1 <<20)
#define GB *(1U<<30)
#define KNUTH 2654435761U
#define MAX_MEM (1920 MB)
#define DEFAULT_CHUNKSIZE (4 MB)
#define ALL_COMPRESSORS 0
#define ALL_DECOMPRESSORS 0
/**************************************
* Local structures
**************************************/
struct chunkParameters
{
U32 id;
char* origBuffer;
char* compressedBuffer;
int origSize;
int compressedSize;
};
/**************************************
* Macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define PROGRESS(...) g_noPrompt ? 0 : DISPLAY(__VA_ARGS__)
/**************************************
* Benchmark Parameters
**************************************/
static int g_chunkSize = DEFAULT_CHUNKSIZE;
static int g_nbIterations = NBLOOPS;
static int g_pause = 0;
static int g_compressionTest = 1;
static int g_compressionAlgo = ALL_COMPRESSORS;
static int g_decompressionTest = 1;
static int g_decompressionAlgo = ALL_DECOMPRESSORS;
static int g_noPrompt = 0;
static void BMK_setBlocksize(int bsize)
{
g_chunkSize = bsize;
DISPLAY("-Using Block Size of %i KB-\n", g_chunkSize>>10);
}
static void BMK_setNbIterations(int nbLoops)
{
g_nbIterations = nbLoops;
DISPLAY("- %i iterations -\n", g_nbIterations);
}
static void BMK_setPause(void)
{
g_pause = 1;
}
/*********************************************************
* Private functions
*********************************************************/
static clock_t BMK_GetClockSpan( clock_t clockStart )
{
return clock() - clockStart; /* works even if overflow; max span ~30 mn */
}
static size_t BMK_findMaxMem(U64 requiredMem)
{
size_t step = 64 MB;
BYTE* testmem = NULL;
requiredMem = (((requiredMem >> 26) + 1) << 26);
requiredMem += 2*step;
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
while (!testmem) {
if (requiredMem > step) requiredMem -= step;
else requiredMem >>= 1;
testmem = (BYTE*) malloc ((size_t)requiredMem);
}
free (testmem);
/* keep some space available */
if (requiredMem > step) requiredMem -= step;
else requiredMem >>= 1;
return (size_t)requiredMem;
}
/*********************************************************
* Memory management, to test LZ4_USER_MEMORY_FUNCTIONS
*********************************************************/
void* LZ4_malloc(size_t s) { return malloc(s); }
void* LZ4_calloc(size_t n, size_t s) { return calloc(n,s); }
void LZ4_free(void* p) { free(p); }
/*********************************************************
* Benchmark function
*********************************************************/
static LZ4_stream_t LZ4_stream;
static void local_LZ4_resetDictT(void)
{
void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
assert(r != NULL); (void)r;
}
static void local_LZ4_createStream(void)
{
void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
assert(r != NULL); (void)r;
}
static void* g_chunk0 = NULL;
static size_t g_chunk0Size = 0;
static void local_LZ4_saveDict_init(void)
{
LZ4_loadDict(&LZ4_stream, (const char*)g_chunk0, (int)g_chunk0Size);
}
static int local_LZ4_saveDict(const char* in, char* out, int inSize)
{
(void)in;
return LZ4_saveDict(&LZ4_stream, out, inSize);
}
static int local_LZ4_compress_default_large(const char* in, char* out, int inSize)
{
return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize));
}
static int local_LZ4_compress_default_small(const char* in, char* out, int inSize)
{
return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize)-1);
}
static int local_LZ4_compress_destSize(const char* in, char* out, int inSize)
{
return LZ4_compress_destSize(in, out, &inSize, LZ4_compressBound(inSize)-1);
}
static int local_LZ4_compress_fast0(const char* in, char* out, int inSize)
{
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 0);
}
static int local_LZ4_compress_fast1(const char* in, char* out, int inSize)
{
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 1);
}
static int local_LZ4_compress_fast2(const char* in, char* out, int inSize)
{
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 2);
}
static int local_LZ4_compress_fast17(const char* in, char* out, int inSize)
{
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 17);
}
static int local_LZ4_compress_fast_extState0(const char* in, char* out, int inSize)
{
return LZ4_compress_fast_extState(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
}
static int local_LZ4_compress_fast_continue0(const char* in, char* out, int inSize)
{
return LZ4_compress_fast_continue(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
}
#ifndef LZ4_DLL_IMPORT
#if defined (__cplusplus)
extern "C" {
#endif
/* declare hidden function */
extern int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize);
#if defined (__cplusplus)
}
#endif
static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize)
{
return LZ4_compress_forceExtDict(&LZ4_stream, in, out, inSize);
}
#endif
/* HC compression functions */
LZ4_streamHC_t LZ4_streamHC;
static void local_LZ4_resetStreamHC(void)
{
LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC));
}
static void local_LZ4_saveDictHC_init(void)
{
LZ4_loadDictHC(&LZ4_streamHC, (const char*)g_chunk0, (int)g_chunk0Size);
}
static int local_LZ4_saveDictHC(const char* in, char* out, int inSize)
{
(void)in;
return LZ4_saveDictHC(&LZ4_streamHC, out, inSize);
}
static int local_LZ4_compress_HC(const char* in, char* out, int inSize)
{
return LZ4_compress_HC(in, out, inSize, LZ4_compressBound(inSize), 9);
}
static int local_LZ4_compress_HC_extStateHC(const char* in, char* out, int inSize)
{
return LZ4_compress_HC_extStateHC(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize), 9);
}
static int local_LZ4_compress_HC_continue(const char* in, char* out, int inSize)
{
return LZ4_compress_HC_continue(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize));
}
/* decompression functions */
static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize)
{
(void)inSize;
LZ4_decompress_fast(in, out, outSize);
return outSize;
}
static int local_LZ4_decompress_fast_usingDict_prefix(const char* in, char* out, int inSize, int outSize)
{
(void)inSize;
LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536);
return outSize;
}
static int local_LZ4_decompress_fast_usingExtDict(const char* in, char* out, int inSize, int outSize)
{
(void)inSize;
LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65535);
return outSize;
}
static int local_LZ4_decompress_safe(const char* in, char* out, int inSize, int outSize)
{
return LZ4_decompress_safe(in, out, inSize, outSize);
}
static int local_LZ4_decompress_safe_withPrefix64k(const char* in, char* out, int inSize, int outSize)
{
LZ4_decompress_safe_withPrefix64k(in, out, inSize, outSize);
return outSize;
}
static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize)
{
LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536);
return outSize;
}
static int local_LZ4_decompress_safe_partial_usingDict(const char* in, char* out, int inSize, int outSize)
{
int result = LZ4_decompress_safe_partial_usingDict(in, out, inSize, outSize - 5, outSize, out - 65536, 65536);
if (result < 0) return result;
return outSize;
}
#ifndef LZ4_DLL_IMPORT
#if defined (__cplusplus)
extern "C" {
#endif
extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const void* dict, size_t dictSize);
#if defined (__cplusplus)
}
#endif
static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize)
{
LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536);
return outSize;
}
#endif
#ifndef LZ4_DLL_IMPORT
#if defined (__cplusplus)
extern "C" {
#endif
extern int LZ4_decompress_safe_partial_forceExtDict(const char* in, char* out, int inSize, int targetOutputSize, int dstCapacity, const void* dict, size_t dictSize);
#if defined (__cplusplus)
}
#endif
static int local_LZ4_decompress_safe_partial_forceExtDict(const char* in, char* out, int inSize, int outSize)
{
int result = LZ4_decompress_safe_partial_forceExtDict(in, out, inSize, outSize - 5, outSize, out - 65536, 65536);
if (result < 0) return result;
return outSize;
}
#endif
static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize)
{
int result = LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize);
if (result < 0) return result;
return outSize;
}
/* frame functions */
static int local_LZ4F_compressFrame(const char* in, char* out, int inSize)
{
assert(inSize >= 0);
return (int)LZ4F_compressFrame(out, LZ4F_compressFrameBound((size_t)inSize, NULL), in, (size_t)inSize, NULL);
}
LZ4F_cctx* g_cctx = NULL;
static int local_LZ4F_compress(const char* in, char* out, int inSize)
{
/* output buffer size is assumed */
size_t const outSize = LZ4F_compressFrameBound((size_t)inSize, NULL);
size_t cSize = 0;
assert(inSize >= 0);
if (g_cctx == NULL) {
/* create and initialize LZ4F compression context the first time */
LZ4F_createCompressionContext(&g_cctx, LZ4F_VERSION);
assert(g_cctx != NULL);
} /* re-use existing compression context otherwise */
{ size_t const cbSize = LZ4F_compressBegin(g_cctx, out, outSize, NULL);
assert(!LZ4F_isError(cbSize));
cSize += cbSize;
}
{ size_t const cuSize = LZ4F_compressUpdate(g_cctx, out+cSize, outSize-cSize, in, (size_t)inSize, NULL);
assert(!LZ4F_isError(cuSize));
cSize += cuSize;
}
{ size_t const ceSize = LZ4F_compressEnd(g_cctx, out+cSize, outSize-cSize, NULL);
assert(!LZ4F_isError(ceSize));
cSize += ceSize;
}
return (int)cSize;
}
static LZ4F_decompressionContext_t g_dCtx;
static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize)
{
size_t srcSize = (size_t)inSize;
size_t dstSize = (size_t)outSize;
size_t result;
assert(inSize >= 0);
assert(outSize >= 0);
result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL);
if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame \n"); exit(8); }
if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect \n"); exit(9); }
return (int)dstSize;
}
static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcSize, int dstSize)
{
size_t totalInSize = (size_t)srcSize;
size_t maxOutSize = (size_t)dstSize;
size_t inPos = 0;
size_t inSize = 0;
size_t outPos = 0;
size_t outRemaining = maxOutSize - outPos;
for (;;) {
size_t const sizeHint =
LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
assert(!LZ4F_isError(sizeHint));
inPos += inSize;
inSize = sizeHint;
outPos += outRemaining;
outRemaining = maxOutSize - outPos;
if (!sizeHint) break;
}
/* frame completed */
if (inPos != totalInSize) {
DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n",
(unsigned)inPos, (unsigned)totalInSize);
exit(10);
}
return (int)outPos;
}
/* always provide input by block of 64 KB */
static int local_LZ4F_decompress_noHint(const char* src, char* dst, int srcSize, int dstSize)
{
size_t totalInSize = (size_t)srcSize;
size_t maxOutSize = (size_t)dstSize;
size_t inPos = 0;
size_t inSize = 64 KB;
size_t outPos = 0;
size_t outRemaining = maxOutSize - outPos;
for (;;) {
size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
assert(!LZ4F_isError(sizeHint));
inPos += inSize;
inSize = (inPos + 64 KB <= totalInSize) ? 64 KB : totalInSize - inPos;
outPos += outRemaining;
outRemaining = maxOutSize - outPos;
if (!sizeHint) break;
}
/* frame completed */
if (inPos != totalInSize) {
DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n",
(unsigned)inPos, (unsigned)totalInSize);
exit(10);
}
return (int)outPos;
}
typedef struct {
const char* name;
int (*compressionF)(const char*, char*, int);
void (*initFunction)(void);
int singleChunk;
} CompressionDesc;
static const CompressionDesc compDescArray[] = {
{ NULL, NULL, NULL, 0 },
{ "LZ4_compress_default", local_LZ4_compress_default_large, NULL, 0 },
{ "LZ4_compress_default(small dst)", local_LZ4_compress_default_small, NULL, 0 },
{ "LZ4_compress_destSize", local_LZ4_compress_destSize, NULL, 0 },
{ "LZ4_compress_fast(0)", local_LZ4_compress_fast0, NULL, 0 },
{ "LZ4_compress_fast(1)", local_LZ4_compress_fast1, NULL, 0 },
{ "LZ4_compress_fast(2)", local_LZ4_compress_fast2, NULL, 0 },
{ "LZ4_compress_fast(17)", local_LZ4_compress_fast17, NULL, 0 },
{ "LZ4_compress_fast_extState(0)", local_LZ4_compress_fast_extState0, NULL, 0 },
{ "LZ4_compress_fast_continue(0)", local_LZ4_compress_fast_continue0, local_LZ4_createStream, 0 },
{ "LZ4_compress_HC", local_LZ4_compress_HC, NULL, 0 },
{ "LZ4_compress_HC_extStateHC", local_LZ4_compress_HC_extStateHC, NULL, 0 },
{ "LZ4_compress_HC_continue", local_LZ4_compress_HC_continue, local_LZ4_resetStreamHC, 0 },
#ifndef LZ4_DLL_IMPORT
{ "LZ4_compress_forceDict", local_LZ4_compress_forceDict, local_LZ4_resetDictT, 0 },
#endif
{ "LZ4F_compressFrame", local_LZ4F_compressFrame, NULL, 1 },
{ "LZ4F_compressUpdate", local_LZ4F_compress, NULL, 1 },
{ "LZ4_saveDict", local_LZ4_saveDict, local_LZ4_saveDict_init, 0 },
{ "LZ4_saveDictHC", local_LZ4_saveDictHC, local_LZ4_saveDictHC_init, 0 },
};
typedef struct {
const char* name;
int (*decompressionF)(const char*, char*, int, int);
int checkResult;
int frameFormat;
} DecompressionDesc;
static const DecompressionDesc decDescArray[] = {
{ NULL, NULL, 0, 0 },
{ "LZ4_decompress_fast", local_LZ4_decompress_fast, 1, 0 },
{ "LZ4_decompress_fast_usingDict(prefix)", local_LZ4_decompress_fast_usingDict_prefix, 1, 0 },
{ "LZ4_decompress_fast_using(Ext)Dict", local_LZ4_decompress_fast_usingExtDict, 1, 0 },
{ "LZ4_decompress_safe", local_LZ4_decompress_safe, 1, 0 },
{ "LZ4_decompress_safe_withPrefix64k", local_LZ4_decompress_safe_withPrefix64k, 1, 0 },
{ "LZ4_decompress_safe_usingDict", local_LZ4_decompress_safe_usingDict, 1, 0 },
{ "LZ4_decompress_safe_partial", local_LZ4_decompress_safe_partial, 0, 0 },
{ "LZ4_decompress_safe_partial_usingDict", local_LZ4_decompress_safe_partial_usingDict, 0, 0 },
#ifndef LZ4_DLL_IMPORT
{ "LZ4_decompress_safe_partial_forceExtDict", local_LZ4_decompress_safe_partial_forceExtDict, 0, 0 },
{ "LZ4_decompress_safe_forceExtDict", local_LZ4_decompress_safe_forceExtDict, 1, 0 },
#endif
{ "LZ4F_decompress", local_LZ4F_decompress, 1, 1 },
{ "LZ4F_decompress_followHint", local_LZ4F_decompress_followHint, 1, 1 },
{ "LZ4F_decompress_noHint", local_LZ4F_decompress_noHint, 1, 1 },
};
#define ARRAY_SIZE(a) (sizeof(a) / (sizeof((a)[0])))
#define NB_COMPRESSION_ALGORITHMS ARRAY_SIZE(compDescArray)
#define NB_DECOMPRESSION_ALGORITHMS ARRAY_SIZE(decDescArray)
int fullSpeedBench(const char** fileNamesTable, int nbFiles)
{
int fileIdx=0;
clock_t loopDuration = TIMELOOP;
if (g_nbIterations==0) {
loopDuration = CLOCKS_PER_SEC / 50 + 1;
g_nbIterations = 1;
}
/* Init */
{ size_t const errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION);
if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } }
/* Loop for each fileName */
while (fileIdx<nbFiles) {
char* orig_buff = NULL;
struct chunkParameters* chunkP = NULL;
char* compressed_buff=NULL;
const char* const inFileName = fileNamesTable[fileIdx++];
FILE* const inFile = fopen( inFileName, "rb" );
U64 const inFileSize = UTIL_getFileSize(inFileName);
size_t benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */
int nbChunks;
int maxCompressedChunkSize;
size_t readSize;
int compressedBuffSize;
U32 crcOriginal;
/* Check infile pre-requisites */
if (inFile==NULL) { DISPLAY("Pb opening %s \n", inFileName); return 11; }
if (inFileSize==0) { DISPLAY("file is empty \n"); fclose(inFile); return 11; }
if (benchedSize==0) { DISPLAY("not enough memory \n"); fclose(inFile); return 11; }
/* Memory size adjustments */
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
if (benchedSize < inFileSize) {
DISPLAY("Not enough memory for '%s' full size; testing %i MB only... \n",
inFileName, (int)(benchedSize>>20));
}
if (benchedSize < 8 || g_chunkSize < 8) {
DISPLAY(" cannot bench using less then 8 bytes \n");
return 1;
}
/* Allocation */
nbChunks = (int) ((benchedSize + (size_t)g_chunkSize - 1) / (size_t)g_chunkSize);
chunkP = (struct chunkParameters*) malloc((size_t)nbChunks * sizeof(chunkP[0]));
orig_buff = (char*) malloc(benchedSize);
maxCompressedChunkSize = LZ4_compressBound(g_chunkSize);
compressedBuffSize = nbChunks * maxCompressedChunkSize;
compressed_buff = (char*)malloc((size_t)compressedBuffSize);
if(!chunkP || !orig_buff || !compressed_buff) {
DISPLAY("\nError: not enough memory! \n");
fclose(inFile);
free(orig_buff);
free(compressed_buff);
free(chunkP);
return(12);
}
/* Fill in src buffer */
DISPLAY("Loading %s... \r", inFileName);
readSize = fread(orig_buff, 1, benchedSize, inFile);
fclose(inFile);
if (readSize != benchedSize) {
DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
free(orig_buff);
free(compressed_buff);
free(chunkP);
return 13;
}
/* Calculating input Checksum */
crcOriginal = XXH32(orig_buff, benchedSize,0);
/* Bench */
{ int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb;
size_t cSize=0;
double ratio=0.;
DISPLAY("\r%79s\r", "");
DISPLAY(" %s : \n", inFileName);
/* Bench Compression Algorithms */
for (cAlgNb=0; (cAlgNb <= (int)NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) {
const char* compressorName;
int (*compressionFunction)(const char*, char*, int);
void (*initFunction)(void) = NULL;
double bestTime = 100000000.;
/* filter compressionAlgo only */
if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue;
/* Init data chunks */
{ int i;
size_t remaining = benchedSize;
char* in = orig_buff;
char* out = compressed_buff;
assert(nbChunks >= 1);
for (i=0; i<nbChunks; i++) {
chunkP[i].id = (U32)i;
chunkP[i].origBuffer = in; in += g_chunkSize;
assert(g_chunkSize > 0);
if (remaining > (size_t)g_chunkSize) {
chunkP[i].origSize = g_chunkSize;
remaining -= (size_t)g_chunkSize;
} else {
chunkP[i].origSize = (int)remaining;
remaining = 0;
}
chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
chunkP[i].compressedSize = 0;
}
}
g_chunk0 = chunkP[0].origBuffer;
g_chunk0Size = (size_t)chunkP[0].origSize;
if (cAlgNb==0) {
DISPLAY("Compression functions : \n");
continue;
}
if (cAlgNb >= (int)NB_COMPRESSION_ALGORITHMS) {
continue;
}
compressorName = compDescArray[cAlgNb].name;
compressionFunction = compDescArray[cAlgNb].compressionF;
initFunction = compDescArray[cAlgNb].initFunction;
if (compDescArray[cAlgNb].singleChunk) {
nbChunks=1;
chunkP[0].origSize = (int)benchedSize;
}
if (compressorName==NULL || compressionFunction==NULL) {
continue;
}
for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
double averageTime;
clock_t clockTime;
PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, compressorName, (int)benchedSize);
{ size_t i; for (i=0; i<benchedSize; i++) compressed_buff[i]=(char)i; } /* warming up memory */
nb_loops = 0;
clockTime = clock();
while(clock() == clockTime);
clockTime = clock();
while(BMK_GetClockSpan(clockTime) < loopDuration) {
if (initFunction!=NULL) initFunction();
for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
if (chunkP[chunkNb].compressedSize==0) {
DISPLAY("ERROR ! %s() = 0 !! \n", compressorName);
exit(1);
} }
nb_loops++;
}
clockTime = BMK_GetClockSpan(clockTime);
nb_loops += !nb_loops; /* avoid division by zero */
averageTime = ((double)clockTime) / nb_loops / CLOCKS_PER_SEC;
if (averageTime < bestTime) bestTime = averageTime;
cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += (size_t)chunkP[chunkNb].compressedSize;
ratio = (double)cSize/(double)benchedSize*100.;
PROGRESS("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
}
if (ratio<100.)
DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
else
DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 100000);
}
/* Prepare layout for decompression */
/* Init data chunks */
{ int i;
size_t remaining = benchedSize;
char* in = orig_buff;
char* out = compressed_buff;
nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
for (i=0; i<nbChunks; i++) {
chunkP[i].id = (U32)i;
chunkP[i].origBuffer = in; in += g_chunkSize;
if ((int)remaining > g_chunkSize) {
chunkP[i].origSize = g_chunkSize;
remaining -= (size_t)g_chunkSize;
} else {
chunkP[i].origSize = (int)remaining;
remaining = 0;
}
chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
chunkP[i].compressedSize = 0;
}
}
for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
chunkP[chunkNb].compressedSize = LZ4_compress_default(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize, maxCompressedChunkSize);
if (chunkP[chunkNb].compressedSize==0) {
DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress");
exit(1);
} }
/* Decompression Algorithms */
for (dAlgNb=0; (dAlgNb <= (int)NB_DECOMPRESSION_ALGORITHMS) && g_decompressionTest; dAlgNb++) {
const char* dName = NULL;
int (*decompressionFunction)(const char*, char*, int, int) = NULL;
double bestTime = 100000000.;
int checkResult = 1;
if ((g_decompressionAlgo != ALL_DECOMPRESSORS) && (g_decompressionAlgo != dAlgNb)) continue;
if (dAlgNb==0) { /* just for display */
DISPLAY("Decompression functions : \n");
continue;
}
if (dAlgNb >= (int)NB_DECOMPRESSION_ALGORITHMS)
continue;
dName = decDescArray[dAlgNb].name;
decompressionFunction = decDescArray[dAlgNb].decompressionF;
if (dName==NULL || decompressionFunction == NULL)
continue;
checkResult = decDescArray[dAlgNb].checkResult;
if (decDescArray[dAlgNb].frameFormat) {
/* prepare compressed data using LZ4F frame format */
size_t const fcsize = LZ4F_compressFrame(compressed_buff, (size_t)compressedBuffSize, orig_buff, benchedSize, NULL);
assert(!LZ4F_isError(fcsize));
chunkP[0].origSize = (int)benchedSize;
chunkP[0].compressedSize = (int)fcsize;
nbChunks = 1;
}
{ size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } /* zeroing source area, for CRC checking */
for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
double averageTime;
clock_t clockTime;
U32 crcDecoded;
PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, dName, (int)benchedSize);
nb_loops = 0;
clockTime = clock();
while(clock() == clockTime);
clockTime = clock();
while(BMK_GetClockSpan(clockTime) < loopDuration) {
for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
int const decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer,
chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
if (chunkP[chunkNb].origSize != decodedSize) {
DISPLAY("ERROR ! %s() == %i != %i !! \n",
dName, decodedSize, chunkP[chunkNb].origSize);
exit(1);
} }
nb_loops++;
}
clockTime = BMK_GetClockSpan(clockTime);
nb_loops += !nb_loops; /* Avoid division by zero */
averageTime = (double)clockTime / nb_loops / CLOCKS_PER_SEC;
if (averageTime < bestTime) bestTime = averageTime;
PROGRESS("%2i-%-34.34s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
/* CRC Checking */
crcDecoded = XXH32(orig_buff, benchedSize, 0);
if (checkResult && (crcOriginal!=crcDecoded)) {
DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n",
inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded);
exit(1);
} }
DISPLAY("%2i-%-34.34s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
}
}
free(orig_buff);
free(compressed_buff);
free(chunkP);
}
LZ4F_freeDecompressionContext(g_dCtx);
if (g_pause) { printf("press enter...\n"); (void)getchar(); }
return 0;
}
static int list(void)
{
size_t n;
DISPLAY("Compression Algorithm ids: \n");
for (n=0; n<NB_COMPRESSION_ALGORITHMS; n++) {
if (compDescArray[n].name) {
DISPLAY("%2u: %s \n", (unsigned)n, compDescArray[n].name);
}
}
DISPLAY("Decompression Algorithm ids: \n");
for (n=0; n<NB_DECOMPRESSION_ALGORITHMS; n++) {
if (decDescArray[n].name) {
DISPLAY("%2u: %s \n", (unsigned)n, decDescArray[n].name);
}
}
return 0;
}
static int usage(const char* exename)
{
DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
DISPLAY( "Arguments :\n");
DISPLAY( " -c : compression tests only\n");
DISPLAY( " -d : decompression tests only\n");
DISPLAY( " -H/-h : Help (this text + advanced options)\n");
return 0;
}
static int usage_advanced(void)
{
DISPLAY( "\nAdvanced options :\n");
DISPLAY( " -c# : test only compression function # [1-%i]\n", (int)NB_COMPRESSION_ALGORITHMS);
DISPLAY( " -d# : test only decompression function # [1-%i]\n", (int)NB_DECOMPRESSION_ALGORITHMS);
DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
DISPLAY( " -B# : Block size [4-7](default : 7)\n");
return list();
}
static int badusage(const char* exename)
{
DISPLAY("Wrong parameters\n");
usage(exename);
return 0;
}
int main(int argc, const char** argv)
{
int i,
filenamesStart=2;
const char* exename = argv[0];
const char* input_filename=0;
// Welcome message
DISPLAY(WELCOME_MESSAGE);
if (argc<2) { badusage(exename); return 1; }
for(i=1; i<argc; i++) {
const char* argument = argv[i];
if(!argument) continue; // Protection if argument empty
if (!strcmp(argument, "--no-prompt")) {
g_noPrompt = 1;
continue;
}
// Decode command (note : aggregated commands are allowed)
if (argument[0]=='-') {
while (argument[1]!=0) {
argument ++;
switch(argument[0])
{
// Select compression algorithm only
case 'c':
g_decompressionTest = 0;
while ((argument[1]>= '0') && (argument[1]<= '9')) {
g_compressionAlgo *= 10;
g_compressionAlgo += argument[1] - '0';
argument++;
}
break;
// Select decompression algorithm only
case 'd':
g_compressionTest = 0;
while ((argument[1]>= '0') && (argument[1]<= '9')) {
g_decompressionAlgo *= 10;
g_decompressionAlgo += argument[1] - '0';
argument++;
}
break;
// Display help on usage
case 'h' :
case 'H': usage(exename); return usage_advanced();
case 'l': return list();
// Modify Block Properties
case 'B':
while (argument[1]!=0)
switch(argument[1])
{
case '4':
case '5':
case '6':
case '7':
{ int B = argument[1] - '0';
int S = 1 << (8 + 2*B);
BMK_setBlocksize(S);
argument++;
break;
}
case 'D': argument++; break;
default : goto _exit_blockProperties;
}
_exit_blockProperties:
break;
// Modify Nb Iterations
case 'i':
if ((argument[1] >='0') && (argument[1] <='9')) {
int iters = argument[1] - '0';
BMK_setNbIterations(iters);
argument++;
}
break;
// Pause at the end (hidden option)
case 'p': BMK_setPause(); break;
// Unknown command
default : badusage(exename); return 1;
}
}
continue;
}
// first provided filename is input
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
}
// No input filename ==> Error
if(!input_filename) { badusage(exename); return 1; }
return fullSpeedBench(argv+filenamesStart, argc-filenamesStart);
}

1950
lz4/tests/fuzzer.c Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

62
lz4/tests/loremOut.c Normal file
View File

@@ -0,0 +1,62 @@
/*
lorem.c - lorem ipsum generator to stdout
Copyright (C) Yann Collet 2024
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 source repository : https://github.com/lz4/lz4
- Public forum : https://groups.google.com/forum/#!forum/lz4c
*/
/* Implementation notes:
* Generates a stream of Lorem ipsum paragraphs to stdout,
* up to the requested size, which can be very large (> 4 GB).
* Note that, beyond 1 paragraph, this generator produces
* a different content than LOREM_genBuffer (even when using same seed).
*/
#include "platform.h" /* Compiler options, SET_BINARY_MODE */
#include "loremOut.h"
#include "lorem.h" /* LOREM_genBlock */
#include <stdio.h>
#include <assert.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define LOREM_BLOCKSIZE (2 << 10)
void LOREM_genOut(unsigned long long size, unsigned seed)
{
char buff[LOREM_BLOCKSIZE] = {0};
unsigned long long total = 0;
size_t genBlockSize = (size_t)MIN(size, LOREM_BLOCKSIZE);
/* init */
SET_BINARY_MODE(stdout);
/* Generate Ipsum text, one paragraph at a time */
while (total < size) {
size_t generated = LOREM_genBlock(buff, genBlockSize, seed++, total == 0, 0);
assert(generated <= genBlockSize);
total += generated;
assert(total <= size);
fwrite(buff, 1, generated, stdout); /* note: should check potential write error */
if (size - total < genBlockSize)
genBlockSize = (size_t)(size - total);
}
assert(total == size);
}

30
lz4/tests/loremOut.h Normal file
View File

@@ -0,0 +1,30 @@
/*
loremOut.h - lorem ipsum generator to stdout
Copyright (C) Yann Collet 2024
GPL v2 License
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- LZ4 source repository : https://github.com/lz4/lz4
- Public forum : https://groups.google.com/forum/#!forum/lz4c
*/
/* LOREM_genOut():
* Generate @size bytes of compressible data using lorem ipsum generator into stdout.
*/
void LOREM_genOut(unsigned long long size, unsigned seed);

254
lz4/tests/roundTripTest.c Normal file
View File

@@ -0,0 +1,254 @@
/*
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree),
* meaning you may select, at your option, one of the above-listed licenses.
*/
/*
* This program takes a file in input,
* performs an LZ4 round-trip test (compress + decompress)
* compares the result with original
* and generates an abort() on corruption detection,
* in order for afl to register the event as a crash.
*/
/*===========================================
* Tuning Constant
*==========================================*/
#ifndef MIN_CLEVEL
# define MIN_CLEVEL (int)(-5)
#endif
/*===========================================
* Dependencies
*==========================================*/
#include <stddef.h> /* size_t */
#include <stdlib.h> /* malloc, free, exit */
#include <stdio.h> /* fprintf */
#include <string.h> /* strcmp */
#include <assert.h>
#include <sys/types.h> /* stat */
#include <sys/stat.h> /* stat */
#include "xxhash.h"
#include "lz4.h"
#include "lz4hc.h"
/*===========================================
* Macros
*==========================================*/
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define MSG(...) fprintf(stderr, __VA_ARGS__)
#define CONTROL_MSG(c, ...) { \
if ((c)) { \
MSG(__VA_ARGS__); \
MSG(" \n"); \
abort(); \
} \
}
static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize)
{
const char* const ip1 = (const char*)buff1;
const char* const ip2 = (const char*)buff2;
size_t pos;
for (pos=0; pos<buffSize; pos++)
if (ip1[pos]!=ip2[pos])
break;
return pos;
}
/* select a compression level
* based on first bytes present in a reference buffer */
static int select_clevel(const void* refBuff, size_t refBuffSize)
{
const int minCLevel = MIN_CLEVEL;
const int maxClevel = LZ4HC_CLEVEL_MAX;
const int cLevelSpan = maxClevel - minCLevel;
size_t const hashLength = MIN(16, refBuffSize);
unsigned const h32 = XXH32(refBuff, hashLength, 0);
int const randL = h32 % (cLevelSpan+1);
return minCLevel + randL;
}
typedef int (*compressFn)(const char* src, char* dst, int srcSize, int dstSize, int cLevel);
/** roundTripTest() :
* Compresses `srcBuff` into `compressedBuff`,
* then decompresses `compressedBuff` into `resultBuff`.
* If clevel==0, compression level is derived from srcBuff's content head bytes.
* This function abort() if it detects any round-trip error.
* Therefore, if it returns, round trip is considered successfully validated.
* Note : `compressedBuffCapacity` should be `>= LZ4_compressBound(srcSize)`
* for compression to be guaranteed to work */
static void roundTripTest(void* resultBuff, size_t resultBuffCapacity,
void* compressedBuff, size_t compressedBuffCapacity,
const void* srcBuff, size_t srcSize,
int clevel)
{
int const proposed_clevel = clevel ? clevel : select_clevel(srcBuff, srcSize);
int const selected_clevel = proposed_clevel < 0 ? -proposed_clevel : proposed_clevel; /* if level < 0, it becomes an acceleration value */
compressFn compress = selected_clevel >= LZ4HC_CLEVEL_MIN ? LZ4_compress_HC : LZ4_compress_fast;
int const cSize = compress((const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, selected_clevel);
CONTROL_MSG(cSize == 0, "Compression error !");
{ int const dSize = LZ4_decompress_safe((const char*)compressedBuff, (char*)resultBuff, cSize, (int)resultBuffCapacity);
CONTROL_MSG(dSize < 0, "Decompression detected an error !");
CONTROL_MSG(dSize != (int)srcSize, "Decompression corruption error : wrong decompressed size !");
}
/* check potential content corruption error */
assert(resultBuffCapacity >= srcSize);
{ size_t const errorPos = checkBuffers(srcBuff, resultBuff, srcSize);
CONTROL_MSG(errorPos != srcSize,
"Silent decoding corruption, at pos %u !!!",
(unsigned)errorPos);
}
}
static void roundTripCheck(void* srcBuff, size_t srcSize, int clevel)
{
size_t const cBuffSize = LZ4_COMPRESSBOUND(srcSize);
void* const cBuff = malloc(cBuffSize);
void* const rBuff = malloc(cBuffSize);
if (!cBuff || !rBuff) {
fprintf(stderr, "not enough memory ! \n");
free(srcBuff);
exit(1);
}
roundTripTest(rBuff, cBuffSize,
cBuff, cBuffSize,
srcBuff, srcSize,
clevel);
free(rBuff);
free(cBuff);
}
static size_t getFileSize(const char* infilename)
{
int r;
#if defined(_MSC_VER)
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
#endif
return (size_t)statbuf.st_size;
}
static int isDirectory(const char* infilename)
{
int r;
#if defined(_MSC_VER)
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
if (!r && S_ISDIR(statbuf.st_mode)) return 1;
#endif
return 0;
}
/** loadFile() :
* requirement : `buffer` size >= `fileSize` */
static void loadFile(void* buffer, const char* fileName, size_t fileSize)
{
FILE* const f = fopen(fileName, "rb");
if (isDirectory(fileName)) {
MSG("Ignoring %s directory \n", fileName);
free(buffer);
fclose(f);
exit(2);
}
if (f==NULL) {
MSG("Impossible to open %s \n", fileName);
free(buffer);
exit(3);
}
{ size_t const readSize = fread(buffer, 1, fileSize, f);
if (readSize != fileSize) {
MSG("Error reading %s \n", fileName);
free(buffer);
fclose(f);
exit(5);
} }
fclose(f);
}
static void fileCheck(const char* fileName, int clevel)
{
size_t const fileSize = getFileSize(fileName);
void* const buffer = malloc(fileSize + !fileSize /* avoid 0 */);
if (!buffer) {
MSG("not enough memory \n");
exit(4);
}
loadFile(buffer, fileName, fileSize);
roundTripCheck(buffer, fileSize, clevel);
free (buffer);
}
int bad_usage(const char* exeName)
{
MSG(" \n");
MSG("bad usage: \n");
MSG(" \n");
MSG("%s [Options] fileName \n", exeName);
MSG(" \n");
MSG("Options: \n");
MSG("-# : use #=[0-9] compression level (default:0 == random) \n");
return 1;
}
int main(int argCount, const char** argv)
{
const char* const exeName = argv[0];
int argNb = 1;
int clevel = 0;
assert(argCount >= 1);
if (argCount < 2) return bad_usage(exeName);
if (argv[1][0] == '-') {
clevel = argv[1][1] - '0';
argNb = 2;
}
if (argNb >= argCount) return bad_usage(exeName);
fileCheck(argv[argNb], clevel);
MSG("no pb detected \n");
return 0;
}

188
lz4/tests/test-lz4-abi.py Normal file
View File

@@ -0,0 +1,188 @@
#!/usr/bin/env python3
"""Test LZ4 interoperability between versions"""
#
# Copyright (C) 2011-present, Takayuki Matsuoka
# All rights reserved.
# GPL v2 License
#
import argparse
import glob
import subprocess
import filecmp
import os
import shutil
import sys
import hashlib
repo_url = 'https://github.com/lz4/lz4.git'
tmp_dir_name = 'tests/abiTests'
env_flags = ' ' # '-j CFLAGS="-g -O0 -fsanitize=address"'
make_cmd = 'make'
git_cmd = 'git'
test_dat_src = ['README.md']
head = 'v999'
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
args = parser.parse_args()
def debug_message(msg):
if args.verbose:
print(msg)
def proc(cmd_args, pipe=True, env=False):
if env == False:
env = os.environ.copy()
debug_message("Executing command {} with env {}".format(cmd_args, env))
if pipe:
s = subprocess.Popen(cmd_args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env = env)
else:
s = subprocess.Popen(cmd_args, env = env)
stdout_data, stderr_data = s.communicate()
if s.poll() != 0:
print('Error Code:', s.poll())
print('Standard Error:', stderr_data.decode())
sys.exit(1)
return stdout_data, stderr_data
def make(args, pipe=True, env=False):
if env == False:
env = os.environ.copy()
# we want the address sanitizer for abi tests
if 'CFLAGS' in env:
env["CFLAGS"] += " -fsanitize=address"
if 'LDFLAGS' in env:
env["LDFLAGS"] += " -fsanitize=address"
return proc([make_cmd] + ['-j'] + ['V=1'] + ['DEBUGFLAGS='] + args, pipe, env)
def git(args, pipe=True):
return proc([git_cmd] + args, pipe)
def get_git_tags():
# Only start from first v1.7.x format release
stdout, stderr = git(['tag', '-l', 'v[1-9].[0-9].[0-9]'])
tags = stdout.decode('utf-8').split()
return tags
# https://stackoverflow.com/a/19711609/2132223
def sha1_of_file(filepath):
with open(filepath, 'rb') as f:
return hashlib.sha1(f.read()).hexdigest()
if __name__ == '__main__':
if sys.platform == "darwin":
print("!!! Warning: this test is not validated for macos !!!")
error_code = 0
base_dir = os.getcwd() + '/..' # /path/to/lz4
tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/lz4/tests/versionsTest
clone_dir = tmp_dir + '/' + 'lz4' # /path/to/lz4/tests/versionsTest/lz4
lib_dir = base_dir + '/lib' # /path/to/lz4/lib
test_dir = base_dir + '/tests'
os.makedirs(tmp_dir, exist_ok=True)
# since Travis clones limited depth, we should clone full repository
if not os.path.isdir(clone_dir):
git(['clone', repo_url, clone_dir])
# Retrieve all release tags
print('Retrieve release tags >= v1.7.5 :')
os.chdir(clone_dir)
tags = [head] + get_git_tags()
tags = [x for x in tags if (x >= 'v1.7.5')]
print(tags)
# loop across architectures
# note : '-mx32' was removed, because some CI environment (GA) do not support x32 well
for march in ['-m64', '-m32']:
print(' ')
print('=====================================')
print('Testing architecture ' + march);
print('=====================================')
# Build all versions of liblz4
# note : naming scheme only works on Linux
for tag in tags:
print('building library ', tag)
os.chdir(base_dir)
# if not os.path.isfile(dst_liblz4) or tag == head:
if tag != head:
r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/<TAG>
#print('r_dir = ', r_dir) # for debug
os.makedirs(r_dir, exist_ok=True)
os.chdir(clone_dir)
git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'])
os.chdir(r_dir + '/lib') # /path/to/lz4/lz4test/<TAG>/lib
else:
# print('lib_dir = {}', lib_dir) # for debug
os.chdir(lib_dir)
make(['clean'])
build_env = os.environ.copy()
build_env["CFLAGS"] = "-O1 " + march
make(['liblz4'], env=build_env)
print(' ')
print('******************************')
print('Round trip expecting current ABI but linking to older Dynamic Library version')
print('******************************')
os.chdir(test_dir)
# Start with matching version : should be no problem
build_env = os.environ.copy()
# we use asan to detect any out-of-bound read or write
build_env["CFLAGS"] = "-O1 " + march
build_env["LDFLAGS"] = "-L../lib"
build_env["LDLIBS"] = "-llz4"
if os.path.isfile('abiTest'):
os.remove('abiTest')
make(['abiTest'], env=build_env, pipe=False)
for tag in tags:
print('linking to lib tag = ', tag)
run_env = os.environ.copy()
if tag == head:
run_env["LD_LIBRARY_PATH"] = '../lib'
else:
run_env["LD_LIBRARY_PATH"] = 'abiTests/{}/lib'.format(tag)
# check we are linking to the right library version at run time
proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env)
# now run with mismatched library version
proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env)
print(' ')
print('******************************')
print('Round trip using current Dynamic Library expecting older ABI version')
print('******************************')
for tag in tags:
print(' ')
print('building using older lib ', tag)
build_env = os.environ.copy()
if tag != head:
build_env["CPPFLAGS"] = '-IabiTests/{}/lib'.format(tag)
build_env["LDFLAGS"] = '-LabiTests/{}/lib'.format(tag)
else:
build_env["CPPFLAGS"] = '-I../lib'
build_env["LDFLAGS"] = '-L../lib'
build_env["LDLIBS"] = "-llz4"
build_env["CFLAGS"] = "-O1 " + march
os.remove('abiTest')
make(['abiTest'], pipe=False, env=build_env)
print('run with CURRENT library version (head)')
run_env = os.environ.copy()
run_env["LD_LIBRARY_PATH"] = '../lib'
# check we are linking to the right library version at run time
proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env)
# now run with mismatched library version
proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env)
if error_code != 0:
print('ERROR')
sys.exit(error_code)

85
lz4/tests/test-lz4-basic.sh Executable file
View File

@@ -0,0 +1,85 @@
#!/bin/sh
FPREFIX="tmp-tlb"
set -e
remove () {
rm -rf $FPREFIX*
}
trap remove EXIT
set -x
datagen -g0 | lz4 -v | lz4 -t
datagen -g16KB | lz4 -9 | lz4 -t
datagen -g20KB > $FPREFIX-dg20k
lz4 < $FPREFIX-dg20k | lz4 -d > $FPREFIX-dec
diff -q $FPREFIX-dg20k $FPREFIX-dec
lz4 --no-frame-crc < $FPREFIX-dg20k | lz4 -d > $FPREFIX-dec
diff -q $FPREFIX-dg20k $FPREFIX-dec
datagen | lz4 -BI | lz4 -t
datagen | lz4 --no-crc | lz4 -t
datagen -g6M -P99 | lz4 -9BD | lz4 -t
datagen -g17M | lz4 -9v | lz4 -qt
datagen -g33M | lz4 --no-frame-crc | lz4 -t
datagen -g256MB | lz4 -vqB4D | lz4 -t --no-crc
echo "hello world" > $FPREFIX-hw
lz4 --rm -f $FPREFIX-hw $FPREFIX-hw.lz4
test ! -f $FPREFIX-hw # must fail (--rm)
test -f $FPREFIX-hw.lz4
lz4 -d --rm -f $FPREFIX-hw.lz4
test ! -f $FPREFIX-hw.lz4
lz4 --rm -f $FPREFIX-hw > /dev/null
test -f $FPREFIX-hw.lz4 # no more implicit stdout
lz4cat $FPREFIX-hw.lz4 | grep "hello world"
unlz4 --rm $FPREFIX-hw.lz4 $FPREFIX-hw
test -f $FPREFIX-hw
test ! -f $FPREFIX-hw.lz4 # must fail (--rm)
test ! -f $FPREFIX-hw.lz4.lz4 # must fail (unlz4)
lz4cat $FPREFIX-hw # pass-through mode
test -f $FPREFIX-hw
test ! -f $FPREFIX-hw.lz4 # must fail (lz4cat)
lz4 $FPREFIX-hw $FPREFIX-hw.lz4 # creates $FPREFIX-hw.lz4
lz4cat < $FPREFIX-hw.lz4 > ${FPREFIX}3 # checks lz4cat works with stdin (#285)
diff -q $FPREFIX-hw ${FPREFIX}3
lz4cat < $FPREFIX-hw > ${FPREFIX}2 # checks lz4cat works in pass-through mode
diff -q $FPREFIX-hw ${FPREFIX}2
cp $FPREFIX-hw ./-d
lz4 --rm -- -d -d.lz4 # compresses ./d into ./-d.lz4
test -f ./-d.lz4
test ! -f ./-d
mv ./-d.lz4 ./-z
lz4 -d --rm -- -z ${FPREFIX}4 # uncompresses ./-z into $FPREFIX4
test ! -f ./-z
diff -q $FPREFIX-hw ${FPREFIX}4
lz4 ${FPREFIX}2 ${FPREFIX}3 ${FPREFIX}4 && exit 1 # must fail: refuse to handle 3+ file names
mkdir -p ${FPREFIX}-dir
lz4 ${FPREFIX}-dir && exit 1 # must fail: refuse to compress directory
test ! -f ${FPREFIX}-dir.lz4 # must not create artifact (#1211)
lz4 -f $FPREFIX-hw # create $FPREFIX-hw.lz4, for next tests
lz4 --list $FPREFIX-hw.lz4 # test --list on valid single-frame file
lz4 --list < $FPREFIX-hw.lz4 # test --list from stdin (file only)
cat $FPREFIX-hw >> $FPREFIX-hw.lz4
lz4 -f $FPREFIX-hw.lz4 && exit 1 # uncompress valid frame followed by invalid data (must fail now)
lz4 -BX $FPREFIX-hw -c -q | lz4 -tv # test block checksum
# datagen -g20KB generates the same file every single time
# cannot save output of datagen -g20KB as input file to lz4 because the following shell commands are run before datagen -g20KB
test "$(datagen -g20KB | lz4 -c --fast | wc -c)" -lt "$(datagen -g20KB | lz4 -c --fast=9 | wc -c)" # -1 vs -9
test "$(datagen -g20KB | lz4 -c -1 | wc -c)" -lt "$(datagen -g20KB| lz4 -c --fast=1 | wc -c)" # 1 vs -1
test "$(datagen -g20KB | lz4 -c --fast=1 | wc -c)" -eq "$(datagen -g20KB| lz4 -c --fast| wc -c)" # checks default fast compression is -1
lz4 -c --fast=0 $FPREFIX-dg20K && exit 1 # lz4 should fail when fast=0
lz4 -c --fast=-1 $FPREFIX-dg20K && exit 1 # lz4 should fail when fast=-1
# Multithreading commands
datagen -g16M | lz4 -T2 | lz4 -t
datagen -g16M | lz4 --threads=2 | lz4 -t
# High --fast values can result in out-of-bound dereferences #876
datagen -g1M | lz4 -c --fast=999999999 > $FPREFIX-trash
# Test for #596
echo "TEST" > $FPREFIX-test
lz4 -m $FPREFIX-test
lz4 $FPREFIX-test.lz4 $FPREFIX-test2
diff -q $FPREFIX-test $FPREFIX-test2
# bug #1374
datagen -g4194302 | lz4 -B4 -c > $FPREFIX-test3

View File

@@ -0,0 +1,27 @@
#!/bin/sh
FPREFIX="tmp-lzc"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
datagen -g15M > $FPREFIX
lz4 -v $FPREFIX -c | lz4 -t
lz4 -v --content-size $FPREFIX -c | lz4 -d > $FPREFIX-dup
diff $FPREFIX $FPREFIX-dup
lz4 -f $FPREFIX -c > $FPREFIX.lz4 # compressed with content size
lz4 --content-size $FPREFIX -c > $FPREFIX-wcz.lz4
diff $FPREFIX.lz4 $FPREFIX-wcz.lz4 && exit 1 # must differ, due to content size
lz4 --content-size < $FPREFIX > $FPREFIX-wcz2.lz4 # can determine content size because stdin is just a file
diff $FPREFIX-wcz.lz4 $FPREFIX-wcz2.lz4 # both must contain content size
cat $FPREFIX | lz4 > $FPREFIX-ncz.lz4
diff $FPREFIX.lz4 $FPREFIX-ncz.lz4 # both don't have content size
cat $FPREFIX | lz4 --content-size > $FPREFIX-ncz2.lz4 # can't determine content size
diff $FPREFIX.lz4 $FPREFIX-ncz2.lz4 # both don't have content size

46
lz4/tests/test-lz4-dict.sh Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/sh
FPREFIX="tmp-dict"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
datagen -g16KB > $FPREFIX
datagen -g32KB > $FPREFIX-sample-32k
< $FPREFIX-sample-32k lz4 -D $FPREFIX | lz4 -dD $FPREFIX | diff - $FPREFIX-sample-32k
datagen -g16MB > $FPREFIX-sample-16m
lz4 -v -B5 $FPREFIX-sample-16m -D $FPREFIX -c | lz4 -d -D $FPREFIX | diff - $FPREFIX-sample-16m
# Check dictionary compression efficiency
size_dict=$( lz4 -3 -B4 $FPREFIX-sample-16m -D $FPREFIX -c | wc -c)
size_nodict=$( lz4 -3 -B4 $FPREFIX-sample-16m -c | wc -c)
if [ "$size_dict" -lt "$size_nodict" ]; then
echo "Test Passed: dictionary is effective."
else
echo "Test Failed: dictionary wasn't effective."
exit 1
fi
touch $FPREFIX-sample-0
< $FPREFIX-sample-0 lz4 -D $FPREFIX | lz4 -dD $FPREFIX | diff - $FPREFIX-sample-0
< $FPREFIX-sample-32k lz4 -D $FPREFIX-sample-0 | lz4 -dD $FPREFIX-sample-0 | diff - $FPREFIX-sample-32k
< $FPREFIX-sample-0 lz4 -D $FPREFIX-sample-0 | lz4 -dD $FPREFIX-sample-0 | diff - $FPREFIX-sample-0
lz4 -bi0 -D $FPREFIX $FPREFIX-sample-32k $FPREFIX-sample-32k
echo "---- test lz4 dictionary loading ----"
datagen -g128KB > $FPREFIX-data-128KB
set -e; \
for l in 0 1 4 128 32767 32768 32769 65535 65536 65537 98303 98304 98305 131071 131072 131073; do \
datagen -g$l > $FPREFIX-$l; \
dd if=$FPREFIX-$l of=$FPREFIX-$l-tail bs=1 count=65536 skip=$((l > 65536 ? l - 65536 : 0)); \
< $FPREFIX-$l lz4 -D stdin $FPREFIX-data-128KB -c | lz4 -dD $FPREFIX-$l-tail | diff - $FPREFIX-data-128KB; \
< $FPREFIX-$l-tail lz4 -D stdin $FPREFIX-data-128KB -c | lz4 -dD $FPREFIX-$l | diff - $FPREFIX-data-128KB; \
done

View File

@@ -0,0 +1,21 @@
#!/bin/sh
FPREFIX="tmp-lfh"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
datagen -g6GB | lz4 -vB5 | lz4 -qt
# test large file size [2-4] GB
datagen -g3G -P100 | lz4 -vv | lz4 --decompress --force --sparse - ${FPREFIX}1
ls -ls ${FPREFIX}1
datagen -g3G -P100 | lz4 --quiet --content-size | lz4 --verbose --decompress --force --sparse - ${FPREFIX}2
ls -ls ${FPREFIX}2
diff -s ${FPREFIX}1 ${FPREFIX}2

View File

@@ -0,0 +1,21 @@
#!/bin/sh
FPREFIX="tmp-lfc"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
echo > $FPREFIX-empty
echo hi > $FPREFIX-nonempty
cat $FPREFIX-nonempty $FPREFIX-empty $FPREFIX-nonempty > $FPREFIX-src
lz4 -zq $FPREFIX-empty -c > $FPREFIX-empty.lz4
lz4 -zq $FPREFIX-nonempty -c > $FPREFIX-nonempty.lz4
cat $FPREFIX-nonempty.lz4 $FPREFIX-empty.lz4 $FPREFIX-nonempty.lz4 > $FPREFIX-concat.lz4
lz4 -d $FPREFIX-concat.lz4 -c > $FPREFIX-result
cmp $FPREFIX-src $FPREFIX-result

263
lz4/tests/test-lz4-list.py Normal file
View File

@@ -0,0 +1,263 @@
#!/usr/bin/env python3
import subprocess
import time
import glob
import os
import tempfile
import unittest
import sys
SIZES = [3, 11] # Always 2 sizes
MIB = 1048576
LZ4 = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/../lz4")
if not os.path.exists(LZ4):
LZ4 = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/../programs/lz4")
TEMP = tempfile.gettempdir()
class NVerboseFileInfo:
def __init__(self, line_in):
self.line = line_in
splitlines = line_in.split()
if len(splitlines) != 7:
errout(f"Unexpected line: {line_in}")
self.frames, self.type, self.block, self.compressed, self.uncompressed, self.ratio, self.filename = splitlines
self.exp_unc_size = 0
# Get real file sizes
if "concat-all" in self.filename or "2f--content-size" in self.filename:
for i in SIZES:
self.exp_unc_size += os.path.getsize(f"{TEMP}/test_list_{i:02d}M")
else:
uncompressed_filename = self.filename.split("-")[0]
self.exp_unc_size += os.path.getsize(f"{TEMP}/{uncompressed_filename}")
self.exp_comp_size = os.path.getsize(f"{TEMP}/{self.filename}")
def sorted_list(pattern):
return sorted(glob.glob(pattern))
class TestNonVerbose(unittest.TestCase):
@classmethod
def setUpClass(self):
self.nvinfo_list = []
test_list_files = sorted_list(f"{TEMP}/test_list_*.lz4")
# One of the files has 2 frames so duplicate it in this list to map each frame 1 to a single file
for i, filename in enumerate(test_list_files):
for i, line in enumerate(execute(f"{LZ4} --list -m {filename}", print_output=True)):
if i > 0:
self.nvinfo_list.append(NVerboseFileInfo(line))
def test_frames(self):
all_concat_frames = 0
all_concat_index = None
for i, nvinfo in enumerate(self.nvinfo_list):
if "concat-all" in nvinfo.filename:
all_concat_index = i
elif "2f--content-size" in nvinfo.filename:
self.assertEqual("2", nvinfo.frames, nvinfo.line)
all_concat_frames += 2
else:
self.assertEqual("1", nvinfo.frames, nvinfo.line)
all_concat_frames += 1
self.assertNotEqual(None, all_concat_index, "Couldn't find concat-all file index.")
self.assertEqual(self.nvinfo_list[all_concat_index].frames, str(all_concat_frames), self.nvinfo_list[all_concat_index].line)
def test_frame_types(self):
for nvinfo in self.nvinfo_list:
if "-lz4f-" in nvinfo.filename:
self.assertEqual(nvinfo.type, "LZ4Frame", nvinfo.line)
elif "-legc-" in nvinfo.filename:
self.assertEqual(nvinfo.type, "LegacyFrame", nvinfo.line)
elif "-skip-" in nvinfo.filename:
self.assertEqual(nvinfo.type, "SkippableFrame", nvinfo.line)
def test_block(self):
for nvinfo in self.nvinfo_list:
# if "-leg" in nvinfo.filename or "-skip" in nvinfo.filename:
# self.assertEqual(nvinfo.block, "-", nvinfo.line)
if "--BD" in nvinfo.filename:
self.assertRegex(nvinfo.block, "^B[0-9]+D$", nvinfo.line)
elif "--BI" in nvinfo.filename:
self.assertRegex(nvinfo.block, "^B[0-9]+I$", nvinfo.line)
def test_compressed_size(self):
for nvinfo in self.nvinfo_list:
self.assertEqual(nvinfo.compressed, to_human(nvinfo.exp_comp_size), nvinfo.line)
def test_ratio(self):
for nvinfo in self.nvinfo_list:
if "--content-size" in nvinfo.filename:
self.assertEqual(nvinfo.ratio, f"{float(nvinfo.exp_comp_size) / float(nvinfo.exp_unc_size) * 100:.2f}%", nvinfo.line)
def test_uncompressed_size(self):
for nvinfo in self.nvinfo_list:
if "--content-size" in nvinfo.filename:
self.assertEqual(nvinfo.uncompressed, to_human(nvinfo.exp_unc_size), nvinfo.line)
class VerboseFileInfo:
def __init__(self, lines):
# Parse lines
self.frame_list = []
self.file_frame_map = []
for i, line in enumerate(lines):
if i == 0:
self.filename = line
continue
elif i == 1:
# Skip header
continue
frame_info = dict(zip(["frame", "type", "block", "checksum", "compressed", "uncompressed", "ratio"], line.split()))
frame_info["line"] = line
self.frame_list.append(frame_info)
class TestVerbose(unittest.TestCase):
@classmethod
def setUpClass(self):
# Even do we're listing 2 files to test multiline working as expected.
# we're only really interested in testing the output of the concat-all file.
self.vinfo_list = []
start = end = 0
test_list_SM_lz4f = sorted_list(f"{TEMP}/test_list_*M-lz4f-2f--content-size.lz4")
for i, filename in enumerate(test_list_SM_lz4f):
output = execute(f"{LZ4} --list -m -v {TEMP}/test_list_concat-all.lz4 {filename}", print_output=True)
for i, line in enumerate(output):
if line.startswith("test_list"):
if start != 0 and end != 0:
self.vinfo_list.append(VerboseFileInfo(output[start:end]))
start = i
if not line:
end = i
self.vinfo_list.append(VerboseFileInfo(output[start:end]))
# Populate file_frame_map as a reference of the expected info
concat_file_list = sorted_list(f"{TEMP}/test_list_[!concat]*.lz4")
# One of the files has 2 frames so duplicate it in this list to map each frame 1 to a single file
for i, filename in enumerate(concat_file_list):
if "2f--content-size" in filename:
concat_file_list.insert(i, filename)
break
self.cvinfo = self.vinfo_list[0]
self.cvinfo.file_frame_map = concat_file_list
self.cvinfo.compressed_size = os.path.getsize(f"{TEMP}/test_list_concat-all.lz4")
def test_frame_number(self):
for vinfo in self.vinfo_list:
for i, frame_info in enumerate(vinfo.frame_list):
self.assertEqual(frame_info["frame"], str(i + 1), frame_info["line"])
def test_block(self):
for i, frame_info in enumerate(self.cvinfo.frame_list):
if "--BD" in self.cvinfo.file_frame_map[i]:
self.assertRegex(self.cvinfo.frame_list[i]["block"], "^B[0-9]+D$", self.cvinfo.frame_list[i]["line"])
elif "--BI" in self.cvinfo.file_frame_map[i]:
self.assertEqual(self.cvinfo.frame_list[i]["block"], "^B[0-9]+I$", self.cvinfo.frame_list[i]["line"])
def test_checksum(self):
for i, frame_info in enumerate(self.cvinfo.frame_list):
if "-lz4f-" in self.cvinfo.file_frame_map[i] and "--no-frame-crc" not in self.cvinfo.file_frame_map[i]:
self.assertEqual(self.cvinfo.frame_list[i]["checksum"], "XXH32", self.cvinfo.frame_list[i]["line"])
def test_ratio(self):
for i, frame_info in enumerate(self.cvinfo.frame_list):
if "--content-size" in self.cvinfo.file_frame_map[i]:
self.assertEqual(self.cvinfo.frame_list[i]['ratio'],
f"{float(self.cvinfo.frame_list[i]['compressed']) / float(self.cvinfo.frame_list[i]['uncompressed']) * 100:.2f}%",
self.cvinfo.frame_list[i]["line"])
def to_human(size):
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']:
if size < 1024.0:
break
size /= 1024.0
return f"{size:.2f}{unit}"
def log(text):
print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
def errout(text, err=1):
log(text)
exit(err)
def execute(command, print_command=True, print_output=False, print_error=True):
if os.environ.get('QEMU_SYS'):
command = f"{os.environ['QEMU_SYS']} {command}"
if print_command:
log("> " + command)
popen = subprocess.Popen(command.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_lines, stderr_lines = popen.communicate()
stderr_lines = stderr_lines.decode("utf-8")
stdout_lines = stdout_lines.decode("utf-8")
if print_output:
if stdout_lines:
print(stdout_lines)
if stderr_lines:
print(stderr_lines)
if popen.returncode is not None and popen.returncode != 0:
if stderr_lines and not print_output and print_error:
print(stderr_lines)
errout(f"Failed to run: {command}, {stdout_lines + stderr_lines}\n")
return (stdout_lines).splitlines()
def cleanup(silent=False):
for f in sorted_list(f"{TEMP}/test_list*"):
if not silent:
log(f"Deleting {f}")
os.unlink(f)
def datagen(file_name, size):
non_sparse_size = size // 2
sparse_size = size - non_sparse_size
with open(file_name, "wb") as f:
f.seek(sparse_size)
f.write(os.urandom(non_sparse_size))
def generate_files():
# file format ~ test_list<frametype>-<no_frames>f<create-args>.lz4 ~
# Generate LZ4Frames
for i in SIZES:
filename = f"{TEMP}/test_list_{i:02d}M"
log(f"Generating {filename}")
datagen(filename, i * MIB)
for j in ["--content-size", "-BI", "-BD", "-BX", "--no-frame-crc"]:
lz4file = f"{filename}-lz4f-1f{j}.lz4"
execute(f"{LZ4} {j} {filename} {lz4file}")
# Generate skippable frames
lz4file = f"{filename}-skip-1f.lz4"
skipsize = i * 1024
skipbytes = bytes([80, 42, 77, 24]) + skipsize.to_bytes(4, byteorder='little', signed=False)
with open(lz4file, 'wb') as f:
f.write(skipbytes)
f.write(os.urandom(skipsize))
# Generate legacy frames
lz4file = f"{filename}-legc-1f.lz4"
execute(f"{LZ4} -l {filename} {lz4file}")
# Concatenate --content-size files
file_list = sorted_list(f"{TEMP}/test_list_*-lz4f-1f--content-size.lz4")
with open(f"{TEMP}/test_list_{sum(SIZES)}M-lz4f-2f--content-size.lz4", 'ab') as outfile:
for fname in file_list:
with open(fname, 'rb') as infile:
outfile.write(infile.read())
# Concatenate all files
file_list = sorted_list(f"{TEMP}/test_list_*.lz4")
with open(f"{TEMP}/test_list_concat-all.lz4", 'ab') as outfile:
for fname in file_list:
with open(fname, 'rb') as infile:
outfile.write(infile.read())
if __name__ == '__main__':
cleanup()
generate_files()
ret = unittest.main(verbosity=2, exit=False)
cleanup(silent=True)
sys.exit(not ret.result.wasSuccessful())

View File

@@ -0,0 +1,51 @@
#!/bin/sh
FPREFIX="tmp-lml"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
datagen -s1 > "${FPREFIX}1" 2> $FPREFIX-trash
datagen -s2 -g100K > "${FPREFIX}2" 2> $FPREFIX-trash
datagen -s3 -g200K > "${FPREFIX}3" 2> $FPREFIX-trash
# compress multiple files using legacy format: one .lz4 per source file
lz4 -f -l -m $FPREFIX*
test -f ${FPREFIX}1.lz4
test -f ${FPREFIX}2.lz4
test -f ${FPREFIX}3.lz4
# decompress multiple files compressed using legacy format: one output file per .lz4
mv ${FPREFIX}1 ${FPREFIX}1-orig
mv ${FPREFIX}2 ${FPREFIX}2-orig
mv ${FPREFIX}3 ${FPREFIX}3-orig
lz4 -d -f -m $FPREFIX*.lz4
lz4 -l -d -f -m $FPREFIX*.lz4 # -l mustn't impact -d option
cmp ${FPREFIX}1 ${FPREFIX}1-orig # must be identical
cmp ${FPREFIX}2 ${FPREFIX}2-orig
cmp ${FPREFIX}3 ${FPREFIX}3-orig
# compress multiple files into stdout using legacy format
cat ${FPREFIX}1.lz4 ${FPREFIX}2.lz4 ${FPREFIX}3.lz4 > $FPREFIX-concat1
rm -f $FPREFIX*.lz4
lz4 -l -m ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3 -c > $FPREFIX-concat2
test ! -f ${FPREFIX}1.lz4 # must not create .lz4 artefact
cmp $FPREFIX-concat1 $FPREFIX-concat2 # must be equivalent
# # # decompress multiple files into stdout using legacy format
rm $FPREFIX-concat1 $FPREFIX-concat2
lz4 -l -f -m ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3 # generate .lz4 to decompress
cat ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3 > ${FPREFIX}-concat1 # create concatenated reference
rm ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3
lz4 -d -m ${FPREFIX}1.lz4 ${FPREFIX}2.lz4 ${FPREFIX}3.lz4 -c > $FPREFIX-concat2
lz4 -d -l -m ${FPREFIX}1.lz4 ${FPREFIX}2.lz4 ${FPREFIX}3.lz4 -c > $FPREFIX-concat2 # -l mustn't impact option -d
test ! -f ${FPREFIX}1 # must not create file artefact
cmp $FPREFIX-concat1 $FPREFIX-concat2 # must be equivalent
# # # compress multiple files, one of which is absent (must fail)
rm -f $FPREFIX-concat2.lz4
lz4 -f -l -m $FPREFIX-concat1 notHere-legacy $FPREFIX-concat2 && exit 1 # must fail : notHere-legacy not present
test -f $FPREFIX-concat2.lz4 # notHere was a non-blocking error, concat2.lz4 should be present
true

58
lz4/tests/test-lz4-multiple.sh Executable file
View File

@@ -0,0 +1,58 @@
#!/bin/sh
FPREFIX="tmp-tml"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
datagen -s1 > ${FPREFIX}1 2> $FPREFIX-trash
datagen -s2 -g100K > ${FPREFIX}2 2> $FPREFIX-trash
datagen -s3 -g200K > ${FPREFIX}3 2> $FPREFIX-trash
# compress multiple files : one .lz4 per source file
lz4 -f -m $FPREFIX*
test -f ${FPREFIX}1.lz4
test -f ${FPREFIX}2.lz4
test -f ${FPREFIX}3.lz4
# decompress multiple files : one output file per .lz4
mv ${FPREFIX}1 ${FPREFIX}1-orig
mv ${FPREFIX}2 ${FPREFIX}2-orig
mv ${FPREFIX}3 ${FPREFIX}3-orig
lz4 -d -f -m $FPREFIX*.lz4
cmp ${FPREFIX}1 ${FPREFIX}1-orig # must be identical
cmp ${FPREFIX}2 ${FPREFIX}2-orig
cmp ${FPREFIX}3 ${FPREFIX}3-orig
# compress multiple files into stdout
cat ${FPREFIX}1.lz4 ${FPREFIX}2.lz4 ${FPREFIX}3.lz4 > $FPREFIX-concat1
rm $FPREFIX*.lz4
lz4 -m ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3 -c > $FPREFIX-concat2
test ! -f ${FPREFIX}1.lz4 # must not create .lz4 artefact
cmp $FPREFIX-concat1 $FPREFIX-concat2 # must be equivalent
# decompress multiple files into stdout
rm $FPREFIX-concat1 $FPREFIX-concat2
lz4 -f -m ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3 # generate .lz4 to decompress
cat ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3 > $FPREFIX-concat1 # create concatenated reference
rm ${FPREFIX}1 ${FPREFIX}2 ${FPREFIX}3
lz4 -d -m ${FPREFIX}1.lz4 ${FPREFIX}2.lz4 ${FPREFIX}3.lz4 -c > $FPREFIX-concat2
test ! -f ${FPREFIX}1 # must not create file artefact
cmp $FPREFIX-concat1 $FPREFIX-concat2 # must be equivalent
# compress multiple files, one of which is absent (must fail)
lz4 -f -m $FPREFIX-concat1 notHere $FPREFIX-concat2 && exit 1 # must fail : notHere not present
# test lz4-compressed file
rm $FPREFIX-concat1 $FPREFIX-concat2
lz4 -tm $FPREFIX-concat1.lz4
# ensure the test doesn't create artifact
test ! -f $FPREFIX-concat1 # must not create file artefact
# test multiple lz4-compressed file
lz4 -tm $FPREFIX-concat1.lz4 $FPREFIX-concat2.lz4
test ! -f $FPREFIX-concat1 # must not create file artefact
test ! -f $FPREFIX-concat2 # must not create file artefact
# test multiple lz4 files, one of which is absent (must fail)
lz4 -tm $FPREFIX-concat1.lz4 notHere.lz4 $FPREFIX-concat2.lz4 && exit 1
true

View File

@@ -0,0 +1,16 @@
#!/bin/sh
set -e
set -x
datagen -g16KB | lz4 -12 | lz4 -t
datagen -P10 | lz4 -12B4 | lz4 -t
datagen -g256K | lz4 -12B4D | lz4 -t
datagen -g512K -P25 | lz4 -12BD | lz4 -t
datagen -g1M | lz4 -12B5 | lz4 -t
datagen -g1M -s2 | lz4 -12B4D | lz4 -t
datagen -g2M -P99 | lz4 -11B4D | lz4 -t
datagen -g4M | lz4 -11vq | lz4 -qt
datagen -g8M | lz4 -11B4 | lz4 -t
datagen -g16M -P90 | lz4 -11B5 | lz4 -t
datagen -g32M -P10 | lz4 -11B5D | lz4 -t

19
lz4/tests/test-lz4-skippable.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/sh
SKIPFILE="goldenSamples/skip.bin"
FPREFIX="tmp-lsk"
set -e
remove () {
rm "$FPREFIX"*
}
trap remove EXIT
set -x
lz4 -dc $SKIPFILE
lz4 -dc < $SKIPFILE
printf "Hello from Valid Frame!\n" | lz4 -c > $FPREFIX.lz4
cat $SKIPFILE $FPREFIX.lz4 $SKIPFILE | lz4 -dc

42
lz4/tests/test-lz4-sparse.sh Executable file
View File

@@ -0,0 +1,42 @@
#!/bin/sh
FPREFIX="tmp-tls"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
datagen -g5M -P100 > ${FPREFIX}dg5M
lz4 -B4D ${FPREFIX}dg5M -c | lz4 -dv --sparse > ${FPREFIX}cB4
diff -s ${FPREFIX}dg5M ${FPREFIX}cB4
lz4 -B5D ${FPREFIX}dg5M -c | lz4 -dv --sparse > ${FPREFIX}cB5
diff -s ${FPREFIX}dg5M ${FPREFIX}cB5
lz4 -B6D ${FPREFIX}dg5M -c | lz4 -dv --sparse > ${FPREFIX}cB6
diff -s ${FPREFIX}dg5M ${FPREFIX}cB6
lz4 -B7D ${FPREFIX}dg5M -c | lz4 -dv --sparse > ${FPREFIX}cB7
diff -s ${FPREFIX}dg5M ${FPREFIX}cB7
lz4 ${FPREFIX}dg5M -c | lz4 -dv --no-sparse > ${FPREFIX}nosparse
diff -s ${FPREFIX}dg5M ${FPREFIX}nosparse
ls -ls $FPREFIX*
datagen -s1 -g1200007 -P100 | lz4 | lz4 -dv --sparse > ${FPREFIX}odd # Odd size file (to generate non-full last block)
datagen -s1 -g1200007 -P100 | diff -s - ${FPREFIX}odd
ls -ls ${FPREFIX}odd
rm $FPREFIX*
printf "\n Compatibility with Console :"
echo "Hello World 1 !" | lz4 | lz4 -d -c
echo "Hello World 2 !" | lz4 | lz4 -d | cat
echo "Hello World 3 !" | lz4 --no-frame-crc | lz4 -d -c
printf "\n Compatibility with Append :"
datagen -P100 -g1M > ${FPREFIX}dg1M
cat ${FPREFIX}dg1M ${FPREFIX}dg1M > ${FPREFIX}2M
lz4 -B5 -v ${FPREFIX}dg1M ${FPREFIX}c
lz4 -d -v ${FPREFIX}c ${FPREFIX}r
lz4 -d -v ${FPREFIX}c -c >> ${FPREFIX}r
ls -ls $FPREFIX*
diff ${FPREFIX}2M ${FPREFIX}r

351
lz4/tests/test-lz4-speed.py Normal file
View File

@@ -0,0 +1,351 @@
#!/usr/bin/env python3
#
# Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#
# Limitations:
# - doesn't support filenames with spaces
# - dir1/lz4 and dir2/lz4 will be merged in a single results file
import argparse
import os
import string
import subprocess
import time
import traceback
import hashlib
script_version = 'v1.7.2 (2016-11-08)'
default_repo_url = 'https://github.com/lz4/lz4.git'
working_dir_name = 'speedTest'
working_path = os.getcwd() + '/' + working_dir_name # /path/to/lz4/tests/speedTest
clone_path = working_path + '/' + 'lz4' # /path/to/lz4/tests/speedTest/lz4
email_header = 'lz4_speedTest'
pid = str(os.getpid())
verbose = False
clang_version = "unknown"
gcc_version = "unknown"
args = None
def hashfile(hasher, fname, blocksize=65536):
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(blocksize), b""):
hasher.update(chunk)
return hasher.hexdigest()
def log(text):
print(time.strftime("%Y/%m/%d %H:%M:%S") + ' - ' + text)
def execute(command, print_command=True, print_output=False, print_error=True, param_shell=True):
if print_command:
log("> " + command)
popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=param_shell, cwd=execute.cwd)
stdout_lines, stderr_lines = popen.communicate(timeout=args.timeout)
stderr_lines = stderr_lines.decode("utf-8")
stdout_lines = stdout_lines.decode("utf-8")
if print_output:
if stdout_lines:
print(stdout_lines)
if stderr_lines:
print(stderr_lines)
if popen.returncode is not None and popen.returncode != 0:
if stderr_lines and not print_output and print_error:
print(stderr_lines)
raise RuntimeError(stdout_lines + stderr_lines)
return (stdout_lines + stderr_lines).splitlines()
execute.cwd = None
def does_command_exist(command):
try:
execute(command, verbose, False, False)
except Exception:
return False
return True
def send_email(emails, topic, text, have_mutt, have_mail):
logFileName = working_path + '/' + 'tmpEmailContent'
with open(logFileName, "w") as myfile:
myfile.writelines(text)
myfile.close()
if have_mutt:
execute('mutt -s "' + topic + '" ' + emails + ' < ' + logFileName, verbose)
elif have_mail:
execute('mail -s "' + topic + '" ' + emails + ' < ' + logFileName, verbose)
else:
log("e-mail cannot be sent (mail or mutt not found)")
def send_email_with_attachments(branch, commit, last_commit, args, text, results_files,
logFileName, have_mutt, have_mail):
with open(logFileName, "w") as myfile:
myfile.writelines(text)
myfile.close()
email_topic = '[%s:%s] Warning for %s:%s last_commit=%s speed<%s ratio<%s' \
% (email_header, pid, branch, commit, last_commit,
args.lowerLimit, args.ratioLimit)
if have_mutt:
execute('mutt -s "' + email_topic + '" ' + args.emails + ' -a ' + results_files
+ ' < ' + logFileName)
elif have_mail:
execute('mail -s "' + email_topic + '" ' + args.emails + ' < ' + logFileName)
else:
log("e-mail cannot be sent (mail or mutt not found)")
def git_get_branches():
execute('git fetch -p', verbose)
branches = execute('git branch -rl', verbose)
output = []
for line in branches:
if ("HEAD" not in line) and ("coverity_scan" not in line) and ("gh-pages" not in line):
output.append(line.strip())
return output
def git_get_changes(branch, commit, last_commit):
fmt = '--format="%h: (%an) %s, %ar"'
if last_commit is None:
commits = execute('git log -n 10 %s %s' % (fmt, commit))
else:
commits = execute('git --no-pager log %s %s..%s' % (fmt, last_commit, commit))
return str('Changes in %s since %s:\n' % (branch, last_commit)) + '\n'.join(commits)
def get_last_results(resultsFileName):
if not os.path.isfile(resultsFileName):
return None, None, None, None
commit = None
csize = []
cspeed = []
dspeed = []
with open(resultsFileName, 'r') as f:
for line in f:
words = line.split()
if len(words) <= 4: # branch + commit + compilerVer + md5
commit = words[1]
csize = []
cspeed = []
dspeed = []
if (len(words) == 8) or (len(words) == 9): # results: "filename" or "XX files"
csize.append(int(words[1]))
cspeed.append(float(words[3]))
dspeed.append(float(words[5]))
return commit, csize, cspeed, dspeed
def benchmark_and_compare(branch, commit, last_commit, args, executableName, md5sum, compilerVersion, resultsFileName,
testFilePath, fileName, last_csize, last_cspeed, last_dspeed):
sleepTime = 30
while os.getloadavg()[0] > args.maxLoadAvg:
log("WARNING: bench loadavg=%.2f is higher than %s, sleeping for %s seconds"
% (os.getloadavg()[0], args.maxLoadAvg, sleepTime))
time.sleep(sleepTime)
start_load = str(os.getloadavg())
result = execute('programs/%s -rqi5b1e%s %s' % (executableName, args.lastCLevel, testFilePath), print_output=True)
end_load = str(os.getloadavg())
linesExpected = args.lastCLevel + 1
if len(result) != linesExpected:
raise RuntimeError("ERROR: number of result lines=%d is different that expected %d\n%s" % (len(result), linesExpected, '\n'.join(result)))
with open(resultsFileName, "a") as myfile:
myfile.write('%s %s %s md5=%s\n' % (branch, commit, compilerVersion, md5sum))
myfile.write('\n'.join(result) + '\n')
myfile.close()
if (last_cspeed == None):
log("WARNING: No data for comparison for branch=%s file=%s " % (branch, fileName))
return ""
commit, csize, cspeed, dspeed = get_last_results(resultsFileName)
text = ""
for i in range(0, min(len(cspeed), len(last_cspeed))):
print("%s:%s -%d cSpeed=%6.2f cLast=%6.2f cDiff=%1.4f dSpeed=%6.2f dLast=%6.2f dDiff=%1.4f ratioDiff=%1.4f %s" % (branch, commit, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], float(last_csize[i])/csize[i], fileName))
if (cspeed[i]/last_cspeed[i] < args.lowerLimit):
text += "WARNING: %s -%d cSpeed=%.2f cLast=%.2f cDiff=%.4f %s\n" % (executableName, i+1, cspeed[i], last_cspeed[i], cspeed[i]/last_cspeed[i], fileName)
if (dspeed[i]/last_dspeed[i] < args.lowerLimit):
text += "WARNING: %s -%d dSpeed=%.2f dLast=%.2f dDiff=%.4f %s\n" % (executableName, i+1, dspeed[i], last_dspeed[i], dspeed[i]/last_dspeed[i], fileName)
if (float(last_csize[i])/csize[i] < args.ratioLimit):
text += "WARNING: %s -%d cSize=%d last_cSize=%d diff=%.4f %s\n" % (executableName, i+1, csize[i], last_csize[i], float(last_csize[i])/csize[i], fileName)
if text:
text = args.message + ("\nmaxLoadAvg=%s load average at start=%s end=%s\n%s last_commit=%s md5=%s\n" % (args.maxLoadAvg, start_load, end_load, compilerVersion, last_commit, md5sum)) + text
return text
def update_config_file(branch, commit):
last_commit = None
commitFileName = working_path + "/commit_" + branch.replace("/", "_") + ".txt"
if os.path.isfile(commitFileName):
with open(commitFileName, 'r') as infile:
last_commit = infile.read()
with open(commitFileName, 'w') as outfile:
outfile.write(commit)
return last_commit
def double_check(branch, commit, args, executableName, md5sum, compilerVersion, resultsFileName, filePath, fileName):
last_commit, csize, cspeed, dspeed = get_last_results(resultsFileName)
if not args.dry_run:
text = benchmark_and_compare(branch, commit, last_commit, args, executableName, md5sum, compilerVersion, resultsFileName, filePath, fileName, csize, cspeed, dspeed)
if text:
log("WARNING: redoing tests for branch %s: commit %s" % (branch, commit))
text = benchmark_and_compare(branch, commit, last_commit, args, executableName, md5sum, compilerVersion, resultsFileName, filePath, fileName, csize, cspeed, dspeed)
return text
def test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail):
local_branch = branch.split('/')[1]
version = local_branch.rpartition('-')[2] + '_' + commit
if not args.dry_run:
execute('make clean; CFLAGS="-Werror -Wconversion -Wno-sign-conversion" CPPFLAGS="-DLZ4_GIT_COMMIT=%s" make -C programs lz4 CC=clang && ' % version +
'mv programs/lz4 programs/lz4_clang && ' +
'make clean && CPPFLAGS="-DLZ4_GIT_COMMIT=%s" make -C programs lz4 lz4c32 ' % version)
md5_lz4 = hashfile(hashlib.md5(), clone_path + '/programs/lz4')
md5_lz4c32 = hashfile(hashlib.md5(), clone_path + '/programs/lz4c32')
md5_lz4_clang = hashfile(hashlib.md5(), clone_path + '/programs/lz4_clang')
print("md5(lz4)=%s\nmd5(lz4c32)=%s\nmd5(lz4_clang)=%s" % (md5_lz4, md5_lz4c32, md5_lz4_clang))
print("gcc_version=%s clang_version=%s" % (gcc_version, clang_version))
logFileName = working_path + "/log_" + branch.replace("/", "_") + ".txt"
text_to_send = []
results_files = ""
for filePath in testFilePaths:
fileName = filePath.rpartition('/')[2]
resultsFileName = working_path + "/results_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
text = double_check(branch, commit, args, 'lz4', md5_lz4, 'gcc_version='+gcc_version, resultsFileName, filePath, fileName)
if text:
text_to_send.append(text)
results_files += resultsFileName + " "
resultsFileName = working_path + "/results32_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
text = double_check(branch, commit, args, 'lz4c32', md5_lz4c32, 'gcc_version='+gcc_version, resultsFileName, filePath, fileName)
if text:
text_to_send.append(text)
results_files += resultsFileName + " "
resultsFileName = working_path + "/resultsClang_" + branch.replace("/", "_") + "_" + fileName.replace(".", "_") + ".txt"
text = double_check(branch, commit, args, 'lz4_clang', md5_lz4_clang, 'clang_version='+clang_version, resultsFileName, filePath, fileName)
if text:
text_to_send.append(text)
results_files += resultsFileName + " "
if text_to_send:
send_email_with_attachments(branch, commit, last_commit, args, text_to_send, results_files, logFileName, have_mutt, have_mail)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('testFileNames', help='file or directory names list for speed benchmark')
parser.add_argument('emails', help='list of e-mail addresses to send warnings')
parser.add_argument('--message', '-m', help='attach an additional message to e-mail', default="")
parser.add_argument('--repoURL', help='changes default repository URL', default=default_repo_url)
parser.add_argument('--lowerLimit', '-l', type=float, help='send email if speed is lower than given limit', default=0.98)
parser.add_argument('--ratioLimit', '-r', type=float, help='send email if ratio is lower than given limit', default=0.999)
parser.add_argument('--maxLoadAvg', type=float, help='maximum load average to start testing', default=0.75)
parser.add_argument('--lastCLevel', type=int, help='last compression level for testing', default=5)
parser.add_argument('--sleepTime', '-s', type=int, help='frequency of repository checking in seconds', default=300)
parser.add_argument('--timeout', '-t', type=int, help='timeout for executing shell commands', default=1800)
parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='not build', default=False)
parser.add_argument('--verbose', '-v', action='store_true', help='more verbose logs', default=False)
args = parser.parse_args()
verbose = args.verbose
# check if test files are accessible
testFileNames = args.testFileNames.split()
testFilePaths = []
for fileName in testFileNames:
fileName = os.path.expanduser(fileName)
if os.path.isfile(fileName) or os.path.isdir(fileName):
testFilePaths.append(os.path.abspath(fileName))
else:
log("ERROR: File/directory not found: " + fileName)
exit(1)
# check availability of e-mail senders
have_mutt = does_command_exist("mutt -h")
have_mail = does_command_exist("mail -V")
if not have_mutt and not have_mail:
log("ERROR: e-mail senders 'mail' or 'mutt' not found")
exit(1)
clang_version = execute("clang -v 2>&1 | grep 'clang version' | sed -e 's:.*version \\([0-9.]*\\).*:\\1:' -e 's:\\.\\([0-9][0-9]\\):\\1:g'", verbose)[0];
gcc_version = execute("gcc -dumpversion", verbose)[0];
if verbose:
print("PARAMETERS:\nrepoURL=%s" % args.repoURL)
print("working_path=%s" % working_path)
print("clone_path=%s" % clone_path)
print("testFilePath(%s)=%s" % (len(testFilePaths), testFilePaths))
print("message=%s" % args.message)
print("emails=%s" % args.emails)
print("maxLoadAvg=%s" % args.maxLoadAvg)
print("lowerLimit=%s" % args.lowerLimit)
print("ratioLimit=%s" % args.ratioLimit)
print("lastCLevel=%s" % args.lastCLevel)
print("sleepTime=%s" % args.sleepTime)
print("timeout=%s" % args.timeout)
print("dry_run=%s" % args.dry_run)
print("verbose=%s" % args.verbose)
print("have_mutt=%s have_mail=%s" % (have_mutt, have_mail))
# clone lz4 repo if needed
if not os.path.isdir(working_path):
os.mkdir(working_path)
if not os.path.isdir(clone_path):
execute.cwd = working_path
execute('git clone ' + args.repoURL)
if not os.path.isdir(clone_path):
log("ERROR: lz4 clone not found: " + clone_path)
exit(1)
execute.cwd = clone_path
# check if speedTest.pid already exists
pidfile = "./speedTest.pid"
if os.path.isfile(pidfile):
log("ERROR: %s already exists, exiting" % pidfile)
exit(1)
send_email(args.emails, '[%s:%s] test-lz4-speed.py %s has been started' % (email_header, pid, script_version), args.message, have_mutt, have_mail)
with open(pidfile, 'w') as the_file:
the_file.write(pid)
branch = ""
commit = ""
first_time = True
while True:
try:
if first_time:
first_time = False
else:
if verbose:
log("sleep for %s seconds" % args.sleepTime)
time.sleep(args.sleepTime)
loadavg = os.getloadavg()[0]
if (loadavg <= args.maxLoadAvg):
branches = git_get_branches()
for branch in branches:
commit = execute('git show -s --format=%h ' + branch, verbose)[0]
last_commit = update_config_file(branch, commit)
if commit == last_commit:
log("skipping branch %s: head %s already processed" % (branch, commit))
else:
log("build branch %s: head %s is different from prev %s" % (branch, commit, last_commit))
execute('git checkout -- . && git checkout ' + branch)
print(git_get_changes(branch, commit, last_commit))
test_commit(branch, commit, last_commit, args, testFilePaths, have_mutt, have_mail)
else:
log("WARNING: main loadavg=%.2f is higher than %s" % (loadavg, args.maxLoadAvg))
except Exception as e:
stack = traceback.format_exc()
email_topic = '[%s:%s] ERROR in %s:%s' % (email_header, pid, branch, commit)
send_email(args.emails, email_topic, stack, have_mutt, have_mail)
print(stack)
except KeyboardInterrupt:
os.unlink(pidfile)
send_email(args.emails, '[%s:%s] test-lz4-speed.py %s has been stopped' % (email_header, pid, script_version), args.message, have_mutt, have_mail)
exit(0)

38
lz4/tests/test-lz4-testmode.sh Executable file
View File

@@ -0,0 +1,38 @@
#!/bin/sh
FPREFIX="tmp-ltm"
set -e
remove () {
rm $FPREFIX*
}
trap remove EXIT
set -x
lz4 -bi0
datagen > $FPREFIX
lz4 -f $FPREFIX -c > $FPREFIX.lz4
lz4 -bdi0 $FPREFIX.lz4 # test benchmark decode-only mode
lz4 -bdi0 --no-crc $FPREFIX.lz4 # test benchmark decode-only mode
echo "---- test mode ----"
datagen | lz4 -t && exit 1
datagen | lz4 -tf && exit 1
echo "---- pass-through mode ----"
echo "Why hello there " > ${FPREFIX}2.lz4
lz4 -f ${FPREFIX}2.lz4 > $FPREFIX-trash && exit 1
datagen | lz4 -dc > $FPREFIX-trash && exit 1
datagen | lz4 -df > $FPREFIX-trash && exit 1
datagen | lz4 -dcf > $FPREFIX-trash
echo "Hello World !" > ${FPREFIX}1
lz4 -dcf ${FPREFIX}1
echo "from underground..." > ${FPREFIX}2
lz4 -dcfm ${FPREFIX}1 ${FPREFIX}2
echo "---- non-existing source (must fail cleanly) ----"
lz4 file-does-not-exist && exit 1
lz4 -f file-does-not-exist && exit 1
lz4 -t file-does-not-exist && exit 1
lz4 -fm file1-dne file2-dne && exit 1
true

View File

@@ -0,0 +1,186 @@
#!/usr/bin/env python3
"""Test LZ4 interoperability between versions"""
#
# Copyright (C) 2011-present, Takayuki Matsuoka
# All rights reserved.
# GPL v2 License
#
import argparse
import glob
import subprocess
import filecmp
import os
import shutil
import sys
import hashlib
repo_url = 'https://github.com/lz4/lz4.git'
tmp_dir_name = 'tests/versionsTest'
make_cmd = 'make'
git_cmd = 'git'
test_dat_src = 'README.md'
test_dat = 'test_dat'
head = 'v999'
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
args = parser.parse_args()
def debug_message(msg):
if args.verbose:
print(msg)
def env_or_empty(env, key):
if key in env:
return " " + env[key]
return ""
def proc(cmd_args, pipe=True, env=False):
if env == False:
env = os.environ.copy()
debug_message("Executing command {} with env {}".format(cmd_args, env))
if pipe:
s = subprocess.Popen(cmd_args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env = env)
else:
s = subprocess.Popen(cmd_args, env = env)
stdout_data, stderr_data = s.communicate()
if s.poll() != 0:
print('Error Code:', s.poll())
print('Standard Error:', stderr_data.decode())
sys.exit(1)
return stdout_data, stderr_data
def make(args, pipe=True, env=False):
if env == False:
env = os.environ.copy()
# favor compilation speed for faster total test time, actual runtime is very short
env["CFLAGS"] = env_or_empty(env, 'CFLAGS') + " -O0"
# old versions of lz4 may require MOREFLAGS
env["MOREFLAGS"] = env_or_empty(env, 'MOREFLAGS') + env_or_empty(env, 'CFLAGS') + env_or_empty(env, 'CPPFLAGS') + env_or_empty(env, 'LDFLAGS')
return proc([make_cmd] + args, pipe, env)
def git(args, pipe=True):
return proc([git_cmd] + args, pipe)
def get_git_tags():
stdout, stderr = git(['tag', '-l', 'r[0-9][0-9][0-9]'])
tags = stdout.decode('utf-8').split()
stdout, stderr = git(['tag', '-l', 'v[1-9].[0-9].[0-9]'])
tags += stdout.decode('utf-8').split()
return tags
# https://stackoverflow.com/a/19711609/2132223
def sha1_of_file(filepath):
with open(filepath, 'rb') as f:
return hashlib.sha1(f.read()).hexdigest()
if __name__ == '__main__':
error_code = 0
base_dir = os.getcwd() + '/..' # /path/to/lz4
tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/lz4/tests/versionsTest
clone_dir = tmp_dir + '/' + 'lz4' # /path/to/lz4/tests/versionsTest/lz4
programs_dir = base_dir + '/programs' # /path/to/lz4/programs
os.makedirs(tmp_dir, exist_ok=True)
# since Travis clones limited depth, we should clone full repository
if not os.path.isdir(clone_dir):
git(['clone', repo_url, clone_dir])
shutil.copy2(base_dir + '/' + test_dat_src, tmp_dir + '/' + test_dat)
# Retrieve all release tags
print('Retrieve all release tags :')
os.chdir(clone_dir)
tags = [head] + get_git_tags()
print(tags);
# Build all release lz4c and lz4c32
for tag in tags:
print("processing tag " + tag)
os.chdir(base_dir)
dst_lz4c = '{}/lz4c.{}' .format(tmp_dir, tag) # /path/to/lz4/test/lz4test/lz4c.<TAG>
dst_lz4c32 = '{}/lz4c32.{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/lz4c32.<TAG>
if not os.path.isfile(dst_lz4c) or not os.path.isfile(dst_lz4c32) or tag == head:
if tag != head:
r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/<TAG>
os.makedirs(r_dir, exist_ok=True)
os.chdir(clone_dir)
git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'], False)
os.chdir(r_dir + '/programs') # /path/to/lz4/lz4test/<TAG>/programs
else:
os.chdir(programs_dir)
make(['clean'], False)
make(['lz4c'], False)
shutil.copy2('lz4c', dst_lz4c)
make(['clean'], False)
make(['lz4c32'], False)
shutil.copy2('lz4c32', dst_lz4c32)
# Compress test.dat by all released lz4c and lz4c32
print('Compress test.dat by all released lz4c and lz4c32')
os.chdir(tmp_dir)
for lz4 in glob.glob("*.lz4"):
os.remove(lz4)
for tag in tags:
proc(['./lz4c.' + tag, '-1fz', test_dat, test_dat + '_1_64_' + tag + '.lz4'])
proc(['./lz4c.' + tag, '-9fz', test_dat, test_dat + '_9_64_' + tag + '.lz4'])
proc(['./lz4c32.' + tag, '-1fz', test_dat, test_dat + '_1_32_' + tag + '.lz4'])
proc(['./lz4c32.' + tag, '-9fz', test_dat, test_dat + '_9_32_' + tag + '.lz4'])
print('Full list of compressed files')
lz4s = sorted(glob.glob('*.lz4'))
for lz4 in lz4s:
print(lz4 + ' : ' + repr(os.path.getsize(lz4)))
# Remove duplicated .lz4 files
print('')
print('Duplicated files')
lz4s = sorted(glob.glob('*.lz4'))
for i, lz4 in enumerate(lz4s):
if not os.path.isfile(lz4):
continue
for j in range(i+1, len(lz4s)):
lz4t = lz4s[j]
if not os.path.isfile(lz4t):
continue
if filecmp.cmp(lz4, lz4t):
os.remove(lz4t)
print('{} == {}'.format(lz4, lz4t))
print('Enumerate only different compressed files')
lz4s = sorted(glob.glob('*.lz4'))
for lz4 in lz4s:
print(lz4 + ' : ' + repr(os.path.getsize(lz4)) + ', ' + sha1_of_file(lz4))
# Decompress remained .lz4 files by all released lz4c and lz4c32
print('Decompression tests and verifications')
lz4s = sorted(glob.glob('*.lz4'))
for dec in glob.glob("*.dec"):
os.remove(dec)
for lz4 in lz4s:
print(lz4, end=" ")
for tag in tags:
print(tag, end=" ")
proc(['./lz4c.' + tag, '-df', lz4, lz4 + '_d64_' + tag + '.dec'])
proc(['./lz4c32.' + tag, '-df', lz4, lz4 + '_d32_' + tag + '.dec'])
print(' OK') # well, here, decompression has worked; but file is not yet verified
# Compare all '.dec' files with test_dat
decs = glob.glob('*.dec')
for dec in decs:
if not filecmp.cmp(dec, test_dat):
print('ERR : ' + dec)
error_code = 1
else:
print('OK : ' + dec)
os.remove(dec)
if error_code != 0:
print('ERROR')
sys.exit(error_code)

View File

@@ -0,0 +1,6 @@
#!/bin/sh
set -e
set -x
datagen -g4200MB | lz4 -v3 | lz4 -qt

View File

@@ -0,0 +1,72 @@
#!/usr/bin/env sh
set -e
LZ4=../lz4
CHECKFRAME=./checkFrame
DATAGEN=./datagen
failures=""
TMPFILE=/tmp/test_custom_block_sizes.$$
TMPFILE1=/tmp/test_custom_block_sizes1.$$
TMPFILE2=/tmp/test_custom_block_sizes2.$$
$DATAGEN -g12345678 > $TMPFILE1
$DATAGEN -g12345678 > $TMPFILE2
echo Testing -B31
$LZ4 -f -B31 $TMPFILE1 && failures="31 (should fail) "
for blocksize in 32 65535 65536
do
echo Testing -B$blocksize
$LZ4 -f -B$blocksize $TMPFILE1
$LZ4 -f -B$blocksize $TMPFILE2
cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4
$CHECKFRAME -B$blocksize -b4 $TMPFILE.lz4 || failures="$failures $blocksize "
done
for blocksize in 65537 262143 262144
do
echo Testing -B$blocksize
$LZ4 -f -B$blocksize $TMPFILE1
$LZ4 -f -B$blocksize $TMPFILE2
cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4
$CHECKFRAME -B$blocksize -b5 $TMPFILE.lz4 || failures="$failures $blocksize "
done
for blocksize in 262145 1048575 1048576
do
echo Testing -B$blocksize
$LZ4 -f -B$blocksize $TMPFILE1
$LZ4 -f -B$blocksize $TMPFILE2
cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4
$CHECKFRAME -B$blocksize -b6 $TMPFILE.lz4 || failures="$failures $blocksize "
done
for blocksize in 1048577 4194303 4194304
do
echo Testing -B$blocksize
$LZ4 -f -B$blocksize $TMPFILE1
$LZ4 -f -B$blocksize $TMPFILE2
cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4
$CHECKFRAME -B$blocksize -b7 $TMPFILE.lz4 || failures="$failures $blocksize "
done
for blocksize in 4194305 10485760
do
echo Testing -B$blocksize
$LZ4 -f -B$blocksize $TMPFILE1
$LZ4 -f -B$blocksize $TMPFILE2
cat $TMPFILE1.lz4 $TMPFILE2.lz4 > $TMPFILE.lz4
$CHECKFRAME -B4194304 -b7 $TMPFILE.lz4 || failures="$failures $blocksize "
done
rm $TMPFILE.lz4 $TMPFILE1 $TMPFILE1.lz4 $TMPFILE2 $TMPFILE2.lz4
if [ "$failures" = "" ]
then
echo ---- All tests passed
exit 0
else
echo ---- The following tests had failures: $failures
exit 1
fi

28
lz4/tests/test_install.sh Executable file
View File

@@ -0,0 +1,28 @@
#!/usr/bin/env sh
set -e
make="make -C $lz4_root"
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
make="make -C $lz4_root"
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'OpenBSD' ]; then
make="gmake -C $lz4_root"
fi
for cmd in install uninstall; do
for upper in DUMMY PREFIX EXEC_PREFIX LIBDIR INCLUDEDIR PKGCONFIGDIR BINDIR MANDIR MAN1DIR ; do
lower=$(echo $upper | tr '[:upper:]' '[:lower:]')
tmp_lower="$(pwd)/tmp-lower-$lower/"
tmp_upper="$(pwd)/tmp-upper-$lower/"
echo $make $cmd DESTDIR="$tmp_upper" $upper="test"
$make $cmd DESTDIR="$tmp_upper" $upper="test" >/dev/null
echo $make $cmd DESTDIR="$tmp_lower" $lower="test"
$make $cmd DESTDIR="$tmp_lower" $lower="test" >/dev/null
command diff -r "$tmp_lower" "$tmp_upper" && echo "SAME!" || false
if [ "x$cmd" = "xuninstall" ]; then
test -z "$(find "$tmp_lower" -type f)" && echo "EMPTY!" || false
rm -rf "$tmp_upper" "$tmp_lower"
fi
done
done

48
lz4/tests/unicode_lint.sh Normal file
View File

@@ -0,0 +1,48 @@
#!/bin/bash
# `unicode_lint.sh' determines whether source files under the ./lib/, ./tests/ and ./programs/ directories
# contain Unicode characters, and fails if any do.
#
# See https://github.com/lz4/lz4/issues/1018
echo "Ensure no unicode character is present in source files *.{c,h}"
pass=true
# Scan ./lib/ for Unicode in source (*.c, *.h) files
echo "Scanning lib/"
result=$(
find ./lib/ -regex '.*\.\(c\|h\)$' -exec grep -P -n "[^\x00-\x7F]" {} \; -exec echo "{}: FAIL" \;
)
if [[ $result ]]; then
echo "$result"
pass=false
fi
# Scan ./programs/ for Unicode in source (*.c, *.h) files
echo "Scanning programs/"
result=$(
find ./programs/ -regex '.*\.\(c\|h\)$' -exec grep -P -n "[^\x00-\x7F]" {} \; -exec echo "{}: FAIL" \;
)
if [[ $result ]]; then
echo "$result"
pass=false
fi
# Scan ./tests/ for Unicode in source (*.c, *.h) files
echo "Scanning tests/"
result=$(
find ./tests/ -regex '.*\.\(c\|h\)$' -exec grep -P -n "[^\x00-\x7F]" {} \; -exec echo "{}: FAIL" \;
)
if [[ $result ]]; then
echo "$result"
pass=false
fi
if [ "$pass" = true ]; then
echo "All tests successful: no unicode character detected"
echo "Result: PASS"
exit 0
else
echo "Result: FAIL"
exit 1
fi