Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/57149 )

Change subject: sim: Add some helpers for working with values in buffers.
......................................................................

sim: Add some helpers for working with values in buffers.

This change adds helpers to get and set values from byte buffers of
variable size, and to print them. It also provides a helper to print a
buffer of bytes.

Change-Id: I23210505f99488a901bed244ffc70ef4736470d0
---
M src/sim/SConscript
A src/sim/bufval.cc
A src/sim/bufval.hh
A src/sim/bufval.test.cc
4 files changed, 559 insertions(+), 0 deletions(-)



diff --git a/src/sim/SConscript b/src/sim/SConscript
index bf46ccb..72797b7 100644
--- a/src/sim/SConscript
+++ b/src/sim/SConscript
@@ -45,6 +45,7 @@

 Source('async.cc')
 Source('backtrace_%s.cc' % env['BACKTRACE_IMPL'], add_tags='gem5 trace')
+Source('bufval.cc')
 Source('core.cc')
 Source('cur_tick.cc', add_tags='gem5 trace')
 Source('tags.cc')
@@ -93,6 +94,7 @@
 env.TagImplies('gem5 events', ['gem5 serialize', 'gem5 trace'])
 env.TagImplies('gem5 serialize', 'gem5 trace')

+GTest('bufval.test', 'bufval.test.cc', 'bufval.cc')
 GTest('byteswap.test', 'byteswap.test.cc', '../base/types.cc')
 GTest('globals.test', 'globals.test.cc', 'globals.cc',
     with_tag('gem5 serialize'))
