liblzma: Very minor API doc tweaks.
[xz.git] / tests / tests.h
blob8d53e9de154448f4868b47cd976bae6fc871802b
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"
19 #include "tuktest.h"
22 // Invalid value for the lzma_check enumeration. This must be positive
23 // but small enough to fit into signed char since the underlying type might
24 // one some platform be a signed char.
26 // Don't put LZMA_ at the beginning of the name so that it is obvious that
27 // this constant doesn't come from the API headers.
28 #define INVALID_LZMA_CHECK_ID ((lzma_check)(LZMA_CHECK_ID_MAX + 1))
31 #define memcrap(buf, size) memset(buf, 0xFD, size)
34 // TODO: Remove these three macros once all tests have been converted.
35 #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%d: %s\n", \
36 __FILE__, __LINE__, #test), abort(), 0))
38 #define succeed(test) expect(!(test))
40 #define fail(test) expect(test)
43 // This table and macro allow getting more readable error messages when
44 // comparing the lzma_ret enumeration values.
45 static const char enum_strings_lzma_ret[][24] = {
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 "LZMA_SEEK_NEEDED",
61 #define assert_lzma_ret(test_expr, ref_val) \
62 assert_enum_eq(test_expr, ref_val, enum_strings_lzma_ret)
65 static const char enum_strings_lzma_check[][24] = {
66 "LZMA_CHECK_NONE",
67 "LZMA_CHECK_CRC32",
68 "LZMA_CHECK_UNKNOWN_2",
69 "LZMA_CHECK_UNKNOWN_3",
70 "LZMA_CHECK_CRC64",
71 "LZMA_CHECK_UNKNOWN_5",
72 "LZMA_CHECK_UNKNOWN_6",
73 "LZMA_CHECK_UNKNOWN_7",
74 "LZMA_CHECK_UNKNOWN_8",
75 "LZMA_CHECK_UNKNOWN_9",
76 "LZMA_CHECK_SHA256",
77 "LZMA_CHECK_UNKNOWN_11",
78 "LZMA_CHECK_UNKNOWN_12",
79 "LZMA_CHECK_UNKNOWN_13",
80 "LZMA_CHECK_UNKNOWN_14",
81 "LZMA_CHECK_UNKNOWN_15",
84 #define assert_lzma_check(test_expr, ref_val) \
85 assert_enum_eq(test_expr, ref_val, enum_strings_lzma_check)
88 static inline bool
89 coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
90 uint8_t *out, size_t out_size,
91 lzma_ret expected_ret, lzma_action finishing_action)
93 size_t in_left = in_size;
94 size_t out_left = out_size > 0 ? out_size + 1 : 0;
95 lzma_action action = LZMA_RUN;
96 lzma_ret ret;
98 strm->next_in = NULL;
99 strm->avail_in = 0;
100 strm->next_out = NULL;
101 strm->avail_out = 0;
103 while (true) {
104 if (in_left > 0) {
105 if (--in_left == 0)
106 action = finishing_action;
108 strm->next_in = in++;
109 strm->avail_in = 1;
112 if (out_left > 0) {
113 --out_left;
114 strm->next_out = out++;
115 strm->avail_out = 1;
118 ret = lzma_code(strm, action);
119 if (ret != LZMA_OK)
120 break;
123 bool error = false;
125 if (ret != expected_ret)
126 error = true;
128 if (strm->total_in != in_size || strm->total_out != out_size)
129 error = true;
131 return error;
135 static inline bool
136 decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
137 lzma_ret expected_ret)
139 return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
143 static inline bool
144 decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
146 return coder_loop(strm, in, in_size, NULL, 0,
147 LZMA_STREAM_END, LZMA_RUN);
150 #endif