this repo has no description
1#!/usr/bin/env bash
2set -euo pipefail
3
4# Test functionality of cmprss by comparing it with the official tools
5
6CACHE_DIR="${PRJ_ROOT}/.cache"
7
8# Create a tmp directory and cd into it
9tmpdir() {
10 mkdir -p "$CACHE_DIR"
11 local dir
12 dir=$(mktemp --directory --tmpdir="$CACHE_DIR")
13 cd "$dir"
14}
15
16# Compare the two files/directories and exit with an error if they are different
17compare() {
18 local file1="$1"
19 local file2="$2"
20 if ! diff -qr "$file1" "$file2" >/dev/null; then
21 echo "Diff Detected: $file1 $file2"
22 exit 1
23 fi
24}
25
26# Compare the two archive sizes to check if they are similar
27# TODO: This doesn't work anywhere in the file because we're compressing random data
28# So the algos will always fail to compress things very well
29compare_size() {
30 local file1="$1"
31 local file2="$2"
32 # Use $3 as the max difference or 100 bytes
33 local max_diff=${3:-100}
34 local size1
35 local size2
36 size1=$(stat -c %s "$file1")
37 size2=$(stat -c %s "$file2")
38 if [ $((size1 - size2)) -gt "$max_diff" ]; then
39 echo "Size difference too large: $file1:$size1 $file2:$size2"
40 exit 1
41 fi
42}
43
44# Create a random file with the given size
45random_file() {
46 local size="$1"
47 local file="$2"
48 dd if=/dev/urandom of="$file" bs=1 count="$size" 2>/dev/null
49}
50
51# Create a random directory with the given size
52random_dir() {
53 local size="$1"
54 local dir="$2"
55 mkdir -p "$dir"
56 for i in $(seq 1 "$size"); do
57 random_file 128 "$dir/$i"
58 done
59}
60
61# Run cmprss using cargo to test the current version
62cmprss() {
63 cargo run --release --quiet -- "$1" --ignore-pipes "${@:2}"
64}
65
66# Test gzip using the provided compression level
67test_gzip_level() {
68 tmpdir
69 echo "Testing gzip level $1 in $PWD"
70 echo "Creating random data"
71 random_file 1000000 file
72 echo "Compressing with gzip and cmprss"
73 gzip -"$1" -c file >gzip_file.gz
74 cmprss gzip --level "$1" file cmprss_file.gz --progress=off
75 # Compare the two archives
76 # The archives may have slight variations (versioning or whatever) so we
77 # only compare the sizes to make sure they are similar
78 compare_size gzip_file.gz cmprss_file.gz
79 # Decompress the 4 variations
80 echo "Decompressing"
81 gzip -c -d gzip_file.gz >gzip_gzip
82 gzip -c -d cmprss_file.gz >cmprss_gzip
83 cmprss gzip --extract cmprss_file.gz cmprss_cmprss --progress=off
84 cmprss gzip --extract gzip_file.gz gzip_cmprss --progress=off
85 echo "Comparing the decompressed files"
86 compare file gzip_gzip
87 compare file gzip_cmprss
88 compare file cmprss_cmprss
89 compare file cmprss_gzip
90 echo "No errors detected"
91}
92
93test_gzip() {
94 test_gzip_level 1
95 test_gzip_level 6 # Default
96 test_gzip_level 9
97}
98
99# Test xz using the provided compression level
100test_xz_level() {
101 tmpdir
102 echo "Testing xz level $1 in $PWD"
103 echo "Creating random data"
104 random_file 1000000 file
105 echo "Compressing with xz and cmprss"
106 xz -"$1" --stdout file >xz_file.xz
107 cmprss xz --level "$1" file cmprss_file.xz --progress=off
108 # Compare the two archives
109 # The archives may have slight variations (versioning or whatever) so we
110 # only compare the sizes to make sure they are similar
111 compare_size xz_file.xz cmprss_file.xz
112 # Decompress the 4 variations
113 echo "Decompressing"
114 xz --stdout --decompress xz_file.xz >xz_xz
115 xz --stdout --decompress cmprss_file.xz >xz_cmprss
116 cmprss xz --extract cmprss_file.xz cmprss_cmprss --progress=off
117 cmprss xz --extract xz_file.xz cmprss_xz --progress=off
118 echo "Comparing the decompressed files"
119 compare file xz_xz
120 compare file xz_cmprss
121 compare file cmprss_cmprss
122 compare file cmprss_xz
123 echo "No errors detected"
124}
125
126test_xz() {
127 test_xz_level 0 # No compression
128 test_xz_level 1
129 test_xz_level 6 # Default
130 test_xz_level 9
131}
132
133# Test bzip2 using the provided compression level
134test_bzip2_level() {
135 tmpdir
136 echo "Testing bzip2 level $1 in $PWD"
137 echo "Creating random data"
138 random_file 1000000 file
139 echo "Compressing with bzip2 and cmprss"
140 bzip2 -"$1" --stdout file >bzip2_file.bz2
141 cmprss bzip2 --level "$1" file cmprss_file.bz2 --progress=off
142 # Compare the two archives
143 # The archives may have slight variations (versioning or whatever) so we
144 # only compare the sizes to make sure they are similar
145 compare_size bzip2_file.bz2 cmprss_file.bz2
146 # Decompress the 4 variations
147 echo "Decompressing"
148 bzip2 --stdout --decompress bzip2_file.bz2 >bzip2_bzip2
149 bzip2 --stdout --decompress cmprss_file.bz2 >cmprss_bzip2
150 cmprss bzip2 --extract cmprss_file.bz2 cmprss_cmprss --progress=off
151 cmprss bzip2 --extract bzip2_file.bz2 bzip2_cmprss --progress=off
152 echo "Comparing the decompressed files"
153 compare file bzip2_bzip2
154 compare file bzip2_cmprss
155 compare file cmprss_cmprss
156 compare file cmprss_bzip2
157 echo "No errors detected"
158}
159
160test_bzip2() {
161 test_bzip2_level 1
162 test_bzip2_level 6
163 test_bzip2_level 9 # Default
164}
165
166# Test zstd using the provided compression level
167test_zstd_level() {
168 tmpdir
169 echo "Testing zstd level $1 in $PWD"
170 echo "Creating random data"
171 random_file 1000000 file
172 echo "Compressing with zstd and cmprss"
173 zstd -"$1" -c file >zstd_file.zst
174 cmprss zstd --level "$1" file cmprss_file.zst --progress=off
175 # Compare the two archives
176 # The archives may have slight variations (versioning or whatever) so we
177 # only compare the sizes to make sure they are similar
178 compare_size zstd_file.zst cmprss_file.zst
179 # Decompress the 4 variations
180 echo "Decompressing"
181 zstd -d -c zstd_file.zst >zstd_zstd
182 zstd -d -c cmprss_file.zst >cmprss_zstd
183 cmprss zstd --extract cmprss_file.zst cmprss_cmprss --progress=off
184 cmprss zstd --extract zstd_file.zst zstd_cmprss --progress=off
185 echo "Comparing the decompressed files"
186 compare file zstd_zstd
187 compare file zstd_cmprss
188 compare file cmprss_cmprss
189 compare file cmprss_zstd
190 echo "No errors detected"
191}
192
193test_zstd() {
194 test_zstd_level 1 # Fast compression
195 test_zstd_level 3
196 test_zstd_level 6 # Default
197 test_zstd_level 9 # High compression
198}
199
200# Test lz4 compression
201test_lz4() {
202 tmpdir
203 echo "Testing lz4 in $PWD"
204 echo "Creating random data"
205 random_file 1000000 file
206 echo "Compressing with lz4 and cmprss"
207 lz4 -c file >lz4_file.lz4
208 cmprss lz4 file cmprss_file.lz4 --progress=off
209 # Compare the two archives
210 # The archives may have slight variations (versioning or whatever) so we
211 # only compare the sizes to make sure they are similar
212 compare_size lz4_file.lz4 cmprss_file.lz4
213 # Decompress the 4 variations
214 echo "Decompressing"
215 lz4 -d -c lz4_file.lz4 >lz4_lz4
216 lz4 -d -c cmprss_file.lz4 >cmprss_lz4
217 cmprss lz4 --extract cmprss_file.lz4 cmprss_cmprss --progress=off
218 cmprss lz4 --extract lz4_file.lz4 lz4_cmprss --progress=off
219 echo "Comparing the decompressed files"
220 compare file lz4_lz4
221 compare file lz4_cmprss
222 compare file cmprss_cmprss
223 compare file cmprss_lz4
224 echo "No errors detected"
225}
226
227# Run all the tests if no arguments are given
228if [ $# -eq 0 ]; then
229 set -- gzip xz bzip2 zstd lz4
230fi
231
232# Run the tests given on the command line
233for test in "$@"; do
234 test_"$test"
235done