assert.h

Single-file assertion library for C
git clone git://git.finwo.net/lib/assert.h
Log | Files | Refs | README

commit 01014ac93e9ad6cb42236af52c6652dbb1f9e726
parent 61fa96b7a8728967bf278cfbeed31806e62159eb
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Mon, 23 Jan 2023 23:07:08 +0100

Added example project using this library

Diffstat:
Aexample/.gitignore | 1+
Aexample/Makefile | 29+++++++++++++++++++++++++++++
Aexample/README.md | 17+++++++++++++++++
Aexample/src/functions.c | 11+++++++++++
Aexample/src/functions.test.c | 23+++++++++++++++++++++++
Aexample/src/main.c | 3+++
Minclude/test.h | 5++---
7 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/example/.gitignore b/example/.gitignore @@ -0,0 +1 @@ +/example diff --git a/example/Makefile b/example/Makefile @@ -0,0 +1,29 @@ +SRC=$(filter-out src/%.test.c, $(wildcard src/*.c)) +TESTS=$(wildcard src/*.test.c) + +CFLAGS?= + +ifeq (1,$(DEBUG)) +CFLAGS+=-DDEBUG +endif + +.PHONY: default +default: example + +# Don't do this in a real project +example: $(SRC) + $(CC) $(CFLAGS) -o $@ $^ + +# Compile, run and cleanup of unit tests +.PHONY: tests +tests: $(TESTS) ../include/test.h + for TEST in $(TESTS); do \ + NAME=$$(echo $$TEST | sed 's/.test.c$$//g') && \ + $(CC) $(CFLAGS) -I../include -o $$NAME $$TEST && \ + ./$$NAME && \ + rm $$NAME ; \ + done + +.PHONY: clean +clean: + rm -f example diff --git a/example/README.md b/example/README.md @@ -0,0 +1,17 @@ +Example +======= + +This directory contains an example usage of the c-assert library, of how you +might use it in a project when **not** mixing application code with the tests. + +To build: + +```sh +make +``` + +To test: + +```sh +make tests +``` diff --git a/example/src/functions.c b/example/src/functions.c @@ -0,0 +1,11 @@ +int add(int a, int b) { + return a + b; +} + +int mult(int a, int b) { + return a * b; +} + +const char * pizza() { + return "calzone"; +} diff --git a/example/src/functions.test.c b/example/src/functions.test.c @@ -0,0 +1,23 @@ +#include "test.h" +#include "./functions.c" + +void test_add() { + ASSERT("2 + 2 = 4", add(2, 2) == 4); + ASSERT_EQUALS(add(3, 3), 6); +} + +void test_mult() { + ASSERT("2 * 2 = 4", mult(2, 2) == 4); + ASSERT_EQUALS(mult(3, 3), 9); +} + +void test_str() { + ASSERT_STRING_EQUALS("calzone", pizza()); +} + +int main() { + RUN(test_add); + RUN(test_mult); + RUN(test_str); + return TEST_REPORT(); +} diff --git a/example/src/main.c b/example/src/main.c @@ -0,0 +1,3 @@ +int main() { + return add(21, 21); +} diff --git a/include/test.h b/include/test.h @@ -37,9 +37,7 @@ * * To run the tests, compile the tests as a binary and run it. * - * Project home page: http://github.com/joewalnes/tinytest - * - * 2010, -Joe Walnes <joe@walnes.com> http://joewalnes.com + * Project home page: http://github.com/finwo/c-assert */ #ifndef __TINYTEST_INCLUDED_H__ @@ -47,6 +45,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> /* Main assertion method */ #define ASSERT(msg, expression) if (!tap_assert(__FILE__, __LINE__, (msg), (#expression), (expression) ? 1 : 0)) return