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