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

#define BUF_SIZE 1024 * 3

int check_block_sizes(struct test_state *state, int id, int id_len)
{
10
    state->id = id;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
11 12
    for (int bs = 8; bs <= 64; bs *= 2) {
        int max_rsi;
13

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
14
        state->strm->block_size = bs;
15
        max_rsi = (int)(state->buf_len / (bs * state->bytes_per_sample));
16 17 18
        if (max_rsi > 4096)
            max_rsi = 4096;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
19 20
        for (int rsi = 1; rsi <= max_rsi; rsi++) {
            int status;
21
            state->strm->rsi = rsi;
22
            status = state->codec(state);
23 24 25 26
            if (status)
                return status;

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
42 43 44 45
    if (state->strm->flags & AEC_DATA_PREPROCESS)
        memset(state->ubuf, 0x55, state->buf_len);
    else
        memset(state->ubuf, 0, state->buf_len);
46 47 48 49 50 51

    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
52
    printf ("%s\n", CHECK_PASS);
53 54 55 56 57
    return 0;
}

int check_splitting(struct test_state *state, int k)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58
    int status;
59 60
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
61
    int size = state->bytes_per_sample;
62

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    if (state->strm->flags & AEC_DATA_PREPROCESS) {
        for (tmp = state->ubuf;
             tmp < state->ubuf + state->buf_len;
             tmp += 4 * size) {
            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);
        }
    } else {
        for (tmp = state->ubuf;
             tmp < state->ubuf + state->buf_len;
             tmp += 4 * size) {
            state->out(tmp, 0, size);
            state->out(tmp + size, (1ULL << k) - 1, size);
            state->out(tmp + 2 * size, 0, size);
            state->out(tmp + 3 * size, (1ULL << (k + 2)) - 1, size);
        }
82 83 84 85 86 87 88
    }

    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
89
    printf ("%s\n", CHECK_PASS);
90 91 92 93 94
    return 0;
}

int check_uncompressed(struct test_state *state)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
95
    int status;
96 97
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
98
    int size = state->bytes_per_sample;
99 100 101

    for (tmp = state->ubuf;
         tmp < state->ubuf + state->buf_len;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
102
         tmp += 2 * size) {
103 104 105 106 107 108 109 110 111 112 113
        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
114
    printf ("%s\n", CHECK_PASS);
115 116 117 118 119
    return 0;
}

int check_fs(struct test_state *state)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
120
    int status;
121 122
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
123
    int size = state->bytes_per_sample;
124

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    if (state->strm->flags & AEC_DATA_PREPROCESS) {
        for (tmp = state->ubuf;
             tmp < state->ubuf + state->buf_len;
             tmp += 4 * size) {
            state->out(tmp, state->xmin + 2, size);
            state->out(tmp + size, state->xmin, size);
            state->out(tmp + 2 * size, state->xmin, size);
            state->out(tmp + 3 * size, state->xmin, size);
        }
    } else {
        for (tmp = state->ubuf;
             tmp < state->ubuf + state->buf_len;
             tmp += 4 * size) {
            state->out(tmp, 0, size);
            state->out(tmp + size, 0, size);
            state->out(tmp + 2 * size, 0, size);
            state->out(tmp + 3 * size, 4, size);
        }
143 144 145 146 147 148 149
    }

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
150
    printf ("%s\n", CHECK_PASS);
151 152 153 154 155
    return 0;
}

int check_se(struct test_state *state)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
156
    int status;
157 158
    unsigned char *tmp;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
159
    int size = state->bytes_per_sample;