diff --git a/src/sim/bufval.cc b/src/sim/bufval.cc
new file mode 100644
index 0000000..2dbb32e
--- /dev/null
+++ b/src/sim/bufval.cc
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2022 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "sim/bufval.hh"
+
+#include <cassert>
+#include <sstream>
+
+#include "base/intmath.hh"
+#include "base/logging.hh"
+#include "sim/byteswap.hh"
+
+namespace gem5
+{
+
+std::pair<std::uint64_t, bool>
+getUintX(const void *buf, std::size_t bytes, ByteOrder endian)
+{
+    if (bytes > sizeof(std::uint64_t))
+        return {0, false};
+
+    switch (bytes) {
+      case sizeof(std::uint64_t):
+        return {gtoh(*(const std::uint64_t *)buf, endian), true};
+      case sizeof(std::uint32_t):
+        return {gtoh(*(const std::uint32_t *)buf, endian), true};
+      case sizeof(std::uint16_t):
+        return {gtoh(*(const std::uint16_t *)buf, endian), true};
+      case sizeof(std::uint8_t):
+        return {gtoh(*(const std::uint8_t *)buf, endian), true};
+      default:
+        return {0, false};
+    }
+}
+
+bool
+setUintX(std::uint64_t val, void *buf, std::size_t bytes, ByteOrder endian)
+{
+    if (bytes > sizeof(std::uint64_t))
+        return false;
+
+    switch (bytes) {
+      case sizeof(std::uint64_t):
+        *(std::uint64_t *)buf = htog<std::uint64_t>(val, endian);
+        return true;
+      case sizeof(std::uint32_t):
+        *(std::uint32_t *)buf = htog<std::uint32_t>(val, endian);
+        return true;
+      case sizeof(std::uint16_t):
+        *(std::uint16_t *)buf = htog<std::uint16_t>(val, endian);
+        return true;
+      case sizeof(std::uint8_t):
+        *(std::uint8_t *)buf = htog<std::uint8_t>(val, endian);
+        return true;
+      default:
+        return false;
+    }
+}
+
+std::pair<std::string, bool>
+printUintX(const void *buf, std::size_t bytes, ByteOrder endian)
+{
+    auto [val, success] = getUintX(buf, bytes, endian);
+    if (!success)
+        return {"", false};
+
+    std::ostringstream out;
+    out << "0x";
+    out.width(2 * bytes);
+    out.fill('0');
+    out.setf(std::ios::hex, std::ios::basefield);
+    out << val;
+
+    return {out.str(), true};
+}
+
+std::string
+printByteBuf(const void *buf, std::size_t bytes, ByteOrder endian,
+        std::size_t chunk_size)
+{
+    std::ostringstream out;
+    out << "[";
+
+    out.width(2);
+    out.fill('0');
+    out.setf(std::ios::hex, std::ios::basefield);
+
+    // Bytes that fall outside of a complete chunk. Will always be MSBs.
+    size_t extra = bytes % chunk_size;
+
+    const uint8_t *ptr = (const uint8_t *)buf;
+    int step = 1;
+
+    if (endian == ByteOrder::big) {
+        step = -1;
+        ptr = ptr + bytes - 1;
+
+        // If there's an incomplete chunk, start with that.
+        if (extra) {
+            bytes -= extra;
+            while (extra--) {
+                out.width(2);
+                out << (unsigned)*ptr;
+                ptr += step;
+            }
+            if (bytes)
+                out << " ";
+        }
+    }
+
+    // Print all the complete chunks.
+    while (bytes >= chunk_size) {
+        for (int i = 0; i < chunk_size; i++) {
+            out.width(2);
+            out << (unsigned)*ptr;
+            ptr += step;
+        }
+        bytes -= chunk_size;
+        if (bytes)
+            out << " ";
+    }
+
+    // Print any trailing leftovers. Only happens for little endian.
+    while (bytes--) {
+        out.width(2);
+        out << (unsigned)*ptr;
+        ptr += step;
+    }
+
+    out.width(0);
+    out << "]";
+
+    return out.str();
+}
+
+} // namespace gem5
diff --git a/src/sim/bufval.hh b/src/sim/bufval.hh
new file mode 100644
index 0000000..cf8ead4
--- /dev/null
+++ b/src/sim/bufval.hh
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2022 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __SIM_BUFVAL_HH__
+#define __SIM_BUFVAL_HH__
+
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <utility>
+
+#include "enums/ByteOrder.hh"
+
+namespace gem5
+{
+
+// Extract an integer with a given endianness from a variably sized buffer.
+// Returns the value extraced (if any) and a bool indicating success.
+std::pair<std::uint64_t, bool> getUintX(const void *buf, std::size_t bytes,
+        ByteOrder endian);
+
+// Set a variably sized buffer to an integer value with a given endianness.
+// Returns whether the assignment was successful.
+bool setUintX(std::uint64_t val, void *buf, std::size_t bytes,
+        ByteOrder endian);
+
+// Print an integer with a given endianness into a string from a variably
+// sized buffer. Returns the string (if any) and a bool indicating success.
+std::pair<std::string, bool> printUintX(const void *buf, std::size_t bytes,
+        ByteOrder endian);
+
+// Print a buffer as "chunk_size" sized groups of bytes. The endianness
+// determines if the bytes are output in memory order (little) or inverse of +// memory order (big). The default is in memory order so that this acts like
+// a hexdump type utility. The return value is a string holding the printed
+// bytes.
+std::string printByteBuf(const void *buf, std::size_t bytes,
+        ByteOrder endian=ByteOrder::little, std::size_t chunk_size=4);
+
+} // namespace gem5
+
+#endif // __SIM_BUFVAL_HH__
diff --git a/src/sim/bufval.test.cc b/src/sim/bufval.test.cc
new file mode 100644
index 0000000..8393669
--- /dev/null
+++ b/src/sim/bufval.test.cc
@@ -0,0 +1,318 @@
+/*
+ * Copyright 2022 Google Inc
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "sim/bufval.hh"
+
+using namespace gem5;
+
+using testing::ElementsAre;
+
+template <class First>
+bool
+pairFail(const std::pair<First, bool> &p)
+{
+    auto &[first, success] = p;
+    return !success;
+}
+
+bool
+pairFailU64(const std::pair<std::uint64_t, bool> &p)
+{
+    return pairFail(p);
+}
+bool
+pairFailStr(const std::pair<std::string, bool> &p)
+{
+    return pairFail(p);
+}
+
+template <class First>
+bool
+pairVal(const std::pair<First, bool> &p, const First &expected)
+{
+    auto &[first, success] = p;
+    return success && (first == expected);
+}
+
+bool
+pairValU64(const std::pair<std::uint64_t, bool> &p, std::uint64_t expected)
+{
+    return pairVal(p, expected);
+}
+
+bool
+pairValStr(const std::pair<std::string, bool> &p, const std::string &expected)
+{
+    return pairVal(p, expected);
+}
+
+TEST(GetUintX, BadSize)
+{
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 0, ByteOrder::little));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 0, ByteOrder::big));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 3, ByteOrder::little));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 3, ByteOrder::big));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 5, ByteOrder::little));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 5, ByteOrder::big));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 6, ByteOrder::little));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 6, ByteOrder::big));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 7, ByteOrder::little));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 7, ByteOrder::big));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 9, ByteOrder::little));
+    EXPECT_PRED1(pairFailU64, getUintX(nullptr, 9, ByteOrder::big));
+}
+
+TEST(GetUintX, LittleEndian)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 1, ByteOrder::little),
+            0x01);
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 2, ByteOrder::little),
+            0x0201);
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 4, ByteOrder::little),
+            0x04030201);
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 8, ByteOrder::little),
+            0x0807060504030201);
+}
+
+TEST(GetUintX, BigEndian)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 1, ByteOrder::big),
+            0x01);
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 2, ByteOrder::big),
+            0x0102);
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 4, ByteOrder::big),
+            0x01020304);
+    EXPECT_PRED2(pairValU64, getUintX(buf.data(), 8, ByteOrder::big),
+            0x0102030405060708);
+}
+
+TEST(SetUintX, BadSize)
+{
+    EXPECT_FALSE(setUintX(0, nullptr, 0, ByteOrder::little));
+    EXPECT_FALSE(setUintX(0, nullptr, 0, ByteOrder::big));
+    EXPECT_FALSE(setUintX(0, nullptr, 3, ByteOrder::little));
+    EXPECT_FALSE(setUintX(0, nullptr, 3, ByteOrder::big));
+    EXPECT_FALSE(setUintX(0, nullptr, 5, ByteOrder::little));
+    EXPECT_FALSE(setUintX(0, nullptr, 5, ByteOrder::big));
+    EXPECT_FALSE(setUintX(0, nullptr, 6, ByteOrder::little));
+    EXPECT_FALSE(setUintX(0, nullptr, 6, ByteOrder::big));
+    EXPECT_FALSE(setUintX(0, nullptr, 7, ByteOrder::little));
+    EXPECT_FALSE(setUintX(0, nullptr, 7, ByteOrder::big));
+    EXPECT_FALSE(setUintX(0, nullptr, 9, ByteOrder::little));
+    EXPECT_FALSE(setUintX(0, nullptr, 9, ByteOrder::big));
+}
+
+TEST(SetUintX, LittleEndian)
+{
+    const std::array<uint8_t, 9> orig_buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
+    auto buf = orig_buf;
+
+    EXPECT_TRUE(setUintX(0xf1, buf.data(), 1, ByteOrder::little));
+    EXPECT_THAT(buf,
+            ElementsAre(0xf1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
+    EXPECT_TRUE(setUintX(0xf2f1, buf.data(), 2, ByteOrder::little));
+    EXPECT_THAT(buf,
+            ElementsAre(0xf1, 0xf2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
+    EXPECT_TRUE(setUintX(0xf4f3f2f1, buf.data(), 4, ByteOrder::little));
+    EXPECT_THAT(buf,
+            ElementsAre(0xf1, 0xf2, 0xf3, 0xf4, 0x5, 0x6, 0x7, 0x8, 0x9));
+    EXPECT_TRUE(setUintX(0xf8f7f6f5f4f3f2f1, buf.data(), 8,
+                ByteOrder::little));
+    EXPECT_THAT(buf,
+ ElementsAre(0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0x9));
+}
+
+TEST(SetUintX, BigEndian)
+{
+    const std::array<uint8_t, 9> orig_buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
+    auto buf = orig_buf;
+
+    EXPECT_TRUE(setUintX(0xf1, buf.data(), 1, ByteOrder::big));
+    EXPECT_THAT(buf,
+            ElementsAre(0xf1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
+    EXPECT_TRUE(setUintX(0xf1f2, buf.data(), 2, ByteOrder::big));
+    EXPECT_THAT(buf,
+            ElementsAre(0xf1, 0xf2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9));
+    EXPECT_TRUE(setUintX(0xf1f2f3f4, buf.data(), 4, ByteOrder::big));
+    EXPECT_THAT(buf,
+            ElementsAre(0xf1, 0xf2, 0xf3, 0xf4, 0x5, 0x6, 0x7, 0x8, 0x9));
+ EXPECT_TRUE(setUintX(0xf1f2f3f4f5f6f7f8, buf.data(), 8, ByteOrder::big));
+    EXPECT_THAT(buf,
+ ElementsAre(0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0x9));
+}
+
+TEST(PrintUintX, BadSize)
+{
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 0, ByteOrder::little));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 0, ByteOrder::big));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 3, ByteOrder::little));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 3, ByteOrder::big));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 5, ByteOrder::little));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 5, ByteOrder::big));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 6, ByteOrder::little));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 6, ByteOrder::big));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 7, ByteOrder::little));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 7, ByteOrder::big));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 9, ByteOrder::little));
+    EXPECT_PRED1(pairFailStr, printUintX(nullptr, 9, ByteOrder::big));
+}
+
+TEST(PrintUintX, LittleEndian)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 1, ByteOrder::little),
+            "0x01");
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 2, ByteOrder::little),
+            "0x0201");
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 4, ByteOrder::little),
+            "0x04030201");
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 8, ByteOrder::little),
+            "0x0807060504030201");
+}
+
+TEST(PrintUintX, BigEndian)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9};
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 1, ByteOrder::big),
+            "0x01");
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 2, ByteOrder::big),
+            "0x0102");
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 4, ByteOrder::big),
+            "0x01020304");
+    EXPECT_PRED2(pairValStr, printUintX(buf.data(), 8, ByteOrder::big),
+            "0x0102030405060708");
+}
+
+TEST(PrintByteBuf, LittleEndian)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xa};
+
+    EXPECT_EQ(printByteBuf(buf.data(), 0, ByteOrder::little, 3), "[]");
+    EXPECT_EQ(printByteBuf(buf.data(), 1, ByteOrder::little, 3), "[01]");
+    EXPECT_EQ(printByteBuf(buf.data(), 2, ByteOrder::little, 3), "[0102]");
+    EXPECT_EQ(printByteBuf(buf.data(), 3, ByteOrder::little, 3),
+            "[010203]");
+    EXPECT_EQ(printByteBuf(buf.data(), 4, ByteOrder::little, 3),
+            "[010203 04]");
+    EXPECT_EQ(printByteBuf(buf.data(), 5, ByteOrder::little, 3),
+            "[010203 0405]");
+    EXPECT_EQ(printByteBuf(buf.data(), 6, ByteOrder::little, 3),
+            "[010203 040506]");
+    EXPECT_EQ(printByteBuf(buf.data(), 7, ByteOrder::little, 3),
+            "[010203 040506 07]");
+    EXPECT_EQ(printByteBuf(buf.data(), 8, ByteOrder::little, 3),
+            "[010203 040506 0708]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 3),
+            "[010203 040506 07080a]");
+}
+
+TEST(PrintByteBuf, BigEndian)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xa};
+
+    EXPECT_EQ(printByteBuf(buf.data(), 0, ByteOrder::big, 3), "[]");
+    EXPECT_EQ(printByteBuf(buf.data(), 1, ByteOrder::big, 3), "[01]");
+    EXPECT_EQ(printByteBuf(buf.data(), 2, ByteOrder::big, 3), "[0201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 3, ByteOrder::big, 3), "[030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 4, ByteOrder::big, 3),
+            "[04 030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 5, ByteOrder::big, 3),
+            "[0504 030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 6, ByteOrder::big, 3),
+            "[060504 030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 7, ByteOrder::big, 3),
+            "[07 060504 030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 8, ByteOrder::big, 3),
+            "[0807 060504 030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 3),
+            "[0a0807 060504 030201]");
+}
+
+TEST(PrintByteBuf, ChunkSize)
+{
+    const std::array<uint8_t, 9> buf =
+        {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xa};
+
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 1),
+            "[01 02 03 04 05 06 07 08 0a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 2),
+            "[0102 0304 0506 0708 0a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 3),
+            "[010203 040506 07080a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 4),
+            "[01020304 05060708 0a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 5),
+            "[0102030405 0607080a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 6),
+            "[010203040506 07080a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 7),
+            "[01020304050607 080a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 8),
+            "[0102030405060708 0a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 9),
+            "[01020304050607080a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 10),
+            "[01020304050607080a]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::little, 100),
+            "[01020304050607080a]");
+
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 1),
+            "[0a 08 07 06 05 04 03 02 01]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 2),
+            "[0a 0807 0605 0403 0201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 3),
+            "[0a0807 060504 030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 4),
+            "[0a 08070605 04030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 5),
+            "[0a080706 0504030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 6),
+            "[0a0807 060504030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 7),
+            "[0a08 07060504030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 8),
+            "[0a 0807060504030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 9),
+            "[0a0807060504030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 10),
+            "[0a0807060504030201]");
+    EXPECT_EQ(printByteBuf(buf.data(), 9, ByteOrder::big, 100),
+            "[0a0807060504030201]");
+}

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/57149
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I23210505f99488a901bed244ffc70ef4736470d0
Gerrit-Change-Number: 57149
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to