Skip to content

Commit

Permalink
Ready to deploy new version
Browse files Browse the repository at this point in the history
  • Loading branch information
alecksandr26 committed May 30, 2024
1 parent 17d4ef8 commit b1536ea
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 62 deletions.
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ C = cc
C_DEBUG_FLAGS = -ggdb -pedantic -Wall -fPIC
C_COMPILE_FLAGS = -O2 -DNDEBUG -fno-stack-protector -z execstack -no-pie -fPIC
C_FLAGS = $(C_DEBUG_FLAGS)
C_FLAGS_TRY_CATCH_LIB = -ltc
C_FLAGS_EXCEPT_LIB = -lexcept
C_FLAGS_WHOLE_ARCHIVE = -Wl,--whole-archive
C_FLAGS_NO_WHOLE_ARCHIVE = -Wl,--no-whole-archive
SHARED_LIB_FLAGS = -lunittest

N = nasm
N_DEBUG_FLAGS = -g -f elf64
N_COMPILE_FLAGS = -f elf64
N_FLAGS = $(N_DEBUG_FLAGS)

AR = ar cr
CF = clang-format -i

Expand All @@ -46,7 +51,7 @@ TEST_SRC_DIR = $(addprefix $(TEST_DIR)/, src)
TEST_BIN_DIR = $(addprefix $(TEST_DIR)/, bin)

OBJS = $(addprefix $(OBJ_DIR)/, debug.o info.o tcase.o suit.o valgrind.o command.o compile.o tfile.o dir.o \
hashdates.o rerun.o unittest.o)
hashdates.o rerun.o unittest.o stackjmp.o)

STATIC_LIBS = $(addprefix $(LIB_DIR)/, libunittest.a)
SHARED_LIBS = $(addprefix $(LIB_DIR)/, libunittest.so)
Expand All @@ -65,7 +70,7 @@ TESTS = $(addprefix $(TEST_BIN_DIR)/, test_running_testcase.out\
test_info.out)

.PHONY: all compile pkg upload-aur examples
all: $(OBJS) $(STATIC_LIBS) $(SHARED_LIBS) $(TESTS) $(EXAMPLES)
all: $(OBJS) $(STATIC_LIBS) $(SHARED_LIBS) $(TESTS) # $(EXAMPLES)

$(OBJ_DIR):
@echo Creating: $@
Expand All @@ -91,6 +96,10 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
@echo Compiling: $@
@$(C) $(C_FLAGS) -c $< -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.asm | $(OBJ_DIR)
@echo Compiling: $@
@$(N) $(N_FLAGS) $< -o $@

# Creates the library
$(LIB_DIR)/%.a: $(OBJS) | $(LIB_DIR)
@echo Archiving: $^ -o $@
Expand All @@ -116,13 +125,13 @@ examples: $(addprefix example_, $(notdir $(EXAMPLES)))
# Compile the tests
$(TEST_BIN_DIR)/test_%.out: $(TEST_SRC_DIR)/test_%.c $(SHARED_LIBS) | $(TEST_BIN_DIR)
@echo Compiling: $< -o $@
@$(C) $(C_FLAGS) -L./$(LIB_DIR) $< -o $@ $(SHARED_LIB_FLAGS)
@$(C) $(C_FLAGS) -L./$(LIB_DIR) $< -o $@ $(SHARED_LIB_FLAGS) $(C_FLAGS_EXCEPT_LIB)

# To run some tests
test_%.out: $(TEST_BIN_DIR)/test_%.out
@echo Running: $<
@export LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH && \
$(V) $(V_FLAGS) ./$<
DEBUGINFOD_URLS="https://debuginfod.archlinux.org" $(V) $(V_FLAGS) ./$<
@echo Passed

test: $(notdir $(TESTS))
Expand Down
31 changes: 16 additions & 15 deletions include/unittest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

#include "unittest_debug.h"
#include "unittest_def.h"
#include "unittest_stackjmp.h"

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <trycatch.h>
#include <except.h>

/* ASSERT: Evaluates expression, records failed test info, increments failure count,
outputs 'UnitTestInfo', and jumps back to execute more tests using setjmp(). */
Expand Down Expand Up @@ -250,26 +251,26 @@
} \
} while (0)

