Skip to content

Commit

Permalink
Code patched ready for a patch release v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alecksandr26 committed Jan 28, 2024
1 parent b9482a1 commit 31f3ad3
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 42 deletions.
4 changes: 2 additions & 2 deletions PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

# Move to the build directory
pkgname=unittest-c
pkgver=1.3.0
pkgrel=7
pkgver=1.3.1
pkgrel=8
epoch=
pkgdesc="unittest c is a fast and simple macro-based unit testing framework for C.
It's inspired by the Python unittest module and designed to reduce boilerplate code.
Expand Down
10 changes: 9 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@
---- 2024/01/27 ----
- Develop a way to run isolated each test, not just each testcase [ ]
- Do a refactor of the info.c file, and also the whole comments [ ]
- Document the whole asserts and expects [ ]
- Document the whole asserts and expects [X]
- Solve the printing bug using the macro INFO_VAR with structures [ ]
- Change the INFO_EXPR macro to look more pretty [ ]
- Add the feture to attach flags to valgrind [ ]
- Integrate a testrunnr [ ]

---- URGENT 2024/01/278 ----
- Solve the bug of include testcases and paths from other files [X]
- Solve the bug of redefinition in recompilation time [X]

24 changes: 24 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
C = cc
CFLAGS = -Wall -Werror
LDFLAGS = -lunittest
TEST_DIR = dir
TEST_OBJECTS = $(addprefix $(TEST_DIR)/, simpletest.o)

all: testrunner

testrunner: $(TEST_OBJECTS) testrunner.o
$(C) $(LDFLAGS) $(TEST_OBJECTS) testrunner.o -o testrunner

$(TEST_DIR)/simpletest.o: $(TEST_DIR)/simpletest.c
$(C) $(CFLAGS) -c $(TEST_DIR)/simpletest.c -o $(TEST_DIR)/simpletest.o

testrunner.o: testrunner.c
$(C) $(CFLAGS) -c testrunner.c -o testrunner.o

# Clean all the compiled things
clean:
rm -f $(TEST_OBJECTS) testrunner.o testrunner

# To run the tests
test: testrunner
./testrunner
63 changes: 63 additions & 0 deletions example/dir/simpletest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// simpletest.c
#include <unittest.h>

TESTCASE(SimpleTest) {
// Set up any boilerplate code for all tests in the test case
int var = 1;

// Define individual tests using the TEST macro
TEST(Test1) {
// Use ASSERT macro to check if var is equal to 1
ASSERT_EQ(var, 1, "It should be one");
}

TEST(Test2) {
ASSERT(var > 0, "It should be greater than zero");
}

TEST(Test3) {
ASSERT(var < 2, "It should be lesser than 2");
}

TEST(Test4) {
ASSERT(var != 0, "It should not be zero");
}
} ENDTESTCASE


TESTCASE(MyTestCases1)
{
// Set up any boilerplate code for all tests in the test case
int x = 42;

// Define individual tests using the TEST macro
TEST(Test1) {
// Use ASSERT macro to check if x equals 42
ASSERT(x == 42, "x should equal 42");
}

TEST(Test2) {
// Use ASSERT macro to check if x is less than 100
ASSERT(x < 100, "x should be less than 100");
}

} ENDTESTCASE

TESTCASE(MyTestCases2)
{
// Set up any boilerplate code for all tests in the test case
int y = 100;

// Define individual tests using the TEST macro
TEST(Test3) {
// Use ASSERT macro to check if y equals 100
ASSERT(y == 100, "y should equal 100");
}

TEST(Test4) {
// Use ASSERT macro to check if y is greater than or equal to 50
ASSERT(y >= 50, "y should be greater than or equal to 50");
}
} ENDTESTCASE

SUIT(MySuit, MyTestCases1, MyTestCases2);
15 changes: 15 additions & 0 deletions example/dir/test_foo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


/* Simple example to test the foo */

#include <unittest.h>

TESTCASE(TestingFoo) {

extern int foo(void);

TEST(FooReturn) {
ASSERT_EQ(foo(), 1, "foo must to return 1");
}
} ENDTESTCASE

16 changes: 16 additions & 0 deletions example/obj/foo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include <string.h>
#include <malloc.h>

