gem5 v24.0.0.0
Loading...
Searching...
No Matches
bufval.test.cc
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google Inc
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <gmock/gmock.h>
29#include <gtest/gtest.h>
30
31#include <array>
32
33#include "sim/bufval.hh"
34
35using namespace gem5;
36
37using testing::ElementsAre;
38
39template <class First>
40bool
42{
43 return !p.second;
44}
45
46bool
51bool
56
57template <class First>
58bool
60{
61 auto &[first, success] = p;
62 return success && (first == expected);
63}
64
65bool
67{
68 return pairVal(p, expected);
69}
70
71bool
73{
74 return pairVal(p, expected);
75}
76
77TEST(GetUintX, BadSize)
78{
79 uint8_t dummy{};
80 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 0, ByteOrder::little));
81 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 0, ByteOrder::big));
82 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 3, ByteOrder::little));
83 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 3, ByteOrder::big));
84 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 5, ByteOrder::little));
85 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 5, ByteOrder::big));
86 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 6, ByteOrder::little));
87 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 6, ByteOrder::big));
88 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 7, ByteOrder::little));
89 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 7, ByteOrder::big));
90 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 9, ByteOrder::little));
91 EXPECT_PRED1(pairFailU64, getUintX(&dummy, 9, ByteOrder::big));
92}
93
94TEST(GetUintX, LittleEndian)
95{
96 const std::array<uint8_t, 9> buf =
97 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
98 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 1, ByteOrder::little),
99 0x01);
100 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 2, ByteOrder::little),
101 0x0201);
102 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 4, ByteOrder::little),
103 0x04030201);
104 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 8, ByteOrder::little),
105 0x0807060504030201);
106}
107
108TEST(GetUintX, BigEndian)
109{
110 const std::array<uint8_t, 9> buf =
111 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
112 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 1, ByteOrder::big),
113 0x01);
114 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 2, ByteOrder::big),
115 0x0102);
116 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 4, ByteOrder::big),
117 0x01020304);
118 EXPECT_PRED2(pairValU64, getUintX(buf.data(), 8, ByteOrder::big),
119 0x0102030405060708);
120}
121
122TEST(SetUintX, BadSize)
123{
124 uint8_t dummy{};
125 EXPECT_FALSE(setUintX(0, &dummy, 0, ByteOrder::little));
126 EXPECT_FALSE(setUintX(0, &dummy, 0, ByteOrder::big));
127 EXPECT_FALSE(setUintX(0, &dummy, 3, ByteOrder::little));
128 EXPECT_FALSE(setUintX(0, &dummy, 3, ByteOrder::big));
129 EXPECT_FALSE(setUintX(0, &dummy, 5, ByteOrder::little));
130 EXPECT_FALSE(setUintX(0, &dummy, 5, ByteOrder::big));
131 EXPECT_FALSE(setUintX(0, &dummy, 6, ByteOrder::little));
132 EXPECT_FALSE(setUintX(0, &dummy, 6, ByteOrder::big));
133 EXPECT_FALSE(setUintX(0, &dummy, 7, ByteOrder::little));
134 EXPECT_FALSE(setUintX(0, &dummy, 7, ByteOrder::big));
135 EXPECT_FALSE(setUintX(0, &dummy, 9, ByteOrder::little));
136 EXPECT_FALSE(setUintX(0, &dummy, 9, ByteOrder::big));
137}
138
139TEST(SetUintX, LittleEndian)
140{
141 const std::array<uint8_t, 9> orig_buf =
142 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
143 auto buf = orig_buf;
144
145 EXPECT_TRUE(setUintX(0xf1, buf.data(), 1, ByteOrder::little));
146 EXPECT_THAT(buf,
147 ElementsAre(0xf1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
148 EXPECT_TRUE(setUintX(0xe2e1, buf.data(), 2, ByteOrder::little));
149 EXPECT_THAT(buf,
150 ElementsAre(0xe1, 0xe2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
151 EXPECT_TRUE(setUintX(0xd4d3d2d1, buf.data(), 4, ByteOrder::little));
152 EXPECT_THAT(buf,
153 ElementsAre(0xd1, 0xd2, 0xd3, 0xd4, 0x5, 0x6, 0x7, 0x8, 0x9));
154 EXPECT_TRUE(setUintX(0xc8c7c6c5c4c3c2c1, buf.data(), 8,
155 ByteOrder::little));
156 EXPECT_THAT(buf,
157 ElementsAre(0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0x9));
158}
159
160TEST(SetUintX, BigEndian)
161{
162 const std::array<uint8_t, 9> orig_buf =
163 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
164 auto buf = orig_buf;
165
166 EXPECT_TRUE(setUintX(0xf1, buf.data(), 1, ByteOrder::big));
167 EXPECT_THAT(buf,
168 ElementsAre(0xf1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
169 EXPECT_TRUE(setUintX(0xe1e2, buf.data(), 2, ByteOrder::big));
170 EXPECT_THAT(buf,
171 ElementsAre(0xe1, 0xe2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
172 EXPECT_TRUE(setUintX(0xd1d2d3d4, buf.data(), 4, ByteOrder::big));
173 EXPECT_THAT(buf,
174 ElementsAre(0xd1, 0xd2, 0xd3, 0xd4, 0x5, 0x6, 0x7, 0x8, 0x9));
175 EXPECT_TRUE(setUintX(0xc1c2c3c4c5c6c7c8, buf.data(), 8, ByteOrder::big));
176 EXPECT_THAT(buf,
177 ElementsAre(0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0x9));
178}
179
180TEST(PrintUintX, BadSize)
181{
182 uint8_t dummy{};
183 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 0, ByteOrder::little));
184 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 0, ByteOrder::big));
185 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 3, ByteOrder::little));
186 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 3, ByteOrder::big));
187 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 5, ByteOrder::little));
188 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 5, ByteOrder::big));
189 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 6, ByteOrder::little));
190 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 6, ByteOrder::big));
191 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 7, ByteOrder::little));
192 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 7, ByteOrder::big));
193 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 9, ByteOrder::little));
194 EXPECT_PRED1(pairFailStr, printUintX(&dummy, 9, ByteOrder::big));
195}
196
197TEST(PrintUintX, LittleEndian)
198{
199 const std::array<uint8_t, 9> buf =
200 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
201 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 1, ByteOrder::little),
202 "0x01");
203 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 2, ByteOrder::little),
204 "0x0201");
205 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 4, ByteOrder::little),
206 "0x04030201");
207 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 8, ByteOrder::little),
208 "0x0807060504030201");
209}
210
211TEST(PrintUintX, BigEndian)
212{
213 const std::array<uint8_t, 9> buf =
214 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
215 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 1, ByteOrder::big),
216 "0x01");
217 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 2, ByteOrder::big),
218 "0x0102");
219 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 4, ByteOrder::big),
220 "0x01020304");
221 EXPECT_PRED2(pairValStr, printUintX(buf.data(), 8, ByteOrder::big),
222 "0x0102030405060708");
223}
224
225TEST(PrintByteBuf, LittleEndian)
226{
227 const std::array<uint8_t, 9> buf =
228 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xa};
229
230 EXPECT_EQ(printByteBuf(buf.data(), 0, ByteOrder::little, 3), "[]");
231 EXPECT_EQ(printByteBuf(buf.data(), 1, ByteOrder::little, 3), "[01]");
232 EXPECT_EQ(printByteBuf(buf.data(), 2, ByteOrder::little, 3), "[0102]");
233 EXPECT_EQ(printByteBuf(buf.data(), 3, ByteOrder::little, 3),
234 "[010203]");
235 EXPECT_EQ(printByteBuf(buf.data(), 4, ByteOrder::little, 3),
236 "[010203 04]");
237 EXPECT_EQ(printByteBuf(buf.data(), 5, ByteOrder::little, 3),
238 "[010203 0405]");
239 EXPECT_EQ(printByteBuf(buf.data(), 6, ByteOrder::little, 3),
240 "[010203 040506]");
241 EXPECT_EQ(printByteBuf(buf.data(), 7, ByteOrder::little, 3),
242 "[010203 040506 07]");
243 EXPECT_EQ(printByteBuf(buf.data(), 8, ByteOrder::little, 3),
244 "[010203 040506 0708]");
245 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 3),
246 "[010203 040506 07080a]");
247}
248
249TEST(PrintByteBuf, BigEndian)
250{
251 const std::array<uint8_t, 9> buf =
252 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xa};
253
254 EXPECT_EQ(printByteBuf(buf.data(), 0, ByteOrder::big, 3), "[]");
255 EXPECT_EQ(printByteBuf(buf.data(), 1, ByteOrder::big, 3), "[01]");
256 EXPECT_EQ(printByteBuf(buf.data(), 2, ByteOrder::big, 3), "[0201]");
257 EXPECT_EQ(printByteBuf(buf.data(), 3, ByteOrder::big, 3), "[030201]");
258 EXPECT_EQ(printByteBuf(buf.data(), 4, ByteOrder::big, 3),
259 "[04 030201]");
260 EXPECT_EQ(printByteBuf(buf.data(), 5, ByteOrder::big, 3),
261 "[0504 030201]");
262 EXPECT_EQ(printByteBuf(buf.data(), 6, ByteOrder::big, 3),
263 "[060504 030201]");
264 EXPECT_EQ(printByteBuf(buf.data(), 7, ByteOrder::big, 3),
265 "[07 060504 030201]");
266 EXPECT_EQ(printByteBuf(buf.data(), 8, ByteOrder::big, 3),
267 "[0807 060504 030201]");
268 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 3),
269 "[0a0807 060504 030201]");
270}
271
272TEST(PrintByteBuf, ChunkSize)
273{
274 const std::array<uint8_t, 9> buf =
275 {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xa};
276
277 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 1),
278 "[01 02 03 04 05 06 07 08 0a]");
279 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 2),
280 "[0102 0304 0506 0708 0a]");
281 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 3),
282 "[010203 040506 07080a]");
283 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 4),
284 "[01020304 05060708 0a]");
285 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 5),
286 "[0102030405 0607080a]");
287 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 6),
288 "[010203040506 07080a]");
289 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 7),
290 "[01020304050607 080a]");
291 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 8),
292 "[0102030405060708 0a]");
293 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 9),
294 "[01020304050607080a]");
295 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 10),
296 "[01020304050607080a]");
297 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 100),
298 "[01020304050607080a]");
299
300 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 1),
301 "[0a 08 07 06 05 04 03 02 01]");
302 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 2),
303 "[0a 0807 0605 0403 0201]");
304 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 3),
305 "[0a0807 060504 030201]");
306 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 4),
307 "[0a 08070605 04030201]");
308 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 5),
309 "[0a080706 0504030201]");
310 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 6),
311 "[0a0807 060504030201]");
312 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 7),
313 "[0a08 07060504030201]");
314 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 8),
315 "[0a 0807060504030201]");
316 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 9),
317 "[0a0807060504030201]");
318 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 10),
319 "[0a0807060504030201]");
320 EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 100),
321 "[0a0807060504030201]");
322}
bool pairVal(const std::pair< First, bool > &p, const First &expected)
bool pairFailStr(const std::pair< std::string, bool > &p)
bool pairFail(const std::pair< First, bool > &p)
TEST(GetUintX, BadSize)
bool pairFailU64(const std::pair< std::uint64_t, bool > &p)
bool pairValU64(const std::pair< std::uint64_t, bool > &p, std::uint64_t expected)
bool pairValStr(const std::pair< std::string, bool > &p, const std::string &expected)
STL pair class.
Definition stl.hh:58
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::pair< std::string, bool > printUintX(const void *buf, std::size_t bytes, ByteOrder endian)
Definition bufval.cc:82
std::pair< std::uint64_t, bool > getUintX(const void *buf, std::size_t bytes, ByteOrder endian)
Definition bufval.cc:41
bool setUintX(std::uint64_t val, void *buf, std::size_t bytes, ByteOrder endian)
Definition bufval.cc:59
std::string printByteBuf(const void *buf, std::size_t bytes, ByteOrder endian, std::size_t chunk_size)
Definition bufval.cc:99

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