]> icculus.org git repositories - icculus/xz.git/blob - tests/tests.h
Tweak the compression presets -0 .. -5.
[icculus/xz.git] / tests / tests.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       tests.h
4 /// \brief      Common definitions for test applications
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef LZMA_TESTS_H
14 #define LZMA_TESTS_H
15
16 #include "sysdefs.h"
17 #include "tuklib_integer.h"
18 #include "lzma.h"
19
20 #include <stdio.h>
21
22 #define memcrap(buf, size) memset(buf, 0xFD, size)
23
24 #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%d: %s\n", \
25         __FILE__, __LINE__, #test), abort(), 0))
26
27 #define succeed(test) expect(!(test))
28
29 #define fail(test) expect(test)
30
31
32 static inline const char *
33 lzma_ret_sym(lzma_ret ret)
34 {
35         if ((unsigned int)(ret) > LZMA_PROG_ERROR)
36                 return "UNKNOWN_ERROR";
37
38         static const char *msgs[] = {
39                 "LZMA_OK",
40                 "LZMA_STREAM_END",
41                 "LZMA_NO_CHECK",
42                 "LZMA_UNSUPPORTED_CHECK",
43                 "LZMA_GET_CHECK",
44                 "LZMA_MEM_ERROR",
45                 "LZMA_MEMLIMIT_ERROR",
46                 "LZMA_FORMAT_ERROR",
47                 "LZMA_OPTIONS_ERROR",
48                 "LZMA_DATA_ERROR",
49                 "LZMA_BUF_ERROR",
50                 "LZMA_PROG_ERROR"
51         };
52
53         return msgs[ret];
54 }
55
56
57 static inline bool
58 coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
59                 uint8_t *out, size_t out_size,
60                 lzma_ret expected_ret, lzma_action finishing_action)
61 {
62         size_t in_left = in_size;
63         size_t out_left = out_size > 0 ? out_size + 1 : 0;
64         lzma_action action = LZMA_RUN;
65         lzma_ret ret;
66
67         strm->next_in = NULL;
68         strm->avail_in = 0;
69         strm->next_out = NULL;
70         strm->avail_out = 0;
71
72         while (true) {
73                 if (in_left > 0) {
74                         if (--in_left == 0)
75                                 action = finishing_action;
76
77                         strm->next_in = in++;
78                         strm->avail_in = 1;
79                 }
80
81                 if (out_left > 0) {
82                         --out_left;
83                         strm->next_out = out++;
84                         strm->avail_out = 1;
85                 }
86
87                 ret = lzma_code(strm, action);
88                 if (ret != LZMA_OK)
89                         break;
90         }
91
92         bool error = false;
93
94         if (ret != expected_ret)
95                 error = true;
96
97         if (expected_ret == LZMA_STREAM_END) {
98                 if (strm->total_in != in_size || strm->total_out != out_size)
99                         error = true;
100         } else {
101                 if (strm->total_in != in_size || strm->total_out != out_size)
102                         error = true;
103         }
104
105         return error;
106 }
107
108
109 static inline bool
110 decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
111                 lzma_ret expected_ret)
112 {
113         return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
114 }
115
116
117 static inline bool
118 decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
119 {
120         return coder_loop(strm, in, in_size, NULL, 0,
121                         LZMA_STREAM_END, LZMA_RUN);
122 }
123
124 #endif