2021-12-25 00:16:06 +08:00
|
|
|
#include "SystemUtil.hpp"
|
|
|
|
|
2022-01-23 09:11:08 +08:00
|
|
|
#include <SFML/System/Angle.hpp>
|
2018-08-19 07:30:53 +08:00
|
|
|
#include <SFML/System/String.hpp>
|
|
|
|
#include <SFML/System/Time.hpp>
|
2021-12-25 00:16:06 +08:00
|
|
|
|
2022-02-18 00:22:00 +08:00
|
|
|
#include <doctest.h> // for Approx
|
2022-01-23 09:11:08 +08:00
|
|
|
#include <cassert>
|
2022-01-21 14:41:18 +08:00
|
|
|
#include <filesystem>
|
2021-12-25 00:16:06 +08:00
|
|
|
#include <fstream>
|
2022-01-23 09:11:08 +08:00
|
|
|
#include <iomanip>
|
|
|
|
#include <limits>
|
2021-12-29 13:03:15 +08:00
|
|
|
#include <ostream>
|
2018-08-19 07:30:53 +08:00
|
|
|
#include <sstream>
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
namespace sf
|
2018-08-19 07:30:53 +08:00
|
|
|
{
|
2022-01-23 09:11:08 +08:00
|
|
|
std::ostream& operator <<(std::ostream& os, const sf::Angle& angle)
|
|
|
|
{
|
|
|
|
os << std::fixed << std::setprecision(std::numeric_limits<float>::max_digits10);
|
|
|
|
os << angle.asDegrees() << " deg";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:03:15 +08:00
|
|
|
std::ostream& operator <<(std::ostream& os, const sf::String& string)
|
2018-08-19 07:30:53 +08:00
|
|
|
{
|
2021-12-29 13:03:15 +08:00
|
|
|
os << string.toAnsiString();
|
|
|
|
return os;
|
2018-08-19 07:30:53 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 13:03:15 +08:00
|
|
|
std::ostream& operator <<(std::ostream& os, sf::Time time)
|
2018-08-19 07:30:53 +08:00
|
|
|
{
|
2021-12-29 13:03:15 +08:00
|
|
|
os << time.asMicroseconds() << "us";
|
|
|
|
return os;
|
2018-08-19 07:30:53 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 00:16:06 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
bool operator==(const float& lhs, const Approx<float>& rhs)
|
2022-05-05 01:03:26 +08:00
|
|
|
{
|
2022-05-03 06:38:33 +08:00
|
|
|
return static_cast<double>(lhs) == doctest::Approx(static_cast<double>(rhs.value));
|
2022-05-05 01:03:26 +08:00
|
|
|
}
|
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
bool operator==(const sf::Vector2f& lhs, const Approx<sf::Vector2f>& rhs)
|
2022-02-18 00:22:00 +08:00
|
|
|
{
|
2022-05-03 06:38:33 +08:00
|
|
|
return (lhs - rhs.value).length() == Approx(0.f);
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
bool operator==(const sf::Vector3f& lhs, const Approx<sf::Vector3f>& rhs)
|
2022-02-18 00:22:00 +08:00
|
|
|
{
|
2022-05-03 06:38:33 +08:00
|
|
|
return (lhs - rhs.value).length() == Approx(0.f);
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs)
|
2022-05-05 01:03:26 +08:00
|
|
|
{
|
2022-05-03 06:38:33 +08:00
|
|
|
return lhs.asDegrees() == Approx(rhs.value.asDegrees());
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 00:16:06 +08:00
|
|
|
namespace sf::Testing
|
|
|
|
{
|
|
|
|
static std::string getTemporaryFilePath()
|
|
|
|
{
|
|
|
|
static int counter = 0;
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "sfmltemp" << counter << ".tmp";
|
|
|
|
++counter;
|
|
|
|
|
|
|
|
std::filesystem::path result;
|
|
|
|
result /= std::filesystem::temp_directory_path();
|
|
|
|
result /= oss.str();
|
|
|
|
|
|
|
|
return result.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryFile::TemporaryFile(const std::string& contents)
|
|
|
|
: m_path(getTemporaryFilePath())
|
|
|
|
{
|
|
|
|
std::ofstream ofs(m_path);
|
|
|
|
assert(ofs);
|
|
|
|
|
|
|
|
ofs << contents;
|
|
|
|
assert(ofs);
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryFile::~TemporaryFile()
|
|
|
|
{
|
|
|
|
[[maybe_unused]] const bool removed = std::filesystem::remove(m_path);
|
|
|
|
assert(removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& TemporaryFile::getPath() const
|
|
|
|
{
|
|
|
|
return m_path;
|
|
|
|
}
|
|
|
|
}
|