diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-13 00:01:06 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 35a996490200126e72775398fa3d6daa0ec4f435 (patch) | |
tree | 065bc639ff3a2bd4e06e63b2a6c6255f509ddbb0 /asl/tests | |
parent | ac47be51b79f4c3e49656870e135453eefe759ea (diff) |
Introduce byte, use span<byte> on io Writer
Diffstat (limited to 'asl/tests')
-rw-r--r-- | asl/tests/format_tests.cpp | 8 | ||||
-rw-r--r-- | asl/tests/integers_tests.cpp | 2 | ||||
-rw-r--r-- | asl/tests/meta_tests.cpp | 5 | ||||
-rw-r--r-- | asl/tests/span_tests.cpp | 28 |
4 files changed, 39 insertions, 4 deletions
diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index bfde454..f051034 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -16,11 +16,11 @@ class StringSink : public asl::writer char* m_data{};
public:
- void write(const char* str, isize_t len) override
+ void write(asl::span<const asl::byte> str) override
{
- m_data = (char*)realloc(m_data, (size_t)(m_current_len + len + 1));
- memcpy(m_data + m_current_len, str, (size_t)len);
- m_current_len += len;
+ m_data = (char*)realloc(m_data, (size_t)(m_current_len + str.size() + 1));
+ memcpy(m_data + m_current_len, str.data(), (size_t)str.size());
+ m_current_len += str.size();
m_data[m_current_len] = '\0';
}
diff --git a/asl/tests/integers_tests.cpp b/asl/tests/integers_tests.cpp index d15168e..fdfa0fc 100644 --- a/asl/tests/integers_tests.cpp +++ b/asl/tests/integers_tests.cpp @@ -9,3 +9,5 @@ static_assert(sizeof(uint8_t) == 1); static_assert(sizeof(uint16_t) == 2);
static_assert(sizeof(uint32_t) == 4);
static_assert(sizeof(uint64_t) == 8);
+
+static_assert(sizeof(asl::byte) == 1);
diff --git a/asl/tests/meta_tests.cpp b/asl/tests/meta_tests.cpp index 5fc4ef4..40b9046 100644 --- a/asl/tests/meta_tests.cpp +++ b/asl/tests/meta_tests.cpp @@ -197,3 +197,8 @@ static_assert(!asl::derived_from<uint8_t, uint16_t>); static_assert(!asl::derived_from<uint16_t, uint8_t>);
static_assert(!asl::derived_from<int, int>);
+static_assert(!asl::is_const<int>);
+static_assert(asl::is_const<const int>);
+static_assert(!asl::is_const<const int*>);
+static_assert(asl::is_const<int* const>);
+
diff --git a/asl/tests/span_tests.cpp b/asl/tests/span_tests.cpp index 3baaf5c..547ba16 100644 --- a/asl/tests/span_tests.cpp +++ b/asl/tests/span_tests.cpp @@ -423,3 +423,31 @@ ASL_TEST(last_dynamic) ASL_TEST_EXPECT(s3[2] == 3); ASL_TEST_EXPECT(s3[3] == 4); } + +template<typename T> +concept HasAsMutableBytes = requires(asl::span<T> s) +{ + asl::as_mutable_bytes(s); +}; + +static_assert(HasAsMutableBytes<int>); +static_assert(!HasAsMutableBytes<const int>); +static_assert(!HasAsMutableBytes<int* const>); +static_assert(HasAsMutableBytes<const int*>); + +ASL_TEST(as_bytes) +{ + uint32_t data[] = {0x01020304, 0x05060708}; + asl::span s1(data); + asl::span s2 = asl::as_bytes(s1); + + ASL_TEST_ASSERT(s2.size() == 8); + ASL_TEST_ASSERT(static_cast<int>(s2[0]) == 0x04); + ASL_TEST_ASSERT(static_cast<int>(s2[1]) == 0x03); + ASL_TEST_ASSERT(static_cast<int>(s2[2]) == 0x02); + ASL_TEST_ASSERT(static_cast<int>(s2[3]) == 0x01); + ASL_TEST_ASSERT(static_cast<int>(s2[4]) == 0x08); + ASL_TEST_ASSERT(static_cast<int>(s2[5]) == 0x07); + ASL_TEST_ASSERT(static_cast<int>(s2[6]) == 0x06); + ASL_TEST_ASSERT(static_cast<int>(s2[7]) == 0x05); +} |