emberemu/tests/IntrusiveStorage.cpp

118 lines
3.7 KiB
C++
Raw Permalink Normal View History

2024-02-25 00:07:54 +00:00
/*
* Copyright (c) 2015 - 2025 Ember
2024-02-25 00:07:54 +00:00
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <spark/buffers/detail/IntrusiveStorage.h>
2024-02-25 00:07:54 +00:00
#include <gtest/gtest.h>
2024-09-02 15:30:00 +01:00
#include <array>
#include <string_view>
2024-02-25 00:07:54 +00:00
using namespace ember;
2024-02-25 00:07:54 +00:00
2024-07-06 02:06:49 +01:00
TEST(IntrusiveStorage, Size) {
2024-02-25 00:07:54 +00:00
const int iterations = 5;
spark::io::detail::IntrusiveStorage<sizeof(int) * iterations> buffer;
2024-02-25 00:07:54 +00:00
int foo = 24221;
std::size_t written = 0;
for(int i = 0; i < 5; ++i) {
written += buffer.write(&foo, sizeof(int));
}
ASSERT_EQ(sizeof(int) * iterations, written) << "Number of bytes written is incorrect";
ASSERT_EQ(sizeof(int) * iterations, buffer.size()) << "Buffer size is incorrect";
// attempt to exceed capacity - write should return 0
written = buffer.write(&foo, sizeof(int));
ASSERT_EQ(0, written) << "Number of bytes written is incorrect";
ASSERT_EQ(sizeof(int) * iterations, buffer.size()) << "Buffer size is incorrect";
}
2024-07-06 02:06:49 +01:00
TEST(IntrusiveStorage, ReadWriteConsistency) {
2024-02-25 00:07:54 +00:00
const char text[] = "The quick brown fox jumps over the lazy dog";
spark::io::detail::IntrusiveStorage<sizeof(text)> buffer;
2024-02-25 00:07:54 +00:00
std::size_t written = buffer.write(text, sizeof(text));
ASSERT_EQ(sizeof(text), written) << "Incorrect write size";
char text_out[sizeof(text)];
std::size_t read = buffer.read(text_out, sizeof(text));
ASSERT_EQ(sizeof(text), read) << "Incorrect read size";
ASSERT_STREQ(text, text_out) << "Read produced incorrect result";
ASSERT_EQ(0, buffer.size()) << "Buffer should be empty";
}
2024-07-06 02:06:49 +01:00
TEST(IntrusiveStorage, Skip) {
2024-02-25 00:07:54 +00:00
const char text[] = "The quick brown fox jumps over the lazy dog";
spark::io::detail::IntrusiveStorage<sizeof(text)> buffer;
2024-02-25 00:07:54 +00:00
buffer.write(text, sizeof(text));
2024-09-02 15:30:00 +01:00
std::array<char, sizeof(text)> text_out{};
2024-02-25 00:07:54 +00:00
std::size_t skipped = buffer.skip(4);
ASSERT_EQ(4, skipped) << "Incorrect skip length";
2024-09-02 15:30:00 +01:00
buffer.read(text_out.data(), sizeof(text) - 4);
ASSERT_STREQ("quick brown fox jumps over the lazy dog", text_out.data())
2024-02-25 00:07:54 +00:00
<< "Skip/read produced incorrect result";
2024-09-02 15:30:00 +01:00
}
2025-03-09 00:24:00 +00:00
TEST(IntrusiveStorage, ReadWriteStringview) {
2024-09-02 15:30:00 +01:00
spark::io::detail::IntrusiveStorage<128, char> buffer;
std::string_view str { "The quick brown fox jumped over the lazy dog" };
buffer.write(str.data(), str.size());
ASSERT_EQ(str.size(), buffer.size());
2024-09-02 15:30:00 +01:00
std::array<char, 128> out{};
buffer.read(out.data(), str.size());
2024-09-02 15:30:00 +01:00
ASSERT_STREQ(str.data(), out.data());
ASSERT_TRUE(buffer.size() == 0);
}
TEST(IntrusiveStorage, ReadWriteInts) {
spark::io::detail::IntrusiveStorage<128, char> buffer;
std::array<int, 4> in { 42, 1657, 1558, -1563 };
buffer.write(in.data(), sizeof(in));
ASSERT_EQ(sizeof(in), buffer.size());
std::array<int, 4> out {};
buffer.read(out.data(), sizeof(out));
ASSERT_EQ(in, out);
ASSERT_TRUE(buffer.size() == 0);
}
TEST(IntrusiveStorage, Subscript) {
spark::io::detail::IntrusiveStorage<8, char> buffer;
std::string_view str { "ABC" };
buffer.write(str.data(), str.size());
2024-09-02 15:30:00 +01:00
ASSERT_EQ(str[0], buffer[0]);
ASSERT_EQ(str[1], buffer[1]);
ASSERT_EQ(str[2], buffer[2]);
buffer[0] = 'C';
buffer[1] = 'D';
buffer[2] = 'E';
ASSERT_EQ('C', buffer[0]);
ASSERT_EQ('D', buffer[1]);
ASSERT_EQ('E', buffer[2]);
}
TEST(IntrusiveStorage, AdvanceWrite) {
spark::io::detail::IntrusiveStorage<32, char> buffer;
constexpr std::string_view str {"A short string"};
const std::size_t advance = 10;
buffer.advance_write(advance);
ASSERT_EQ(buffer.size(), advance);
buffer.write(str.data(), str.size() + 1);
ASSERT_EQ(buffer.size(), advance + str.size() + 1);
std::array<char, str.size() + 1> out{};
buffer.read(out.data(), advance); // skip the 'empty' data
buffer.read(out.data(), str.size() + 1);
ASSERT_STREQ(str.data(), out.data());
}