Skip to content

Commit

Permalink
Add tests for Audio module types
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Apr 11, 2024
1 parent 6eaf300 commit 360d780
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
- { name: Linux Clang, os: ubuntu-22.04, flags: -DCMAKE_CXX_COMPILER=clang++ -DSFML_RUN_DISPLAY_TESTS=ON -GNinja , gcovr_options: '--gcov-executable="llvm-cov-$CLANG_VERSION gcov"' }
- { name: Linux GCC DRM, os: ubuntu-22.04, flags: -DSFML_USE_DRM=ON -DSFML_RUN_DISPLAY_TESTS=OFF -GNinja }
- { name: Linux GCC OpenGL ES, os: ubuntu-22.04, flags: -DSFML_OPENGL_ES=ON -DSFML_RUN_DISPLAY_TESTS=OFF -GNinja }
- { name: macOS x64, os: macos-12, flags: -GNinja }
- { name: macOS x64 Xcode, os: macos-12, flags: -GXcode }
- { name: macOS x64, os: macos-12, flags: -GNinja -DSFML_RUN_AUDIO_DEVICE_TESTS=ON }
- { name: macOS x64 Xcode, os: macos-12, flags: -GXcode -DSFML_RUN_AUDIO_DEVICE_TESTS=ON }
- { name: macOS arm64, os: macos-14, flags: -GNinja }
- { name: iOS, os: macos-12, flags: -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES=arm64 }
- { name: iOS Xcode, os: macos-12, flags: -DCMAKE_SYSTEM_NAME=iOS -GXcode -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO }
Expand Down Expand Up @@ -251,7 +251,7 @@ jobs:
- { name: Linux, os: ubuntu-22.04 }
- { name: Linux DRM, os: ubuntu-22.04, flags: -DSFML_USE_DRM=TRUE }
- { name: Linux OpenGL ES, os: ubuntu-22.04, flags: -DSFML_OPENGL_ES=ON }
- { name: macOS, os: macos-12 }
- { name: macOS, os: macos-12, flags: -DSFML_RUN_AUDIO_DEVICE_TESTS=ON }
- { name: iOS, os: macos-12, flags: -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES=arm64 }
- { name: Android, os: ubuntu-22.04, flags: -DCMAKE_ANDROID_ARCH_ABI=x86_64 -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DSFML_BUILD_TEST_SUITE=FALSE -DCMAKE_ANDROID_NDK=$ANDROID_NDK -DCMAKE_ANDROID_STL_TYPE=c++_shared }

Expand Down
218 changes: 214 additions & 4 deletions test/Audio/Music.test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,218 @@
#include <SFML/Audio/Music.hpp>

// Other 1st party headers
#include <SFML/System/FileInputStream.hpp>

#include <catch2/catch_test_macros.hpp>

#include <AudioUtil.hpp>
#include <SystemUtil.hpp>
#include <array>
#include <fstream>
#include <type_traits>

static_assert(!std::is_copy_constructible_v<sf::Music>);
static_assert(!std::is_copy_assignable_v<sf::Music>);
static_assert(!std::is_nothrow_move_constructible_v<sf::Music>);
static_assert(!std::is_nothrow_move_assignable_v<sf::Music>);
TEST_CASE("[Audio] sf::Music", runAudioDeviceTests())
{
SECTION("Type traits")
{
STATIC_CHECK(!std::is_copy_constructible_v<sf::Music>);
STATIC_CHECK(!std::is_copy_assignable_v<sf::Music>);
STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::Music>);
STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::Music>);
STATIC_CHECK(std::has_virtual_destructor_v<sf::Music>);
}

SECTION("Span")
{
const sf::Music::Span<float> span;
CHECK(span.offset == 0);
CHECK(span.length == 0);

const sf::Music::TimeSpan timeSpan;
CHECK(timeSpan.offset == sf::Time::Zero);
CHECK(timeSpan.length == sf::Time::Zero);
}

SECTION("Construction")
{
const sf::Music music;
CHECK(music.getDuration() == sf::Time::Zero);
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::Time::Zero);
CHECK(music.getChannelCount() == 0);
CHECK(music.getSampleRate() == 0);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}

SECTION("openFromFile()")
{
sf::Music music;

SECTION("Invalid file")
{
REQUIRE(!music.openFromFile("does/not/exist.wav"));
CHECK(music.getDuration() == sf::Time::Zero);
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::Time::Zero);
CHECK(music.getChannelCount() == 0);
CHECK(music.getSampleRate() == 0);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}

SECTION("Valid file")
{
REQUIRE(music.openFromFile("Audio/ding.mp3"));
CHECK(music.getDuration() == sf::microseconds(1990884));
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::microseconds(1990884));
CHECK(music.getChannelCount() == 1);
CHECK(music.getSampleRate() == 44100);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}
}

SECTION("openFromMemory()")
{
sf::Music music;

SECTION("Invalid buffer")
{
const std::array<std::byte, 5> memory{};
REQUIRE(!music.openFromMemory(memory.data(), memory.size()));
CHECK(music.getDuration() == sf::Time::Zero);
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::Time::Zero);
CHECK(music.getChannelCount() == 0);
CHECK(music.getSampleRate() == 0);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}

