Initial commit

This commit is contained in:
2024-04-13 14:52:29 +03:00
commit ac5df0ebe9
20 changed files with 1821 additions and 0 deletions

22
unit/CMakeLists.txt Normal file
View File

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.10)
# Project and C standard configuration
project(bhunit LANGUAGES C)
set(CMAKE_C_STANDARD 90)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Disable extensions
set(CMAKE_C_EXTENSIONS OFF)
# Library code
set(BHUNIT_SOURCE
src/unit.c
)
set(BHUNIT_HEADER
include/bh/unit.h
)
# Library
add_library(bhunit STATIC ${BHUNIT_SOURCE} ${BHUNIT_HEADER})
target_include_directories(bhunit PUBLIC include)

24
unit/include/bh/unit.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef BHLIB_UNIT_H
#define BHLIB_UNIT_H
#include <stdio.h>
typedef int (*bh_unit_cb_t)(void);
#define bh_unit_assert(e) \
if (!(e)) { \
printf("%s:%d\t%s", __FILE__, __LINE__, #e); \
return -1; \
}
#define bh_unit_assert_delta(x, y, e) \
if ((((x)>(y))?((x)-(y)):((y)-(x)))>(e)) { \
printf("%s:%d\t%s", __FILE__, __LINE__, #x " == " #y); \
return -1; \
}
void bh_unit_add(const char *name, bh_unit_cb_t func);
int bh_unit_run(void);
#endif /* BHLIB_UNIT_H */

55
unit/src/unit.c Normal file
View File

@@ -0,0 +1,55 @@
#include <bh/unit.h>
#include <stdlib.h>
typedef struct bh_unit_s
{
struct bh_unit_s *next;
const char *name;
bh_unit_cb_t func;
} bh_unit_t;
static bh_unit_t *root = NULL;
void bh_unit_add(const char *name, bh_unit_cb_t func)
{
bh_unit_t *unit, *current;
unit = malloc(sizeof(*unit));
if (!unit)
return;
unit->name = name;
unit->func = func;
unit->next = NULL;
current = root;
while (current && current->next)
current = current->next;
if (current)
current->next = unit;
else
root = unit;
}
int bh_unit_run(void)
{
bh_unit_t *current;
printf("Running tests...\n");
current = root;
while (current)
{
printf("%s\n", current->name);
if (current->func())
{
printf("\tFAIL\n");
return -1;
}
printf("\tPASS\n");
current = current->next;
}
return 0;
}