1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_filter_flags.c
4 /// \brief Tests Filter Flags coders
6 // Copyright (C) 2007 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
23 static uint8_t buffer[4096];
24 static lzma_options_filter known_flags;
25 static lzma_options_filter decoded_flags;
26 static lzma_stream strm = LZMA_STREAM_INIT;
30 encode(uint32_t known_size)
32 memcrap(buffer, sizeof(buffer));
35 if (lzma_filter_flags_size(&tmp, &known_flags) != LZMA_OK)
38 if (tmp != known_size)
42 if (lzma_filter_flags_encode(buffer, &out_pos, known_size,
43 &known_flags) != LZMA_OK)
46 if (out_pos != known_size)
54 decode_ret(uint32_t known_size, lzma_ret expected_ret)
56 memcrap(&decoded_flags, sizeof(decoded_flags));
59 if (lzma_filter_flags_decode(&decoded_flags, NULL,
60 buffer, &pos, known_size) != expected_ret
69 decode(uint32_t known_size)
71 if (decode_ret(known_size, LZMA_OK))
74 if (known_flags.id != decoded_flags.id)
81 #ifdef HAVE_FILTER_SUBBLOCK
86 known_flags.id = LZMA_FILTER_SUBBLOCK;
87 known_flags.options = NULL;
91 expect(decoded_flags.options != NULL);
92 expect(((lzma_options_subblock *)(decoded_flags.options))
96 known_flags.options = decoded_flags.options;
99 expect(decoded_flags.options != NULL);
100 expect(((lzma_options_subblock *)(decoded_flags.options))
103 free(decoded_flags.options);
104 free(known_flags.options);
107 buffer[0] = LZMA_FILTER_SUBBLOCK;
110 expect(!decode_ret(3, LZMA_HEADER_ERROR));
115 #ifdef HAVE_FILTER_SIMPLE
120 known_flags.id = LZMA_FILTER_X86;
121 known_flags.options = NULL;
125 expect(decoded_flags.options == NULL);
128 lzma_options_simple options;
129 options.start_offset = 0;
130 known_flags.options = &options;
133 expect(decoded_flags.options == NULL);
136 options.start_offset = 123456;
137 known_flags.options = &options;
140 expect(decoded_flags.options != NULL);
142 lzma_options_simple *decoded = decoded_flags.options;
143 expect(decoded->start_offset == options.start_offset);
150 #ifdef HAVE_FILTER_DELTA
155 known_flags.id = LZMA_FILTER_DELTA;
156 known_flags.options = NULL;
160 lzma_options_delta options = { 0 };
161 known_flags.options = &options;
165 options.distance = LZMA_DELTA_DISTANCE_MIN;
168 expect(((lzma_options_delta *)(decoded_flags.options))
169 ->distance == options.distance);
171 free(decoded_flags.options);
174 options.distance = LZMA_DELTA_DISTANCE_MAX;
177 expect(((lzma_options_delta *)(decoded_flags.options))
178 ->distance == options.distance);
180 free(decoded_flags.options);
183 options.distance = LZMA_DELTA_DISTANCE_MAX + 1;
189 #ifdef HAVE_FILTER_LZMA
193 const lzma_options_lzma *known = known_flags.options;
194 const lzma_options_lzma *decoded = decoded_flags.options;
196 expect(known->dictionary_size <= decoded->dictionary_size);
198 if (known->dictionary_size == 1)
199 expect(decoded->dictionary_size == 1);
201 expect(known->dictionary_size + known->dictionary_size / 2
202 > decoded->dictionary_size);
204 expect(known->literal_context_bits == decoded->literal_context_bits);
205 expect(known->literal_pos_bits == decoded->literal_pos_bits);
206 expect(known->pos_bits == decoded->pos_bits);
214 known_flags.id = LZMA_FILTER_LZMA;
215 known_flags.options = NULL;
219 lzma_options_lzma options = {
220 .dictionary_size = 0,
221 .literal_context_bits = 0,
222 .literal_pos_bits = 0,
224 .preset_dictionary = NULL,
225 .preset_dictionary_size = 0,
226 .mode = LZMA_MODE_INVALID,
228 .match_finder = LZMA_MF_INVALID,
229 .match_finder_cycles = 0,
232 // Test 3 (empty dictionary not allowed)
233 known_flags.options = &options;
236 // Test 4 (brute-force test some valid dictionary sizes)
237 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
238 while (options.dictionary_size != LZMA_DICTIONARY_SIZE_MAX) {
239 if (++options.dictionary_size == 5000)
240 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX - 5;
246 free(decoded_flags.options);
249 // Test 5 (too big dictionary size)
250 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX + 1;
253 // Test 6 (brute-force test lc/lp/pb)
254 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
255 for (uint32_t lc = LZMA_LITERAL_CONTEXT_BITS_MIN;
256 lc <= LZMA_LITERAL_CONTEXT_BITS_MAX; ++lc) {
257 for (uint32_t lp = LZMA_LITERAL_POS_BITS_MIN;
258 lp <= LZMA_LITERAL_POS_BITS_MAX; ++lp) {
259 for (uint32_t pb = LZMA_POS_BITS_MIN;
260 pb <= LZMA_POS_BITS_MAX; ++pb) {
261 options.literal_context_bits = lc;
262 options.literal_pos_bits = lp;
263 options.pos_bits = pb;
269 free(decoded_flags.options);
282 #ifdef HAVE_FILTER_SUBBLOCK
285 #ifdef HAVE_FILTER_SIMPLE
288 #ifdef HAVE_FILTER_DELTA
291 #ifdef HAVE_FILTER_LZMA