1
// Copyright 2008, Google Inc.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are met:
6
// 1. Redistributions of source code must retain the above copyright notice,
7
// this list of conditions and the following disclaimer.
8
// 2. Redistributions in binary form must reproduce the above copyright notice,
9
// this list of conditions and the following disclaimer in the documentation
10
// and/or other materials provided with the distribution.
11
// 3. Neither the name of Google Inc. nor the names of its contributors may be
12
// used to endorse or promote products derived from this software without
13
// specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#include "gears/base/common/circular_buffer.h"
27
#include "gears/base/common/common.h"
31
bool TestCircularBuffer(std::string16 *error) {
33
#define TEST_ASSERT(b) \
36
LOG(("TestCircularBuffer - failed (%d)\n", __LINE__)); \
38
*error += STRING16(L"TestCircularBuffer - failed. "); \
43
CircularBuffer circular;
44
const size_t kCapacity = 4;
45
const size_t kBufSize = kCapacity + 1;
46
char buf_in[kCapacity];
47
char buf_out[kCapacity];
48
char buf_working[kBufSize];
50
for (size_t i = 0; i < kCapacity; ++i) {
51
buf_in[i] = static_cast<char>(i);
54
circular.set_buffer(buf_working, kBufSize);
55
TEST_ASSERT(circular.is_empty());
56
TEST_ASSERT(!circular.is_full());
57
TEST_ASSERT(circular.data_available() == 0);
58
TEST_ASSERT(circular.space_available() == kCapacity);
59
TEST_ASSERT(circular.head() == 0);
60
TEST_ASSERT(circular.tail() == 0);
61
TEST_ASSERT(circular.peek(buf_out, kCapacity) == 0);
62
TEST_ASSERT(circular.read(buf_out, kCapacity) == 0);
63
TEST_ASSERT(circular.advance_head(1) == 0);
64
TEST_ASSERT(circular.head() == 0);
65
TEST_ASSERT(circular.tail() == 0);
67
TEST_ASSERT(circular.write(buf_in, kCapacity * 2) == kCapacity);
68
TEST_ASSERT(circular.data_available() == kCapacity);
69
TEST_ASSERT(circular.advance_tail(1) == 0);
70
TEST_ASSERT(circular.read(buf_out, kCapacity * 2) == kCapacity);
71
TEST_ASSERT(circular.space_available() == kCapacity);
72
TEST_ASSERT(memcmp(buf_in, buf_out, kCapacity) == 0);
74
for (size_t position = 0; position < kBufSize; ++position) {
75
// Reset to an empty buffer with head and tail at this position
76
memset(buf_working, 0, sizeof(buf_working));
77
memset(buf_out, 0xff, sizeof(buf_out));
78
circular.set_head(position);
79
circular.set_tail(position);
81
// Check initial conditions
82
TEST_ASSERT(circular.is_empty());
83
TEST_ASSERT(!circular.is_full());
84
TEST_ASSERT(circular.data_available() == 0);
85
TEST_ASSERT(circular.space_available() == kCapacity);
88
TEST_ASSERT(circular.write(buf_in, kCapacity) == kCapacity);
89
TEST_ASSERT(!circular.is_empty());
90
TEST_ASSERT(circular.is_full());
91
TEST_ASSERT(circular.data_available() == kCapacity);
92
TEST_ASSERT(circular.space_available() == 0);
95
TEST_ASSERT(circular.read(buf_out, kCapacity) == kCapacity);
96
TEST_ASSERT(circular.is_empty());
97
TEST_ASSERT(!circular.is_full());
98
TEST_ASSERT(circular.data_available() == 0);
99
TEST_ASSERT(circular.space_available() == kCapacity);
100
TEST_ASSERT(memcmp(buf_in, buf_out, kCapacity) == 0);
102
// Reset to empty at this position again
103
memset(buf_working, 0, sizeof(buf_working));
104
memset(buf_out, 0xff, sizeof(buf_out));
105
circular.set_head(position);
106
circular.set_tail(position);
108
// Write one byte at a time
109
TEST_ASSERT(circular.write(&buf_in[0], 1) == 1);
110
for (size_t i = 1; i < kCapacity; ++i) {
111
TEST_ASSERT(!circular.is_empty());
112
TEST_ASSERT(circular.data_available() == i);
113
TEST_ASSERT(circular.space_available() == kCapacity - i);
114
TEST_ASSERT(circular.write(&buf_in[i], 1) == 1);
116
TEST_ASSERT(!circular.is_empty());
117
TEST_ASSERT(circular.is_full());
118
TEST_ASSERT(circular.data_available() == kCapacity);
119
TEST_ASSERT(circular.space_available() == 0);
121
// Read one byte at a time
122
TEST_ASSERT(circular.read(&buf_out[0], 1) == 1);
123
for (size_t i = 1; i < kCapacity; ++i) {
124
TEST_ASSERT(!circular.is_full());
125
TEST_ASSERT(circular.data_available() == kCapacity - i);
126
TEST_ASSERT(circular.space_available() == i);
127
TEST_ASSERT(circular.read(&buf_out[i], 1) == 1);
129
TEST_ASSERT(circular.is_empty());
130
TEST_ASSERT(!circular.is_full());
131
TEST_ASSERT(circular.data_available() == 0);
132
TEST_ASSERT(circular.space_available() == kCapacity);
133
TEST_ASSERT(memcmp(buf_in, buf_out, kCapacity) == 0);