aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMart Lubbers2016-08-28 17:35:54 +0200
committerMart Lubbers2016-08-28 17:35:54 +0200
commitfd0fa061c30021396571beb1cfadd0aa464825b3 (patch)
tree68073d4d12f4376ef4206510df49357eb0dfef88
parentMore timing (diff)
linux kernel coding style, fix deprecated libraries
-rw-r--r--Makefile4
-rw-r--r--example.c3
-rw-r--r--minctest.c94
-rw-r--r--minctest.h24
4 files changed, 65 insertions, 60 deletions
diff --git a/Makefile b/Makefile
index f330bdd..231a81d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC=gcc
-CFLAGS=-O3
+CFLAGS=-O3 -Wall -Wextra -Werror -pedantic
DEPS=minctest.h
OBJS=example.o minctest.o
@@ -10,6 +10,6 @@ example: $(OBJS)
$(CC) -o $@ $^ $(CFLAGS)
clean:
- rm *.o example
+ $(RM) *.o example
diff --git a/example.c b/example.c
index 52f3a49..9fcc46c 100644
--- a/example.c
+++ b/example.c
@@ -16,10 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stddef.h>
+
#include "minctest.h"
int main(void) {
- tester* tester = test_initialize();
+ struct tester *tester = test_initialize();
tester->show_pass = false;
test_true(tester, 1 == 1, "1 = 1");
diff --git a/minctest.c b/minctest.c
index 66faf53..2157bd1 100644
--- a/minctest.c
+++ b/minctest.c
@@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
-#include <malloc.h>
#include <stdlib.h>
+
#include "minctest.h"
// Colours for terminal output
@@ -25,62 +25,66 @@
#define KGRN "\x1B[32m"
#define RESET "\x1B[0m"
-tester* test_initialize(void) {
- tester* test = malloc(sizeof(tester));
- if (test == NULL) return NULL;
- test->passed = 0;
- test->failed = 0;
- test->show_pass = true;
- gettimeofday(&test->created, NULL);
- test_start_timer(test);
- return test;
+struct tester *test_initialize() {
+ struct tester *test = malloc(sizeof(struct tester));
+ if (test == NULL)
+ return NULL;
+ test->passed = 0;
+ test->failed = 0;
+ test->show_pass = true;
+ gettimeofday(&test->created, NULL);
+ test_start_timer(test);
+ return test;
}
-void test_wrapup(tester* tester) {
- struct timeval end;
- gettimeofday(&end, NULL);
- float time = ((float) end.tv_usec - tester->created.tv_usec) / 1000;
- float used = ((float) tester->used) / 1000;
-
- if (tester->failed == 0) {
- printf(KGRN "\nAll tests passed (%d) [%.3fs / %.3fs]\n" RESET, tester->passed, used, time);
- } else {
- printf(KRED "\n%d test%s failed" RESET " (%d passed) [%.3fs / %.3fs]\n",
- tester->failed, tester->failed > 1 ? "s" : "", tester->passed, used, time);
- }
+void test_wrapup(struct tester* tester) {
+ struct timeval end;
+ gettimeofday(&end, NULL);
+ float time = (end.tv_usec - tester->created.tv_usec) / 1000.0;
+ float used = (tester->used) / 1000.0;
+
+ if (tester->failed == 0) {
+ printf(KGRN "\nAll tests passed (%d) [%.3fs / %.3fs]\n" RESET,
+ tester->passed, used, time);
+ } else {
+ printf(KRED "\n%d test%s failed" RESET
+ " (%d passed) [%.3fs / %.3fs]\n",
+ tester->failed, tester->failed > 1 ? "s" : "",
+ tester->passed, used, time);
+ }
}
-void test_destroy(tester* tester) {
- free(tester);
+void test_destroy(struct tester *tester) {
+ free(tester);
}
-void test_exit(tester* tester) {
- bool all_passed = tester->failed == 0;
- test_destroy(tester);
- exit(all_passed ? 0 : -1);
+void test_exit(struct tester *tester) {
+ bool all_passed = tester->failed == 0;
+ test_destroy(tester);
+ exit(all_passed ? 0 : -1);
}
-void test_start_timer(tester* tester) {
- gettimeofday(&tester->start_last_test, NULL);
+void test_start_timer(struct tester *tester) {
+ gettimeofday(&tester->start_last_test, NULL);
}
-void test_true(tester* tester, bool check, const char* text) {
- struct timeval end;
- gettimeofday(&end, NULL);
- tester->used += end.tv_usec - tester->start_last_test.tv_usec;
- float time = ((float) end.tv_usec - tester->start_last_test.tv_usec) / 1000;
+void test_true(struct tester *tester, bool check, const char* text) {
+ struct timeval end;
+ gettimeofday(&end, NULL);
+ tester->used += end.tv_usec - tester->start_last_test.tv_usec;
+ float time = (end.tv_usec - tester->start_last_test.tv_usec) / 1000.0;
- if (!check || tester->show_pass) {
- printf(check ? KGRN : KRED);
- printf(check ? "Test passed" : "Test failed");
- if (text != NULL)
- printf(": %s", text);
- printf(" [%.3fs]", time);
- printf(RESET "\n");
- }
+ if (!check || tester->show_pass) {
+ puts(check ? KGRN : KRED);
+ puts(check ? "Test passed" : "Test failed");
+ if (text != NULL)
+ printf(": %s", text);
+ printf(" [%.3fs]", time);
+ puts(RESET "\n");
+ }
- *(check ? &tester->passed : &tester->failed) += 1;
+ *(check ? &tester->passed : &tester->failed) += 1;
- test_start_timer(tester);
+ test_start_timer(tester);
}
diff --git a/minctest.h b/minctest.h
index 64b4519..0170480 100644
--- a/minctest.h
+++ b/minctest.h
@@ -15,21 +15,21 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef MINCTEST_MINCTEST_H
-#define MINCTEST_MINCTEST_H
+#ifndef MINCTEST_H
+#define MINCTEST_H
-#include <inttypes.h>
+#include <stdint.h>
#include <stdbool.h>
#include <sys/time.h>
-typedef struct {
+struct tester {
uint16_t passed; // Number of passed tests
uint16_t failed; // Number of failed tests
bool show_pass; // Whether or not to print a message when a test passes
struct timeval start_last_test; // Time last test was started
struct timeval created; // Time this was created
__suseconds_t used; // usecs used for tests
-} tester;
+};
/**
* Initialize a tester struct
@@ -39,7 +39,7 @@ typedef struct {
*
* @return a struct on the heap for use in the other test_* functions
*/
-tester* test_initialize(void);
+struct tester *test_initialize(void);
/**
* Show summarised data about the tester
@@ -49,7 +49,7 @@ tester* test_initialize(void);
*
* @param tester* a pointer to a tester from test_initialize
*/
-void test_wrapup(tester*);
+void test_wrapup(struct tester *);
/**
* Destroy a tester struct
@@ -58,7 +58,7 @@ void test_wrapup(tester*);
*
* @param tester* a pointer to a tester from test_initialize
*/
-void test_destroy(tester*);
+void test_destroy(struct tester *);
/**
* Exit the program with an appropriate return value
@@ -68,12 +68,12 @@ void test_destroy(tester*);
* @param tester* a pointer to a tester from test_initialize
* @return don't expect this to return
*/
-void test_exit(tester*);
+void test_exit(struct tester *);
/**
* Start the timer for a test
*/
-void test_start_timer(tester*);
+void test_start_timer(struct tester *);
/**
* Test for truth on a bool
@@ -86,6 +86,6 @@ void test_start_timer(tester*);
* @param check the boolean that should be true
* @param test a custom message to show
*/
-void test_true(tester* tester, bool check, const char* text);
+void test_true(struct tester *tester, bool check, const char* text);
-#endif // MINCTEST_MINCTEST_H
+#endif // MINCTEST_H