2023-11-06 00:04:12 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
9unit
|
2023-11-07 14:48:41 -05:00
|
|
|
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
|
2023-11-06 00:04:12 -05:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2023-11-09 18:24:48 -05:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
2023-11-06 00:04:12 -05:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2023-11-09 18:24:48 -05:00
|
|
|
Lesser General Public License for more details.
|
2023-11-06 00:04:12 -05:00
|
|
|
|
2023-11-09 18:24:48 -05:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this program. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>.
|
2023-11-06 00:04:12 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-11-06 13:59:19 -05:00
|
|
|
#include <u.h>
|
2023-11-06 18:23:18 -05:00
|
|
|
#include <libc.h>
|
2023-11-06 15:20:09 -05:00
|
|
|
#include <stdio.h>
|
2023-11-06 13:59:19 -05:00
|
|
|
|
|
|
|
#include "9unit.h"
|
|
|
|
|
2023-11-15 18:46:25 -05:00
|
|
|
// Internal Types
|
|
|
|
|
2023-11-18 20:48:14 -05:00
|
|
|
typedef struct RunTestWith RunTestWith;
|
2023-11-15 18:46:25 -05:00
|
|
|
typedef struct ContextData ContextData;
|
2023-11-15 19:52:01 -05:00
|
|
|
typedef struct SingleTestContext SingleTestContext;
|
2023-11-17 16:30:52 -05:00
|
|
|
typedef struct TestContextWith TestContextWith;
|
2023-11-18 11:38:00 -05:00
|
|
|
typedef struct SingleTestContextWith SingleTestContextWith;
|
2023-11-17 18:07:05 -05:00
|
|
|
typedef struct CheckValue CheckValue;
|
2023-11-16 14:53:06 -05:00
|
|
|
|
2023-11-18 20:48:14 -05:00
|
|
|
// data required by run_test_with()
|
|
|
|
struct RunTestWith
|
|
|
|
{
|
|
|
|
void *ptr; // the state's previous ptr value
|
|
|
|
TestResult (*test)(TestState *, void *); // the test
|
|
|
|
void *val; // the value passed in
|
|
|
|
};
|
|
|
|
|
2023-11-15 18:46:25 -05:00
|
|
|
struct ContextData
|
|
|
|
{
|
|
|
|
const char *old_c; // previous context
|
|
|
|
const char *old_fc; // previous full context
|
|
|
|
const char *new_c; // new context
|
|
|
|
char *new_fc; // new full context
|
|
|
|
};
|
|
|
|
|
2023-11-15 19:52:01 -05:00
|
|
|
struct SingleTestContext
|
|
|
|
{
|
|
|
|
void *ptr; // the state's previous ptr value
|
|
|
|
TestResult (*test)(TestState *); // the test to run
|
|
|
|
};
|
|
|
|
|
2023-11-17 16:30:52 -05:00
|
|
|
struct TestContextWith
|
|
|
|
{
|
|
|
|
void *ptr; // state's original ptr value
|
|
|
|
void *val; // the value being passed in
|
|
|
|
void (*test)(TestState *, void *); // the test function
|
|
|
|
};
|
|
|
|
|
2023-11-18 11:38:00 -05:00
|
|
|
struct SingleTestContextWith
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
TestResult (*test)(TestState *, void *);
|
|
|
|
void *val;
|
|
|
|
};
|
|
|
|
|
2023-11-17 18:07:05 -05:00
|
|
|
struct CheckValue
|
2023-11-16 14:53:06 -05:00
|
|
|
{
|
|
|
|
void *chk_val; // the value being checked
|
|
|
|
void *ref_val; // the reference value
|
|
|
|
void (*test)(TestState *, void *, void *); // the test
|
|
|
|
};
|
|
|
|
|
2023-11-06 18:23:18 -05:00
|
|
|
// Internal Prototypes
|
|
|
|
|
2023-11-06 23:33:22 -05:00
|
|
|
static void init_TestState(TestState *);
|
|
|
|
static void print_log(TestState *);
|
|
|
|
static void clear_log(TestState *);
|
|
|
|
static void reindex(TestState *);
|
2023-11-14 18:53:43 -05:00
|
|
|
static void report(const char *);
|
2023-11-17 18:35:02 -05:00
|
|
|
static TestResult run_test_with_test(TestState *);
|
2023-11-15 18:46:25 -05:00
|
|
|
static void build_new_context(TestState *, ContextData *);
|
|
|
|
static void display_context(TestState *);
|
|
|
|
static void restore_context(TestState *, ContextData *);
|
2023-11-18 15:11:28 -05:00
|
|
|
static void single_test_context_test(TestState *);
|
2023-11-17 16:30:52 -05:00
|
|
|
static void test_context_with_test(TestState *);
|
2023-11-18 11:38:00 -05:00
|
|
|
static TestResult single_test_context_with_test(TestState *);
|
2023-11-17 18:07:05 -05:00
|
|
|
static void check_value_test(TestState *, void *);
|
2023-11-06 18:23:18 -05:00
|
|
|
|
|
|
|
// Public Functions
|
2023-11-06 14:46:01 -05:00
|
|
|
|
|
|
|
void
|
2023-11-06 18:23:18 -05:00
|
|
|
run_test(TestState *s, TestResult (*t)(TestState *))
|
2023-11-06 14:46:01 -05:00
|
|
|
{
|
2023-11-11 20:25:55 -05:00
|
|
|
if (!s) return;
|
2023-11-06 14:46:01 -05:00
|
|
|
s->run++;
|
2023-11-11 20:25:55 -05:00
|
|
|
|
|
|
|
// a null test function is a pending test
|
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
s->pending++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-06 18:23:18 -05:00
|
|
|
switch ((*t)(s))
|
2023-11-06 14:26:55 -05:00
|
|
|
{
|
2023-11-06 14:46:01 -05:00
|
|
|
case test_success:
|
2023-11-06 15:20:09 -05:00
|
|
|
s->passed++;
|
2023-11-06 14:46:01 -05:00
|
|
|
break;
|
|
|
|
case test_failure:
|
2023-11-06 15:20:09 -05:00
|
|
|
s->failed++;
|
2023-11-06 14:46:01 -05:00
|
|
|
break;
|
2023-11-07 18:44:53 -05:00
|
|
|
case test_pending:
|
|
|
|
s->pending++;
|
2023-11-06 14:46:01 -05:00
|
|
|
break;
|
2023-11-06 18:23:18 -05:00
|
|
|
default:
|
|
|
|
exits("test returned an invalid response");
|
2023-11-06 14:26:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:35:02 -05:00
|
|
|
void
|
|
|
|
run_test_with(
|
|
|
|
TestState *s, // the state
|
|
|
|
TestResult (*test)(TestState *, void *), // the test
|
|
|
|
void *val // the value being passed in
|
|
|
|
)
|
|
|
|
{
|
2023-11-18 20:48:14 -05:00
|
|
|
if (!s) return;
|
|
|
|
if (!test)
|
|
|
|
{
|
|
|
|
run_test(s, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RunTestWith d;
|
|
|
|
d.ptr = s->ptr;
|
|
|
|
d.test = test;
|
|
|
|
d.val = val;
|
|
|
|
s->ptr = &d;
|
|
|
|
run_test(s, run_test_with_test);
|
2023-11-17 18:35:02 -05:00
|
|
|
}
|
|
|
|
|
2023-11-06 15:20:09 -05:00
|
|
|
void
|
2023-11-06 18:23:18 -05:00
|
|
|
run_tests(void (*tests)(TestState *))
|
2023-11-06 15:20:09 -05:00
|
|
|
{
|
2023-11-07 18:44:53 -05:00
|
|
|
if (!tests) return;
|
2023-11-06 15:20:09 -05:00
|
|
|
TestState s;
|
2023-11-14 18:08:19 -05:00
|
|
|
memset(&s, 0, sizeof(TestState));
|
2023-11-14 18:53:43 -05:00
|
|
|
s.report = report;
|
2023-11-06 15:20:09 -05:00
|
|
|
(*tests)(&s);
|
2023-11-07 14:36:54 -05:00
|
|
|
print_log(&s);
|
2023-11-06 15:20:09 -05:00
|
|
|
printf("Tests run: %d\n", s.run);
|
|
|
|
printf("Tests passed: %d\n", s.passed);
|
|
|
|
printf("Tests failed: %d\n", s.failed);
|
2023-11-07 18:44:53 -05:00
|
|
|
printf("Tests pending: %d\n", s.pending);
|
2023-11-06 23:33:22 -05:00
|
|
|
clear_log(&s);
|
2023-11-07 18:44:53 -05:00
|
|
|
if (s.failed) exits("test(s) failed");
|
2023-11-06 23:33:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-07 18:44:53 -05:00
|
|
|
append_test_log(TestState *s, const char *msg)
|
2023-11-06 23:33:22 -05:00
|
|
|
{
|
|
|
|
if (!(s && msg)) return;
|
|
|
|
|
|
|
|
// build a new entry:
|
|
|
|
TestLogEntry *entry = malloc(sizeof(TestLogEntry));
|
|
|
|
entry->text = malloc(strlen(msg) + 1);
|
|
|
|
strcpy(entry->text, msg);
|
|
|
|
entry->next = 0;
|
|
|
|
|
|
|
|
// add it to the list:
|
|
|
|
if (!s->last_log)
|
|
|
|
{
|
|
|
|
if (s->first_log) // no last entry but we have a first?
|
|
|
|
{
|
|
|
|
reindex(s);
|
|
|
|
s->last_log->next = entry;
|
|
|
|
}
|
|
|
|
else s->first_log = entry;
|
|
|
|
}
|
|
|
|
else // there's already a last entry
|
|
|
|
{
|
|
|
|
if (!s->first_log) // no first entry but we have a last?
|
|
|
|
reindex(s); // do our best to fix that
|
|
|
|
s->last_log->next = entry;
|
|
|
|
}
|
|
|
|
s->last_log = entry;
|
2023-11-06 15:20:09 -05:00
|
|
|
}
|
|
|
|
|
2023-11-15 18:46:25 -05:00
|
|
|
void
|
|
|
|
test_context(
|
|
|
|
TestState *s,
|
|
|
|
const char *label,
|
|
|
|
void (*test)(TestState *)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!(s && test)) return;
|
|
|
|
if (label)
|
|
|
|
{
|
|
|
|
ContextData cd;
|
|
|
|
cd.new_c = label;
|
|
|
|
build_new_context(s, &cd);
|
|
|
|
display_context(s);
|
|
|
|
(*test)(s);
|
|
|
|
restore_context(s, &cd);
|
|
|
|
}
|
|
|
|
else (*test)(s);
|
|
|
|
}
|
|
|
|
|
2023-11-15 19:52:01 -05:00
|
|
|
void
|
|
|
|
single_test_context(
|
|
|
|
TestState *s,
|
|
|
|
const char *label,
|
|
|
|
TestResult (*test)(TestState *)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
2023-11-18 15:11:28 -05:00
|
|
|
SingleTestContext d;
|
|
|
|
d.ptr = s->ptr;
|
|
|
|
d.test = test;
|
|
|
|
s->ptr = &d;
|
|
|
|
test_context(s, label, single_test_context_test);
|
2023-11-15 19:52:01 -05:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:30:52 -05:00
|
|
|
void
|
|
|
|
test_context_with(
|
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
|
|
|
void (*test)(TestState *, void *),
|
|
|
|
void *val
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!(s && test)) return;
|
|
|
|
TestContextWith d;
|
|
|
|
d.ptr = s->ptr;
|
|
|
|
d.val = val;
|
|
|
|
d.test = test;
|
|
|
|
s->ptr = &d;
|
|
|
|
test_context(s, context, test_context_with_test);
|
|
|
|
}
|
|
|
|
|
2023-11-18 11:38:00 -05:00
|
|
|
void
|
|
|
|
single_test_context_with(
|
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
|
|
|
TestResult (*test)(TestState *, void *),
|
|
|
|
void *val
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
if (!test) single_test_context(s, context, 0);
|
|
|
|
SingleTestContextWith d;
|
|
|
|
d.ptr = s->ptr;
|
|
|
|
d.test = test;
|
|
|
|
d.val = val;
|
|
|
|
s->ptr = &d;
|
|
|
|
single_test_context(s, context, single_test_context_with_test);
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:30:52 -05:00
|
|
|
void
|
|
|
|
check_value(
|
2023-11-16 14:53:06 -05:00
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
2023-11-16 17:41:42 -05:00
|
|
|
void (*test)(TestState *, void *, void *),
|
2023-11-16 14:53:06 -05:00
|
|
|
void *chk_val,
|
2023-11-16 17:41:42 -05:00
|
|
|
void *ref_val
|
2023-11-16 14:53:06 -05:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!(s && test)) return;
|
2023-11-17 18:07:05 -05:00
|
|
|
CheckValue d;
|
2023-11-16 14:53:06 -05:00
|
|
|
d.chk_val = chk_val;
|
|
|
|
d.ref_val = ref_val;
|
|
|
|
d.test = test;
|
2023-11-17 18:07:05 -05:00
|
|
|
test_context_with(s, context, check_value_test, &d);
|
2023-11-16 14:53:06 -05:00
|
|
|
}
|
|
|
|
|
2023-11-06 18:23:18 -05:00
|
|
|
// Internal Functions
|
|
|
|
|
2023-11-06 23:33:22 -05:00
|
|
|
static void
|
|
|
|
print_log(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
TestLogEntry *e = s->first_log;
|
|
|
|
while(e)
|
|
|
|
{
|
|
|
|
if(e->text) printf("%s\n", e->text);
|
|
|
|
else print("(empty message)\n");
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_log(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
if(s->last_log && !s->first_log) reindex(s); // fix if broken
|
|
|
|
TestLogEntry *e = s->first_log, *next;
|
|
|
|
s->first_log = 0;
|
|
|
|
s->last_log = 0;
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
next = e->next;
|
|
|
|
free(e->text);
|
|
|
|
free(e);
|
|
|
|
e = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reindex(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
if (s->first_log)
|
|
|
|
{
|
|
|
|
TestLogEntry *e = s->first_log;
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
s->last_log = e;
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (s->last_log) // we have a last log but no first?
|
|
|
|
{
|
|
|
|
s->first_log = s->last_log;
|
|
|
|
fprint(2, "potential memory leak in test log\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:53:43 -05:00
|
|
|
static void
|
|
|
|
report(const char *str)
|
|
|
|
{
|
2023-11-14 19:04:35 -05:00
|
|
|
print("%s", str);
|
2023-11-14 18:53:43 -05:00
|
|
|
}
|
|
|
|
|
2023-11-15 18:46:25 -05:00
|
|
|
static void
|
|
|
|
build_new_context(TestState *s, ContextData *cd)
|
|
|
|
{
|
|
|
|
cd->old_c = s->context;
|
|
|
|
cd->old_fc = s->full_context;
|
|
|
|
if (s->full_context)
|
|
|
|
{
|
|
|
|
cd->new_fc = malloc(strlen(cd->old_fc) + strlen(cd->new_c) + 3);
|
|
|
|
sprintf(cd->new_fc, "%s: %s", cd->old_fc, cd->new_c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cd->new_fc = malloc(strlen(cd->new_c) + 1);
|
|
|
|
strcpy(cd->new_fc, cd->new_c);
|
|
|
|
}
|
|
|
|
s->context = cd->new_c;
|
|
|
|
s->full_context = cd->new_fc;
|
|
|
|
s->depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display_context(TestState *s)
|
|
|
|
{
|
|
|
|
for (int i = 1; i < s->depth; i++)
|
|
|
|
s->report("\t");
|
|
|
|
s->report(s->context);
|
|
|
|
s->report("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
restore_context(TestState *s, ContextData *cd)
|
|
|
|
{
|
|
|
|
s->context = cd->old_c;
|
|
|
|
s->full_context = cd->old_fc;
|
|
|
|
s->depth--;
|
|
|
|
free(cd->new_fc);
|
|
|
|
}
|
|
|
|
|
2023-11-18 20:48:14 -05:00
|
|
|
static TestResult
|
|
|
|
run_test_with_test(TestState *s)
|
|
|
|
{
|
|
|
|
RunTestWith *d = s->ptr;
|
|
|
|
s->ptr = d->ptr;
|
|
|
|
return (*d->test)(s, d->val);
|
|
|
|
}
|
|
|
|
|
2023-11-15 19:52:01 -05:00
|
|
|
static void
|
2023-11-18 15:11:28 -05:00
|
|
|
single_test_context_test(TestState *s)
|
2023-11-15 19:52:01 -05:00
|
|
|
{
|
2023-11-18 15:11:28 -05:00
|
|
|
SingleTestContext *d = s->ptr;
|
|
|
|
s->ptr = d->ptr;
|
|
|
|
run_test(s, d->test);
|
2023-11-15 19:52:01 -05:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:30:52 -05:00
|
|
|
static void
|
|
|
|
test_context_with_test(TestState *s)
|
|
|
|
{
|
|
|
|
TestContextWith *d = s->ptr;
|
|
|
|
s->ptr = d->ptr;
|
|
|
|
(*d->test)(s, d->val);
|
|
|
|
}
|
|
|
|
|
2023-11-18 11:38:00 -05:00
|
|
|
static TestResult
|
|
|
|
single_test_context_with_test(TestState *s)
|
|
|
|
{
|
|
|
|
SingleTestContextWith *d = s->ptr;
|
|
|
|
s->ptr = d->ptr;
|
|
|
|
return (*d->test)(s, d->val);
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:53:06 -05:00
|
|
|
static void
|
2023-11-17 18:07:05 -05:00
|
|
|
check_value_test(TestState *s, void *ptr)
|
2023-11-16 14:53:06 -05:00
|
|
|
{
|
2023-11-17 18:07:05 -05:00
|
|
|
CheckValue *d = ptr;
|
2023-11-16 14:53:06 -05:00
|
|
|
(*d->test)(s, d->chk_val, d->ref_val);
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:04:12 -05:00
|
|
|
//jl
|