Compare commits
5 Commits
cfc98e0137
...
7fb13f2439
Author | SHA1 | Date | |
---|---|---|---|
7fb13f2439 | |||
0b63bc746d | |||
6ee3c762df | |||
59f1c3eb58 | |||
55d0c25c90 |
27
compile
27
compile
@ -21,9 +21,16 @@ LIBFLAGS="-fPIC -shared"
|
|||||||
INCLUDE="\
|
INCLUDE="\
|
||||||
$(find src -type d | xargs -I{} echo -n "-I{} ") \
|
$(find src -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
"
|
"
|
||||||
|
TEST_INCLUDE="\
|
||||||
|
$(find tests -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
|
"
|
||||||
SRC="\
|
SRC="\
|
||||||
$(find src -type f -name "*.c" | xargs -I{} echo -n "{} ") \
|
$(find src -type f -name "*.c" | xargs -I{} echo -n "{} ") \
|
||||||
"
|
"
|
||||||
|
TEST_SRC="\
|
||||||
|
$(find tests -type f -name "*.c" | xargs -I{} echo -n "{} ") \
|
||||||
|
"
|
||||||
|
|
||||||
if [[ $BUILD_TYPE == "release" ]]; then
|
if [[ $BUILD_TYPE == "release" ]]; then
|
||||||
CFLAGS+="-O3"
|
CFLAGS+="-O3"
|
||||||
else
|
else
|
||||||
@ -31,9 +38,29 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
OUT="libwapp.so"
|
OUT="libwapp.so"
|
||||||
|
TEST_OUT="./wapptest"
|
||||||
|
|
||||||
|
# Compile tests
|
||||||
|
if [[ $(echo $TEST_SRC | xargs) != "" ]]; then
|
||||||
|
(set -x ; $CC $CFLAGS $INCLUDE $TEST_INCLUDE $SRC $TEST_SRC -o $TEST_OUT)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Run tests and exit on failure
|
||||||
|
if [[ -f $TEST_OUT ]]; then
|
||||||
|
$TEST_OUT
|
||||||
|
STATUS="$?"
|
||||||
|
|
||||||
|
rm $TEST_OUT
|
||||||
|
|
||||||
|
if [[ $STATUS != "0" ]]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Compile library
|
||||||
(set -x ; $CC $CFLAGS $LIBFLAGS $INCLUDE $SRC -o $OUT)
|
(set -x ; $CC $CFLAGS $LIBFLAGS $INCLUDE $SRC -o $OUT)
|
||||||
|
|
||||||
|
# Compile test.c if it exists
|
||||||
if [[ -f ./test.c ]]; then
|
if [[ -f ./test.c ]]; then
|
||||||
(set -x ; $CC $CFLAGS $INCLUDE $SRC ./test.c -o test)
|
(set -x ; $CC $CFLAGS $INCLUDE $SRC ./test.c -o test)
|
||||||
fi
|
fi
|
||||||
|
@ -1,62 +1,63 @@
|
|||||||
#ifndef PLATFORM_H
|
#ifndef PLATFORM_H
|
||||||
#define PLATFORM_H
|
#define PLATFORM_H
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#if defined(__ANDROID__)
|
#if defined(__ANDROID__)
|
||||||
#define WAPP_PLATFORM_ANDROID
|
#define WAPP_PLATFORM_ANDROID
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__FreeBSD__)
|
#elif defined(__FreeBSD__)
|
||||||
#define WAPP_PLATFORM_FREE_BSD
|
#define WAPP_PLATFORM_FREE_BSD
|
||||||
#define WAPP_PLATFORM_BSD
|
#define WAPP_PLATFORM_BSD
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__NetBSD__)
|
#elif defined(__NetBSD__)
|
||||||
#define WAPP_PLATFORM_NET_BSD
|
#define WAPP_PLATFORM_NET_BSD
|
||||||
#define WAPP_PLATFORM_BSD
|
#define WAPP_PLATFORM_BSD
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__OpenBSD__)
|
#elif defined(__OpenBSD__)
|
||||||
#define WAPP_PLATFORM_OPEN_BSD
|
#define WAPP_PLATFORM_OPEN_BSD
|
||||||
#define WAPP_PLATFORM_BSD
|
#define WAPP_PLATFORM_BSD
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__DragonFly__)
|
#elif defined(__DragonFly__)
|
||||||
#define WAPP_PLATFORM_DRAGON_FLY
|
#define WAPP_PLATFORM_DRAGON_FLY
|
||||||
#define WAPP_PLATFORM_BSD
|
#define WAPP_PLATFORM_BSD
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__bsdi__)
|
#elif defined(__bsdi__)
|
||||||
#define WAPP_PLATFORM_BSD
|
#define WAPP_PLATFORM_BSD
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__linux__) || defined(linux) || defined(__linux) || \
|
#elif defined(__linux__) || defined(linux) || defined(__linux) || defined(__gnu_linux__)
|
||||||
defined(__gnu_linux__)
|
#define WAPP_PLATFORM_LINUX
|
||||||
#define WAPP_PLATFORM_LINUX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#define WAPP_PLATFORM_POSIX
|
|
||||||
#elif defined(__GNU__) || defined(__gnu_hurd__)
|
#elif defined(__GNU__) || defined(__gnu_hurd__)
|
||||||
#define WAPP_PLATFORM_GNU
|
#define WAPP_PLATFORM_GNU
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif defined(__APPLE__) || defined(__MACH__)
|
#elif defined(__APPLE__) || defined(__MACH__)
|
||||||
#include <TargetConditionals.h>
|
#include <TargetConditionals.h>
|
||||||
#if TARGET_OS_IPHONE
|
#if TARGET_OS_IPHONE
|
||||||
#define WAPP_PLATFORM_IOS
|
#define WAPP_PLATFORM_IOS
|
||||||
#define WAPP_PLATFORM_APPLE
|
#define WAPP_PLATFORM_APPLE
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#elif TARGET_OS_MAC
|
#elif TARGET_OS_MAC
|
||||||
#define WAPP_PLATFORM_MACOS
|
#define WAPP_PLATFORM_MACOS
|
||||||
#define WAPP_PLATFORM_APPLE
|
#define WAPP_PLATFORM_APPLE
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#else
|
#else
|
||||||
#error "Unrecognised Apple platform"
|
#error "Unrecognised Apple platform"
|
||||||
#endif
|
#endif
|
||||||
#elif defined(_WIN64)
|
#elif defined(_WIN64)
|
||||||
#define WAPP_PLATFORM_WINDOWS64
|
#define WAPP_PLATFORM_WINDOWS64
|
||||||
#define WAPP_PLATFORM_WINDOWS
|
#define WAPP_PLATFORM_WINDOWS
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
#define WAPP_PLATFORM_WINDOWS32
|
#define WAPP_PLATFORM_WINDOWS32
|
||||||
#define WAPP_PLATFORM_WINDOWS
|
#define WAPP_PLATFORM_WINDOWS
|
||||||
#elif defined(__CYGWIN__)
|
#elif defined(__CYGWIN__)
|
||||||
#define WAPP_PLATFORM_CYGWIN
|
#define WAPP_PLATFORM_CYGWIN
|
||||||
#define WAPP_PLATFORM_WINDOWS
|
#define WAPP_PLATFORM_WINDOWS
|
||||||
#elif defined(__unix__) || defined(__unix)
|
#elif defined(__unix__) || defined(__unix)
|
||||||
#define WAPP_PLATFORM_UNIX
|
#define WAPP_PLATFORM_UNIX
|
||||||
#define WAPP_PLATFORM_POSIX
|
#define WAPP_PLATFORM_POSIX
|
||||||
#else
|
#else
|
||||||
#error "Unrecognised platform"
|
#error "Unrecognised platform"
|
||||||
#endif
|
#endif
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
#endif // !PLATFORM_H
|
#endif // !PLATFORM_H
|
||||||
|
48
src/termcolour/termcolour.h
Normal file
48
src/termcolour/termcolour.h
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#ifndef TERM_COLOUR_H
|
||||||
|
#define TERM_COLOUR_H
|
||||||
|
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
#if defined(WAPP_PLATFORM_WINDOWS)
|
||||||
|
#define TERM_COLOUR_CLEAR ""
|
||||||
|
#define TERM_COLOUR_BOLD ""
|
||||||
|
#define TERM_COLOUR_FG_BLACK ""
|
||||||
|
#define TERM_COLOUR_FG_RED ""
|
||||||
|
#define TERM_COLOUR_FG_GREEN ""
|
||||||
|
#define TERM_COLOUR_FG_YELLOW ""
|
||||||
|
#define TERM_COLOUR_FG_BLUE ""
|
||||||
|
#define TERM_COLOUR_FG_MAGENTA ""
|
||||||
|
#define TERM_COLOUR_FG_CYAN ""
|
||||||
|
#define TERM_COLOUR_FG_WHITE ""
|
||||||
|
#define TERM_COLOUR_FG_BR_BLACK ""
|
||||||
|
#define TERM_COLOUR_FG_BR_RED ""
|
||||||
|
#define TERM_COLOUR_FG_BR_GREEN ""
|
||||||
|
#define TERM_COLOUR_FG_BR_YELLOW ""
|
||||||
|
#define TERM_COLOUR_FG_BR_BLUE ""
|
||||||
|
#define TERM_COLOUR_FG_BR_MAGENTA ""
|
||||||
|
#define TERM_COLOUR_FG_BR_CYAN ""
|
||||||
|
#define TERM_COLOUR_FG_BR_WHITE ""
|
||||||
|
#elif defined(WAPP_PLATFORM_POSIX)
|
||||||
|
#define TERM_COLOUR_CLEAR "\033[0m"
|
||||||
|
#define TERM_COLOUR_BOLD "\033[1m"
|
||||||
|
#define TERM_COLOUR_FG_BLACK "\033[30m"
|
||||||
|
#define TERM_COLOUR_FG_RED "\033[31m"
|
||||||
|
#define TERM_COLOUR_FG_GREEN "\033[32m"
|
||||||
|
#define TERM_COLOUR_FG_YELLOW "\033[33m"
|
||||||
|
#define TERM_COLOUR_FG_BLUE "\033[34m"
|
||||||
|
#define TERM_COLOUR_FG_MAGENTA "\033[35m"
|
||||||
|
#define TERM_COLOUR_FG_CYAN "\033[36m"
|
||||||
|
#define TERM_COLOUR_FG_WHITE "\033[37m"
|
||||||
|
#define TERM_COLOUR_FG_BR_BLACK "\033[90m"
|
||||||
|
#define TERM_COLOUR_FG_BR_RED "\033[91m"
|
||||||
|
#define TERM_COLOUR_FG_BR_GREEN "\033[92m"
|
||||||
|
#define TERM_COLOUR_FG_BR_YELLOW "\033[93m"
|
||||||
|
#define TERM_COLOUR_FG_BR_BLUE "\033[94m"
|
||||||
|
#define TERM_COLOUR_FG_BR_MAGENTA "\033[95m"
|
||||||
|
#define TERM_COLOUR_FG_BR_CYAN "\033[96m"
|
||||||
|
#define TERM_COLOUR_FG_BR_WHITE "\033[97m"
|
||||||
|
#endif
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
#endif // !TERM_COLOUR_H
|
48
src/tester/tester.c
Normal file
48
src/tester/tester.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include "tester.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "termcolour/termcolour.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
internal void print_test_result(TestFuncResult result);
|
||||||
|
|
||||||
|
void run_tests(TestFunc *func1, ...) {
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
print_test_result(func1());
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start(args, func1);
|
||||||
|
|
||||||
|
TestFunc *func = NULL;
|
||||||
|
|
||||||
|
while ((func = va_arg(args, TestFunc *))) {
|
||||||
|
TestFuncResult result = func();
|
||||||
|
print_test_result(result);
|
||||||
|
|
||||||
|
if (!result.success) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void print_test_result(TestFuncResult result) {
|
||||||
|
const char *colour;
|
||||||
|
const char *result_text;
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
colour = TERM_COLOUR_FG_BR_GREEN;
|
||||||
|
result_text = "PASSED";
|
||||||
|
} else {
|
||||||
|
colour = TERM_COLOUR_FG_BR_RED;
|
||||||
|
result_text = "FAILED";
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("[%s%s%s%s] %s\n", colour, TERM_COLOUR_BOLD, result_text,
|
||||||
|
TERM_COLOUR_CLEAR, result.name);
|
||||||
|
}
|
26
src/tester/tester.h
Normal file
26
src/tester/tester.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef TESTER_H
|
||||||
|
#define TESTER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#define TEST_RESULT(BOOL) ((TestFuncResult){.name = __func__, .success = BOOL})
|
||||||
|
|
||||||
|
typedef struct test_func_result TestFuncResult;
|
||||||
|
struct test_func_result {
|
||||||
|
const char *name;
|
||||||
|
bool success;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef TestFuncResult(TestFunc)(void);
|
||||||
|
|
||||||
|
void run_tests(TestFunc *func1, ...);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !TESTER_H
|
59
tests/arena/test_arena.c
Normal file
59
tests/arena/test_arena.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include "test_arena.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
#include "mem_utils.h"
|
||||||
|
#include "tester.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define ARENA_CAPACITY 1024
|
||||||
|
|
||||||
|
internal Arena *arena = NULL;
|
||||||
|
internal u64 count = 20;
|
||||||
|
internal i32 *array = NULL;
|
||||||
|
|
||||||
|
TestFuncResult test_arena_init(void) {
|
||||||
|
bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY,
|
||||||
|
WAPP_MEM_ALLOC_RESERVE, false);
|
||||||
|
|
||||||
|
return TEST_RESULT(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) {
|
||||||
|
array = wapp_mem_arena_alloc(arena, count * sizeof(i32));
|
||||||
|
bool result = array != NULL;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < count; ++i) {
|
||||||
|
array[i] = i * 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TEST_RESULT(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_arena_alloc_fails_when_over_capacity(void) {
|
||||||
|
u8 *bytes = wapp_mem_arena_alloc(arena, ARENA_CAPACITY * 2);
|
||||||
|
bool result = bytes == NULL;
|
||||||
|
|
||||||
|
return TEST_RESULT(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_arena_clear(void) {
|
||||||
|
wapp_mem_arena_clear(arena);
|
||||||
|
bool result = true;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < count; ++i) {
|
||||||
|
if (array[i] != 0) {
|
||||||
|
result = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return TEST_RESULT(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TestFuncResult test_arena_destroy(void) {
|
||||||
|
wapp_mem_arena_destroy(&arena);
|
||||||
|
bool result = arena == NULL;
|
||||||
|
|
||||||
|
return TEST_RESULT(result);
|
||||||
|
}
|
20
tests/arena/test_arena.h
Normal file
20
tests/arena/test_arena.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef TEST_ARENA_H
|
||||||
|
#define TEST_ARENA_H
|
||||||
|
|
||||||
|
#include "tester.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
TestFuncResult test_arena_init(void);
|
||||||
|
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void);
|
||||||
|
TestFuncResult test_arena_alloc_fails_when_over_capacity(void);
|
||||||
|
TestFuncResult test_arena_clear(void);
|
||||||
|
TestFuncResult test_arena_destroy(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !TEST_ARENA_H
|
11
tests/wapptest.c
Normal file
11
tests/wapptest.c
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "test_arena.h"
|
||||||
|
#include "tester.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
run_tests(test_arena_init, test_arena_alloc_succeeds_when_within_capacity,
|
||||||
|
test_arena_alloc_fails_when_over_capacity, test_arena_clear,
|
||||||
|
test_arena_destroy);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user