check_buffer_sizes.c 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#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 bs, status;

    for (bs = 8; bs <= 64; bs *= 2) {
        state->strm->block_size = bs;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
16
        state->strm->rsi = state->buf_len / (bs * state->bytes_per_sample);
17

18
        status = encode_decode_large(state);
19 20 21 22 23 24 25 26 27 28 29 30 31 32
        if (status)
            return status;
    }
    return 0;
}

int check_block_sizes_short(struct test_state *state)
{
    int bs, status;
    size_t tmp;

    tmp = state->ibuf_len;
    for (bs = 8; bs <= 64; bs *= 2) {
        state->strm->block_size = bs;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
33
        state->strm->rsi = state->buf_len / (bs * state->bytes_per_sample);
34
        state->ibuf_len = state->buf_len - 2 * bs + 4;
35
        status = encode_decode_large(state);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        if (status)
            return status;
        if (state->strm->total_out != state->buf_len) {
            printf("FAIL: Unexpected buffer length. Got %i expected %i\n",
                   (int)state->strm->total_out,
                   (int)state->buf_len);
            return 99;
        }
    }
    state->ibuf_len = tmp;
    return 0;
}

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
54
    size = state->bytes_per_sample;
55 56 57

    for (tmp = state->ubuf;
         tmp < state->ubuf + state->buf_len;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58
         tmp += 2 * state->bytes_per_sample) {
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
        state->out(tmp, state->xmax, size);
        state->out(tmp + size, state->xmin, size);
    }

    printf("Checking full rsi ... ");
    status = check_block_sizes(state);
    if (status)
        return status;

    printf ("OK\n");

    printf("Checking short rsi ... ");
    status = check_block_sizes_short(state);
    if (status)
        return status;

    printf ("OK\n");
    return 0;
}

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);

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

    strm.flags = AEC_DATA_PREPROCESS;
    state.strm = &strm;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
99
    strm.bits_per_sample = 32;
100 101 102 103 104 105 106 107 108 109 110 111 112
    update_state(&state);

    status = check_rsi(&state);
    if (status)
        goto DESTRUCT;

DESTRUCT:
    free(state.ubuf);
    free(state.cbuf);
    free(state.obuf);

    return status;
}