Skip to content

Commit

Permalink
Merge pull request #24 from sparky-game/feature/unit-test-darray
Browse files Browse the repository at this point in the history
Unit tests for `sk_darray` module
  • Loading branch information
iWas-Coder authored May 8, 2024
2 parents a47d3dd + 99f76de commit 88ff295
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ jobs:
paths-ignore: [vendor]
- name: build
run: make -j$(nproc)
- name: test
run: make check -j$(nproc)
- name: analyze
uses: github/codeql-action/analyze@v3
- name: build-editor
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "vendor/lua"]
path = vendor/lua
url = https://github.com/lua/lua
[submodule "vendor/carbon"]
path = vendor/carbon
url = https://github.com/sparky-game/carbon
112 changes: 84 additions & 28 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
##################
# === MACROS === #
##################
LAUNCHER_NAME = sk_launcher
GAME_NAME = sparky
EDITOR_NAME = sk_editor
LAUNCHER_NAME = sk_launcher
GAME_NAME = sparky
TEST_ENGINE_NAME = carbon
EDITOR_NAME = sk_editor

include config.mk

Expand All @@ -43,47 +44,54 @@ endif
FULL_VERSION = $(DIST_VERSION)$(DEVEXTRAVERSION)

# Pretty Printing Output (PPO)
PPO_MKDIR = MKDIR
PPO_CLEAN = CLEAN
PPO_RSC = RSC
PPO_CC = CC
PPO_AR = AR
PPO_LD = LD
PPO_HOSTCC = HOSTCC
PPO_HOSTLD = HOSTLD
PPO_MKDIR = MKDIR
PPO_CLEAN = CLEAN
PPO_RSC = RSC
PPO_CC = CC
PPO_AR = AR
PPO_LD = LD

# Dependencies
CHECKDEPS_BINS = mkdir mkfifo $(CC) ar rustc cargo jq
CHECKDEPS_HDRS = stdint.h stddef.h
CHECKDEPS_HDRS = stdint.h stddef.h X11/Xlib.h X11/Xcursor/Xcursor.h X11/extensions/Xrandr.h X11/extensions/Xinerama.h X11/extensions/XInput2.h
CHECKDEPS_TYPES = uint8_t int8_t uint16_t int16_t uint32_t int32_t uint64_t int64_t size_t long float double

# Directories
SRC_DIR = src
HDR_DIR = include
BUILD_ROOT_DIR = build
BUILD_DEBUG_DIR = $(BUILD_ROOT_DIR)/debug
BUILD_RELEASE_DIR = $(BUILD_ROOT_DIR)/release
HDR_DIR = include
SRC_DIR = src
TEST_DIR = test
BUILD_ROOT_DIR = build
BUILD_DEBUG_DIR = $(BUILD_ROOT_DIR)/debug
BUILD_RELEASE_DIR = $(BUILD_ROOT_DIR)/release
EXTRAS_BUILD_ROOT_DIR = $(BUILD_ROOT_DIR)/$(GAME_NAME)_extras
ifdef D
EXTRAS_BUILD_DIR = $(EXTRAS_BUILD_ROOT_DIR)/debug
BUILD_DIR = $(BUILD_DEBUG_DIR)
EXTRAS_BUILD_DIR = $(EXTRAS_BUILD_ROOT_DIR)/debug
BUILD_DIR = $(BUILD_DEBUG_DIR)
else
EXTRAS_BUILD_DIR = $(EXTRAS_BUILD_ROOT_DIR)/release
BUILD_DIR = $(BUILD_RELEASE_DIR)
EXTRAS_BUILD_DIR = $(EXTRAS_BUILD_ROOT_DIR)/release
BUILD_DIR = $(BUILD_RELEASE_DIR)
endif
TEST_BUILD_DIR = $(BUILD_DIR)/$(TEST_DIR)
VENDOR_DIR = vendor
RAYLIB_SRC_DIR = $(VENDOR_DIR)/raylib/$(SRC_DIR)
RAYLIB_BUILD_DIR = $(BUILD_DIR)/raylib
LUA_SRC_DIR = $(VENDOR_DIR)/lua
LUA_BUILD_DIR = $(BUILD_DIR)/lua

