Skip to content

Commit

Permalink
Add shared object class
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 16, 2023
1 parent 0e1189f commit d40f01a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 14 deletions.
1 change: 1 addition & 0 deletions include/centurion/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef CENTURION_SYSTEM_HPP_
#define CENTURION_SYSTEM_HPP_

#include <centurion/system/shared_object.hpp>
#include <centurion/system/time.hpp>

#endif // CENTURION_SYSTEM_HPP_
64 changes: 64 additions & 0 deletions include/centurion/system/shared_object.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2019-2023 Albin Johansson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef CENTURION_SYSTEM_SHARED_OBJECT_HPP_
#define CENTURION_SYSTEM_SHARED_OBJECT_HPP_

#include <cassert> // assert
#include <memory> // unique_ptr

#include <SDL3/SDL.h>

#include <centurion/common/errors.hpp>

namespace cen {

class SharedObject final {
public:
struct SharedObjectDeleter final {
void operator()(void* object) noexcept { SDL_UnloadObject(object); }
};

explicit SharedObject(const char* object)
: mObject {SDL_LoadObject(object)}
{
if (!mObject) {
throw SDLError {};
}
}

template <typename T>
[[nodiscard]] auto load_function(const char* name) const noexcept -> T*
{
assert(name);
return reinterpret_cast<T*>(SDL_LoadFunction(mObject.get(), name));
}

private:
std::unique_ptr<void, SharedObjectDeleter> mObject;
};

} // namespace cen

#endif // CENTURION_SYSTEM_SHARED_OBJECT_HPP_
15 changes: 9 additions & 6 deletions tests/mocked-tests/src/sdl_mocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@

#include "sdl_mocks.hpp"

DEFINE_FAKE_VALUE_FUNC(Uint64, SDL_GetTicks)
DEFINE_FAKE_VALUE_FUNC(Uint64, SDL_GetTicksNS)
DEFINE_FAKE_VALUE_FUNC(Uint64, SDL_GetPerformanceCounter)
DEFINE_FAKE_VALUE_FUNC(Uint64, SDL_GetPerformanceFrequency)
CEN_MOCK_FUNCTIONS(DEFINE)

namespace cen::testing {
namespace testing {

void reset_mocks()
{
RESET_FAKE(SDL_GetError)

RESET_FAKE(SDL_GetTicks)
RESET_FAKE(SDL_GetTicksNS)
RESET_FAKE(SDL_GetPerformanceCounter)
RESET_FAKE(SDL_GetPerformanceFrequency)

RESET_FAKE(SDL_LoadObject)
RESET_FAKE(SDL_LoadFunction)
RESET_FAKE(SDL_UnloadObject)
}

} // namespace cen::testing
} // namespace testing
36 changes: 28 additions & 8 deletions tests/mocked-tests/src/sdl_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,46 @@

#pragma once

#include <cstring> // memcpy

#include <SDL3/SDL.h>

#include <centurion/common/primitives.hpp>
#include <fff/fff.h>

DECLARE_FAKE_VALUE_FUNC(Uint64, SDL_GetTicks)
DECLARE_FAKE_VALUE_FUNC(Uint64, SDL_GetTicksNS)
DECLARE_FAKE_VALUE_FUNC(Uint64, SDL_GetPerformanceCounter)
DECLARE_FAKE_VALUE_FUNC(Uint64, SDL_GetPerformanceFrequency)
#define CEN_MOCK_FUNCTIONS(Op) \
Op##_FAKE_VALUE_FUNC(const char*, SDL_GetError); \
Op##_FAKE_VALUE_FUNC(Uint64, SDL_GetTicks); \
Op##_FAKE_VALUE_FUNC(Uint64, SDL_GetTicksNS); \
Op##_FAKE_VALUE_FUNC(Uint64, SDL_GetPerformanceCounter); \
Op##_FAKE_VALUE_FUNC(Uint64, SDL_GetPerformanceFrequency); \
Op##_FAKE_VALUE_FUNC(void*, SDL_LoadObject, const char*); \
Op##_FAKE_VALUE_FUNC(SDL_FunctionPointer, SDL_LoadFunction, void*, const char*); \
Op##_FAKE_VOID_FUNC(SDL_UnloadObject, void*)

CEN_MOCK_FUNCTIONS(DECLARE)

namespace cen::testing {
namespace testing {

void reset_mocks();

} // namespace cen::testing
template <typename Ptr>
[[nodiscard]] auto make_ptr(const cen::usize value) noexcept -> Ptr
{
static_assert(sizeof(Ptr) == sizeof(cen::usize));

Ptr ptr;
std::memcpy(&ptr, &value, sizeof value);
return ptr;
}

} // namespace testing

#define CEN_MOCK_FIXTURE(Name) \
class Name : public testing::Test { \
public: \
static void TearDownTestSuite() \
void TearDown() override \
{ \
cen::testing::reset_mocks(); \
testing::reset_mocks(); \
} \
}
56 changes: 56 additions & 0 deletions tests/mocked-tests/src/system/shared_object_mtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* MIT License
*
* Copyright (c) 2019-2023 Albin Johansson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <centurion/system/shared_object.hpp>
#include <gtest/gtest.h>

#include "sdl_mocks.hpp"

CEN_MOCK_FIXTURE(SharedObjectFixture);

TEST_F(SharedObjectFixture, Constructor)
{
EXPECT_THROW(cen::SharedObject {"dummy.dll"}, cen::SDLError);
EXPECT_EQ(SDL_LoadObject_fake.call_count, 1);
EXPECT_STREQ(SDL_LoadObject_fake.arg0_val, "dummy.dll");
}

TEST_F(SharedObjectFixture, Usage)
{
void* so_handle = testing::make_ptr<void*>(123);
SET_RETURN_SEQ(SDL_LoadObject, &so_handle, 1)

{
cen::SharedObject so {"dummy.dll"};
EXPECT_EQ(SDL_LoadObject_fake.call_count, 1);

auto* function [[maybe_unused]] = so.load_function<void()>("do_something");
EXPECT_EQ(SDL_LoadFunction_fake.call_count, 1);
EXPECT_EQ(SDL_LoadFunction_fake.arg0_val, so_handle);
EXPECT_STREQ(SDL_LoadFunction_fake.arg1_val, "do_something");
}

EXPECT_EQ(SDL_UnloadObject_fake.call_count, 1);
EXPECT_EQ(SDL_UnloadObject_fake.arg0_val, so_handle);
}

0 comments on commit d40f01a

Please sign in to comment.