Adding unit tests.

This commit is contained in:
2023-11-01 23:52:36 -05:00
parent c4032b4a84
commit f573ff8366
14 changed files with 158 additions and 17 deletions

5
.gitmodules vendored
View File

@ -18,4 +18,7 @@
url = https://gitlab.freedesktop.org/freetype/freetype.git
[submodule "lib/libarchive"]
path = lib/libarchive
url = https://github.com/libarchive/libarchive
url = https://github.com/libarchive/libarchive
[submodule "lib/Catch2"]
path = lib/Catch2
url = https://github.com/catchorg/Catch2.git

View File

@ -34,6 +34,10 @@ project(Dawn
LANGUAGES C CXX
)
# Setup tests
add_executable(dawntests)
target_link_libraries(dawntests PRIVATE Catch2::Catch2WithMain)
# Add tools
add_subdirectory(tools)
@ -41,4 +45,10 @@ add_subdirectory(tools)
add_subdirectory(lib)
# Add Project Files
add_subdirectory(src)
add_subdirectory(src)
# Run tests
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(dawntests)

View File

@ -45,7 +45,11 @@ if(DAWN_TARGET_OPENAL)
add_subdirectory(AudioFile)
endif()
# Test
# Catch2 Testing Framework
set(catch2_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Catch2 CACHE INTERNAL ${DAWN_CACHE_TARGET})
add_subdirectory(Catch2)
# Emscripten (TESTING ONLY)
if(DEFINED DAWN_EMSCRIPTEN_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DAWN_EMSCRIPTEN_FLAGS}" CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DAWN_EMSCRIPTEN_FLAGS}" CACHE INTERNAL ${DAWN_CACHE_TARGET})

1
lib/Catch2 Submodule

Submodule lib/Catch2 added at a8cf3e6710

View File

@ -13,16 +13,9 @@ target_link_libraries(${DAWN_TARGET_NAME}
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${DAWN_SHARED_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
target_sources(${DAWN_TARGET_NAME}
PRIVATE
${DAWN_SHARED_SOURCES}
)
# Subdirs
add_subdirectory(assert)
add_subdirectory(asset)
@ -43,6 +36,17 @@ add_subdirectory(util)
# Definitions
target_compile_definitions(${DAWN_TARGET_NAME}
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_DEBUG_BUILD=${DAWN_DEBUG_BUILD}
)
# Tests
target_link_libraries(dawntests
PUBLIC
glm
freetype
)
target_include_directories(dawntests
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)

View File

@ -6,4 +6,10 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
assert.cpp
)
target_sources(dawntests
PRIVATE
assert.cpp
_test_.assert.cpp
)

View File

@ -0,0 +1,77 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include <catch2/catch_test_macros.hpp>
#include "assert.hpp"
TEST_CASE("assertTrue", "[assert]") {
SECTION("true") {
assertTrue(true, "This should not fail");
}
SECTION("false") {
REQUIRE_THROWS(assertTrue(false, "This should fail"));
}
}
TEST_CASE("assertFalse", "[assert]") {
SECTION("true") {
REQUIRE_THROWS(assertFalse(true, "This should fail"));
}
SECTION("false") {
assertFalse(false, "This should not fail");
}
}
TEST_CASE("assertUnreachable", "[assert]") {
REQUIRE_THROWS(assertUnreachable("This should fail all the time."));
}
TEST_CASE("assertNotNull", "[assert]") {
SECTION("nullptr") {
REQUIRE_THROWS(assertNotNull(nullptr, "This should fail"));
}
SECTION("NULL") {
REQUIRE_THROWS(assertNotNull(NULL, "This should fail"));
}
SECTION("not nullptr or NULL") {
assertNotNull((void*)0x1, "This should not fail");
}
}
TEST_CASE("assertNull", "[assert]") {
SECTION("nullptr") {
assertNull(nullptr, "This should not fail");
}
SECTION("NULL") {
assertNull(NULL, "This should not fail");
}
SECTION("not nullptr or NULL") {
REQUIRE_THROWS(assertNull((void*)0x1, "This should fail"));
}
}
TEST_CASE("assertMapHasKey", "[assert]") {
SECTION("has key") {
std::map<std::string, std::string> map;
map["key"] = "value";
assertMapHasKey(map, "key", "This should not fail");
}
SECTION("does not have key") {
std::map<std::string, std::string> map;
map["different_key"] = "Some other value";
REQUIRE_THROWS(assertMapHasKey(map, "key", "This should fail"));
}
}
TEST_CASE("assertDeprecated", "[assert]") {
REQUIRE_THROWS(assertDeprecated("This should fail"));
}

View File

@ -31,5 +31,5 @@ void assertTrueImplement(
vfprintf(stderr, message, argptr);
va_end(argptr);
fprintf(stderr, "\n");
abort();
throw std::runtime_error("Assert failed.");
}

View File

@ -32,9 +32,7 @@ extern "C" {
#include <sstream>
#include <string>
#include <functional>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <cstdarg>
#include <glm/glm.hpp>
#include <glm/vec3.hpp>

View File

@ -4,6 +4,8 @@
// https://opensource.org/licenses/MIT
#pragma once
#include <ft2build.h>
#include FT_FREETYPE_H
#include "util/flag.hpp"
#define TRUE_TYPE_CHAR_BEGIN 0x00

View File

@ -10,3 +10,14 @@ target_sources(${DAWN_TARGET_NAME}
UsageLock.cpp
Xml.cpp
)
# Tests
target_sources(dawntests
PRIVATE
memory.cpp
UsageLock.cpp
Xml.cpp
_test_.memory.cpp
)

View File

@ -0,0 +1,27 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include <catch2/catch_test_macros.hpp>
#include "util/memory.hpp"
#include "assert/assert.hpp"
TEST_CASE("memorycallMalloc", "[memory]") {
SECTION("memory isn't null") {
void *p = memoryCallMalloc(10);
REQUIRE(p != nullptr);
REQUIRE(p != NULL);
memoryCallFree(p);
}
SECTION("memory is writeable") {
void *p = memoryCallMalloc(10);
for(int i = 0; i < 10; i++) {
((char*)p)[i] = 'a';
}
REQUIRE(((char*)p)[0] == 'a');
REQUIRE(((char*)p)[9] == 'a');
memoryCallFree(p);
}
}

View File

@ -12,7 +12,6 @@ target_link_libraries(${DAWN_TARGET_NAME}
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${DAWN_SHARED_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)

View File

@ -6,7 +6,6 @@
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${DAWN_SHARED_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)