/* Foo function */

int x;

int foo(void)
{
void *ptr = malloc(sizeof(x));

return 1;
}


51 changes: 40 additions & 11 deletions example/simple_example.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,70 @@


/* To compile this exmaple run:
cc simple_example.c -lunittest
cc simple_example.c -lunittest && ./a.out
*/

#include <unittest.h>

#include <math.h>
#include <malloc.h>
#include <string.h>


TESTCASE(TestCaseSimple) {
TEST(TestSimple) {
TESTCASE(MyTestCase1) {
TEST(TestSqrt2) {
double x = sqrt(2.0);
INFO_VAR(x);
ASSERT_NEAR(x, 1.414, 1e-3, "Testing the sqrt of 2");
}

void *null_ptr, *non_void_ptr;
void *null_ptr, *non_null_ptr;

null_ptr = NULL;
non_void_ptr = malloc(100);
non_null_ptr = malloc(100);

TEST(TestVoidPtr) {
ASSERT_EQ(null_ptr, non_void_ptr, "Cheking null ptr");
TEST(TestingTheAssert_eq) {
INFO("%p", null_ptr);
INFO("%p", non_null_ptr);
ASSERT_EQ(null_ptr, non_null_ptr, "Cheking null ptr");
}


free(non_void_ptr);
free(non_null_ptr);
} ENDTESTCASE


TESTCASE(MyTestCase2) {

struct Person {
char name[100];
int age;
};

TEST(TestingNonEq) {
struct Person p1;
memset(&p1, 0, sizeof(struct Person));
p1.age = 10;
strcpy(p1.name, "Pedrito");


struct Person p2;
memset(&p2, 0, sizeof(struct Person));
p2.age = 10;
strcpy(p2.name, "Pedrito");

INFO_EXPR(strcmp(p1.name, p2.name));

EXPECT_NEQ(p1, p2, "Should throw a warning");
}
} ENDTESTCASE


SUIT(MySuit, MyTestCase1, MyTestCase2);

int main(void)
{
RUN(TestCaseSimple);

RUN(MySuit);
return unittest_ret;
}

Expand Down
Binary file added example/testrunner
Binary file not shown.
22 changes: 22 additions & 0 deletions example/testrunner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* To compile run:
cc -o testrunner testrunner.c dir/simpletest.c -lunittest && ./testrunner
*/

#include <stdio.h>

#define TEST_DIR "dir/"
#include <unittest.h>

int main(void)
{
// Include a testcase
INCLUDE_TESTCASE("simpletest.c", SimpleTest);

// Include a suit
INCLUDE_SUIT("simpletest.c", MySuit);

/* Run the selected testcases or suits */
RUN(SimpleTest, MySuit);

return unittest_ret;
}
Binary file added example/testrunner2
Binary file not shown.
34 changes: 34 additions & 0 deletions example/testrunner2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* To compile run:
cc -ggdb -c obj/foo.c -o obj/foo.o && cc -ggdb testrunner2.c -o testrunner2 -lunittest && ./testrunner2
*/

#include <stdio.h>

#define TEST_DIR "dir/"
#define UNITTEST_RECOMPILE
#include <unittest.h>


int main(void)
{
INCLUDE_TESTCASE("simpletest.c", SimpleTest);
INCLUDE_SUIT("simpletest.c", MySuit);
INCLUDE_TESTCASE("test_foo.c", TestingFoo);

ATTACH_EXTRA_COMPILE_FLAGS("-ggdb");

ATTACH_EXTRA_LINKING_FLAGS("obj/foo.o");

ACTIVE_VALGRIND(true);

/* Run the selected testcases or suits */
RUN(SimpleTest, MySuit, TestingFoo);

return unittest_ret;
}






18 changes: 8 additions & 10 deletions include/unittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ extern bool unittest_running_tests;

#else
#undef INCLUDE_SUIT
#define INCLUDE_SUIT(filename, ...) \
extern UnitSuit __VA_ARGS__; \
CATCH(__VA_ARGS__)
#define INCLUDE_SUIT(filename, ...) extern UnitSuit __VA_ARGS__
#undef INCLUDE_TESTCASE
#define INCLUDE_TESTCASE(filename, ...) \
extern UnitTestCase __VA_ARGS__; \
CATCH(__VA_ARGS__)
#define RUN(...) \
unittest_running_tests = true; \
__VA_OPT__(CATCH(__VA_ARGS__)); \
unittest_run_tests()
#define INCLUDE_TESTCASE(filename, ...) extern UnitTestCase __VA_ARGS__
#define RUN(...) \
do { \
unittest_running_tests = true; \
__VA_OPT__(CATCH(__VA_ARGS__)); \
unittest_run_tests(); \
} while (0)
#endif

#define _CATCH_GENERIC(X) \
Expand Down
6 changes: 2 additions & 4 deletions include/unittest_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@
#endif

#define LIB_UNITTEST "-lunittest"

#define VALGRIND "valgrind"
#define VALGRIND_PATH "/usr/bin/"

#ifndef VALGRIND_FLAGS
#define VALGRIND_FLAGS "--quiet"
#endif
#define VALGRIND_FLAGS "--quiet --leak-check=full --show-leak-kinds=all --track-origins=yes"

#ifndef DATE_HASHED_FILE
#define DATE_HASHED_FILE ".date_hashed.bin"
Expand Down
19 changes: 14 additions & 5 deletions include/unittest_tfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ typedef struct {
long date_hashed;
} UnitTestFile;

#define INCLUDE_SUIT(filename, ...) \
unittest_tfiles[unittest_tfile_count++].name = filename

#define INCLUDE_TESTCASE(filename, ...) \
unittest_tfiles[unittest_tfile_count++].name = filename
#define INCLUDE_SUIT(filename, ...) \
do { \
if (!unittest_file_already_included(filename)) \
unittest_tfiles[unittest_tfile_count++].name = filename; \
} while (0)

#define INCLUDE_TESTCASE(filename, ...) \
do { \
if (!unittest_file_already_included(filename)) \
unittest_tfiles[unittest_tfile_count++].name = filename; \
} while (0)

extern size_t unittest_tfile_count;
extern UnitTestFile unittest_tfiles[MAX_AMOUNT_OF_TEST_FILES];

/* unittest_file_already_included: Checks if a file has been already included. */
extern bool unittest_file_already_included(const char *file);

/* unittest_include_files: Include the whole files. */
extern void unittest_include_files(void);

Expand Down
2 changes: 1 addition & 1 deletion include/unittest_valgrind.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
extern bool unittest_run_valgrind;

/* ACTIVE_VALGRIND: To active valgrind into the execution */
#define ACTIVE_VALGRIND(BOOL) unittest_run_valgrind = (BOOL) ? true : false
#define ACTIVE_VALGRIND(BOOL) unittest_run_valgrind = BOOL

#endif
3 changes: 2 additions & 1 deletion src/rerun.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void unittest_rerun(void)
if (unittest_run_valgrind) {
unittest_attach_args(&command, VALGRIND);
unittest_attach_args(&command, VALGRIND_FLAGS);
strcpy(command.executable_command, COMPILER_PATH VALGRIND);
strcpy(command.executable_command, VALGRIND_PATH);
strcat(command.executable_command, VALGRIND);
} else strcpy(command.executable_command, executable);

unittest_attach_args(&command, executable);
Expand Down
12 changes: 12 additions & 0 deletions src/tfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ size_t unittest_tfile_count = 0;
_Static_assert(MAX_AMOUNT_OF_TEST_FILES == 128, "Should be 128 test files at max");
UnitTestFile unittest_tfiles[MAX_AMOUNT_OF_TEST_FILES];

/* unittest_file_already_included: Checks if a file has been already included. */
bool unittest_file_already_included(const char *file)
{
assert(file != NULL);

for (size_t i = 0; i < unittest_tfile_count; i++)
if (strcmp(unittest_tfiles[i].name, file) == 0)
return true;
return false;
}


/* unittest_include_tfile: This function includes a file with a given unittest_hashed_file
* into a specific test directory. */
void unittest_include_tfile(UnitTestFile *tf)
Expand Down
Loading

0 comments on commit 31f3ad3

Please sign in to comment.