SECTION("Valid buffer")
{
const auto memory = []()
{
std::ifstream file("Audio/ding.flac", std::ios::binary | std::ios::ate);
REQUIRE(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(static_cast<std::size_t>(size));
REQUIRE(file.read(buffer.data(), size));
return buffer;
}();

REQUIRE(music.openFromMemory(memory.data(), memory.size()));
CHECK(music.getDuration() == sf::microseconds(1990884));
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::microseconds(1990884));
CHECK(music.getChannelCount() == 1);
CHECK(music.getSampleRate() == 44100);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}
}

SECTION("openFromStream()")
{
sf::Music music;
sf::FileInputStream stream;

SECTION("Invalid stream")
{
CHECK(!music.openFromStream(stream));
CHECK(music.getDuration() == sf::Time::Zero);
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::Time::Zero);
CHECK(music.getChannelCount() == 0);
CHECK(music.getSampleRate() == 0);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}

SECTION("Valid stream")
{
REQUIRE(stream.open("Audio/doodle_pop.ogg"));
REQUIRE(music.openFromStream(stream));
CHECK(music.getDuration() == sf::microseconds(24002176));
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::microseconds(24002176));
CHECK(music.getChannelCount() == 2);
CHECK(music.getSampleRate() == 44100);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}
}

SECTION("play/pause/stop")
{
sf::Music music;
REQUIRE(music.openFromFile("Audio/ding.mp3"));

// Wait for background thread to start
music.play();
while (music.getStatus() == sf::SoundSource::Status::Stopped)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
CHECK(music.getStatus() == sf::SoundSource::Status::Playing);

// Wait for background thread to pause
music.pause();
while (music.getStatus() == sf::SoundSource::Status::Playing)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
CHECK(music.getStatus() == sf::SoundSource::Status::Paused);

// Wait for background thread to stop
music.stop();
while (music.getStatus() == sf::SoundSource::Status::Paused)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
}

SECTION("setLoopPoints()")
{
sf::Music music;

SECTION("No file")
{
music.setLoopPoints({sf::Time::Zero, sf::Time::Zero});
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::Time::Zero);
CHECK(length == sf::Time::Zero);
CHECK(music.getChannelCount() == 0);
CHECK(music.getSampleRate() == 0);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}

SECTION("Loaded file")
{
REQUIRE(music.openFromFile("Audio/killdeer.wav"));
music.setLoopPoints({sf::seconds(1), sf::seconds(2)});
const auto [offset, length] = music.getLoopPoints();
CHECK(offset == sf::seconds(1));
CHECK(length == sf::seconds(2));
CHECK(music.getChannelCount() == 1);
CHECK(music.getSampleRate() == 22050);
CHECK(music.getStatus() == sf::SoundSource::Status::Stopped);
CHECK(music.getPlayingOffset() == sf::Time::Zero);
CHECK(!music.getLoop());
}
}
}
62 changes: 55 additions & 7 deletions test/Audio/Sound.test.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
#include <SFML/Audio/Sound.hpp>

// Other 1st party headers
#include <SFML/Audio/SoundBuffer.hpp>

#include <SFML/System/Time.hpp>

#include <catch2/catch_test_macros.hpp>

#include <AudioUtil.hpp>
#include <SystemUtil.hpp>
#include <type_traits>

static_assert(!std::is_constructible_v<sf::Sound, sf::SoundBuffer&&>);
static_assert(std::is_copy_constructible_v<sf::Sound>);
static_assert(std::is_copy_assignable_v<sf::Sound>);
static_assert(std::is_move_constructible_v<sf::Sound>);
static_assert(!std::is_nothrow_move_constructible_v<sf::Sound>);
static_assert(std::is_move_assignable_v<sf::Sound>);
static_assert(!std::is_nothrow_move_assignable_v<sf::Sound>);
TEST_CASE("[Audio] sf::Sound", runAudioDeviceTests())
{
SECTION("Type traits")
{
STATIC_CHECK(!std::is_constructible_v<sf::Sound, sf::SoundBuffer&&>);
STATIC_CHECK(std::is_copy_constructible_v<sf::Sound>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Sound>);
STATIC_CHECK(std::is_move_constructible_v<sf::Sound>);
STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::Sound>);
STATIC_CHECK(std::is_move_assignable_v<sf::Sound>);
STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::Sound>);
}

const sf::SoundBuffer soundBuffer;

SECTION("Construction")
{
const sf::Sound sound(soundBuffer);
CHECK(&sound.getBuffer() == &soundBuffer);
CHECK(!sound.getLoop());
CHECK(sound.getPlayingOffset() == sf::Time::Zero);
CHECK(sound.getStatus() == sf::SoundSource::Status::Stopped);
}

SECTION("Set/get buffer")
{
const sf::SoundBuffer otherSoundBuffer;
sf::Sound sound(soundBuffer);
sound.setBuffer(otherSoundBuffer);
CHECK(&sound.getBuffer() == &otherSoundBuffer);
}

SECTION("Set/get loop")
{
sf::Sound sound(soundBuffer);
sound.setLoop(true);
CHECK(sound.getLoop());
}

SECTION("Set/get playing offset")
{
sf::Sound sound(soundBuffer);
sound.setPlayingOffset(sf::seconds(10));
CHECK(sound.getPlayingOffset() == sf::Time::Zero);
}
}

0 comments on commit 360d780

Please sign in to comment.