Skip to content

Commit

Permalink
renderer max_channels | package test mocks (#267)
Browse files Browse the repository at this point in the history
* refactor

* code rev
  • Loading branch information
Raffaello authored Nov 2, 2023
1 parent e3174a9 commit 95594c7
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 126 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
endif()


project ("sdl2-hyper-sonic-drivers" VERSION 0.13.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
project ("sdl2-hyper-sonic-drivers" VERSION 0.13.1 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
include (TestBigEndian)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
Expand Down
7 changes: 7 additions & 0 deletions sdl2-hyper-sonic-drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ install(DIRECTORY
PATTERN "*.h"
)

install(DIRECTORY
test/
DESTINATION static/test/include
FILES_MATCHING
PATTERN "*Mock.hpp"
)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${LIB_VER}.cmake
VERSION ${CMAKE_PROJECT_VERSION}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include <cstdint>
#include <filesystem>
#include <memory>
#include <vector>
#include <HyperSonicDrivers/audio/IMixer.hpp>
#include <HyperSonicDrivers/audio/IAudioStream.hpp>
#include <HyperSonicDrivers/hardware/opl/OPL.hpp>
#include <HyperSonicDrivers/devices/IDevice.hpp>
#include <HyperSonicDrivers/files/WAVFile.hpp>

namespace HyperSonicDrivers::audio
{
Expand All @@ -25,5 +28,7 @@ namespace HyperSonicDrivers::audio

protected:
std::shared_ptr<IMixer> m_mixer;
std::unique_ptr<files::WAVFile> m_out;
std::vector<int16_t> m_buf;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ namespace HyperSonicDrivers::audio::sdl2

Mixer::~Mixer()
{
SDL_CloseAudioDevice(m_device_id);

SDL_QuitSubSystem(SDL_INIT_AUDIO);
if (m_ready)
{
SDL_CloseAudioDevice(m_device_id);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
m_ready = false;
}
}

bool Mixer::init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace HyperSonicDrivers::audio::sdl2
{
Renderer::Renderer(const uint32_t freq, const uint16_t buffer_size)
Renderer::Renderer(const uint32_t freq, const uint16_t buffer_size, const uint8_t max_channels)
{
m_mixer = make_mixer<Mixer>(1, freq, buffer_size);
m_mixer = make_mixer<Mixer>(max_channels, freq, buffer_size);
}

void Renderer::openOutputFile(const std::filesystem::path& path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#pragma once

#include <cstdint>
#include <filesystem>
#include <HyperSonicDrivers/audio/IRenderer.hpp>
#include <HyperSonicDrivers/audio/IMixer.hpp>
#include <HyperSonicDrivers/audio/IAudioStream.hpp>
#include <HyperSonicDrivers/files/WAVFile.hpp>
#include <vector>
#include <memory>
#include <filesystem>

namespace HyperSonicDrivers::audio::sdl2
{
class Renderer : public IRenderer
{
public:
Renderer(const uint32_t freq, const uint16_t buffer_size);
Renderer(const uint32_t freq, const uint16_t buffer_size, const uint8_t max_channels = 1);
~Renderer() override = default;

void openOutputFile(const std::filesystem::path& path) override;
void closeOutputFile() noexcept override;

void renderBuffer(IAudioStream* stream) override;
using IRenderer::renderBuffer;
private:
std::unique_ptr<files::WAVFile> m_out;
std::vector<int16_t> m_buf;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#include <HyperSonicDrivers/audio/IAudioStream.hpp>
#include <HyperSonicDrivers/audio/scummvm/Timestamp.hpp>

namespace HyperSonicDrivers::audio::stubs
namespace HyperSonicDrivers::audio
{
class StubMixer : public IMixer
class IMixerMock : public IMixer
{
public:
int rate = 44100;

StubMixer() : IMixer(32, 44100, 1024) {};
explicit StubMixer(const int freq) : IMixer(32, freq, 1024) {};
IMixerMock() : IMixer(32, 44100, 1024) {};
explicit IMixerMock(const int freq) : IMixer(32, freq, 1024) {};

bool init() override { return true; };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <HyperSonicDrivers/drivers/westwood/ADLDriver.hpp>
#include <HyperSonicDrivers/audio/mixer/ChannelGroup.hpp>
#include <HyperSonicDrivers/hardware/opl/OplEmulator.hpp>
#include <HyperSonicDrivers/audio/stubs/StubMixer.hpp>
#include <HyperSonicDrivers/audio/IMixerMock.hpp>
#include <filesystem>
#include <string>

Expand All @@ -28,7 +28,7 @@ namespace HyperSonicDrivers::audio::sdl2

RendererTest()
{
mixer = std::make_shared<stubs::StubMixer>();
mixer = std::make_shared<IMixerMock>();
switch (device_name)
{
using enum devices::eDeviceName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <HyperSonicDrivers/audio/midi/MIDIEvent.hpp>
#include <HyperSonicDrivers/devices/IDevice.hpp>
#include <HyperSonicDrivers/audio/IMixerMock.hpp>


namespace HyperSonicDrivers::devices
{
template<class T>
class DeviceMock : public T
{
public:
explicit DeviceMock(const std::shared_ptr<audio::IMixer>& mixer) : T(mixer) {
static_assert(std::is_base_of_v<IDevice, T>);
};
DeviceMock() : T(audio::make_mixer<audio::IMixerMock>()) {
static_assert(std::is_base_of_v<IDevice, T>);
};

bool init() noexcept override { return true; };
bool shutdown() noexcept override { return true; };
std::optional<uint8_t> getChannelId() const noexcept { return std::nullopt; };
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <HyperSonicDrivers/hardware/opl/OplEmulator.hpp>
#include <HyperSonicDrivers/audio/stubs/StubMixer.hpp>
#include <HyperSonicDrivers/audio/IMixerMock.hpp>
#include <stdexcept>

namespace HyperSonicDrivers::devices
{
using audio::stubs::StubMixer;
using audio::IMixerMock;
using hardware::opl::OplEmulator;

template<class T>
Expand All @@ -17,7 +17,7 @@ namespace HyperSonicDrivers::devices
public:
const OplEmulator oplEmu = std::get<0>(GetParam());
const bool shouldFail = std::get<1>(GetParam());
const std::shared_ptr<StubMixer> mixer = std::make_shared<StubMixer>();
const std::shared_ptr<IMixerMock> mixer = std::make_shared<IMixerMock>();

void test_case()
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <gmock/gmock.h>
#include <HyperSonicDrivers/devices/Adlib.hpp>
#include <HyperSonicDrivers/drivers/MIDDriverMock.hpp>
#include <HyperSonicDrivers/audio/stubs/StubMixer.hpp>
#include <HyperSonicDrivers/audio/IMixerMock.hpp>
#include <HyperSonicDrivers/hardware/opl/OplEmulator.hpp>
#include <HyperSonicDrivers/hardware/opl/OplType.hpp>
#include <HyperSonicDrivers/files/dmx/OP2File.hpp>
Expand All @@ -13,13 +13,13 @@ namespace HyperSonicDrivers::devices
{
TEST(Adlib, cstor_AUTO)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
EXPECT_NO_THROW(auto a = std::make_shared<Adlib>(mixer));
}

TEST(Adlib, device_name)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
auto a = std::make_shared<Adlib>(mixer);
EXPECT_EQ(a->getName(), eDeviceName::Adlib);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <HyperSonicDrivers/devices/IDevice.hpp>
#include <HyperSonicDrivers/devices/SpyDevice.hpp>
#include <HyperSonicDrivers/devices/DeviceMock.hpp>
#include <HyperSonicDrivers/devices/Adlib.hpp>
#include <HyperSonicDrivers/drivers/MIDDriverMock.hpp>

Expand All @@ -11,15 +11,15 @@ namespace HyperSonicDrivers::devices

TEST(IDevice, double_acquire)
{
auto device = std::make_shared<SpyDevice<Adlib>>();
auto device = std::make_shared<DeviceMock<Adlib>>();

MIDDriverMock middrv(device);
EXPECT_THROW(MIDDriverMock middrv2(device), std::runtime_error);
}

TEST(IDevice, acquire_release)
{
auto device = std::make_shared<SpyDevice<Adlib>>();
auto device = std::make_shared<DeviceMock<Adlib>>();

MIDDriverMock middrv(device);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
#include <gmock/gmock.h>
#include <HyperSonicDrivers/devices/Opl.hpp>
#include <HyperSonicDrivers/drivers/MIDDriverMock.hpp>
#include <HyperSonicDrivers/audio/stubs/StubMixer.hpp>
#include <HyperSonicDrivers/audio/IMixerMock.hpp>
#include <HyperSonicDrivers/hardware/opl/OplEmulator.hpp>
#include <HyperSonicDrivers/hardware/opl/OplType.hpp>
#include <HyperSonicDrivers/hardware/opl/OPL.hpp>
#include <HyperSonicDrivers/devices/OplDeviceMock.hpp>

namespace HyperSonicDrivers::devices::midi
{
using audio::stubs::StubMixer;
using audio::IMixerMock;
using hardware::opl::OplType;
using hardware::opl::OplEmulator;
using hardware::opl::OPL;
using audio::mixer::eChannelGroup;

TEST(MidiOpl, cstor_)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
EXPECT_NO_THROW(OplDeviceMock(OplType::OPL2, OplEmulator::AUTO, mixer));
}

Expand All @@ -28,7 +28,7 @@ namespace HyperSonicDrivers::devices::midi
const OplType oplType = std::get<0>(GetParam());
const OplEmulator oplEmu = std::get<1>(GetParam());
const bool shouldFail = std::get<2>(GetParam());
const std::shared_ptr<StubMixer> mixer = std::make_shared<StubMixer>();
const std::shared_ptr<IMixerMock> mixer = std::make_shared<IMixerMock>();
};
TEST_P(OplEmulator_, cstr_type_emu)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace HyperSonicDrivers::devices

TEST(SbPro, cstor_)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
EXPECT_NO_THROW(auto s = SbPro(mixer, OplEmulator::DOS_BOX));
}

TEST(Adlib, device_name)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
auto s = std::make_shared<SbPro>(mixer);
EXPECT_EQ(s->getName(), eDeviceName::SbPro);
}
Expand All @@ -41,7 +41,7 @@ namespace HyperSonicDrivers::devices

TEST(SbPro, cstr_AUTO)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
EXPECT_NO_THROW(SbPro(mixer, OplEmulator::AUTO));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace HyperSonicDrivers::devices
{
TEST(SbPro2, cstor_)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
EXPECT_NO_THROW(auto s = SbPro2(mixer, OplEmulator::NUKED));
}

TEST(Adlib, device_name)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
auto s = std::make_shared<SbPro2>(mixer);
EXPECT_EQ(s->getName(), eDeviceName::SbPro2);
}
Expand All @@ -41,7 +41,7 @@ namespace HyperSonicDrivers::devices

TEST(SbPro2, cstor_AUTO)
{
auto mixer = std::make_shared<StubMixer>();
auto mixer = std::make_shared<IMixerMock>();
EXPECT_NO_THROW(auto s = SbPro2(mixer));
}
}
Expand Down
Loading

0 comments on commit 95594c7

Please sign in to comment.