Skip to content

Commit

Permalink
Tweak initialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 16, 2023
1 parent 42ae724 commit fdb7f26
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/centurion/initialization/sdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SDL final {

[[nodiscard]] explicit SDL(const uint32 flags = SDL_INIT_EVERYTHING)
{
if (SDL_Init(flags) < 0) {
if (SDL_Init(flags) != 0) {
throw SDLError {};
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/centurion/initialization/sdl_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SDLImage final {
IMG_INIT_TIF | IMG_INIT_WEBP |
IMG_INIT_JXL | IMG_INIT_AVIF)
{
if (IMG_Init(flags) == 0) {
if (IMG_Init(flags) != flags) {
throw SDLImageError {};
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/centurion/initialization/sdl_mixer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SDLMixer final {

[[nodiscard]] explicit SDLMixer(const SDLMixerConfig& cfg = {})
{
if (Mix_Init(cfg.flags) == 0) {
if (Mix_Init(cfg.flags) != cfg.flags) {
throw SDLMixerError {};
}

Expand Down
95 changes: 95 additions & 0 deletions tests/unit-tests/src/common/errors_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* MIT License
*
* Copyright (c) 2019-2023 Albin Johansson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <centurion/common/errors.hpp>
#include <gtest/gtest.h>

TEST(Errors, ErrorWithNoReason)
{
const cen::Error error;
EXPECT_STREQ(error.what(), "?");
}

TEST(Errors, ErrorWithReason)
{
const char* reason = "This is a test";
const cen::Error error {reason};
EXPECT_STREQ(error.what(), reason);
}

TEST(Errors, SDLError)
{
const char* reason = "SDLError test";
SDL_SetError(reason);

const cen::SDLError error;
EXPECT_STREQ(error.what(), reason);

SDL_ClearError();
}

#if CEN_USE_SDL_IMAGE

TEST(Errors, SDLImageError)
{
const char* reason = "SDLImageError test";
IMG_SetError(reason);

const cen::SDLImageError error;
EXPECT_STREQ(error.what(), reason);

SDL_ClearError();
}

#endif // CEN_USE_SDL_IMAGE

#if CEN_USE_SDL_MIXER

TEST(Errors, SDLMixerError)
{
const char* reason = "SDLMixerError test";
Mix_SetError(reason);

const cen::SDLMixerError error;
EXPECT_STREQ(error.what(), reason);

SDL_ClearError();
}

#endif // CEN_USE_SDL_MIXER

#if CEN_USE_SDL_TTF

TEST(Errors, SDLTTFError)
{
const char* reason = "SDLTTFError test";
TTF_SetError(reason);

const cen::SDLTTFError error;
EXPECT_STREQ(error.what(), reason);

SDL_ClearError();
}

#endif // CEN_USE_SDL_TTF
25 changes: 19 additions & 6 deletions tests/unit-tests/src/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,30 @@
* SOFTWARE.
*/

#include <exception> // exception
#include <cstdlib> // EXIT_FAILURE
#include <iostream> // cerr

#include <gtest/gtest.h>

#include <centurion/centurion.hpp>

auto main(int argc, char* argv[]) -> int
{
const cen::SDL sdl;
const cen::SDLImage sdl_image;
const cen::SDLMixer sdl_mixer;
const cen::SDLTTF sdl_ttf;
try {
cen::SDLMixerConfig mixer_cfg = {};
mixer_cfg.flags = MIX_INIT_MP3;

const cen::SDL sdl;
const cen::SDLImage sdl_image {IMG_INIT_PNG};
const cen::SDLMixer sdl_mixer {mixer_cfg};
const cen::SDLTTF sdl_ttf;

testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
catch (const std::exception& e) {
std::cerr << "Unit tests threw exception: " << e.what() << '\n';
return EXIT_FAILURE;
}
}

0 comments on commit fdb7f26

Please sign in to comment.