debian/copyright: INSTALL.generic update
[xz/debian.git] / tests / tests.h
blob8f3c745d8ec8df50907bb5061a5e60a004e20a06
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.
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef LZMA_TESTS_H
14 #define LZMA_TESTS_H
16 #include "sysdefs.h"
17 #include "tuklib_integer.h"
18 #include "lzma.h"
20 #include <stdio.h>
22 #define memcrap(buf, size) memset(buf, 0xFD, size)
24 #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%d: %s\n", \
25 __FILE__, __LINE__, #test), abort(), 0))
27 #define succeed(test) expect(!(test))
29 #define fail(test) expect(test)
32 static inline const char *
33 lzma_ret_sym(lzma_ret ret)
35 if ((unsigned int)(ret) > LZMA_PROG_ERROR)
36 return "UNKNOWN_ERROR";
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"
53 return msgs[ret];
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)
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;
67 strm->next_in = NULL;
68 strm->avail_in = 0;
69 strm->next_out = NULL;
70 strm->avail_out = 0;
72 while (true) {
73 if (in_left > 0) {
74 if (--in_left == 0)
75 action = finishing_action;
77 strm->next_in = in++;
78 strm->avail_in = 1;
81 if (out_left > 0) {
82 --out_left;
83 strm->next_out = out++;
84 strm->avail_out = 1;
87 ret = lzma_code(strm, action);
88 if (ret != LZMA_OK)
89 break;
92 bool error = false;
94 if (ret != expected_ret)
95 error = true;
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;
105 return error;
109 static inline bool
110 decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
111 lzma_ret expected_ret)
113 return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
117 static inline bool
118 decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
120 return coder_loop(strm, in, in_size, NULL, 0,
121 LZMA_STREAM_END, LZMA_RUN);
124 #endif