1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file test_filter_flags.c
4 /// \brief Tests Filter Flags coders
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
16 static uint8_t buffer[4096];
17 static lzma_filter known_flags;
18 static lzma_filter decoded_flags;
19 static lzma_stream strm = LZMA_STREAM_INIT;
23 encode(uint32_t known_size)
25 memcrap(buffer, sizeof(buffer));
28 if (lzma_filter_flags_size(&tmp, &known_flags) != LZMA_OK)
31 if (tmp != known_size)
35 if (lzma_filter_flags_encode(&known_flags,
36 buffer, &out_pos, known_size) != LZMA_OK)
39 if (out_pos != known_size)
47 decode_ret(uint32_t known_size, lzma_ret expected_ret)
49 memcrap(&decoded_flags, sizeof(decoded_flags));
52 if (lzma_filter_flags_decode(&decoded_flags, NULL,
53 buffer, &pos, known_size) != expected_ret
62 decode(uint32_t known_size)
64 if (decode_ret(known_size, LZMA_OK))
67 if (known_flags.id != decoded_flags.id)
74 #if defined(HAVE_ENCODER_SUBBLOCK) && defined(HAVE_DECODER_SUBBLOCK)
79 known_flags.id = LZMA_FILTER_SUBBLOCK;
80 known_flags.options = NULL;
83 expect(decoded_flags.options == NULL);
86 buffer[0] = LZMA_FILTER_SUBBLOCK;
89 expect(!decode_ret(3, LZMA_OPTIONS_ERROR));
94 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
99 known_flags.id = LZMA_FILTER_X86;
100 known_flags.options = NULL;
104 expect(decoded_flags.options == NULL);
107 lzma_options_bcj options;
108 options.start_offset = 0;
109 known_flags.options = &options;
112 expect(decoded_flags.options == NULL);
115 options.start_offset = 123456;
116 known_flags.options = &options;
119 expect(decoded_flags.options != NULL);
121 lzma_options_bcj *decoded = decoded_flags.options;
122 expect(decoded->start_offset == options.start_offset);
129 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
134 known_flags.id = LZMA_FILTER_DELTA;
135 known_flags.options = NULL;
139 lzma_options_delta options = {
140 .type = LZMA_DELTA_TYPE_BYTE,
143 known_flags.options = &options;
147 options.dist = LZMA_DELTA_DIST_MIN;
150 expect(((lzma_options_delta *)(decoded_flags.options))->dist
153 free(decoded_flags.options);
156 options.dist = LZMA_DELTA_DIST_MAX;
159 expect(((lzma_options_delta *)(decoded_flags.options))->dist
162 free(decoded_flags.options);
165 options.dist = LZMA_DELTA_DIST_MAX + 1;
171 #ifdef HAVE_FILTER_LZMA
175 const lzma_options_lzma *known = known_flags.options;
176 const lzma_options_lzma *decoded = decoded_flags.options;
178 expect(known->dictionary_size <= decoded->dictionary_size);
180 if (known->dictionary_size == 1)
181 expect(decoded->dictionary_size == 1);
183 expect(known->dictionary_size + known->dictionary_size / 2
184 > decoded->dictionary_size);
186 expect(known->literal_context_bits == decoded->literal_context_bits);
187 expect(known->literal_pos_bits == decoded->literal_pos_bits);
188 expect(known->pos_bits == decoded->pos_bits);
196 known_flags.id = LZMA_FILTER_LZMA1;
197 known_flags.options = NULL;
201 lzma_options_lzma options = {
202 .dictionary_size = 0,
203 .literal_context_bits = 0,
204 .literal_pos_bits = 0,
206 .preset_dictionary = NULL,
207 .preset_dictionary_size = 0,
208 .mode = LZMA_MODE_INVALID,
210 .match_finder = LZMA_MF_INVALID,
211 .match_finder_cycles = 0,
214 // Test 3 (empty dictionary not allowed)
215 known_flags.options = &options;
218 // Test 4 (brute-force test some valid dictionary sizes)
219 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
220 while (options.dictionary_size != LZMA_DICTIONARY_SIZE_MAX) {
221 if (++options.dictionary_size == 5000)
222 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX - 5;
228 free(decoded_flags.options);
231 // Test 5 (too big dictionary size)
232 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX + 1;
235 // Test 6 (brute-force test lc/lp/pb)
236 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
237 for (uint32_t lc = LZMA_LITERAL_CONTEXT_BITS_MIN;
238 lc <= LZMA_LITERAL_CONTEXT_BITS_MAX; ++lc) {
239 for (uint32_t lp = LZMA_LITERAL_POS_BITS_MIN;
240 lp <= LZMA_LITERAL_POS_BITS_MAX; ++lp) {
241 for (uint32_t pb = LZMA_POS_BITS_MIN;
242 pb <= LZMA_POS_BITS_MAX; ++pb) {
243 if (lc + lp > LZMA_LITERAL_BITS_MAX)
246 options.literal_context_bits = lc;
247 options.literal_pos_bits = lp;
248 options.pos_bits = pb;
254 free(decoded_flags.options);
265 #if defined(HAVE_ENCODER_SUBBLOCK) && defined(HAVE_DECODER_SUBBLOCK)
268 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
271 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
274 // #ifdef HAVE_FILTER_LZMA