Skip to content

Commit

Permalink
Use loadIntoMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Apr 12, 2024
1 parent 8ccf534 commit 30fcb35
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
12 changes: 1 addition & 11 deletions test/Audio/InputSoundFile.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,8 @@ TEST_CASE("[Audio] sf::InputSoundFile")

SECTION("openFromMemory()")
{
const auto memory = loadIntoMemory("Audio/killdeer.wav");
sf::InputSoundFile inputSoundFile;
const auto memory = []()
{
std::ifstream file("Audio/killdeer.wav", 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(inputSoundFile.openFromMemory(memory.data(), memory.size()));
CHECK(inputSoundFile.getSampleCount() == 112'941);
CHECK(inputSoundFile.getChannelCount() == 1);
Expand Down
15 changes: 0 additions & 15 deletions test/TestUtilities/GraphicsUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

#include <GraphicsUtil.hpp>
#include <SystemUtil.hpp>
#include <fstream>
#include <limits>
#include <ostream>

#include <cassert>

namespace sf
{
std::ostream& operator<<(std::ostream& os, const BlendMode& blendMode)
Expand Down Expand Up @@ -116,15 +113,3 @@ bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs)
lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) &&
lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]);
}

std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
assert(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(static_cast<std::size_t>(size));
[[maybe_unused]] const auto& result = file.read(reinterpret_cast<char*>(buffer.data()), size);
assert(result);
return buffer;
}
6 changes: 0 additions & 6 deletions test/TestUtilities/GraphicsUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
#pragma once

#include <SystemUtil.hpp>
#include <filesystem>
#include <iosfwd>
#include <vector>

#include <cstddef>

namespace sf
{
Expand Down Expand Up @@ -39,5 +35,3 @@ bool operator==(const sf::Rect<T>& lhs, const Approx<sf::Rect<T>>& rhs)
return lhs.left == Approx(rhs.value.left) && lhs.top == Approx(rhs.value.top) &&
lhs.width == Approx(rhs.value.width) && lhs.height == Approx(rhs.value.height);
}

[[nodiscard]] std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path);
16 changes: 16 additions & 0 deletions test/TestUtilities/SystemUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#include <catch2/catch_approx.hpp>

#include <SystemUtil.hpp>
#include <fstream>
#include <iomanip>
#include <limits>

#include <cassert>


namespace sf
{
void setStreamPrecision(std::ostream& os, int maxDigits10)
Expand Down Expand Up @@ -75,3 +79,15 @@ bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs)
{
return lhs.asDegrees() == Approx(rhs.value.asDegrees());
}

std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
assert(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(static_cast<std::size_t>(size));
[[maybe_unused]] const auto& result = file.read(reinterpret_cast<char*>(buffer.data()), size);
assert(result);
return buffer;
}
6 changes: 6 additions & 0 deletions test/TestUtilities/SystemUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

#pragma once

#include <filesystem>
#include <iosfwd>
#include <vector>

#include <cstddef>

// String conversions for Catch2
namespace sf
Expand Down Expand Up @@ -58,3 +62,5 @@ std::ostream& operator<<(std::ostream& os, const Approx<T>& approx)
{
return os << approx.value;
}

[[nodiscard]] std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path);

0 comments on commit 30fcb35

Please sign in to comment.