check_code_options.c 6.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "libaec.h"
#include "check_aec.h"

#define BUF_SIZE 1024 * 3

int check_block_sizes(struct test_state *state, int id, int id_len)
{
12
    int bs, status, rsi, max_rsi;
13
14
15
16

    for (bs = 8; bs <= 64; bs *= 2) {
        state->strm->block_size = bs;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
17
        max_rsi = state->buf_len / (bs * state->bytes_per_sample);
18
19
20
21
22
        if (max_rsi > 4096)
            max_rsi = 4096;

        for (rsi = 1; rsi <= max_rsi; rsi++) {
            state->strm->rsi = rsi;
23
            status = state->codec(state);
24
25
26
27
            if (status)
                return status;

            if ((state->cbuf[0] >> (8 - id_len)) != id) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
28
29
                printf("%s: Unexpected block of size %i created ID:%x.\n",
                       CHECK_FAIL, bs, state->cbuf[0] >> (8 - id_len));
30
31
                return 99;
            }
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        }
    }
    return 0;
}

int check_zero(struct test_state *state)
{
    int status;

    memset(state->ubuf, 0x55, state->buf_len);

    printf("Checking zero blocks ... ");
    status = check_block_sizes(state, 0, state->id_len + 1);
    if (status)
        return status;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
48
    printf ("%s\n", CHECK_PASS);
49
50
51
52
53
54
55
56
    return 0;
}

int check_splitting(struct test_state *state, int k)
{
    int status, size;
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
57
    size = state->bytes_per_sample;
58
59
60

    for (tmp = state->ubuf;
         tmp < state->ubuf + state->buf_len;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
61
         tmp += 4 * state->bytes_per_sample) {
62
63
64
65
66
67
68
69
70
71
72
        state->out(tmp, state->xmin + (1ULL << (k - 1)) - 1, size);
        state->out(tmp + size, state->xmin, size);
        state->out(tmp + 2 * size, state->xmin + (1ULL << (k + 1)) - 1, size);
        state->out(tmp + 3 * size, state->xmin, size);
    }

    printf("Checking splitting with k=%i ... ", k);
    status = check_block_sizes(state, k + 1, state->id_len);
    if (status)
        return status;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
73
    printf ("%s\n", CHECK_PASS);
74
75
76
77
78
79
80
81
    return 0;
}

int check_uncompressed(struct test_state *state)
{
    int status, size;
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
82
    size = state->bytes_per_sample;
83
84
85

    for (tmp = state->ubuf;
         tmp < state->ubuf + state->buf_len;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
86
         tmp += 2 * state->bytes_per_sample) {
87
88
89
90
91
92
93
94
95
96
97
        state->out(tmp, state->xmax, size);
        state->out(tmp + size, state->xmin, size);
    }

    printf("Checking uncompressed ... ");
    status = check_block_sizes(state,
                               (1ULL << state->id_len) - 1,
                               state->id_len);
    if (status)
        return status;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
98
    printf ("%s\n", CHECK_PASS);
99
100
101
102
103
104
105
106
    return 0;
}

int check_fs(struct test_state *state)
{
    int status, size;
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107
    size = state->bytes_per_sample;
108
109
110

    for (tmp = state->ubuf;
         tmp < state->ubuf + state->buf_len;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
111
         tmp += 4 * state->bytes_per_sample) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
112
        state->out(tmp, state->xmin + 2, size);
113
        state->out(tmp + size, state->xmin, size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
114
115
        state->out(tmp + 2 * size, state->xmin, size);
        state->out(tmp + 3 * size, state->xmin, size);
116
117
118
119
120
121
122
    }

    printf("Checking FS ... ");
    status = check_block_sizes(state, 1, state->id_len);
    if (status)
        return status;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
123
    printf ("%s\n", CHECK_PASS);
124
125
126
127
128
129
130
131
    return 0;
}

