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_X86) && defined(HAVE_DECODER_X86)
79 known_flags.id = LZMA_FILTER_X86;
80 known_flags.options = NULL;
84 expect(decoded_flags.options == NULL);
87 lzma_options_bcj options;
88 options.start_offset = 0;
89 known_flags.options = &options;
92 expect(decoded_flags.options == NULL);
95 options.start_offset = 123456;
96 known_flags.options = &options;
99 expect(decoded_flags.options != NULL);
101 lzma_options_bcj *decoded = decoded_flags.options;
102 expect(decoded->start_offset == options.start_offset);
109 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
114 known_flags.id = LZMA_FILTER_DELTA;
115 known_flags.options = NULL;
119 lzma_options_delta options = {
120 .type = LZMA_DELTA_TYPE_BYTE,
123 known_flags.options = &options;
127 options.dist = LZMA_DELTA_DIST_MIN;
130 expect(((lzma_options_delta *)(decoded_flags.options))->dist
133 free(decoded_flags.options);
136 options.dist = LZMA_DELTA_DIST_MAX;
139 expect(((lzma_options_delta *)(decoded_flags.options))->dist
142 free(decoded_flags.options);
145 options.dist = LZMA_DELTA_DIST_MAX + 1;
151 #ifdef HAVE_FILTER_LZMA
155 const lzma_options_lzma *known = known_flags.options;
156 const lzma_options_lzma *decoded = decoded_flags.options;
158 expect(known->dictionary_size <= decoded->dictionary_size);
160 if (known->dictionary_size == 1)
161 expect(decoded->dictionary_size == 1);
163 expect(known->dictionary_size + known->dictionary_size / 2
164 > decoded->dictionary_size);
166 expect(known->literal_context_bits == decoded->literal_context_bits);
167 expect(known->literal_pos_bits == decoded->literal_pos_bits);
168 expect(known->pos_bits == decoded->pos_bits);
176 known_flags.id = LZMA_FILTER_LZMA1;
177 known_flags.options = NULL;
181 lzma_options_lzma options = {
182 .dictionary_size = 0,
183 .literal_context_bits = 0,
184 .literal_pos_bits = 0,
186 .preset_dictionary = NULL,
187 .preset_dictionary_size = 0,
188 .mode = LZMA_MODE_INVALID,
190 .match_finder = LZMA_MF_INVALID,
191 .match_finder_cycles = 0,
194 // Test 3 (empty dictionary not allowed)
195 known_flags.options = &options;
198 // Test 4 (brute-force test some valid dictionary sizes)
199 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
200 while (options.dictionary_size != LZMA_DICTIONARY_SIZE_MAX) {
201 if (++options.dictionary_size == 5000)
202 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX - 5;
208 free(decoded_flags.options);
211 // Test 5 (too big dictionary size)
212 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX + 1;
215 // Test 6 (brute-force test lc/lp/pb)
216 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
217 for (uint32_t lc = LZMA_LITERAL_CONTEXT_BITS_MIN;
218 lc <= LZMA_LITERAL_CONTEXT_BITS_MAX; ++lc) {
219 for (uint32_t lp = LZMA_LITERAL_POS_BITS_MIN;
220 lp <= LZMA_LITERAL_POS_BITS_MAX; ++lp) {
221 for (uint32_t pb = LZMA_POS_BITS_MIN;
222 pb <= LZMA_POS_BITS_MAX; ++pb) {
223 if (lc + lp > LZMA_LITERAL_BITS_MAX)
226 options.literal_context_bits = lc;
227 options.literal_pos_bits = lp;
228 options.pos_bits = pb;
234 free(decoded_flags.options);
245 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
248 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
251 // #ifdef HAVE_FILTER_LZMA