160

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
161 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
    if (state->strm->flags & AEC_DATA_PREPROCESS) {
        for (tmp = state->ubuf;
             tmp < state->ubuf + state->buf_len;
             tmp += 8 * size) {
            state->out(tmp, state->xmax - 1, size);
            state->out(tmp + size, state->xmax - 1, size);
            state->out(tmp + 2 * size, state->xmax - 1, size);
            state->out(tmp + 3 * size, state->xmax - 1, size);
            state->out(tmp + 4 * size, state->xmax, size);
            state->out(tmp + 5 * size, state->xmax, size);
            state->out(tmp + 6 * size, state->xmax, size);
            state->out(tmp + 7 * size, state->xmax, size);
        }
    } else {
        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, 1, size);
            state->out(tmp + 5 * size, 0, size);
            state->out(tmp + 6 * size, 0, size);
            state->out(tmp + 7 * size, 2, size);
        }
187 188 189 190 191 192 193
    }

    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
194
    printf ("%s\n", CHECK_PASS);
195 196 197 198 199
    return 0;
}

int check_bps(struct test_state *state)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
200
    int status;
201

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
202
    for (int bps = 8; bps <= 32; bps += 8) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
203
        state->strm->bits_per_sample = bps;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        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;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
227
        for (int k = 1; k < bps - 2; k++) {
228 229 230 231 232 233 234 235 236
            status = check_splitting(state, k);
            if (status)
                return status;
        }
        printf("All checks with %i bit per sample passed.\n", bps);
    }
    return 0;
}

237
int check_byte_orderings(struct test_state *state)
238 239 240
{
    int status;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
241 242 243 244 245 246 247 248 249 250 251
    printf("-----------------------------------\n");
    printf("Checking no PP, LSB first, unsigned\n");
    printf("-----------------------------------\n");
    status = check_bps(state);
    if (status)
        return status;

    printf("-----------------------------------\n");
    printf("Checking PP, LSB first, unsigned\n");
    printf("-----------------------------------\n");
    state->strm->flags |= AEC_DATA_PREPROCESS;
252
    status = check_bps(state);
253
    if (status)
254
        return status;
255

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
256 257 258
    printf("-----------------------------------\n");
    printf("Checking PP, LSB first, signed\n");
    printf("-----------------------------------\n");
259
    state->strm->flags |= AEC_DATA_SIGNED;
260

261
    status = check_bps(state);
262
    if (status)
263
        return status;
264

265 266
    state->strm->flags &= ~AEC_DATA_SIGNED;
    state->strm->flags |= AEC_DATA_MSB;
267

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
268 269 270
    printf("-----------------------------------\n");
    printf("Checking PP, MSB first, unsigned\n");
    printf("-----------------------------------\n");
271
    status = check_bps(state);
272
    if (status)
273
        return status;
274

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
275 276 277
    printf("-----------------------------------\n");
    printf("Checking PP, MSB first, signed\n");
    printf("-----------------------------------\n");
278 279 280 281 282
    state->strm->flags |= AEC_DATA_SIGNED;

    status = check_bps(state);
    if (status)
        return status;
283
    return 0;
284 285
}

286
int main(int argc, char *argv[])
287 288 289 290 291
{
    int status;
    struct aec_stream strm;
    struct test_state state;

292 293 294 295 296
    if (argc > 1 && strncmp(argv[1], "-d", 2) == 0)
        state.dump = 1;
    else
        state.dump = 0;

297 298 299 300 301 302
    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);
303

304 305 306 307 308
    if (!state.ubuf || !state.cbuf || !state.obuf) {
        printf("Not enough memory.\n");
        return 99;
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
309
    strm.flags = 0;
310 311 312 313 314 315 316
    state.strm = &strm;

    printf("***************************\n");
    printf("Checking with small buffers\n");
    printf("***************************\n");
    state.codec = encode_decode_small;
    status = check_byte_orderings(&state);
317 318 319
    if (status)
        goto DESTRUCT;

320 321 322 323 324 325
    printf("***************************\n");
    printf("Checking with large buffers\n");
    printf("***************************\n");
    state.codec = encode_decode_large;
    status = check_byte_orderings(&state);

326 327 328 329 330 331 332
DESTRUCT:
    free(state.ubuf);
    free(state.cbuf);
    free(state.obuf);

    return status;
}