int check_se(struct test_state *state)
{
    int status, size;
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
132
    size = state->bytes_per_sample;
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

    for (tmp = state->ubuf;
         tmp < state->ubuf + state->buf_len;
         tmp += 8 * size) {
        state->out(tmp, 0, size);
        state->out(tmp + size, 0, size);
        state->out(tmp + 2 * size, 0, size);
        state->out(tmp + 3 * size, 0, size);
        state->out(tmp + 4 * size, 0, size);
        state->out(tmp + 5 * size, 0, size);
        state->out(tmp + 6 * size, 0, size);
        state->out(tmp + 7 * size, 1, size);
    }

    printf("Checking Second Extension ... ");
    status = check_block_sizes(state, 1, state->id_len + 1);
    if (status)
        return status;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
152
    printf ("%s\n", CHECK_PASS);
153
154
155
156
157
158
159
160
    return 0;
}

int check_bps(struct test_state *state)
{
    int k, status, bps;

    for (bps = 8; bps <= 32; bps += 8) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
161
        state->strm->bits_per_sample = bps;
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        if (bps == 24)
            state->strm->flags |= AEC_DATA_3BYTE;
        else
            state->strm->flags &= ~AEC_DATA_3BYTE;

        update_state(state);

        status = check_zero(state);
        if (status)
            return status;

        status = check_se(state);
        if (status)
            return status;

        status = check_uncompressed(state);
        if (status)
            return status;

        status = check_fs(state);
        if (status)
            return status;

        for (k = 1; k < bps - 2; k++) {
            status = check_splitting(state, k);
            if (status)
                return status;
        }
        printf("All checks with %i bit per sample passed.\n", bps);
    }
    return 0;
}

195
int check_byte_orderings(struct test_state *state)
196
197
198
199
200
201
{
    int status;

    printf("----------------------------\n");
    printf("Checking LSB first, unsigned\n");
    printf("----------------------------\n");
202
    status = check_bps(state);
203
    if (status)
204
        return status;
205
206
207
208

    printf("--------------------------\n");
    printf("Checking LSB first, signed\n");
    printf("--------------------------\n");
209
    state->strm->flags |= AEC_DATA_SIGNED;
210

211
    status = check_bps(state);
212
    if (status)
213
        return status;
214

215
216
    state->strm->flags &= ~AEC_DATA_SIGNED;
    state->strm->flags |= AEC_DATA_MSB;
217
218
219
220

    printf("----------------------------\n");
    printf("Checking MSB first, unsigned\n");
    printf("----------------------------\n");
221
    status = check_bps(state);
222
    if (status)
223
        return status;
224
225
226
227

    printf("--------------------------\n");
    printf("Checking MSB first, signed\n");
    printf("--------------------------\n");
228
229
230
231
232
    state->strm->flags |= AEC_DATA_SIGNED;

    status = check_bps(state);
    if (status)
        return status;
233
    return 0;
234
235
236
237
238
239
240
241
242
243
244
245
246
247
}

int main (void)
{
    int status;
    struct aec_stream strm;
    struct test_state state;

    state.buf_len = state.ibuf_len = BUF_SIZE;
    state.cbuf_len = 2 * BUF_SIZE;

    state.ubuf = (unsigned char *)malloc(state.buf_len);
    state.cbuf = (unsigned char *)malloc(state.cbuf_len);
    state.obuf = (unsigned char *)malloc(state.buf_len);
248

249
250
251
252
253
254
255
256
257
258
259
260
261
    if (!state.ubuf || !state.cbuf || !state.obuf) {
        printf("Not enough memory.\n");
        return 99;
    }

    strm.flags = AEC_DATA_PREPROCESS;
    state.strm = &strm;

    printf("***************************\n");
    printf("Checking with small buffers\n");
    printf("***************************\n");
    state.codec = encode_decode_small;
    status = check_byte_orderings(&state);
262
263
264
    if (status)
        goto DESTRUCT;

265
266
267
268
269
270
    printf("***************************\n");
    printf("Checking with large buffers\n");
    printf("***************************\n");
    state.codec = encode_decode_large;
    status = check_byte_orderings(&state);

271
272
273
274
275
276
277
DESTRUCT:
    free(state.ubuf);
    free(state.cbuf);
    free(state.obuf);

    return status;
}