]> icculus.org git repositories - icculus/xz.git/blob - tests/tests.h
Imported to git.
[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
25 #include <stdio.h>
26
27 #define memcrap(buf, size) memset(buf, 0xFD, size)
28
29 #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%u: %s\n", \
30         __FILE__, __LINE__, #test), exit(1), 0))
31
32 #define succeed(test) expect(!test)
33
34 #define fail(test) expect(test)
35
36
37 static inline const char *
38 lzma_ret_sym(lzma_ret ret)
39 {
40         const char *str = "";
41
42         switch (ret) {
43         case LZMA_OK:
44                 str = "LZMA_OK";
45                 break;
46
47         case LZMA_STREAM_END:
48                 str = "LZMA_STREAM_END";
49                 break;
50
51         case LZMA_PROG_ERROR:
52                 str = "LZMA_PROG_ERROR";
53                 break;
54
55         case LZMA_DATA_ERROR:
56                 str = "LZMA_DATA_ERROR";
57                 break;
58
59         case LZMA_MEM_ERROR:
60                 str = "LZMA_MEM_ERROR";
61                 break;
62
63         case LZMA_BUF_ERROR:
64                 str = "LZMA_BUF_ERROR";
65                 break;
66
67         case LZMA_HEADER_ERROR:
68                 str = "LZMA_HEADER_ERROR";
69                 break;
70
71         case LZMA_UNSUPPORTED_CHECK:
72                 str = "LZMA_UNSUPPORTED_CHECK";
73                 break;
74         }
75
76         return str;
77 }
78
79
80 static inline bool
81 coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
82                 uint8_t *out, size_t out_size,
83                 lzma_ret expected_ret, lzma_action finishing_action)
84 {
85         size_t in_left = in_size;
86         size_t out_left = out_size > 0 ? out_size + 1 : 0;
87         lzma_action action = LZMA_RUN;
88         lzma_ret ret;
89
90         strm->next_in = NULL;
91         strm->avail_in = 0;
92         strm->next_out = NULL;
93         strm->avail_out = 0;
94
95         while (true) {
96                 if (in_left > 0) {
97                         if (--in_left == 0)
98                                 action = finishing_action;
99
100                         strm->next_in = in++;
101                         strm->avail_in = 1;
102                 }
103
104                 if (out_left > 0) {
105                         --out_left;
106                         strm->next_out = out++;
107                         strm->avail_out = 1;
108                 }
109
110                 ret = lzma_code(strm, action);
111                 if (ret != LZMA_OK)
112                         break;
113         }
114
115         bool error = false;
116
117         if (ret != expected_ret)
118                 error = true;
119
120         if (expected_ret == LZMA_STREAM_END) {
121                 if (strm->total_in != in_size || strm->total_out != out_size)
122                         error = true;
123         } else {
124                 if (strm->total_in + 1 != in_size
125                                 || strm->total_out != out_size)
126                         error = true;
127         }
128
129         return error;
130 }
131
132
133 static inline bool
134 decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
135                 lzma_ret expected_ret)
136 {
137         return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
138 }
139
140
141 static inline bool
142 decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
143 {
144         return coder_loop(strm, in, in_size, NULL, 0,
145                         LZMA_STREAM_END, LZMA_RUN);
146 }
147
148 #endif