# Files
RAYLIB_SRCS := $(wildcard $(RAYLIB_SRC_DIR)/*.c)
RAYLIB_OBJS := $(patsubst $(RAYLIB_SRC_DIR)/%.c, $(RAYLIB_BUILD_DIR)/%.o, $(RAYLIB_SRCS))
LUA_SRCS := $(filter-out $(LUA_SRC_DIR)/onelua.c, $(wildcard $(LUA_SRC_DIR)/*.c))
LUA_OBJS := $(patsubst $(LUA_SRC_DIR)/%.c, $(LUA_BUILD_DIR)/%.o, $(LUA_SRCS))
LAUNCHER_SRCS := $(wildcard $(SRC_DIR)/$(LAUNCHER_NAME)*.rs)
SRCS := $(wildcard $(SRC_DIR)/*.c)
OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS))
EDITOR_SRCS := $(wildcard $(SRC_DIR)/$(EDITOR_NAME)*.rs)
RAYLIB_SRCS := $(wildcard $(RAYLIB_SRC_DIR)/*.c)
RAYLIB_OBJS := $(patsubst $(RAYLIB_SRC_DIR)/%.c, $(RAYLIB_BUILD_DIR)/%.o, $(RAYLIB_SRCS))
LUA_SRCS := $(filter-out $(LUA_SRC_DIR)/onelua.c, $(wildcard $(LUA_SRC_DIR)/*.c))
LUA_OBJS := $(patsubst $(LUA_SRC_DIR)/%.c, $(LUA_BUILD_DIR)/%.o, $(LUA_SRCS))
LAUNCHER_SRCS := $(wildcard $(SRC_DIR)/$(LAUNCHER_NAME)*.rs)
SRCS := $(wildcard $(SRC_DIR)/*.c)
OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS))
TEST_SRCS := $(wildcard $(TEST_DIR)/$(SRC_DIR)/*.c)
TEST_OBJS := $(patsubst $(TEST_DIR)/$(SRC_DIR)/%.c, $(TEST_BUILD_DIR)/%.o, $(TEST_SRCS))
TEST_DEPS_OBJS := $(filter-out $(BUILD_DIR)/$(GAME_NAME).o, $(OBJS))
EDITOR_SRCS := $(wildcard $(SRC_DIR)/$(EDITOR_NAME)*.rs)

# Build flags
MAKEFLAGS_JOBS := $(patsubst -j%, %, $(filter -j%, $(MAKEFLAGS)))
Expand Down Expand Up @@ -148,6 +156,12 @@ define CPPFLAGS
-isystem $(LUA_SRC_DIR) \
-I $(HDR_DIR)
endef
define TEST_CPPFLAGS
$(DISABLE_ASSERTS_OPTS) \
-isystem $(VENDOR_DIR)/$(TEST_ENGINE_NAME) \
-I $(HDR_DIR) \
-I $(TEST_DIR)/$(HDR_DIR)
endef
define RAYLIB_CFLAGS
-std=gnu99 \
$(HIDE_WARNS_OPTS) \
Expand All @@ -172,6 +186,17 @@ define CFLAGS
$(RELEASE_OPTS) \
$(MACOS_SPECIFIC_CFLAGS_OPTS)
endef
define TEST_CFLAGS
-std=c99 \
-Wall \
-Wextra \
-pedantic \
-Werror \
-fsanitize=address,undefined \
$(DEBUG_SYM_OPTS) \
$(RELEASE_OPTS) \
$(MACOS_SPECIFIC_CFLAGS_OPTS)
endef
define LDFLAGS
$(BUILDID_OPTS) \
$(HIDE_WARNS_OPTS) \
Expand All @@ -188,6 +213,18 @@ define LDFLAGS
$(MACOS_SPECIFIC_LDFLAGS_OPTS) \
$(OPENBSD_SPECIFIC_LDFLAGS_OPTS)
endef
define TEST_LDFLAGS
$(BUILDID_OPTS) \
$(HIDE_WARNS_OPTS) \
$(STRIP_OPTS) \
$(RELEASE_OPTS) \
-L $(BUILD_DIR) \
-l:libasan.a \
-l:libubsan.a \
-lraylib \
-llua \
-lm
endef

# Build output
LAUNCHER_OUT = $(EXTRAS_BUILD_DIR)/lib$(LAUNCHER_NAME).a
Expand All @@ -198,14 +235,15 @@ ifdef D
else
OUT = $(GAME_NAME)
endif
TEST_OUT = $(TEST_BUILD_DIR)/$(TEST_ENGINE_NAME)
EDITOR_OUT = $(EXTRAS_BUILD_DIR)/$(EDITOR_NAME)


###################
# === TARGETS === #
###################
.WAIT:
.PHONY: all checkdeps game editor clean mrproper version help
.PHONY: all checkdeps game check editor clean mrproper version help

all: checkdeps .WAIT $(BUILD_DIR) $(RAYLIB_BUILD_DIR) $(LUA_BUILD_DIR) game
@:
Expand Down Expand Up @@ -248,6 +286,10 @@ $(LUA_BUILD_DIR):
@echo " $(PPO_MKDIR) $@"
$(Q)mkdir -p $@

$(TEST_BUILD_DIR):
@echo " $(PPO_MKDIR) $@"
$(Q)mkdir -p $@

game: $(OUT)
@echo "INFO: $(OUT) is ready ($(FULL_VERSION))"

Expand Down Expand Up @@ -284,6 +326,18 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@echo " $(PPO_CC) $@"
$(Q)$(CC) $(CPPFLAGS) $(CFLAGS) -c -MD $< -o $@

check: checkdeps .WAIT $(BUILD_DIR) $(RAYLIB_BUILD_DIR) $(LUA_BUILD_DIR) $(TEST_BUILD_DIR) $(TEST_OUT)
@echo "INFO: $(TEST_OUT) is ready ($(FULL_VERSION))"
$(Q)./$(TEST_OUT)

$(TEST_OUT): $(RAYLIB_OUT) $(LUA_OUT) $(TEST_DEPS_OBJS) $(TEST_OBJS)
@echo " $(PPO_HOSTLD) $@"
$(Q)$(CC) $(TEST_DEPS_OBJS) $(TEST_OBJS) $(TEST_LDFLAGS) -o $@

$(TEST_BUILD_DIR)/%.o: $(TEST_DIR)/$(SRC_DIR)/%.c
@echo " $(PPO_HOSTCC) $@"
$(Q)$(CC) $(TEST_CPPFLAGS) $(TEST_CFLAGS) -c -MD $< -o $@

editor: checkdeps .WAIT $(EDITOR_OUT)
@echo "INFO: $(EDITOR_OUT) is ready ($(FULL_VERSION))"

Expand All @@ -298,6 +352,7 @@ $(EDITOR_OUT): $(EDITOR_SRCS)

-include $(BUILD_DIR)/*.d
-include $(LUA_BUILD_DIR)/*.d
-include $(TEST_BUILD_DIR)/*.d
-include $(RAYLIB_BUILD_DIR)/*.d
-include $(EXTRAS_BUILD_DIR)/*.d

Expand Down Expand Up @@ -330,6 +385,7 @@ help:
@echo " all :: Build all targets marked with [*]"
@echo "* checkdeps :: Check dependencies for build process"
@echo "* game :: Build the bare game"
@echo " check :: Build and run the test engine"
@echo " editor :: Build the editor"
@echo " clean :: Remove the 'build' directory"
@echo " mrproper :: Remove and cleans everything"
Expand Down
24 changes: 24 additions & 0 deletions test/include/sk_darray_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

void sk_darray_test_register(void);
29 changes: 29 additions & 0 deletions test/src/carbon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#define CARBON_IMPLEMENTATION
#include <carbon.h>
#include <sk_darray_test.h>

int main(void) {
sk_darray_test_register();
return carbon_test_manager_run();
}
95 changes: 95 additions & 0 deletions test/src/sk_darray_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <carbon.h>
#include <sk_darray.h>
#include <sk_defines.h>
#include <sk_darray_test.h>

static u8 sk_darray_test_create_destroy(void) {
sk_darray x = sk_darray_create(sizeof(int));
carbon_should_be(1, x.capacity);
carbon_should_be(sizeof(int), x.element_size);
carbon_should_be(0, x.curr_len);
carbon_should_be_true(x.data);
sk_darray_destroy(&x);
carbon_should_be(0, x.capacity);
carbon_should_be(0, x.element_size);
carbon_should_be(0, x.curr_len);
carbon_should_be(0, x.data);
return 1;
}

static u8 sk_darray_test_push_element(void) {
sk_darray x = sk_darray_create(sizeof(int));
int i = 7;
carbon_should_be_true(sk_darray_push(&x, &i));
carbon_should_be(1, x.curr_len);
sk_darray_destroy(&x);
return 1;
}

static u8 sk_darray_test_pop_element(void) {
sk_darray x = sk_darray_create(sizeof(int));
int i = 7, j;
sk_darray_push(&x, &i);
carbon_should_be_true(sk_darray_pop(&x, &j));
carbon_should_be(0, x.curr_len);
carbon_should_be(i, j);
sk_darray_destroy(&x);
return 1;
}

static u8 sk_darray_test_pop_element_from_empty_array(void) {
sk_darray x = sk_darray_create(sizeof(int));
int i = 7;
carbon_should_be_false(sk_darray_pop(&x, &i));
carbon_should_be(7, i);
sk_darray_destroy(&x);
return 1;
}

static u8 sk_darray_test_access_element(void) {
sk_darray x = sk_darray_create(sizeof(int));
int i = 7;
sk_darray_push(&x, &i);
int j = ((int *) x.data)[0];
carbon_should_be(i, j);
sk_darray_destroy(&x);
return 1;
}

static u8 sk_darray_test_resizing(void) {
sk_darray x = sk_darray_create(sizeof(int));
for (int i = 0; i < 100; ++i) sk_darray_push(&x, &i);
carbon_should_be(2 << 6, x.capacity);
sk_darray_destroy(&x);
return 1;
}

void sk_darray_test_register(void) {
CARBON_REGISTER_TEST(sk_darray_test_create_destroy);
CARBON_REGISTER_TEST(sk_darray_test_push_element);
CARBON_REGISTER_TEST(sk_darray_test_pop_element);
CARBON_REGISTER_TEST(sk_darray_test_pop_element_from_empty_array);
CARBON_REGISTER_TEST(sk_darray_test_access_element);
CARBON_REGISTER_TEST(sk_darray_test_resizing);
}
1 change: 1 addition & 0 deletions vendor/carbon
Submodule carbon added at 6efa44

0 comments on commit 88ff295

Please sign in to comment.