/* ASSERT_THROW: Verifies that statement throws the exception `EXCEPT`. */
#define ASSERT_THROW(STATEMENT, EXCEPT, ...) \
/* ASSERT_THROW: Verifies that statement throws the exception `E`. */
#define ASSERT_THROW(STATEMENT, E, ...) \
do { \
UnitTestInfo *info = &unitcase->info; \
UnitTestAssertionInfo *info_assert = \
&info->failed_asserts[info->number_failed_asserts]; \
_UNITTEST_CATCH_INFO(info_assert, __LINE__, \
_UNITTEST_FIRST(__VA_ARGS__ __VA_OPT__(, ) NULL), \
unitframe.current_test); \
try { \
TRY { \
STATEMENT; \
} catch(EXCEPT) { \
} EXCEPT(E) { \
; \
} otherwise { \
} ELSE { \
sprintf(info_assert->expr, "The statement \'%s\' never throws the exception \'%s\'", \
#STATEMENT, EXCEPT.reason); \
#STATEMENT, Except_raise_info.reason); \
info->number_failed_asserts++; \
unitcase->res = 'F'; \
jmpback(&unitframe.buf, unitframe.state + 1); \
} endtry; \
} END_TRY; \
} while (0)

/* ASSERT_ANY_THROW: Verifies that statement throws an exception of any kind. */
Expand All @@ -281,16 +282,16 @@
_UNITTEST_CATCH_INFO(info_assert, __LINE__, \
_UNITTEST_FIRST(__VA_ARGS__ __VA_OPT__(, ) NULL), \
unitframe.current_test); \
try { \
TRY { \
STATEMENT; \
sprintf(info_assert->expr, "The statement \'%s\' never throws any exception", \
#STATEMENT); \
info->number_failed_asserts++; \
unitcase->res = 'F'; \
jmpback(&unitframe.buf, unitframe.state + 1); \
} otherwise { \
} ELSE { \
; \
} endtry; \
} END_TRY; \
} while (0)

/* ASSERT_NO_THROW: Verifies that statement does not throw any exception. */
Expand All @@ -302,15 +303,15 @@
_UNITTEST_CATCH_INFO(info_assert, __LINE__, \
_UNITTEST_FIRST(__VA_ARGS__ __VA_OPT__(, ) NULL), \
unitframe.current_test); \
try { \
TRY { \
STATEMENT; \
} otherwise { \
} ELSE { \
sprintf(info_assert->expr, "The statement \'%s\' throws the exception \'%s\'", \
#STATEMENT, __except_frame.exception->reason); \
#STATEMENT, Except_raise_info.reason); \
info->number_failed_asserts++; \
unitcase->res = 'F'; \
jmpback(&unitframe.buf, unitframe.state + 1); \
} endtry; \
} END_TRY; \
} while (0)

/* The structure that captures all information needed for a failed assertion test. */
Expand Down
1 change: 1 addition & 0 deletions include/unittest_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#endif

#define LIB_UNITTEST "-lunittest"
#define LIB_EXCEPT "-lexcept"

#define VALGRIND "valgrind"
#define VALGRIND_PATH "/usr/bin/"
Expand Down
28 changes: 14 additions & 14 deletions include/unittest_expect.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,25 @@
} \
} while (0)

