]> icculus.org git repositories - icculus/xz.git/blob - tests/tests.h
Sort of garbage collection commit. :-| Many things are still
[icculus/xz.git] / tests / tests.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       tests.h
4 /// \brief      Common definitions for test applications
5 //
6 //  Copyright (C) 2006 Lasse Collin
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 2 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program 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
16 //  GNU General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #ifndef LZMA_TESTS_H
21 #define LZMA_TESTS_H
22
23 #include "sysdefs.h"
24 #include "integer.h"
25
26 #include <stdio.h>
27
28 #define memcrap(buf, size) memset(buf, 0xFD, size)
29
30 #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%u: %s\n", \
31         __FILE__, __LINE__, #test), abort(), 0))
32
33 #define succeed(test) expect(!(test))
34
35 #define fail(test) expect(test)
36
37
38 static inline const char *
39 lzma_ret_sym(lzma_ret ret)
40 {
41         const char *str = "";
42
43         switch (ret) {
44         case LZMA_OK:
45                 str = "LZMA_OK";
46                 break;
47
48         case LZMA_STREAM_END:
49                 str = "LZMA_STREAM_END";
50                 break;
51
52         case LZMA_PROG_ERROR:
53                 str = "LZMA_PROG_ERROR";
54                 break;
55
56         case LZMA_DATA_ERROR:
57                 str = "LZMA_DATA_ERROR";
58                 break;
59
60         case LZMA_MEM_ERROR:
61                 str = "LZMA_MEM_ERROR";
62                 break;
63
64         case LZMA_BUF_ERROR:
65                 str = "LZMA_BUF_ERROR";
66                 break;
67
68         case LZMA_HEADER_ERROR:
69                 str = "LZMA_HEADER_ERROR";
70                 break;
71
72         case LZMA_NO_CHECK:
73                 str = "LZMA_NO_CHECK";
74                 break;
75
76         case LZMA_UNSUPPORTED_CHECK:
77                 str = "LZMA_UNSUPPORTED_CHECK";
78                 break;
79
80         case LZMA_SEE_CHECK:
81                 str = "LZMA_SEE_CHECK";
82                 break;
83
84         case LZMA_FORMAT_ERROR:
85                 str = "LZMA_FORMAT_ERROR";
86                 break;
87
88         case LZMA_MEMLIMIT_ERROR:
89                 str = "LZMA_MEMLIMIT_ERROR";
90                 break;
91         }
92
93         return str;
94 }
95
96
97 static inline bool
98 coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
99                 uint8_t *out, size_t out_size,
100                 lzma_ret expected_ret, lzma_action finishing_action)
101 {
102         size_t in_left = in_size;
103         size_t out_left = out_size > 0 ? out_size + 1 : 0;
104         lzma_action action = LZMA_RUN;
105         lzma_ret ret;
106
107         strm->next_in = NULL;
108         strm->avail_in = 0;
109         strm->next_out = NULL;
110         strm->avail_out = 0;
111
112         while (true) {
113                 if (in_left > 0) {
114                         if (--in_left == 0)
115                                 action = finishing_action;
116
117                         strm->next_in = in++;
118                         strm->avail_in = 1;
119                 }
120
121                 if (out_left > 0) {
122                         --out_left;
123                         strm->next_out = out++;
124                         strm->avail_out = 1;
125                 }
126
127                 ret = lzma_code(strm, action);
128                 if (ret != LZMA_OK)
129                         break;
130         }
131
132         bool error = false;
133
134         if (ret != expected_ret)
135                 error = true;
136
137         if (expected_ret == LZMA_STREAM_END) {
138                 if (strm->total_in != in_size || strm->total_out != out_size)
139                         error = true;
140         } else {
141                 if (strm->total_in != in_size || strm->total_out != out_size)
142                         error = true;
143         }
144
145         return error;
146 }
147
148
149 static inline bool
150 decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
151                 lzma_ret expected_ret)
152 {
153         return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
154 }
155
156
157 static inline bool
158 decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
159 {
160         return coder_loop(strm, in, in_size, NULL, 0,
161                         LZMA_STREAM_END, LZMA_RUN);
162 }
163
164 #endif