115 |
ira |
1 |
/* Copyright (c) 2005, Ira W. Snyder (devel@irasnyder.com)
|
|
|
2 |
* All rights reserved.
|
|
|
3 |
*
|
|
|
4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
5 |
* of this software and associated documentation files (the "Software"), to
|
|
|
6 |
* deal in the Software without restriction, including without limitation the
|
|
|
7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
8 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
9 |
* furnished to do so, subject to the following conditions:
|
|
|
10 |
*
|
|
|
11 |
* The above copyright notice and this permission notice shall be included in
|
|
|
12 |
* all copies or substantial portions of the Software.
|
|
|
13 |
*
|
|
|
14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
20 |
* IN THE SOFTWARE.
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
#include <stdlib.h>
|
|
|
25 |
#include <stdio.h>
|
|
|
26 |
#include <stdint.h>
|
|
|
27 |
|
|
|
28 |
#include "RingBuffer.h"
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
#define SIZ 16
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
/* Prints out a ring buffer as char's */
|
|
|
35 |
void print_buffer_read (RingBuffer *rb)
|
|
|
36 |
{
|
|
|
37 |
int32_t i = 0;
|
|
|
38 |
int8_t buf[SIZ];
|
|
|
39 |
|
|
|
40 |
rb->read(&buf[0], SIZ);
|
|
|
41 |
|
|
|
42 |
for (i=0; i<SIZ; i++)
|
|
|
43 |
printf("%c", buf[i]);
|
|
|
44 |
|
|
|
45 |
printf("\n");
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/* This is a disgustingly simple way to test the RingBuffer class.
|
|
|
49 |
* It tests that regular reads and writes work.
|
|
|
50 |
* It tests that wraparound reads and writes work.
|
|
|
51 |
* It tests that multiple wraparound writes work.
|
|
|
52 |
* It doesn't do much else.
|
|
|
53 |
*/
|
|
|
54 |
int main(int argc, char** argv[])
|
|
|
55 |
{
|
|
|
56 |
RingBuffer *rb = new RingBuffer(SIZ); //the buffer itself
|
|
|
57 |
|
|
|
58 |
int8_t c = 'c'; //test int8_t
|
|
|
59 |
int8_t d = 'd'; //test int8_t
|
|
|
60 |
int8_t q = '?'; //test int8_t
|
|
|
61 |
|
|
|
62 |
int8_t temp4[4] = {'1','2','3','4'}; //test "string"
|
|
|
63 |
int8_t temp8[8] = {'z','y','x','w','v','u','s','r'}; //test "string"
|
|
|
64 |
|
|
|
65 |
/* Set up a large "string" consisting of 990 '-' and then 10 'h'. */
|
|
|
66 |
int32_t i;
|
|
|
67 |
int8_t hugeval[1000];
|
|
|
68 |
|
|
|
69 |
for (i=0; i<990; i++)
|
|
|
70 |
hugeval[i] = '-';
|
|
|
71 |
|
|
|
72 |
for (; i<1000; i++)
|
|
|
73 |
hugeval[i] = 'h';
|
|
|
74 |
|
|
|
75 |
/*==========================================================================
|
|
|
76 |
* Insert values for the first test.
|
|
|
77 |
*========================================================================*/
|
|
|
78 |
|
|
|
79 |
rb->write(temp4, sizeof(temp4)); //write '1234'
|
|
|
80 |
rb->write(temp4, sizeof(temp4)); //write '1234'
|
|
|
81 |
rb->write(temp4, sizeof(temp4)); //write '1234'
|
|
|
82 |
rb->write(temp8, sizeof(temp8)); //write 'zyxwvusr' -- WRAPS 4 chars!!
|
|
|
83 |
|
|
|
84 |
rb->write(&c, 1); //write 'c' -- WRAPS 1 char!!
|
|
|
85 |
rb->write(&d, 1); //write 'd' -- WRAPS 1 char!!
|
|
|
86 |
rb->write(&c, 1); //write 'c' -- WRAPS 1 char!!
|
|
|
87 |
rb->write(&d, 1); //write 'd' -- WRAPS 1 char!!
|
|
|
88 |
rb->write(&q, 1); //write '?' -- WRAPS 1 char!!
|
|
|
89 |
|
|
|
90 |
/*==========================================================================
|
|
|
91 |
* This just checks normal insertion and wrapping of the buffer.
|
|
|
92 |
*========================================================================*/
|
|
|
93 |
|
|
|
94 |
/* Print first test's output */
|
|
|
95 |
printf ("output should be: 234zyxwvusrcdcd?\n");
|
|
|
96 |
printf ("output actually : ");
|
|
|
97 |
print_buffer_read (rb);
|
|
|
98 |
|
|
|
99 |
printf ("\n");
|
|
|
100 |
|
|
|
101 |
/*==========================================================================
|
|
|
102 |
* This checks "multiple wraps" in the buffer.
|
|
|
103 |
*========================================================================*/
|
|
|
104 |
|
|
|
105 |
rb->write(hugeval, sizeof(hugeval));
|
|
|
106 |
|
|
|
107 |
/* Print second test's output */
|
|
|
108 |
printf ("output should be: ------hhhhhhhhhh\n");
|
|
|
109 |
printf ("output actually : ");
|
|
|
110 |
print_buffer_read (rb);
|
|
|
111 |
|
|
|
112 |
delete rb; //free the ringbuffer object
|
|
|
113 |
return 0;
|
|
|
114 |
}
|
|
|
115 |
|