diff --git a/examples/shader/Effect.hpp b/examples/shader/Effect.hpp index 4f9949d3a..69bec890c 100644 --- a/examples/shader/Effect.hpp +++ b/examples/shader/Effect.hpp @@ -62,7 +62,7 @@ protected: static const sf::Font& getFont() { - assert(s_font != nullptr); + assert(s_font != nullptr && "Cannot get font until setFont() is called"); return *s_font; } diff --git a/include/SFML/System/Angle.inl b/include/SFML/System/Angle.inl index a58023be7..4e91e0245 100644 --- a/include/SFML/System/Angle.inl +++ b/include/SFML/System/Angle.inl @@ -28,7 +28,7 @@ constexpr float pi = 3.141592654f; constexpr float positiveRemainder(float a, float b) { - assert(b > 0.0f); + assert(b > 0.0f && "Cannot calculate remainder with non-positive divisor"); const float val = a - static_cast(static_cast(a / b)) * b; if (val >= 0.f) return val; diff --git a/src/SFML/Audio/SoundFileReaderFlac.cpp b/src/SFML/Audio/SoundFileReaderFlac.cpp index 225bbf992..8c5e081c5 100644 --- a/src/SFML/Audio/SoundFileReaderFlac.cpp +++ b/src/SFML/Audio/SoundFileReaderFlac.cpp @@ -141,7 +141,7 @@ FLAC__StreamDecoderWriteStatus streamWrite(const FLAC__StreamDecoder*, sample = static_cast(buffer[j][i] >> 16); break; default: - assert(false); + assert(false && "Invalid bits per sample. Must be 8, 16, 24, or 32."); break; } @@ -271,7 +271,7 @@ bool SoundFileReaderFlac::open(InputStream& stream, Info& info) //////////////////////////////////////////////////////////// void SoundFileReaderFlac::seek(std::uint64_t sampleOffset) { - assert(m_decoder); + assert(m_decoder && "No decoder available. Call SoundFileReaderFlac::open() to create a new one."); // Reset the callback data (the "write" callback will be called) m_clientData.buffer = nullptr; @@ -300,7 +300,7 @@ void SoundFileReaderFlac::seek(std::uint64_t sampleOffset) //////////////////////////////////////////////////////////// std::uint64_t SoundFileReaderFlac::read(std::int16_t* samples, std::uint64_t maxCount) { - assert(m_decoder); + assert(m_decoder && "No decoder available. Call SoundFileReaderFlac::open() to create a new one."); // If there are leftovers from previous call, use it first const std::size_t left = m_clientData.leftovers.size(); diff --git a/src/SFML/Audio/SoundFileReaderOgg.cpp b/src/SFML/Audio/SoundFileReaderOgg.cpp index 4f2bec301..882a347a2 100644 --- a/src/SFML/Audio/SoundFileReaderOgg.cpp +++ b/src/SFML/Audio/SoundFileReaderOgg.cpp @@ -128,7 +128,7 @@ bool SoundFileReaderOgg::open(InputStream& stream, Info& info) //////////////////////////////////////////////////////////// void SoundFileReaderOgg::seek(std::uint64_t sampleOffset) { - assert(m_vorbis.datasource); + assert(m_vorbis.datasource && "Vorbis datasource is missing. Call SoundFileReaderOgg::open() to initialize it."); ov_pcm_seek(&m_vorbis, static_cast(sampleOffset / m_channelCount)); } @@ -137,7 +137,7 @@ void SoundFileReaderOgg::seek(std::uint64_t sampleOffset) //////////////////////////////////////////////////////////// std::uint64_t SoundFileReaderOgg::read(std::int16_t* samples, std::uint64_t maxCount) { - assert(m_vorbis.datasource); + assert(m_vorbis.datasource && "Vorbis datasource is missing. Call SoundFileReaderOgg::open() to initialize it."); // Try to read the requested number of samples, stop only on error or end of file std::uint64_t count = 0; diff --git a/src/SFML/Audio/SoundFileReaderWav.cpp b/src/SFML/Audio/SoundFileReaderWav.cpp index 251cafbc8..8b5fea3cb 100644 --- a/src/SFML/Audio/SoundFileReaderWav.cpp +++ b/src/SFML/Audio/SoundFileReaderWav.cpp @@ -141,7 +141,7 @@ bool SoundFileReaderWav::open(InputStream& stream, Info& info) //////////////////////////////////////////////////////////// void SoundFileReaderWav::seek(std::uint64_t sampleOffset) { - assert(m_stream); + assert(m_stream && "Input stream cannot be null. Call SoundFileReaderWav::open() to initialize it."); if (m_stream->seek(static_cast(m_dataStart + sampleOffset * m_bytesPerSample) == -1)) err() << "Failed to seek WAV sound stream" << std::endl; @@ -151,7 +151,7 @@ void SoundFileReaderWav::seek(std::uint64_t sampleOffset) //////////////////////////////////////////////////////////// std::uint64_t SoundFileReaderWav::read(std::int16_t* samples, std::uint64_t maxCount) { - assert(m_stream); + assert(m_stream && "Input stream cannot be null. Call SoundFileReaderWav::open() to initialize it."); std::uint64_t count = 0; const auto startPos = static_cast(m_stream->tell()); @@ -204,7 +204,7 @@ std::uint64_t SoundFileReaderWav::read(std::int16_t* samples, std::uint64_t maxC default: { - assert(false); + assert(false && "Invalid bytes per sample. Must be 1, 2, 3, or 4."); return 0; } } @@ -219,7 +219,7 @@ std::uint64_t SoundFileReaderWav::read(std::int16_t* samples, std::uint64_t maxC //////////////////////////////////////////////////////////// bool SoundFileReaderWav::parseHeader(Info& info) { - assert(m_stream); + assert(m_stream && "Input stream cannot be null. Call SoundFileReaderWav::open() to initialize it."); // If we are here, it means that the first part of the header // (the format) has already been checked diff --git a/src/SFML/Audio/SoundFileWriterOgg.cpp b/src/SFML/Audio/SoundFileWriterOgg.cpp index 7cbefeb9c..5900784cf 100644 --- a/src/SFML/Audio/SoundFileWriterOgg.cpp +++ b/src/SFML/Audio/SoundFileWriterOgg.cpp @@ -138,7 +138,7 @@ void SoundFileWriterOgg::write(const std::int16_t* samples, std::uint64_t count) { // Prepare a buffer to hold our samples float** buffer = vorbis_analysis_buffer(&m_state, bufferSize); - assert(buffer); + assert(buffer && "Vorbis buffer failed to allocate"); // Write the samples to the buffer, converted to float for (int i = 0; i < std::min(frameCount, bufferSize); ++i) diff --git a/src/SFML/Audio/SoundFileWriterWav.cpp b/src/SFML/Audio/SoundFileWriterWav.cpp index f2cc1fee5..ecf547308 100644 --- a/src/SFML/Audio/SoundFileWriterWav.cpp +++ b/src/SFML/Audio/SoundFileWriterWav.cpp @@ -110,7 +110,7 @@ bool SoundFileWriterWav::open(const std::filesystem::path& filename, unsigned in //////////////////////////////////////////////////////////// void SoundFileWriterWav::write(const std::int16_t* samples, std::uint64_t count) { - assert(m_file.good()); + assert(m_file.good() && "Most recent I/O operation failed"); while (count--) encode(m_file, *samples++); @@ -120,7 +120,7 @@ void SoundFileWriterWav::write(const std::int16_t* samples, std::uint64_t count) //////////////////////////////////////////////////////////// bool SoundFileWriterWav::writeHeader(unsigned int sampleRate, unsigned int channelCount) { - assert(m_file.good()); + assert(m_file.good() && "Most recent I/O operation failed"); // Write the main chunk ID char mainChunkId[4] = {'R', 'I', 'F', 'F'}; diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp index be5425e76..cc5d865bd 100644 --- a/src/SFML/Audio/SoundRecorder.cpp +++ b/src/SFML/Audio/SoundRecorder.cpp @@ -332,7 +332,7 @@ void SoundRecorder::launchCapturingThread() { m_isCapturing = true; - assert(!m_thread.joinable()); + assert(!m_thread.joinable() && "Capture thread is already running"); m_thread = std::thread(&SoundRecorder::record, this); } diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index 1e08f0ef0..1ebf38167 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -508,7 +508,7 @@ void SoundStream::launchStreamingThread(Status threadStartState) m_threadStartState = threadStartState; } - assert(!m_thread.joinable()); + assert(!m_thread.joinable() && "Background thread is still running"); m_thread = std::thread(&SoundStream::streamData, this); } diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index 08e0736da..b68ae39d8 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -137,7 +137,7 @@ Vector2f Shape::getGeometricCenter() const switch (count) { case 0: - assert(false); + assert(false && "Cannot calculate geometric center of shape with no points"); return Vector2f{}; case 1: return getPoint(0); diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index 13e474937..d7080bb61 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -130,7 +130,7 @@ FloatRect Sprite::getGlobalBounds() const //////////////////////////////////////////////////////////// void Sprite::draw(RenderTarget& target, const RenderStates& states) const { - assert(m_texture); + assert(m_texture && "Cannot use null texture. Call Sprite::setTexture() to initialize it."); RenderStates statesCopy(states); diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index 9462a7c47..4c51bc00f 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -453,8 +453,8 @@ void Texture::update(const std::uint8_t* pixels) //////////////////////////////////////////////////////////// void Texture::update(const std::uint8_t* pixels, const Vector2u& size, const Vector2u& dest) { - assert(dest.x + size.x <= m_size.x); - assert(dest.y + size.y <= m_size.y); + assert(dest.x + size.x <= m_size.x && "Destination x coordinate is outside of texture"); + assert(dest.y + size.y <= m_size.y && "Destination y coordinate is outside of texture"); if (pixels && m_texture) { @@ -497,8 +497,8 @@ void Texture::update(const Texture& texture) //////////////////////////////////////////////////////////// void Texture::update(const Texture& texture, const Vector2u& dest) { - assert(dest.x + texture.m_size.x <= m_size.x); - assert(dest.y + texture.m_size.y <= m_size.y); + assert(dest.x + texture.m_size.x <= m_size.x && "Destination x coordinate is outside of texture"); + assert(dest.y + texture.m_size.y <= m_size.y && "Destination y coordinate is outside of texture"); if (!m_texture || !texture.m_texture) return; @@ -630,8 +630,8 @@ void Texture::update(const Window& window) //////////////////////////////////////////////////////////// void Texture::update(const Window& window, const Vector2u& dest) { - assert(dest.x + window.getSize().x <= m_size.x); - assert(dest.y + window.getSize().y <= m_size.y); + assert(dest.x + window.getSize().x <= m_size.x && "Destination x coordinate is outside of texture"); + assert(dest.y + window.getSize().y <= m_size.y && "Destination y coordinate is outside of texture"); if (m_texture && window.setActive(true)) { diff --git a/src/SFML/Main/MainAndroid.cpp b/src/SFML/Main/MainAndroid.cpp index 86d8912eb..e80a7d9c3 100644 --- a/src/SFML/Main/MainAndroid.cpp +++ b/src/SFML/Main/MainAndroid.cpp @@ -84,7 +84,7 @@ int getAndroidApiLevel(ANativeActivity* activity) //////////////////////////////////////////////////////////// ActivityStates* retrieveStates(ANativeActivity* activity) { - assert(activity != nullptr); + assert(activity != nullptr && "Activity cannot be null"); // Hide the ugly cast we find in each callback function return static_cast(activity->instance); diff --git a/src/SFML/System/Android/Activity.cpp b/src/SFML/System/Android/Activity.cpp index e3f42f49c..8acace418 100644 --- a/src/SFML/System/Android/Activity.cpp +++ b/src/SFML/System/Android/Activity.cpp @@ -71,7 +71,8 @@ void resetActivity(ActivityStates* initializedStates) ActivityStates& getActivity() { ActivityStates* const states = getActivityStatesPtr(); - assert(states != nullptr); + assert(states != nullptr && + "Cannot dereference null activity states pointer. Call priv::resetActivity() to initialize it."); return *states; } diff --git a/src/SFML/Window/EglContext.cpp b/src/SFML/Window/EglContext.cpp index 5173abed5..a38cafd7c 100644 --- a/src/SFML/Window/EglContext.cpp +++ b/src/SFML/Window/EglContext.cpp @@ -355,7 +355,7 @@ EGLConfig EglContext::getBestConfig(EGLDisplay display, unsigned int bitsPerPixe } } - assert(bestScore < 0x7FFFFFFF); + assert(bestScore < 0x7FFFFFFF && "Failed to calculate best config"); return bestConfig; } diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index 7d2469ff5..8677b3a57 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -333,7 +333,7 @@ struct GlContext::TransientContext { // TransientContext should never be created if there is // already a context active on the current thread - assert(!GlContextImpl::CurrentContext::get().id); + assert(!GlContextImpl::CurrentContext::get().id && "Another context is active on the current thread"); // Lock ourselves so we don't create a new object if one doesn't already exist sharedContext = SharedContext::getWeakPtr().lock(); @@ -346,7 +346,7 @@ struct GlContext::TransientContext else { // GlResources exist, currentContextId not yet set - assert(sharedContext); + assert(sharedContext && "Shared context does not exist"); // Lock the shared context for temporary use sharedContextLock = std::unique_lock(sharedContext->mutex); @@ -520,14 +520,14 @@ void GlContext::acquireTransientContext() } // If we don't already have a context active on this thread the count should be 0 - assert(!currentContext.transientCount); + assert(!currentContext.transientCount && "Transient count cannot be non-zero"); // If currentContextId is not set, this must be the first // TransientContextLock on this thread, construct the state object TransientContext::get().emplace(); // Make sure a context is active at this point - assert(currentContext.id); + assert(currentContext.id && "Current context ID cannot be zero"); } @@ -537,7 +537,7 @@ void GlContext::releaseTransientContext() auto& currentContext = GlContextImpl::CurrentContext::get(); // Make sure a context was left active after acquireTransientContext() was called - assert(currentContext.id); + assert(currentContext.id && "Current context ID cannot be zero"); // Fast path if we already had a context active on this thread before acquireTransientContext() was called if (currentContext.transientCount) diff --git a/src/SFML/Window/Unix/Display.cpp b/src/SFML/Window/Unix/Display.cpp index 31528a6c2..457f201a2 100644 --- a/src/SFML/Window/Unix/Display.cpp +++ b/src/SFML/Window/Unix/Display.cpp @@ -82,7 +82,7 @@ void closeDisplay(Display* display) { const std::lock_guard lock(mutex); - assert(display == sharedDisplay); + assert(display == sharedDisplay && "Display must match shared display"); --referenceCount; if (referenceCount == 0) @@ -94,7 +94,7 @@ XIM openXim() { const std::lock_guard lock(mutex); - assert(sharedDisplay != nullptr); + assert(sharedDisplay != nullptr && "Shared display is null. Call priv::openDisplay() to initialize it."); if (referenceCountXIM == 0) { @@ -132,7 +132,7 @@ void closeXim(XIM xim) { const std::lock_guard lock(mutex); - assert(xim == sharedXIM); + assert(xim == sharedXIM && "XIM must match shared XIM"); --referenceCountXIM; diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index a160d36a9..1796af072 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -199,7 +199,8 @@ WglContext::~WglContext() //////////////////////////////////////////////////////////// GlFunctionPointer WglContext::getFunction(const char* name) { - assert(WglContextImpl::currentContext != nullptr); + assert(WglContextImpl::currentContext != nullptr && + "Current WGL context cannot be null. Call WglContext::makeCurrent() to initialize it."); // If we are using the generic GDI implementation, skip to loading directly from OpenGL32.dll since it doesn't support extensions if (!WglContextImpl::currentContext->m_isGeneric) diff --git a/test/System/FileInputStream.test.cpp b/test/System/FileInputStream.test.cpp index 5d4d4d305..fed2a4ba0 100644 --- a/test/System/FileInputStream.test.cpp +++ b/test/System/FileInputStream.test.cpp @@ -37,17 +37,17 @@ public: TemporaryFile(const std::string& contents) : m_path(getTemporaryFilePath()) { std::ofstream ofs(m_path); - assert(ofs); + assert(ofs && "Stream encountered an error"); ofs << contents; - assert(ofs); + assert(ofs && "Stream encountered an error"); } // Close and delete the generated file. ~TemporaryFile() { [[maybe_unused]] const bool removed = std::filesystem::remove(m_path); - assert(removed); + assert(removed && "m_path failed to be removed from filesystem"); } // Prevent copies. diff --git a/test/System/String.test.cpp b/test/System/String.test.cpp index aee50d44d..01813c03a 100644 --- a/test/System/String.test.cpp +++ b/test/System/String.test.cpp @@ -19,7 +19,7 @@ namespace template auto select(const std::basic_string& string16, const std::basic_string& string32) { - assert(string16 != string32); + assert(string16 != string32 && "Invalid to select between identical inputs"); if constexpr (sizeof(wchar_t) == 2) return string16; else