Skip to content

Commit

Permalink
slightly better proxy mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Nov 27, 2024
1 parent b2dcf59 commit f84202f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
16 changes: 4 additions & 12 deletions src/tests/math/mock_binpack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,18 @@ struct ProxyBinPacker {
using MockT = MockBinPacker<IdT>;

ProxyBinPacker() {
MockT::callMock([](MockT& m) {
m.mock_constructor();
});
PROXY_CALL_MOCK(MockT, mock_constructor);
}

~ProxyBinPacker() {
MockT::callMock([](MockT& m) {
m.mock_destructor();
});
PROXY_CALL_MOCK(MockT, mock_destructor);
}

void add(const IdT& id, size_t width, size_t height) {
MockT::callMock([&](MockT& m) {
m.add(id, width, height);
});
PROXY_CALL_MOCK(MockT, add, id, width, height);
}

MockT::Packing pack(size_t dimConstraint, size_t padding = 0) const {
return MockT::callMock([&](MockT& m) {
return m.pack(dimConstraint, padding);
});
return PROXY_CALL_MOCK(MockT, pack, dimConstraint, padding);
}
};
19 changes: 15 additions & 4 deletions src/tests/mock_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ struct MockError : public std::logic_error {
using std::logic_error::logic_error;
};

template <class Mock>
// CRTP mixin that enables proxying for a mock.
// the size_t allows for having multiple proxys of a Mock at a time
template <class Mock, size_t ProxyID = 0>
class ProxyableMock {
private:
inline static Mock* mock{ nullptr };
Expand All @@ -17,7 +19,6 @@ class ProxyableMock {
return std::exchange(mock, newMock);
}
public:

ProxyableMock() {
if (inject(static_cast<Mock*>(this)) != nullptr)
throw MockError("Attempting to proxy mock multiple times!");
Expand All @@ -32,12 +33,22 @@ class ProxyableMock {
ProxyableMock& operator=(const ProxyableMock&) = delete;
ProxyableMock& operator=(ProxyableMock&&) = delete;

template <class Func, class... Ts>
template <class Func>
static decltype(auto) callMock(Func&& func) {
if (mock) {
return func(*mock);
}

throw MockError("Tried to call proxy mock that isn't mocked!");
}
};

[[nodiscard]]
static constexpr size_t proxyID() noexcept {
return ProxyID;
}
};

#define PROXY_CALL_MOCK(MockT, method, ...) \
MockT::callMock([&](MockT& _proxmock) { \
return _proxmock.method(__VA_ARGS__); \
})
12 changes: 4 additions & 8 deletions src/tests/render/image/image_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ namespace sndx::render {

inline const std::filesystem::path test_image_path{ u8"test_data/visual/rgbbw_test_img☃.png" };

template <size_t channels = 3> [[nodiscard]]
template <glm::length_t channels = 3> [[nodiscard]]
ImageData createSolidImage(size_t width, size_t height, glm::vec<channels, std::byte> color = {}) {
std::vector<std::byte> buffer{};
buffer.resize(width * height * channels);

using l = decltype(color)::length_type;

for (size_t y = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x) {
for (size_t c = 0; c < channels; ++c) {
for (glm::length_t c = 0; c < channels; ++c) {
buffer[y * width * channels + x * channels + c] = color[c];
}
}
Expand All @@ -24,18 +22,16 @@ namespace sndx::render {
return ImageData{ width, height, channels, std::move(buffer) };
}

template <size_t channels = 3> [[nodiscard]]
template <glm::length_t channels = 3> [[nodiscard]]
ImageData createCheckeredImage(size_t width, size_t height, glm::vec<channels, std::byte> c1, glm::vec<channels, std::byte> c2 = {}) {
std::vector<std::byte> buffer{};
buffer.resize(width * height * channels);

using l = decltype(c1)::length_type;

for (size_t y = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x) {
auto b = (y + x) % 2 == 0;

for (l c = 0; c < channels; ++c) {
for (glm::length_t c = 0; c < channels; ++c) {
buffer[y * width * channels + x * channels + c] = b ? c1[c] : c2[c];
}
}
Expand Down

0 comments on commit f84202f

Please sign in to comment.