]> icculus.org git repositories - icculus/xz.git/blob - tests/tests.h
Initial changes to change the suffix of the new format to .xz.
[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         if ((unsigned)(ret) > LZMA_PROG_ERROR)
42                 return "UNKNOWN_ERROR";
43
44         static const char *msgs[] = {
45                 "LZMA_OK",
46                 "LZMA_STREAM_END",
47                 "LZMA_NO_CHECK",
48                 "LZMA_UNSUPPORTED_CHECK",
49                 "LZMA_GET_CHECK",
50                 "LZMA_MEM_ERROR",
51                 "LZMA_MEMLIMIT_ERROR",
52                 "LZMA_FORMAT_ERROR",
53                 "LZMA_OPTIONS_ERROR",
54                 "LZMA_DATA_ERROR",
55                 "LZMA_BUF_ERROR",
56                 "LZMA_PROG_ERROR"
57         };
58
59         return msgs[ret];
60 }
61
62
63 static inline bool
64 coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
65                 uint8_t *out, size_t out_size,
66                 lzma_ret expected_ret, lzma_action finishing_action)
67 {
68         size_t in_left = in_size;
69         size_t out_left = out_size > 0 ? out_size + 1 : 0;
70         lzma_action action = LZMA_RUN;
71         lzma_ret ret;
72
73         strm->next_in = NULL;
74         strm->avail_in = 0;
75         strm->next_out = NULL;
76         strm->avail_out = 0;
77
78         while (true) {
79                 if (in_left > 0) {
80                         if (--in_left == 0)
81                                 action = finishing_action;
82
83                         strm->next_in = in++;
84                         strm->avail_in = 1;
85                 }
86
87                 if (out_left > 0) {
88                         --out_left;
89                         strm->next_out = out++;
90                         strm->avail_out = 1;
91                 }
92
93                 ret = lzma_code(strm, action);
94                 if (ret != LZMA_OK)
95                         break;
96         }
97
98         bool error = false;
99
100         if (ret != expected_ret)
101                 error = true;
102
103         if (expected_ret == LZMA_STREAM_END) {
104                 if (strm->total_in != in_size || strm->total_out != out_size)
105                         error = true;
106         } else {
107                 if (strm->total_in != in_size || strm->total_out != out_size)
108                         error = true;
109         }
110
111         return error;
112 }
113
114
115 static inline bool
116 decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
117                 lzma_ret expected_ret)
118 {
119         return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
120 }
121
122
123 static inline bool
124 decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
125 {
126         return coder_loop(strm, in, in_size, NULL, 0,
127                         LZMA_STREAM_END, LZMA_RUN);
128 }
129
130 #endif