Subversion Repositories programming

Rev

Blame | Last modification | View Log | RSS feed

/* Copyright (c) 2005, Ira W. Snyder (devel@irasnyder.com)
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#include "RingBuffer.h"


#define SIZ 16


/* Prints out a ring buffer as char's */
void print_buffer_read (RingBuffer *rb)
{
    int32_t i = 0;
    int8_t  buf[SIZ];

    rb->read(&buf[0], SIZ);

    for (i=0; i<SIZ; i++)
        printf("%c", buf[i]);

    printf("\n");
}

/* This is a disgustingly simple way to test the RingBuffer class.
 * It tests that regular reads and writes work.
 * It tests that wraparound reads and writes work.
 * It tests that multiple wraparound writes work.
 * It doesn't do much else.
 */
int main(int argc, char** argv[])
{
    RingBuffer *rb = new RingBuffer(SIZ); //the buffer itself

    int8_t c = 'c'; //test int8_t
    int8_t d = 'd'; //test int8_t
    int8_t q = '?'; //test int8_t

    int8_t temp4[4] = {'1','2','3','4'};                 //test "string"
    int8_t temp8[8] = {'z','y','x','w','v','u','s','r'}; //test "string"

    /* Set up a large "string" consisting of 990 '-' and then 10 'h'. */
    int32_t i;
    int8_t  hugeval[1000];

    for (i=0; i<990; i++)
        hugeval[i] = '-';

    for (; i<1000; i++)
        hugeval[i] = 'h';

    /*==========================================================================
     * Insert values for the first test.
     *========================================================================*/

    rb->write(temp4, sizeof(temp4)); //write '1234'
    rb->write(temp4, sizeof(temp4)); //write '1234'
    rb->write(temp4, sizeof(temp4)); //write '1234'
    rb->write(temp8, sizeof(temp8)); //write 'zyxwvusr' -- WRAPS 4 chars!!

    rb->write(&c, 1); //write 'c' -- WRAPS 1 char!!
    rb->write(&d, 1); //write 'd' -- WRAPS 1 char!!
    rb->write(&c, 1); //write 'c' -- WRAPS 1 char!!
    rb->write(&d, 1); //write 'd' -- WRAPS 1 char!!
    rb->write(&q, 1); //write '?' -- WRAPS 1 char!!

    /*==========================================================================
     * This just checks normal insertion and wrapping of the buffer.
     *========================================================================*/

    /* Print first test's output */
    printf ("output should be: 234zyxwvusrcdcd?\n");
    printf ("output actually : ");
    print_buffer_read (rb);

    printf ("\n");

    /*==========================================================================
     * This checks "multiple wraps" in the buffer.
     *========================================================================*/

    rb->write(hugeval, sizeof(hugeval));

    /* Print second test's output */
    printf ("output should be: ------hhhhhhhhhh\n");
    printf ("output actually : ");
    print_buffer_read (rb);

    delete rb; //free the ringbuffer object
    return 0;
}