gem5 v24.0.0.0
Loading...
Searching...
No Matches
circlebuf.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <gmock/gmock.h>
39#include <gtest/gtest.h>
40
41#include <vector>
42
43#include "base/circlebuf.hh"
44
45using testing::ElementsAreArray;
46using namespace gem5;
47
48const char data[] = {
49 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
50 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
51};
52
53// A better way to implement this would be with std::span, but that is only
54// available starting in c++20.
55template <typename T>
57subArr(T *arr, int size, int offset=0)
58{
59 return std::vector<T>(arr + offset, arr + offset + size);
60}
61
62// Basic non-overflow functionality
63TEST(CircleBufTest, BasicReadWriteNoOverflow)
64{
65 CircleBuf<char> buf(8);
66 char foo[16];
67
68 // Write empty buffer, no overflow
69 buf.write(data, 8);
70 EXPECT_EQ(buf.size(), 8);
71 buf.peek(foo, 8);
72 EXPECT_THAT(subArr(foo, 8), ElementsAreArray(data, 8));
73
74 // Read 2
75 buf.read(foo, 2);
76 EXPECT_THAT(subArr(foo, 2), ElementsAreArray(data, 2));
77 EXPECT_EQ(buf.size(), 6);
78 buf.read(foo, 6);
79 EXPECT_THAT(subArr(foo, 6), ElementsAreArray(data + 2, 6));
80 EXPECT_EQ(buf.size(), 0);
81}
82
83// Basic single write overflow functionality
84TEST(CircleBufTest, SingleWriteOverflow)
85{
86 CircleBuf<char> buf(8);
87 char foo[16];
88
89 buf.write(data, 16);
90 EXPECT_EQ(buf.size(), 8);
91 buf.peek(foo, 8);
92 EXPECT_THAT(subArr(foo, 8), ElementsAreArray(data + 8, 8));
93}
94
95
96// Multi-write overflow functionality
97TEST(CircleBufTest, MultiWriteOverflow)
98{
99 CircleBuf<char> buf(8);
100 char foo[16];
101
102 // Write, no overflow, write overflow
103 buf.write(data, 6);
104 buf.write(data + 8, 6);
105 EXPECT_EQ(buf.size(), 8);
106 buf.peek(foo, 8);
107 EXPECT_THAT(subArr(foo, 2), ElementsAreArray(data + 4, 2));
108 EXPECT_THAT(subArr(foo, 6, 2), ElementsAreArray(data + 8, 6));
109}
110
111// Pointer wrap around
112TEST(CircleBufTest, PointerWrapAround)
113{
114 CircleBuf<char> buf(8);
115 char foo[16];
116
117 // _start == 0, _stop = 8
118 buf.write(data, 8);
119 // _start == 4, _stop = 8
120 buf.read(foo, 4);
121 // _start == 4, _stop = 12
122 buf.write(data + 8, 4);
123 EXPECT_EQ(buf.size(), 8);
124 // _start == 10, _stop = 12
125 // Normalized: _start == 2, _stop = 4
126 buf.read(foo + 4, 6);
127 EXPECT_EQ(buf.size(), 2);
128 EXPECT_THAT(subArr(foo, 10), ElementsAreArray(data, 10));
129 // Normalized: _start == 4, _stop = 4
130 buf.read(foo + 10, 2);
131 EXPECT_EQ(buf.size(), 0);
132 EXPECT_THAT(subArr(foo, 12), ElementsAreArray(data, 12));
133}
134
135// Consume after produce empties queue
136TEST(CircleBufTest, ProduceConsumeEmpty)
137{
138 CircleBuf<char> buf(8);
139 char foo[1] = {'a'};
140
141 // buf is empty to begin with.
142 EXPECT_TRUE(buf.empty());
143 // Produce one element.
144 buf.write(foo, 1);
145 EXPECT_FALSE(buf.empty());
146
147 // Read it back out.
148 buf.read(foo, 1);
149
150 // Now the buffer should be empty again.
151 EXPECT_TRUE(buf.empty());
152}
TEST(CircleBufTest, BasicReadWriteNoOverflow)
const char data[]
std::vector< T > subArr(T *arr, int size, int offset=0)
Circular buffer backed by a vector.
Definition circlebuf.hh:59
size_t size() const
Definition circlebuf.hh:72
void read(OutputIterator out, size_t len)
Copy buffer contents and advance the read pointer.
Definition circlebuf.hh:140
void write(InputIterator in, size_t len)
Add elements to the end of the ring buffers and advance.
Definition circlebuf.hh:158
bool empty() const
Definition circlebuf.hh:71
void peek(OutputIterator out, size_t len) const
Copy buffer contents without advancing the read pointer.
Definition circlebuf.hh:93
STL vector class.
Definition stl.hh:37
Bitfield< 23, 0 > offset
Definition types.hh:144
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0