Compare commits

...

7 Commits

Author SHA1 Message Date
0c6f39c956 Added a README 2025-09-28 17:51:10 +05:30
285c736b6c Added a simple example file 2025-09-28 13:18:10 +05:30
1bd017adfc setup and teadown
added macros to run function at the start and end of each test

- SETUP     = runs once at the start
- TEARDOWN  = runs once at the end
- BEFORE    = runs before each test
- AFTER     = runs after each test
2025-09-28 13:16:15 +05:30
238db54668 allow the users to set the MAX_TEST_FUNCS value 2025-09-28 12:37:41 +05:30
c1c3e87444 block -> va_args
having them as blocks causes issues when a struct with values is initialized
inside the block, because the compiler treats the comma in the struct
defenition as seperate arguments.
2025-09-28 12:34:05 +05:30
25b0e92ed0 Basic test frameword
- macro to init the test unit
- macro to crate a test
- macro to add assertion stmts
2025-09-28 12:16:04 +05:30
df6988abc3 Added gitignore 2025-09-28 12:15:56 +05:30
4 changed files with 283 additions and 0 deletions

52
.gitignore vendored Normal file
View File

@@ -0,0 +1,52 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

76
README.md Normal file
View File

@@ -0,0 +1,76 @@
# test.h
A simple header file to get test statistics.
> Currently under development things might change without notice
## Usage
Here is a simple simple example for `test.h`
```c
// sample_test.c
#include "test.h"
TEST(sample1, {
ASSERT(1 == 1, "");
})
TEST_INIT({})
```
You can compile and rum this test file, and will get an output like this.
```console
=========================================================================
TEST doc.c
test_sample1
(1/1) passed (0) failed
PASSED - (1/1) passed (0) failed
=========================================================================
```
An example with two tests.
```c
#include "test.h"
TEST(sample1, {
ASSERT(1 == 1, "");
})
TEST(sample2, {
ASSERT(1 == 1, "");
ASSERT(1 == 0, "This assertion will fail");
})
TEST_INIT({})
```
```console
=========================================================================
TEST doc.c
test_sample1
(1/1) passed (0) failed
test_sample2
assertion failed (l.no - 9) [1 == 0] - This assertion will fail
(1/2) passed (1) failed
FAILED - (1/2) passed (1) failed
=========================================================================
```
To know about all the macros that you can use, check the [examples](./examples/) folder.
---
If you have any issues or want to discuss, [Join my Discord](https://discord.gg/BxMbWzZe2Z)
## Bye....

32
examples/basic.c Normal file
View File

@@ -0,0 +1,32 @@
#include <stdio.h>
// #define MAX_TEST_FUNCS 0
#include "test.h"
BEFORE({
printf("run before each test.\n");
})
AFTER({
printf("runs after each test\n");
})
SETUP({
printf("runs once at the start\n");
})
TEARDOWN({
printf("runs once at the end\n");
})
TEST(simple_test, {
ASSERT(1 == 1, "");
// ASSERT(0 == 1, "This assertion will fail");
})
TEST(simple_test_2, {
ASSERT(1 == 1, "");
ASSERT(0 == 1, "This assertion will fail");
})
TEST_INIT()

123
test.h Normal file
View File

@@ -0,0 +1,123 @@
// include this file in all the test files
#ifndef _TEST_H
#define _TEST_H
#include <stdio.h>
#include <stdlib.h>
#define VERSION "0.0.1" // this is not really used anywhere
#define COLOR_RED "\x1b[31;1m"
#define COLOR_RED_REGULAR "\x1b[31m"
#define COLOR_GREEN "\x1b[32;1m"
#define COLOR_YELLOW "\x1b[33;1m"
#define COLOR_RESET "\x1b[0m"
#define BEFORE_IDX 0
#define AFTER_IDX 1
#define SETUP_IDX 2
#define TEARDOWN_IDX 3
#ifndef MAX_TEST_FUNCS
#define MAX_TEST_FUNCS 256
#endif
typedef int (*test_func_t)();
typedef void (*special_func_t)();
int total = 0;
int success = 0;
static test_func_t test_functions[MAX_TEST_FUNCS];
// before, after, setup, teardown
static special_func_t special_funcs[4] = {NULL};
#define TEST_INIT(...) \
int main(void) { \
printf("=========================================================================\n"); \
printf("TEST %s\n", __FILE__); \
__VA_ARGS__ \
if (special_funcs[SETUP_IDX] != NULL) special_funcs[SETUP_IDX](); \
for (int i = 0; i < total; i++) { \
if (special_funcs[BEFORE_IDX] != NULL) special_funcs[BEFORE_IDX](); \
if (test_functions[i]()) success++; \
if (special_funcs[AFTER_IDX] != NULL) special_funcs[AFTER_IDX](); \
} \
if (special_funcs[TEARDOWN_IDX] != NULL) special_funcs[TEARDOWN_IDX](); \
int failed = total - success; \
printf("\n%s - (%d/%d) passed (%d) failed\n" COLOR_RESET, (failed == 0) ? COLOR_GREEN "PASSED" : COLOR_RED "FAILED", success, total, failed); \
printf("=========================================================================\n"); \
return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; \
}
// special ones :)
#define SETUP(...) \
static void _test_setup(); \
__attribute__((constructor)) static void register__test_setup() { \
special_funcs[SETUP_IDX] = _test_setup; \
} \
static void _test_setup() { \
__VA_ARGS__ \
}
#define TEARDOWN(...) \
static void _test_teardown(); \
__attribute__((constructor)) static void register__test_teardown() { \
special_funcs[TEARDOWN_IDX] = _test_teardown; \
} \
static void _test_teardown() { \
__VA_ARGS__ \
}
#define BEFORE(...) \
static void _test_before(); \
__attribute__((constructor)) static void register__test_before() { \
special_funcs[BEFORE_IDX] = _test_before; \
} \
static void _test_before() { \
__VA_ARGS__ \
}
#define AFTER(...) \
static void _test_after(); \
__attribute__((constructor)) static void register__test_after() { \
special_funcs[AFTER_IDX] = _test_after; \
} \
static void _test_after() { \
__VA_ARGS__ \
}
// test stuff
#define TEST(NAME, ...) \
static int test_##NAME(); \
__attribute__((constructor)) static void register_test_##NAME() { \
if (total < MAX_TEST_FUNCS) { \
test_functions[total++] = test_##NAME; \
} else { \
fprintf(stderr, "Max amount of test functions reached\n"); \
exit(EXIT_FAILURE); \
} \
} \
static int test_##NAME() { \
printf("\ntest_" #NAME "\n"); \
int total_assert = 0; \
int total_success = 0; \
__VA_ARGS__ \
printf("\t(%d/%d) passed (%d) failed\n", total_success, total_assert, total_assert - total_success); \
return (total_assert == total_success); \
}
#define ASSERT(COND, MSG) \
do { \
total_assert++; \
if ((COND)) { \
total_success++; \
} else { \
printf("\t" COLOR_RED_REGULAR "assertion failed (l.no - %d) [" #COND "] " COLOR_YELLOW " - %s\n" COLOR_RESET, __LINE__, MSG); \
} \
} while (0)
#endif