Permalink
Browse files

Eliminate use of global variable "cur_test_id"

git-svn-id: svn://mark.heily.com/libkqueue/trunk@523 34a59efb-09c4-4dcf-b95f-994a32aba0d8
  • Loading branch information...
1 parent 9d9fd80 commit 14201e4cc385c4484aabdb358ebd45ff1ed93e9d mheily committed May 30, 2011
Showing with 24 additions and 19 deletions.
  1. +8 −6 test/common.h
  2. +7 −3 test/main.c
  3. +9 −10 test/test.c
View
@@ -65,6 +65,7 @@ struct unit_test {
struct test_context {
struct unit_test tests[50]; //TODO: use MAX_TESTS instead of magic number
+ char *cur_test_id;
int iterations;
int concurrency;
int iteration;
@@ -88,10 +89,11 @@ void test_evfilt_proc(struct test_context *);
void test_evfilt_user(struct test_context *);
#endif
-#define test(f,ctx,...) do { \
- test_begin("test_"#f"()\t"__VA_ARGS__); \
- test_##f(ctx);\
- test_end(); \
+#define test(f,ctx,...) do { \
+ assert(ctx != NULL); \
+ test_begin(ctx, "test_"#f"()\t"__VA_ARGS__); \
+ test_##f(ctx); \
+ test_end(ctx); \
} while (/*CONSTCOND*/0)
extern const char * kevent_to_str(struct kevent *);
@@ -126,8 +128,8 @@ kevent_add(int kqfd, struct kevent *kev,
void _test_no_kevents(int, const char *, int);
/* From test.c */
-void test_begin(const char *);
-void test_end(void);
+void test_begin(struct test_context *, const char *);
+void test_end(struct test_context *);
void test_atexit(void);
void testing_begin(void);
void testing_end(void);
View
@@ -148,18 +148,22 @@ test_harness(struct unit_test tests[], int iterations, int concurrency)
testing_begin();
- test(peer_close_detection, NULL);
+ ctx = calloc(1, sizeof(*ctx));
- test(kqueue, NULL);
+ test(peer_close_detection, ctx);
+
+ test(kqueue, ctx);
if ((kqfd = kqueue()) < 0)
die("kqueue()");
- test(ev_receipt, NULL);
+ test(ev_receipt, ctx);
/* TODO: this fails now, but would be good later
test(kqueue_descriptor_is_pollable);
*/
+ free(ctx);
+
n = 0;
for (i = 0; i < iterations; i++) {
for (j = 0; j < concurrency; j++) {
View
@@ -24,7 +24,6 @@
static int testnum = 1;
static int error_flag = 1;
-static char * cur_test_id = NULL;
/* FIXME: not portable beyond linux */
#ifndef _WIN32
@@ -48,7 +47,7 @@ static void
testing_atexit(void)
{
if (error_flag) {
- printf(" *** TEST FAILED: %s\n", cur_test_id);
+ printf(" *** TEST FAILED ***\n");
//TODO: print detailed log
} else {
printf("\n---\n"
@@ -57,21 +56,21 @@ testing_atexit(void)
}
void
-test_begin(const char *func)
+test_begin(struct test_context *ctx, const char *func)
{
- if (cur_test_id)
- free(cur_test_id);
- cur_test_id = strdup(func);
+ if (ctx->cur_test_id)
+ free(ctx->cur_test_id);
+ ctx->cur_test_id = strdup(func);
- printf("%d: %s\n", testnum++, cur_test_id);
+ printf("%d: %s\n", testnum++, ctx->cur_test_id);
//TODO: redirect stdout/err to logfile
}
void
-test_end(void)
+test_end(struct test_context *ctx)
{
- free(cur_test_id);
- cur_test_id = NULL;
+ free(ctx->cur_test_id);
+ ctx->cur_test_id = NULL;
}
void

0 comments on commit 14201e4

Please sign in to comment.