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_filter known_flags;
25 static lzma_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(&known_flags,
43 buffer, &out_pos, known_size) != 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 #if defined(HAVE_ENCODER_SUBBLOCK) && defined(HAVE_DECODER_SUBBLOCK)
86 known_flags.id = LZMA_FILTER_SUBBLOCK;
87 known_flags.options = NULL;
90 expect(decoded_flags.options == NULL);
93 buffer[0] = LZMA_FILTER_SUBBLOCK;
96 expect(!decode_ret(3, LZMA_OPTIONS_ERROR));
101 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
106 known_flags.id = LZMA_FILTER_X86;
107 known_flags.options = NULL;
111 expect(decoded_flags.options == NULL);
114 lzma_options_simple options;
115 options.start_offset = 0;
116 known_flags.options = &options;
119 expect(decoded_flags.options == NULL);
122 options.start_offset = 123456;
123 known_flags.options = &options;
126 expect(decoded_flags.options != NULL);
128 lzma_options_simple *decoded = decoded_flags.options;
129 expect(decoded->start_offset == options.start_offset);
136 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
141 known_flags.id = LZMA_FILTER_DELTA;
142 known_flags.options = NULL;
146 lzma_options_delta options = {
147 .type = LZMA_DELTA_TYPE_BYTE,
150 known_flags.options = &options;
154 options.distance = LZMA_DELTA_DISTANCE_MIN;
157 expect(((lzma_options_delta *)(decoded_flags.options))
158 ->distance == options.distance);
160 free(decoded_flags.options);
163 options.distance = LZMA_DELTA_DISTANCE_MAX;
166 expect(((lzma_options_delta *)(decoded_flags.options))
167 ->distance == options.distance);
169 free(decoded_flags.options);
172 options.distance = LZMA_DELTA_DISTANCE_MAX + 1;
178 #ifdef HAVE_FILTER_LZMA
182 const lzma_options_lzma *known = known_flags.options;
183 const lzma_options_lzma *decoded = decoded_flags.options;
185 expect(known->dictionary_size <= decoded->dictionary_size);
187 if (known->dictionary_size == 1)
188 expect(decoded->dictionary_size == 1);
190 expect(known->dictionary_size + known->dictionary_size / 2
191 > decoded->dictionary_size);
193 expect(known->literal_context_bits == decoded->literal_context_bits);
194 expect(known->literal_pos_bits == decoded->literal_pos_bits);
195 expect(known->pos_bits == decoded->pos_bits);
203 known_flags.id = LZMA_FILTER_LZMA1;
204 known_flags.options = NULL;
208 lzma_options_lzma options = {
209 .dictionary_size = 0,
210 .literal_context_bits = 0,
211 .literal_pos_bits = 0,
213 .preset_dictionary = NULL,
214 .preset_dictionary_size = 0,
215 .mode = LZMA_MODE_INVALID,
217 .match_finder = LZMA_MF_INVALID,
218 .match_finder_cycles = 0,
221 // Test 3 (empty dictionary not allowed)
222 known_flags.options = &options;
225 // Test 4 (brute-force test some valid dictionary sizes)
226 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
227 while (options.dictionary_size != LZMA_DICTIONARY_SIZE_MAX) {
228 if (++options.dictionary_size == 5000)
229 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX - 5;
235 free(decoded_flags.options);
238 // Test 5 (too big dictionary size)
239 options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX + 1;
242 // Test 6 (brute-force test lc/lp/pb)
243 options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN;
244 for (uint32_t lc = LZMA_LITERAL_CONTEXT_BITS_MIN;
245 lc <= LZMA_LITERAL_CONTEXT_BITS_MAX; ++lc) {
246 for (uint32_t lp = LZMA_LITERAL_POS_BITS_MIN;
247 lp <= LZMA_LITERAL_POS_BITS_MAX; ++lp) {
248 for (uint32_t pb = LZMA_POS_BITS_MIN;
249 pb <= LZMA_POS_BITS_MAX; ++pb) {
250 if (lc + lp > LZMA_LITERAL_BITS_MAX)
253 options.literal_context_bits = lc;
254 options.literal_pos_bits = lp;
255 options.pos_bits = pb;
261 free(decoded_flags.options);
274 #if defined(HAVE_ENCODER_SUBBLOCK) && defined(HAVE_DECODER_SUBBLOCK)
277 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
280 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
283 // #ifdef HAVE_FILTER_LZMA