]> icculus.org git repositories - icculus/xz.git/blob - tests/test_filter_flags.c
Initial changes to change the suffix of the new format to .xz.
[icculus/xz.git] / tests / test_filter_flags.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       test_filter_flags.c
4 /// \brief      Tests Filter Flags coders
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "tests.h"
21
22
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;
27
28
29 static bool
30 encode(uint32_t known_size)
31 {
32         memcrap(buffer, sizeof(buffer));
33
34         uint32_t tmp;
35         if (lzma_filter_flags_size(&tmp, &known_flags) != LZMA_OK)
36                 return true;
37
38         if (tmp != known_size)
39                 return true;
40
41         size_t out_pos = 0;
42         if (lzma_filter_flags_encode(&known_flags,
43                         buffer, &out_pos, known_size) != LZMA_OK)
44                 return true;
45
46         if (out_pos != known_size)
47                 return true;
48
49         return false;
50 }
51
52
53 static bool
54 decode_ret(uint32_t known_size, lzma_ret expected_ret)
55 {
56         memcrap(&decoded_flags, sizeof(decoded_flags));
57
58         size_t pos = 0;
59         if (lzma_filter_flags_decode(&decoded_flags, NULL,
60                                 buffer, &pos, known_size) != expected_ret
61                         || pos != known_size)
62                 return true;
63
64         return false;
65 }
66
67
68 static bool
69 decode(uint32_t known_size)
70 {
71         if (decode_ret(known_size, LZMA_OK))
72                 return true;
73
74         if (known_flags.id != decoded_flags.id)
75                 return true;
76
77         return false;
78 }
79
80
81 #if defined(HAVE_ENCODER_SUBBLOCK) && defined(HAVE_DECODER_SUBBLOCK)
82 static void
83 test_subblock(void)
84 {
85         // Test 1
86         known_flags.id = LZMA_FILTER_SUBBLOCK;
87         known_flags.options = NULL;
88         expect(!encode(2));
89         expect(!decode(2));
90         expect(decoded_flags.options == NULL);
91
92         // Test 2
93         buffer[0] = LZMA_FILTER_SUBBLOCK;
94         buffer[1] = 1;
95         buffer[2] = 0;
96         expect(!decode_ret(3, LZMA_OPTIONS_ERROR));
97 }
98 #endif
99
100
101 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
102 static void
103 test_simple(void)
104 {
105         // Test 1
106         known_flags.id = LZMA_FILTER_X86;
107         known_flags.options = NULL;
108
109         expect(!encode(2));
110         expect(!decode(2));
111         expect(decoded_flags.options == NULL);
112
113         // Test 2
114         lzma_options_simple options;
115         options.start_offset = 0;
116         known_flags.options = &options;
117         expect(!encode(2));
118         expect(!decode(2));
119         expect(decoded_flags.options == NULL);
120
121         // Test 3
122         options.start_offset = 123456;
123         known_flags.options = &options;
124         expect(!encode(6));
125         expect(!decode(6));
126         expect(decoded_flags.options != NULL);
127
128         lzma_options_simple *decoded = decoded_flags.options;
129         expect(decoded->start_offset == options.start_offset);
130
131         free(decoded);
132 }
133 #endif
134
135
136 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
137 static void
138 test_delta(void)
139 {
140         // Test 1
141         known_flags.id = LZMA_FILTER_DELTA;
142         known_flags.options = NULL;
143         expect(encode(99));
144
145         // Test 2
146         lzma_options_delta options = {
147                 .type = LZMA_DELTA_TYPE_BYTE,
148                 .dist = 0
149         };
150         known_flags.options = &options;
151         expect(encode(99));
152
153         // Test 3
154         options.dist = LZMA_DELTA_DIST_MIN;
155         expect(!encode(3));
156         expect(!decode(3));
157         expect(((lzma_options_delta *)(decoded_flags.options))->dist
158                         == options.dist);
159
160         free(decoded_flags.options);
161
162         // Test 4
163         options.dist = LZMA_DELTA_DIST_MAX;
164         expect(!encode(3));
165         expect(!decode(3));
166         expect(((lzma_options_delta *)(decoded_flags.options))->dist
167                         == options.dist);
168
169         free(decoded_flags.options);
170
171         // Test 5
172         options.dist = LZMA_DELTA_DIST_MAX + 1;
173         expect(encode(99));
174 }
175 #endif
176
177 /*
178 #ifdef HAVE_FILTER_LZMA
179 static void
180 validate_lzma(void)
181 {
182         const lzma_options_lzma *known = known_flags.options;
183         const lzma_options_lzma *decoded = decoded_flags.options;
184
185         expect(known->dictionary_size <= decoded->dictionary_size);
186
187         if (known->dictionary_size == 1)
188                 expect(decoded->dictionary_size == 1);
189         else
190                 expect(known->dictionary_size + known->dictionary_size / 2
191                                 > decoded->dictionary_size);
192
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);
196 }
197
198
199 static void
200 test_lzma(void)
201 {
202         // Test 1
203         known_flags.id = LZMA_FILTER_LZMA1;
204         known_flags.options = NULL;
205         expect(encode(99));
206
207         // Test 2
208         lzma_options_lzma options = {
209                 .dictionary_size = 0,
210                 .literal_context_bits = 0,
211                 .literal_pos_bits = 0,
212                 .pos_bits = 0,
213                 .preset_dictionary = NULL,
214                 .preset_dictionary_size = 0,
215                 .mode = LZMA_MODE_INVALID,
216                 .fast_bytes = 0,
217                 .match_finder = LZMA_MF_INVALID,
218                 .match_finder_cycles = 0,
219         };
220
221         // Test 3 (empty dictionary not allowed)
222         known_flags.options = &options;
223         expect(encode(99));
224
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;
230
231                 expect(!encode(4));
232                 expect(!decode(4));
233                 validate_lzma();
234
235                 free(decoded_flags.options);
236         }
237
238         // Test 5 (too big dictionary size)
239         options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX + 1;
240         expect(encode(99));
241
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)
251                                         continue;
252
253                                 options.literal_context_bits = lc;
254                                 options.literal_pos_bits = lp;
255                                 options.pos_bits = pb;
256
257                                 expect(!encode(4));
258                                 expect(!decode(4));
259                                 validate_lzma();
260
261                                 free(decoded_flags.options);
262                         }
263                 }
264         }
265 }
266 #endif
267 */
268
269 int
270 main(void)
271 {
272         lzma_init();
273
274 #if defined(HAVE_ENCODER_SUBBLOCK) && defined(HAVE_DECODER_SUBBLOCK)
275         test_subblock();
276 #endif
277 #if defined(HAVE_ENCODER_X86) && defined(HAVE_DECODER_X86)
278         test_simple();
279 #endif
280 #if defined(HAVE_ENCODER_DELTA) && defined(HAVE_DECODER_DELTA)
281         test_delta();
282 #endif
283 // #ifdef HAVE_FILTER_LZMA
284 //      test_lzma();
285 // #endif
286
287         lzma_end(&strm);
288
289         return 0;
290 }