/* EXPECT_THROW: Verifies that statement throws the exception `EXCEPT`. */
#define EXPECT_THROW(STATEMENT, EXCEPT, ...) \
/* EXPECT_THROW: Verifies that statement throws the exception `E`. */
#define EXPECT_THROW(STATEMENT, E, ...) \
do { \
UnitTestInfo *info = &unitcase->info; \
UnitTestExpectatinoInfo *info_expect = \
&info->warning_expects[info->number_warning_expects]; \
_UNITTEST_CATCH_INFO(info_expect, __LINE__, \
_UNITTEST_FIRST(__VA_ARGS__ __VA_OPT__(, ) NULL), \
unitframe.current_test); \
try { \
TRY { \
STATEMENT; \
} catch(EXCEPT) { \
} EXCEPT(E) { \
; \
} otherwise { \
} ELSE { \
sprintf(info_expect->expr, "The statement \'%s\' never throws the exception \'%s\'", \
#STATEMENT, EXCEPT.reason); \
#STATEMENT, Except_raise_info.reason); \
info->number_warning_expects++; \
unitcase->res = 'W'; \
} endtry; \
} END_TRY; \
} while (0)

/* EXPECT_ANY_THROW: Verifies that statement throws an exception of any kind. */
Expand All @@ -270,15 +270,15 @@
_UNITTEST_CATCH_INFO(info_expect, __LINE__, \
_UNITTEST_FIRST(__VA_ARGS__ __VA_OPT__(, ) NULL), \
unitframe.current_test); \
try { \
TRY { \
STATEMENT; \
sprintf(info_expect->expr, "The statement \'%s\' never throws any exception", \
#STATEMENT); \
info->number_warning_expects++; \
unitcase->res = 'W'; \
} otherwise { \
} ELSE { \
; \
} endtry; \
} END_TRY; \
} while (0)

/* EXPECT_NO_THROW: Verifies that statement does not throw any exception. */
Expand All @@ -290,14 +290,14 @@
_UNITTEST_CATCH_INFO(info_expect, __LINE__, \
_UNITTEST_FIRST(__VA_ARGS__ __VA_OPT__(, ) NULL), \
unitframe.current_test); \
try { \
TRY { \
STATEMENT; \
} otherwise { \
} ELSE { \
sprintf(info_expect->expr, "The statement \'%s\' throws the exception \'%s\'", \
#STATEMENT, __except_frame.exception->reason); \
#STATEMENT, Except_raise_info.reason); \
info->number_warning_expects++; \
unitcase->res = 'W'; \
} endtry; \
} END_TRY; \
} while (0)

/* The structure that captures all information needed for a warning expect test. */
Expand Down
24 changes: 24 additions & 0 deletions include/unittest_stackjmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
@file stackjmp.h
@brief Module that catches the stackjmp of the thread execution of a program.
@author Erick Carrillo.
@copyright Copyright (C) 2023, Erick Alejandro Carrillo López, All rights reserved.
@license This project is released under the MIT License
*/

#ifndef STACKJMP_INCLUDED
#define STACKJMP_INCLUDED

#define B JmpBuf

typedef struct B B;
struct B {
unsigned long buf[6];
};

extern int stackjmp(B *buf);
extern void jmpback(B *buf, int val);

#undef B
#endif
3 changes: 2 additions & 1 deletion include/unittest_tcase.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#include "unittest_debug.h"
#include "unittest_def.h"
#include "unittest_info.h"
#include "unittest_stackjmp.h"

#include <stddef.h>
#include <trycatch.h>


/* The UnitTestCase struct represents a test case and includes information such as file
name, test
Expand Down
1 change: 0 additions & 1 deletion src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <trycatch.h>
#include <unistd.h>

/* get_command_str: To get the plain string version of the command. */
Expand Down
11 changes: 7 additions & 4 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <trycatch.h>
#include <except.h>
#include <unistd.h>
#include <wait.h>

Except UnittestEmptyFlags = {"Error empty flags passed"};
Except_T UnittestEmptyFlags = INIT_EXCEPT_T("Error empty flags passed");

char unittest_compile_extra_args_buff[COMPILING_FLAGS_SIZE];
char unittest_link_extra_args_buff[LINKING_FLAGS_SIZE];
Expand All @@ -33,7 +33,7 @@ void unittest_catch_extra_linking_flags(const char *flags)
{
size_t n = strlen(flags);

if (n == 0) throw(UnittestEmptyFlags);
if (n == 0) RAISE(UnittestEmptyFlags);

memset(unittest_link_extra_args_buff, 0, n);
strcpy(unittest_link_extra_args_buff, flags);
Expand All @@ -44,7 +44,7 @@ void unittest_catch_extra_compile_flags(const char *flags)
{
size_t n = strlen(flags);

if (n == 0) throw(UnittestEmptyFlags);
if (n == 0) RAISE(UnittestEmptyFlags);

memset(unittest_compile_extra_args_buff, 0, n);
strcpy(unittest_compile_extra_args_buff, flags);
Expand Down Expand Up @@ -101,11 +101,14 @@ int link_objs(const UnitCompiler *compiler_contex, const char *file, const char
#endif

unittest_attach_args(&command, LIB_UNITTEST);
unittest_attach_args(&command, LIB_EXCEPT);

if (unittest_link_extra_args)
unittest_attach_args(&command, unittest_link_extra_args_buff);

unittest_attach_args(&command, "-o");
unittest_attach_args(&command, out);

/* Recompile without unittest_recompile feature active */
unittest_attach_args(&command, "-DNUNITTEST_RECOMPILE");

Expand Down
1 change: 0 additions & 1 deletion src/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <trycatch.h>
#include <unistd.h>

bool unittest_fetched_files_name = false;
Expand Down
1 change: 0 additions & 1 deletion src/hashdates.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <trycatch.h>
#include <unistd.h>

bool dumped = false;
Expand Down
7 changes: 4 additions & 3 deletions src/rerun.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <trycatch.h>
#include <unistd.h>
#include <wait.h>

Expand Down Expand Up @@ -74,6 +73,7 @@ void unittest_recompile_without_tests(const UnitCompiler *compiler_contex)
unittest_attach_args(&command, "-L./lib");
#endif
unittest_attach_args(&command, LIB_UNITTEST);
unittest_attach_args(&command, LIB_EXCEPT);

unittest_attach_args(&command, "-o");
unittest_attach_args(&command, unittest_outfile);
Expand Down Expand Up @@ -103,8 +103,9 @@ void unittest_recompile_with_tests(const UnitCompiler *compiler_contex)
/* Change the last character test.c -> test.o */
objs[n_objs][strlen(objs[n_objs]) - 1] = 'o';

/* Check if there were changes */
if (unittest_tfile_needs_update(&unittest_tfiles[i])) {
/* Check if there were changes, or if it even exist that file */
if (unittest_tfile_needs_update(&unittest_tfiles[i])
|| access(objs[n_objs], F_OK) == -1) {
char source[FILE_SIZE_NAME];
memset(source, 0, FILE_SIZE_NAME);
strcat(source, unittest_testdir);
Expand Down
Loading

0 comments on commit b1536ea

Please sign in to comment.