SFML/test/Window/Err.test.cpp

69 lines
2.6 KiB
C++
Raw Permalink Normal View History

2022-04-22 06:00:51 +08:00
#include <SFML/System/Err.hpp>
2024-07-11 04:01:59 +08:00
// Other 1st party headers
#include <SFML/Window/Cursor.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-04-22 06:00:51 +08:00
2024-07-11 04:01:59 +08:00
#include <array>
2022-07-05 00:20:58 +08:00
#include <sstream>
2024-07-11 04:01:59 +08:00
#include <thread>
2022-07-05 00:20:58 +08:00
TEST_CASE("[System] sf::err")
2022-04-22 06:00:51 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Overflow default buffer")
2022-04-22 06:00:51 +08:00
{
2023-01-18 12:51:08 +08:00
// No assertion macros in this section since nothing about this can be directly observed.
2022-04-22 06:00:51 +08:00
// Intention is to ensure DefaultErrStreamBuf::overflow gets called.
sf::err() << "SFML is a simple, fast, cross-platform and object-oriented multimedia API."
2022-07-05 00:20:58 +08:00
"It provides access to windowing, graphics, audio and network."
"It is written in C++, and has bindings for various languages such as C, .Net, Ruby, Python.";
2022-04-22 06:00:51 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Redirect buffer to observe contents")
2022-04-22 06:00:51 +08:00
{
sf::err() << "We'll never be able to observe this" << std::endl; // Ensure buffer is flushed
auto* const defaultStreamBuffer = sf::err().rdbuf();
2022-04-22 06:00:51 +08:00
CHECK(defaultStreamBuffer != nullptr);
const std::stringstream stream;
2022-04-22 06:00:51 +08:00
sf::err().rdbuf(stream.rdbuf());
sf::err() << "Something went wrong!\n";
CHECK(stream.str() == "Something went wrong!\n");
sf::err().rdbuf(nullptr);
sf::err() << "Sent to the abyss";
CHECK(stream.str() == "Something went wrong!\n");
sf::err().rdbuf(stream.rdbuf());
sf::err() << "Back to the stringstream :)\n";
CHECK(stream.str() == "Something went wrong!\nBack to the stringstream :)\n");
// Restore sf::err to default stream defaultStreamBuffer
sf::err().rdbuf(defaultStreamBuffer);
CHECK(sf::err().rdbuf() == defaultStreamBuffer);
}
2024-07-11 04:01:59 +08:00
SECTION("Log errors")
{
using namespace std::string_literals;
using namespace std::string_view_literals;
const std::stringstream stream;
sf::setErrorBuffer(stream.rdbuf());
// Produce logs concurrently from multiple threads
std::array threads{std::thread([] { (void)sf::Cursor::loadFromPixels(nullptr, {}, {}); }),
std::thread([] { (void)sf::Cursor::loadFromPixels(nullptr, {}, {}); }),
std::thread([] { (void)sf::Cursor::loadFromPixels(nullptr, {}, {}); })};
for (auto& thread : threads)
thread.join();
// Ensure all messages come through in their entirety without being interleaved
CHECK(stream.str() ==
"Failed to load cursor from pixels (invalid arguments)\n"
"Failed to load cursor from pixels (invalid arguments)\n"
"Failed to load cursor from pixels (invalid arguments)\n");
}
2022-04-22 06:00:51 +08:00
}