aboutsummaryrefslogtreecommitdiff
path: root/tests/tests.h
blob: c7b43fd0e2962316add6daa2de240e60970ffb71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
///////////////////////////////////////////////////////////////////////////////
//
/// \file       tests.h
/// \brief      Common definitions for test applications
//
//  Copyright (C) 2006 Lasse Collin
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef LZMA_TESTS_H
#define LZMA_TESTS_H

#include "sysdefs.h"

#include <stdio.h>

#define memcrap(buf, size) memset(buf, 0xFD, size)

#define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%u: %s\n", \
	__FILE__, __LINE__, #test), exit(1), 0))

#define succeed(test) expect(!(test))

#define fail(test) expect(test)


static inline const char *
lzma_ret_sym(lzma_ret ret)
{
	const char *str = "";

	switch (ret) {
	case LZMA_OK:
		str = "LZMA_OK";
		break;

	case LZMA_STREAM_END:
		str = "LZMA_STREAM_END";
		break;

	case LZMA_PROG_ERROR:
		str = "LZMA_PROG_ERROR";
		break;

	case LZMA_DATA_ERROR:
		str = "LZMA_DATA_ERROR";
		break;

	case LZMA_MEM_ERROR:
		str = "LZMA_MEM_ERROR";
		break;

	case LZMA_BUF_ERROR:
		str = "LZMA_BUF_ERROR";
		break;

	case LZMA_HEADER_ERROR:
		str = "LZMA_HEADER_ERROR";
		break;

	case LZMA_UNSUPPORTED_CHECK:
		str = "LZMA_UNSUPPORTED_CHECK";
		break;
	}

	return str;
}


static inline bool
coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
		uint8_t *out, size_t out_size,
		lzma_ret expected_ret, lzma_action finishing_action)
{
	size_t in_left = in_size;
	size_t out_left = out_size > 0 ? out_size + 1 : 0;
	lzma_action action = LZMA_RUN;
	lzma_ret ret;

	strm->next_in = NULL;
	strm->avail_in = 0;
	strm->next_out = NULL;
	strm->avail_out = 0;

	while (true) {
		if (in_left > 0) {
			if (--in_left == 0)
				action = finishing_action;

			strm->next_in = in++;
			strm->avail_in = 1;
		}

		if (out_left > 0) {
			--out_left;
			strm->next_out = out++;
			strm->avail_out = 1;
		}

		ret = lzma_code(strm, action);
		if (ret != LZMA_OK)
			break;
	}

	bool error = false;

	if (ret != expected_ret)
		error = true;

	if (expected_ret == LZMA_STREAM_END) {
		if (strm->total_in != in_size || strm->total_out != out_size)
			error = true;
	} else {
		if (strm->total_in + 1 != in_size
				|| strm->total_out != out_size)
			error = true;
	}

	return error;
}


static inline bool
decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size,
		lzma_ret expected_ret)
{
	return coder_loop(strm, in, in_size, NULL, 0, expected_ret, LZMA_RUN);
}


static inline bool
decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
{
	return coder_loop(strm, in, in_size, NULL, 0,
			LZMA_STREAM_END, LZMA_RUN);
}

#endif