Compare commits

..

26 Commits

Author SHA1 Message Date
jlamothe 51dd02ea71 acknowledgement of confusing nature of tests 2023-11-23 20:30:00 +00:00
jlamothe 17ad81f6b6 testing run_test_compare() 2023-11-23 20:23:23 +00:00
jlamothe c6dd2559f8 test run_test_with() 2023-11-23 19:49:49 +00:00
jlamothe 3b616f2e22 fixed memory leak in append_test_log() test 2023-11-23 18:53:00 +00:00
jlamothe 23ed415852 WIP: testing run_test_with() 2023-11-23 18:14:54 +00:00
jlamothe daf189dc8d more minor edits to README.md
I think I have a problem.  Someone stop me.
2023-11-23 01:04:36 +00:00
jlamothe 2054ab3096 removed unnecessary comma 2023-11-22 23:25:13 +00:00
jlamothe 45d2801362 typo 2023-11-22 23:20:17 +00:00
jlamothe c3f7ac96cd edit to description of run_test_compare() 2023-11-22 23:03:44 +00:00
jlamothe a506b8bd40 s/parameter/argument/ 2023-11-22 22:52:34 +00:00
jlamothe 9cdc474456 minor tweaks to README.md 2023-11-22 22:50:05 +00:00
jlamothe 7e0c088cf0 completed documentation in README.md
(hopefully)
2023-11-22 20:55:03 +00:00
jlamothe 0ffc08fc41 started writing documentation 2023-11-22 01:04:59 +00:00
jlamothe 6539b87ea9 warn to log instead of stderr
When the reindex function rebuilds the log because of a missing first_log value, it warns about a potential memory leak to the log itself.
2023-11-21 23:26:32 +00:00
jlamothe 00ffef2f0a more thorough testing of append_test_log() 2023-11-21 22:45:56 +00:00
jlamothe cb3788cf02 check next value for append_test_log() on empty log 2023-11-21 20:55:58 +00:00
jlamothe e5cad7cd20 display pass/fail/pend status in realtime 2023-11-21 20:02:53 +00:00
jlamothe da33bd8955 test test_context() with a prior context 2023-11-21 19:49:19 +00:00
jlamothe ebf398fd33 test test_context() on a state with no initial context 2023-11-21 19:44:35 +00:00
jlamothe bea9a3e34a check the next field of the last log when appending 2023-11-21 18:11:35 +00:00
jlamothe 57e4d2ba49 re-implemented test using new convenience functions 2023-11-21 05:12:41 +00:00
jlamothe 01e9cb8794 implemented log_test_context() 2023-11-20 05:03:33 +00:00
jlamothe 8828a8ab17 WIP: re-writing tests 2023-11-20 04:50:42 +00:00
jlamothe 24ed9060ba implemented single_test_context_compare() 2023-11-20 03:58:21 +00:00
jlamothe eaa8ae06a7 renamed check_values() to test_context_compare() and reimplemented 2023-11-20 01:16:44 +00:00
jlamothe f808eb4cb0 reorganized functions and such 2023-11-19 23:48:11 +00:00
16 changed files with 1173 additions and 841 deletions
+130 -72
View File
@@ -31,8 +31,9 @@ typedef struct RunTestWith RunTestWith;
typedef struct RunTestCompare RunTestCompare;
typedef struct ContextData ContextData;
typedef struct TestContextWith TestContextWith;
typedef struct TestContextCompare TestContextCompare;
typedef struct SingleTestContextWith SingleTestContextWith;
typedef struct CheckValue CheckValue;
typedef struct SingleTestContextCompare SingleTestContextCompare;
// data required by run_test_with()
struct RunTestWith
@@ -66,6 +67,14 @@ struct TestContextWith
void *val; // the value being passed in
};
// data needed by test_context_compare()
struct TestContextCompare
{
void (*test)(TestState *, void *, void *);
void *val1;
void *val2;
};
// data needed by single_test_context_with()
struct SingleTestContextWith
{
@@ -73,11 +82,12 @@ struct SingleTestContextWith
void *val;
};
struct CheckValue
// data needed by single_test_context_compare()
struct SingleTestContextCompare
{
void *chk_val; // the value being checked
void *ref_val; // the reference value
void (*test)(TestState *, void *, void *); // the test
TestResult (*test)(TestState *, void *, void *);
void *val1;
void *val2;
};
// Internal Prototypes
@@ -87,18 +97,74 @@ static void print_log(TestState *);
static void clear_log(TestState *);
static void reindex(TestState *);
static void report(const char *);
static TestResult run_test_with_test(TestState *);
static TestResult run_test_compare_test(TestState *, void *);
static void build_new_context(TestState *, ContextData *);
static void display_context(TestState *);
static void restore_context(TestState *, ContextData *);
static void single_test_context_test(TestState *, void *);
static TestResult run_test_with_test(TestState *);
static TestResult run_test_compare_test(TestState *, void *);
static void test_context_with_test(TestState *);
static void test_context_compare_test(TestState *, void *);
static void single_test_context_test(TestState *, void *);
static void single_test_context_with_test(TestState *, void *);
static void check_value_test(TestState *, void *);
static TestResult single_test_context_compare_test(TestState *, void *);
// Public Functions
void
run_tests(void (*tests)(TestState *))
{
if (!tests) return;
TestState s;
memset(&s, 0, sizeof(TestState));
s.report = report;
(*tests)(&s);
print_log(&s);
print("\n\nTests run: %d\n", s.run);
print("Tests passed: %d\n", s.passed);
print("Tests failed: %d\n", s.failed);
print("Tests pending: %d\n", s.pending);
clear_log(&s);
if (s.failed) exits("test(s) failed");
}
void
append_test_log(TestState *s, const char *msg)
{
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;
}
void
log_test_context(TestState *s)
{
if (!s) return;
if (s->full_context) append_test_log(s, s->full_context);
else append_test_log(s, "<no context>");
}
void
run_test(TestState *s, TestResult (*t)(TestState *))
{
@@ -116,12 +182,15 @@ run_test(TestState *s, TestResult (*t)(TestState *))
{
case test_success:
s->passed++;
s->report(" [PASS]");
break;
case test_failure:
s->failed++;
s->report(" [FAIL]");
break;
case test_pending:
s->pending++;
s->report(" [PEND]");
break;
default:
exits("test returned an invalid response");
@@ -164,53 +233,6 @@ run_test_compare(
run_test_with(s, run_test_compare_test, &d);
}
void
run_tests(void (*tests)(TestState *))
{
if (!tests) return;
TestState s;
memset(&s, 0, sizeof(TestState));
s.report = report;
(*tests)(&s);
print_log(&s);
printf("Tests run: %d\n", s.run);
printf("Tests passed: %d\n", s.passed);
printf("Tests failed: %d\n", s.failed);
printf("Tests pending: %d\n", s.pending);
clear_log(&s);
if (s.failed) exits("test(s) failed");
}
void
append_test_log(TestState *s, const char *msg)
{
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;
}
void
test_context(
TestState *s,
@@ -248,6 +270,27 @@ test_context_with(
test_context(s, context, test_context_with_test);
}
void
test_context_compare(
TestState *s,
const char *context,
void (*test)(TestState *, void *, void *),
void *val1,
void *val2
)
{
TestContextCompare d;
d.test = test;
d.val1 = val1;
d.val2 = val2;
test_context_with(
s,
context,
test_context_compare_test,
&d
);
}
void
single_test_context(
TestState *s,
@@ -278,20 +321,24 @@ single_test_context_with(
}
void
check_value(
single_test_context_compare(
TestState *s,
const char *context,
void (*test)(TestState *, void *, void *),
void *chk_val,
void *ref_val
TestResult (*test)(TestState *s, void *, void *),
void *val1,
void *val2
)
{
if (!(s && test)) return;
CheckValue d;
d.chk_val = chk_val;
d.ref_val = ref_val;
SingleTestContextCompare d;
d.test = test;
test_context_with(s, context, check_value_test, &d);
d.val1 = val1;
d.val2 = val2;
single_test_context_with(
s,
context,
single_test_context_compare_test,
&d
);
}
// Internal Functions
@@ -303,7 +350,7 @@ print_log(TestState *s)
TestLogEntry *e = s->first_log;
while(e)
{
if(e->text) printf("%s\n", e->text);
if(e->text) print("\n%s", e->text);
else print("(empty message)\n");
e = e->next;
}
@@ -342,7 +389,8 @@ reindex(TestState *s)
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");
log_test_context(s);
append_test_log(s, "potential memory leak in test log");
}
}
@@ -375,10 +423,10 @@ build_new_context(TestState *s, ContextData *cd)
static void
display_context(TestState *s)
{
s->report("\n");
for (int i = 1; i < s->depth; i++)
s->report("\t");
s->report(s->context);
s->report("\n");
}
static void
@@ -416,6 +464,16 @@ test_context_with_test(TestState *s)
(*d->test)(s, d->val);
}
static void
test_context_compare_test(
TestState *s,
void *ptr
)
{
TestContextCompare *d = ptr;
(*d->test)(s, d->val1, d->val2);
}
static void
single_test_context_test(TestState *s, void *test)
{
@@ -429,11 +487,11 @@ single_test_context_with_test(TestState *s, void *ptr)
run_test_with(s, d->test, d->val);
}
static void
check_value_test(TestState *s, void *ptr)
static TestResult
single_test_context_compare_test(TestState *s, void *ptr)
{
CheckValue *d = ptr;
(*d->test)(s, d->chk_val, d->ref_val);
SingleTestContextCompare *d = ptr;
return (*d->test)(s, d->val1, d->val2);
}
//jl
+49 -29
View File
@@ -25,6 +25,14 @@
// testing framework. You shouldn't need to concern yourself with
// them.
// Possible results of running a single test
typedef enum TestResult
{
test_success, // the test succeeded
test_failure, // the test failed
test_pending // the test is pending
} TestResult;
typedef struct TestState TestState;
typedef struct TestLogEntry TestLogEntry;
@@ -51,16 +59,22 @@ struct TestLogEntry
TestLogEntry *next; // points to the next entry
};
// Possible results of running a single test
typedef enum TestResult
{
test_success, // the test succeeded
test_failure, // the test failed
test_pending // the test is pending
} TestResult;
// Functions
// Creates an initial TestState, passes it to the supplied function,
// and displays the resulting log and summary
extern void run_tests(void (*)(TestState *));
// Adds an entry to the log that is displayed after the tests have
// completed
extern void append_test_log(
TestState *, // the current state
const char * // the message to append
);
// notes the current full context in the log
extern void log_test_context(TestState *);
// Runs a single test
extern void run_test(
TestState *, // the TestState data
@@ -89,17 +103,6 @@ extern void run_test_compare(
void * // the second value
);
// Creates an initial TestState, passes it to the supplied function,
// and displays the resulting log and summary
extern void run_tests(void (*)(TestState *));
// Adds an entry to the log that is displayed after the tests have
// completed
extern void append_test_log(
TestState *, // the current state
const char * // the message to append
);
// Gives additional context for a test
extern void test_context(
TestState *, // the current state
@@ -115,6 +118,22 @@ extern void test_context_with(
void * // the value being passed in
);
// Passes two values into a test context
void test_context_compare(
TestState *, // the current state
const char *, // the context label
// test function
void (*)(
TestState *, // the current state
void *, // the first value
void * // the second value
),
void *, // the first value
void * // the second value
);
// Runs a single test with a context label
extern void single_test_context(
TestState *, // the current state
@@ -136,20 +155,21 @@ extern void single_test_context_with(
void * // the value being passed
);
// Runs a test to check a value
extern void check_value(
TestState *, // the current test state
const char *, // a description of the context
// Runs a single test with a context, passing it two values to be
// compared
extern void single_test_context_compare(
TestState *, // the state
const char *, // the context
// the test function
void (*)(
TestState *,
void *, // the check value
void * // the reference value
TestResult (*)(
TestState *, // the state
void *, // the first value
void * // the second value
),
void *, // the value being checked
void * // the reference value
void *, // the first value
void * // the second value
);
//jl
+306 -8
View File
@@ -1,16 +1,314 @@
# 9unit
Copyright (C) 2023 Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify 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.
This program is free software: you can redistribute it and/or modify
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.
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 Lesser General Public License for more details.
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
Lesser General Public License for more details.
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/>.
## WARNING
This library is experimental and should be considered subject to change at any time.
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/>.
## Summary
A simple C testing framework for Plan9
A simple unit testing framework for C programs in Plan9
This provides the library file `9unit.a` and the header `9unit.h`.
The header is relatively well commented and can provide a fairly
comprehensive breakdown of the API. This document will however
provide a basic overview below.
This library is used to test itself, consequently the `test` directory
contains a relitively decent real-world (if somewhat confusing)
example of how it can be used.
## `TestState`
The entire testing framework is centred around the `TestState` data
structure. As its name would imply, it contains the current state of
the tests in progress, however it should almost never be necessary to
interact with it directly. With the exception of `run_tests()`
(described below), all functions provided by the library will take
take a pointer to the current `TestState` value as their first
argument.
## `run_tests()`
This will typically be the first function called. It creates an
initial `TestState` value, runs the tests, and displays a test log and
summary at the end. If any of the tests fail, it will cause the test
process to exit with a status of `"test(s) failed"`. Its prototype
follows:
```C
void run_tests(void (*)(TestState *));
```
Its only argument is a pointer to a function which is then
responsible for actually running the tests. A pointer to the newly
created `TestState` value will be passed to this function.
## Simple Tests
The simplest form of test can be represented by a function resembling
the follwoing:
```C
TestResult my_test(TestState *s)
{
// test code goes here...
}
```
This function should return a `TestResult` value representing (perhaps
unsurprisingly) the result of the test. The options are as follows:
- `test_success`: the test was completed successfully
- `test_failure`: the test failed
- `test_pending`: the test is pending, and should be ignored for now
Tests of this type can be run by passing a pointer to them to the
`run_test()` function which has the following prototype:
```C
void run_test(
TestState *,
TestResult (*)(TestState *)
)
```
This function will call the provided test function, and update the
provided `TestState` to reflect the result. Thus, the above
hypothetical test could by run as follows:
```C
void
tests(TestState *s)
{
run_test(s, my_test);
}
void
main()
{
run_tests(tests);
exits(0);
}
```
Passing a null `TestState` pointer will cause nothing to happen. This
is true of all functions in this library. (This behaviour might be
reconsidered later, so don't count on it.) Passing a null function
pointer to `run_test()` is interpreted as a pending test.
## Passing Values to Tests
Since C supports neither lambdas nor closures, this would leave one
with little choice but to come up with a unique name for each
individual test function. This, while possible, would definitely be
rather inconvenient. To combat this shortcoming, it is helpful to be
able to pass data into a generic test function so that it can be
reused multiple times.
### The `ptr` Value
The `TestState` struct has a value called `ptr` which is a `void`
pointer that can be set prior to calling `run_test()` (or any other
function, really). This value can then be referenced by the test
function, giving you the ability to essentially pass in (or out) *any*
type of data you may need. While not ideal, it's *a* solution.
The library does not perform any kind of validation or automatic
memory management on the `ptr` value (this is C after all), so the
responsibility for this falls to the programmer implementing the
tests.
### Convenience Functions
As the test suite becomes more and more complex, managing a single
`ptr` value can become increasingly burdensome. For this reason,
there are a few convenience functions that provide an alternate
mechanism for passing data into a function without altering the `ptr`
value. (They actually do alter it internally, but they restore the
original value before passing the state on.) Two such functions are:
`run_test_with()`, and `run_test_compare()`.
`run_test_with()` has the following prototype:
```C
void run_test_with(
TestState *,
TestResult (*)(TestState *, void *),
void *
);
```
The first argument points to the current test state. The second
points to a test function much like the simple test function described
above, but that takes a void pointer as a second argument. Finally,
the third argument is the pointer that gets passed into the test
function.
`run_test_compare()` is similar, but it allows *two* pointers to be
passed into the test function. This is useful for comparing the
actual output of a function to an expected value, for instance.
The prototype for `run_test_compare()` follows:
```C
void run_test_compare(
TestState *,
TestResult (*)(TestState *, void *, void *),
void *,
void *
);
```
The pointers will be passed into the test function in the same order
they are passed into `run_test_compare()`.
## Test Contexts
It is useful to document what your tests are doing. This can be
achieved using contexts. Contexts are essentially labelled
collections of related tests. Contexts can be nested to create
hierarchies. This is useful both for organization purposes as well as
creating reusable test code. There are several functions written for
managing these contexts. Each of these functions takes as its first
two arguments: a pointer to the current `TestState`, and a pointer to
a string describing the context it defines. If the pointer to the
string is null, the tests are run as a part of the existing context.
### `test_context()`
```C
void test_context(
TestState *,
const char *,
void (*)(TestState *)
);
```
This function takes a pointer to the current `TestState`, a string
describing the context, and a function pointer that is used the same
way as the one passed to `run_tests()`. This function will be called
and its tests will be run within the newly defined context. Nothing
prevents this function from being called again in a different context.
### `test_context_with()`
```C
void test_context_with(
TestState *,
const char *,
void (*)(TestState *, void *),
void *
);
```
This funciton works similarly to `test_context()`, but allows for the
passing of a `void` pointer into the test function in much the same
way as the `run_test_with()` function. Its arguments are (in order),
a pointer to the current state, the context description, a pointer to
the test function, and the pointer to be passed into that function.
### `test_context_compare()`
```C
void test_context_compare(
TestState *,
const char *,
void (*)(TestState *, void *, void *),
void *,
void *
);
```
This funciton allows the passing to two `void` pointers into a context
in a manner similar to `run_test_compare()`.
### `single_test_context()`
```C
void single_test_context(
TestState *,
const char *,
TestState (*)(TestState *)
);
```
This function applies the context label to a *single* test. The
function passed in is expected to operate in the same way as the one
passed to `run_test()`.
### `single_test_context_with()`
```C
void single_test_context_with(
TestState *,
const char *,
TestState (*)(TestState *, void *),
void *
);
```
This is similar to `single_test_context()` but allows a `void` pointer
to be passed as in `run_test_with()`.
### `single_test_context_compare()`
```C
void single_test_context_compare(
TestState *,
const char *,
TestResult (*)(TestState *, void *, void *),
void *,
void *
);
```
I assume you get the idea at this point.
## Logging
When `run_tests()` finishes running the tests, it displays a log and
summary. The summary is simply a tally of the number of tests run,
passed, failed, and pending. While this is useful (and probably all
you need to know when all the tests pass) it is likely desirable to
have more detail when something goes wrong. To facilitate this, tests
can append to the test log, which is automatically displayed just
before the summary. There are two functions for doing this.
### `append_test_log()`
```C
void append_test_log(
TestState *,
const char *
);
```
This appends an arbitrary string to the end of the test log. The
contents of the string are copied into the log, so the value pointed
to by the second argument does not need to persist in memory beyond
the end of the call to the function. Log entries are expected to be
single lines. No trailing newline should be present (but the trailing
NUL character should (obviously)).
### `log_test_context()`
```C
void log_test_context(TestState *);
```
This function appends an entry to the log indicating the test's
current *full* context. If no context is defined, the log entry will
be `"<no context>"`. If the test is inside of a context labeled
`"foo"` which is inside of another context labeled `"bar"`, the
resulting log entry will read `"bar: foo"`.
+138 -131
View File
@@ -21,7 +21,6 @@
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "../9unit.h"
#include "util.h"
@@ -44,30 +43,37 @@ static void append_to_existing(TestState *);
static void missing_last(TestState *);
static void missing_first(TestState *);
static void null_message(TestState *);
decl_test(null_state);
static void chk_last(TestState *, LogData *, const char *);
static TestResult null_state(TestState *);
static void chk_empty_first(TestState *, void *);
static void chk_first(TestState *, void *);
static void chk_second(TestState *, void *);
static void chk_last(TestState *, void *);
static void mf_chk_first(TestState *, void *);
static void mf_chk_second(TestState *, void *);
static void mf_chk_third(TestState*, void *);
static void mk_log_data(LogData *);
static void clean_entry(TestLogEntry *);
static void chk_ptr_chg(
TestState *,
const char *,
const char *,
const void *,
const void *
);
static void chk_ptr_chg_test(TestState *, void *, void *);
// Public Functions
void
test_append_test_log(TestState *s)
{
print("append_test_log()\n");
append_to_empty(s);
append_to_existing(s);
missing_last(s);
missing_first(s);
null_message(s);
run_test(s, null_state);
test_context(s, "append to empty", append_to_empty);
test_context(s, "append to existing", append_to_existing);
test_context(s, "missing last_log", missing_last);
test_context(s, "missing first_log", missing_first);
test_context(s, "null message", null_message);
single_test_context(s, "null state", null_state);
}
// Local Functions
@@ -76,161 +82,161 @@ static void
append_to_empty(TestState *s)
{
TestState test;
print("\tappend to empty\n");
mk_sample_state(&test);
append_test_log(&test, "foo");
// first_log shouldn't be null
print("\t\tfirst_log\n");
chk_ptr_ne(
// first_log
test_context_with(
s,
"ERROR: append_test_log(): append to empty: first_log:",
test.first_log,
0
"first_log",
chk_empty_first,
&test
);
// log should say "foo"
print("\t\t\ttext\n");
chk_str_eq(
s,
"ERROR: append_test_log(): append to empty: first_log: text:",
test.first_log->text,
"foo"
);
// last_log should match first_log
print("\t\tlast_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): append to empty: last_log:",
test.last_log,
test.first_log
);
// last_log
chk_ptr_eq(s, "last_log", test.last_log, test.first_log);
}
static void
append_to_existing(TestState *s)
{
LogData ld;
print("\tappend to existing\n");
mk_log_data(&ld);
append_test_log(&ld.state, "baz");
// first shouldn't change
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): append to existing: first_log:",
ld.state.first_log,
&ld.first
);
chk_last(
s,
&ld,
"ERROR: append_test_log(): append to existing: last_log:"
);
free(ld.state.last_log);
test_context_with(s, "first_log", chk_first, &ld);
test_context_with(s, "second log", chk_second, &ld);
test_context_with(s, "last_log", chk_last, &ld);
}
static void
missing_last(TestState *s)
{
LogData ld;
print("\tlast_log missing\n");
mk_log_data(&ld);
ld.state.last_log = 0;
append_test_log(&ld.state, "baz");
// first shouldn't change
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): last_log missing: first_log:",
ld.state.first_log,
&ld.first
);
chk_last(
s,
&ld,
"ERROR: append_test_log(): last_log missing: last_log:"
);
free(ld.state.last_log);
test_context_with(s, "first_log", chk_first, &ld);
test_context_with(s, "second log", chk_second, &ld);
test_context_with(s, "last_log", chk_last, &ld);
}
static void
missing_first(TestState *s)
{
LogData ld;
print("\tfirst_log missing\n");
mk_log_data(&ld);
ld.state.first_log = 0;
append_test_log(&ld.state, "baz");
// first_log should point to second
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): first_log missing: first_log:",
ld.state.first_log,
&ld.second
);
chk_last(
s,
&ld,
"ERROR: append_test_log(): first_log missing: last_log:"
);
free(ld.state.last_log);
test_context_with(s, "first_log", mf_chk_first, &ld);
test_context_with(s, "last_log", chk_last, &ld);
TestLogEntry
*context = ld.second.next,
*warning = context->next;
clean_entry(context);
clean_entry(warning);
}
static void
null_message(TestState *s)
{
LogData ld;
print("\tnull message\n");
mk_log_data(&ld);
append_test_log(&ld.state, 0);
// first shouldn't change
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): null message: first_log:",
ld.state.first_log,
&ld.first
);
// last shouldn't change
print("\t\tlast_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): null message: last_log:",
ld.state.last_log,
&ld.second
);
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
chk_ptr_eq(s, "last_log", ld.state.last_log, &ld.second);
}
decl_test(null_state)
static TestResult
null_state(TestState *)
{
// ensure it doesn't crash
append_test_log(0, "foo");
return test_success;
}
static void
chk_last(TestState *s, LogData *ld, const char *context)
chk_empty_first(TestState *s, void *ptr)
{
print("\t\tlast_log\n");
chk_ptr_chg(
TestState *test = ptr;
chk_str_eq(s, "text", test->first_log->text, "foo");
chk_ptr_eq(s, "next", test->first_log->next, 0);
}
static void
chk_first(TestState *s, void *ptr)
{
LogData *ld = ptr;
// first_log
chk_ptr_eq(
s,
"\t\t\t",
context,
ld->state.last_log,
0,
ld->state.first_log,
&ld->first
);
// next
chk_ptr_eq(
s,
"next",
ld->state.first_log->next,
&ld->second
);
}
static void
chk_second(TestState *s, void *ptr)
{
LogData *ld = ptr;
chk_ptr_eq(
s,
"next",
ld->second.next,
ld->state.last_log
);
}
static void
chk_last(TestState *s, void *ptr)
{
LogData *ld = ptr;
chk_ptr_chg(s, 0, ld->state.last_log, &ld->second);
chk_str_eq(s, "text", ld->state.last_log->text, "baz");
chk_ptr_eq(s, "next", ld->state.last_log->next, 0);
clean_entry(ld->state.last_log);
}
static void
mf_chk_first(TestState *s, void *ptr)
{
LogData *ld = ptr;
chk_ptr_eq(s, 0, ld->state.first_log, &ld->second);
test_context_with(s, "next", mf_chk_second, ptr);
}
static void
mf_chk_second(TestState *s, void *ptr)
{
LogData *ld = ptr;
TestLogEntry *e = ld->second.next;
chk_str_eq(s, "text", e->text, "<no context>");
test_context_with(s, "next", mf_chk_third, ptr);
}
static void
mf_chk_third(TestState *s, void *ptr)
{
LogData *ld = ptr;
TestLogEntry *e = ld->second.next->next;
chk_str_eq(
s,
"text",
e->text,
"potential memory leak in test log"
);
chk_ptr_eq(s, "next", e->next, ld->state.last_log);
}
static void
mk_log_data(LogData *ld)
{
@@ -246,35 +252,36 @@ mk_log_data(LogData *ld)
static void
chk_ptr_chg(
TestState *s,
const char *prefix,
const char *context,
const void *actual,
const void *prohibited
)
{
char str[STR_BUF_SIZE];
// make sure it's changed
print(prefix);
print("changed\n");
snprintf(str, STR_BUF_SIZE, "%s changed:", context);
chk_ptr_ne(
test_context_compare(
s,
str,
context,
chk_ptr_chg_test,
actual,
prohibited
);
}
// make sure it's not null
print(prefix);
print("not null\n");
snprintf(str, STR_BUF_SIZE, "%s not null:", context);
chk_ptr_ne(
s,
str,
actual,
0
);
static void
chk_ptr_chg_test(
TestState *s,
void *actual,
void *prohibited
)
{
chk_ptr_ne(s, "has changed", actual, prohibited);
chk_ptr_ne(s, "not null", actual, 0);
}
static void
clean_entry(TestLogEntry *e)
{
free(e->text);
free(e);
}
//jl
+13 -119
View File
@@ -26,129 +26,23 @@
#include "util.h"
#include "initial-state.h"
// Internal Prototypes
static void tests(TestState *);
decl_test(initial_test_count);
decl_test(initial_pass_count);
decl_test(initial_fail_count);
decl_test(initial_pend_count);
decl_test(initial_first_log);
decl_test(initial_last_log);
decl_test(initial_ptr);
decl_test(initial_context);
decl_test(initial_full_context);
decl_test(initial_depth);
decl_test(initial_report);
// Public Functions
void
test_initial_state(TestState *s)
test_initial_state(TestState *s, void *ptr)
{
single_test_context(s, "run", initial_test_count);
single_test_context(s, "passsed", initial_pass_count);
single_test_context(s, "failed", initial_fail_count);
single_test_context(s, "pending", initial_pend_count);
single_test_context(s, "first_log", initial_first_log);
single_test_context(s, "last_log", initial_last_log);
single_test_context(s, "ptr", initial_ptr);
single_test_context(s, "context", initial_context);
single_test_context(s, "full_context", initial_full_context);
single_test_context(s, "depth", initial_depth);
single_test_context(s, "report()", initial_report);
}
// Internal Functions
def_test(initial_test_count, s)
{
TestState *scpy = s->ptr;
if (scpy->run == 0) return test_success;
append_test_log(s, "initial run was nonzero");
return test_failure;
}
def_test(initial_pass_count, s)
{
TestState *scpy = s->ptr;
print("\tpassed\n");
if (scpy->passed == 0) return test_success;
append_test_log(s, "initial passed was nonzero");
return test_failure;
}
def_test(initial_fail_count, s)
{
TestState *scpy = s->ptr;
if (scpy->failed == 0) return test_success;
append_test_log(s, "initial failed was nonzero");
return test_failure;
}
def_test(initial_pend_count, s)
{
TestState *scpy = s->ptr;
print("\tpending\n");
if (scpy->pending == 0) return test_success;
append_test_log(s, "initial pending was nonzero");
return test_failure;
}
def_test(initial_first_log, s)
{
TestState *scpy = s->ptr;
if (scpy->first_log == 0) return test_success;
append_test_log(s, "initial first_log was not null");
return test_failure;
}
def_test(initial_last_log, s)
{
TestState *scpy = s->ptr;
if (scpy->last_log == 0) return test_success;
append_test_log(s, "initial last_log was not null");
return test_failure;
}
def_test(initial_ptr, s)
{
TestState *scpy = s->ptr;
if (scpy->ptr == 0) return test_success;
append_test_log(s, "initial ptr was not null");
return test_failure;
}
def_test(initial_context, s)
{
TestState *scpy = s->ptr;
if (scpy->context == 0) return test_success;
append_test_log(s, "initial context was not null");
return test_failure;
}
def_test(initial_full_context, s)
{
TestState *scpy = s->ptr;
if (scpy->context == 0) return test_success;
append_test_log(s, "initial full_context was not null");
return test_failure;
}
def_test(initial_depth, s)
{
TestState *scpy = s->ptr;
if (scpy->depth == 0) return test_success;
append_test_log(s, "initial depth was not zero");
return test_failure;
}
def_test(initial_report, s)
{
TestState *scpy = s->ptr;
if (scpy->report != 0) return test_success;
append_test_log(s, "initial report was null");
return test_failure;
TestState *scpy = ptr;
chk_int_eq(s, "run", scpy->run, 0);
chk_int_eq(s, "passsed", scpy->passed, 0);
chk_int_eq(s, "failed", scpy->failed, 0);
chk_int_eq(s, "pending", scpy->pending, 0);
chk_ptr_eq(s, "first_log", scpy->first_log, 0);
chk_ptr_eq(s, "last_log", scpy->last_log, 0);
chk_ptr_eq(s, "ptr", scpy->ptr, 0);
chk_ptr_eq(s, "context", scpy->context, 0);
chk_ptr_eq(s, "full_context", scpy->full_context, 0);
chk_int_eq(s, "depth", scpy->depth, 0);
chk_ptr_ne(s, "report()", scpy->report, 0);
}
//jl
+1 -1
View File
@@ -19,6 +19,6 @@
*/
void test_initial_state(TestState *);
void test_initial_state(TestState *, void *);
//jl
+4
View File
@@ -22,6 +22,8 @@ HFILES=\
util.h\
initial-state.h\
run-test.h\
run-test-with.h\
run-test-compare.h\
append-test-log.h\
test-context.h
@@ -29,6 +31,8 @@ OFILES=\
util.$O\
initial-state.$O\
run-test.$O\
run-test-with.$O\
run-test-compare.$O\
append-test-log.$O\
test-context.$O
+87
View File
@@ -0,0 +1,87 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
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.
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
Lesser General Public License for more details.
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/>.
*/
#include <u.h>
#include <libc.h>
#include "../9unit.h"
#include "util.h"
#include "run-test-compare.h"
// Internal Prototypes
static TestResult
test_run_test_compare_test(TestState *, void *, void *);
// Public Functions
void
test_run_test_compare(TestState *s)
{
TestState actual, expected;
// build initial state
mk_sample_state(&actual);
void
*pass_in1 = malloc(1),
*pass_in2 = malloc(1),
*passed_in[2];
for (int i = 0; i < 2; i++)
passed_in[i] = 0;
actual.ptr = passed_in;
// run it and see what was passed
run_test_compare(
&actual,
test_run_test_compare_test,
pass_in1,
pass_in2
);
chk_ptr_eq(s, "first value passed", passed_in[0], pass_in1);
chk_ptr_eq(s, "second value passed", passed_in[1], pass_in2);
// ensure the state was updated correctly
mk_sample_state(&expected);
expected.passed = 2;
expected.run = 7;
expected.ptr = &passed_in;
chk_TestState_eq(s, "resulting state", &actual, &expected);
free(pass_in1);
free(pass_in2);
}
// Internal Functions
static TestResult
test_run_test_compare_test(
TestState *s,
void *ptr1,
void *ptr2
)
{
void **passed_in = s->ptr;
passed_in[0] = ptr1;
passed_in[1] = ptr2;
return test_success;
}
//jl
+24
View File
@@ -0,0 +1,24 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
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.
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
Lesser General Public License for more details.
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/>.
*/
extern void test_run_test_compare(TestState *);
//jl
+71
View File
@@ -0,0 +1,71 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
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.
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
Lesser General Public License for more details.
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/>.
*/
#include <u.h>
#include <libc.h>
#include "../9unit.h"
#include "util.h"
#include "run-test-with.h"
// Internal Prototypes
static TestResult test_run_test_with_test(TestState *, void *);
// Public Functions
void
test_run_test_with(TestState *s)
{
TestState actual, expected;
// build initial state
mk_sample_state(&actual);
void
*pass_in = malloc(0),
*passed_in = 0;
actual.ptr = &passed_in;
// run it and see what was passed
run_test_with(&actual, test_run_test_with_test, pass_in);
chk_ptr_eq(s, "value passed", passed_in, pass_in);
// ensure the state was updated correctly
mk_sample_state(&expected);
expected.passed = 2;
expected.run = 7;
expected.ptr = &passed_in;
chk_TestState_eq(s, "resulting state", &actual, &expected);
free(pass_in);
}
// Internal Functions
static TestResult
test_run_test_with_test(TestState *s, void *ptr)
{
void **passed_in = s->ptr;
*passed_in = ptr;
return test_success;
}
//jl
+24
View File
@@ -0,0 +1,24 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
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.
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
Lesser General Public License for more details.
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/>.
*/
extern void test_run_test_with(TestState *);
//jl
+41 -66
View File
@@ -31,23 +31,22 @@
static void test_pass(TestState *);
static void test_fail(TestState *);
static void test_pend(TestState *);
decl_test(null_state);
static TestResult null_state(TestState *);
static void null_test(TestState *);
decl_test(always_passes);
decl_test(always_fails);
decl_test(always_pends);
static TestResult always_passes(TestState *);
static TestResult always_fails(TestState *);
static TestResult always_pends(TestState *);
// Public Functions
void
test_run_test(TestState *s)
{
print("run_test()\n");
test_pass(s);
test_fail(s);
test_pend(s);
run_test(s, null_state);
null_test(s);
test_context(s, "passing", test_pass);
test_context(s, "failing", test_fail);
test_context(s, "pending", test_pend);
single_test_context(s, "null state", null_state);
test_context(s, "null test", null_test);
}
// Internal Functions
@@ -55,78 +54,58 @@ test_run_test(TestState *s)
static void
test_pass(TestState *s)
{
TestState expected, actual;
print("\tpassing\n");
TestState actual, expected;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_passes);
// expected result
mk_sample_state(&expected);
expected.passed = 2;
expected.run = 7;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_passes);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): passing:",
&actual,
&expected
);
chk_TestState_eq(s, 0, &actual, &expected);
}
static void
test_fail(TestState *s)
{
TestState expected, actual;
print("\tfailing\n");
TestState actual, expected;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_fails);
// expected result
mk_sample_state(&expected);
expected.failed = 3;
expected.run = 7;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_fails);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): failing:",
&expected,
&actual
);
chk_TestState_eq(s, 0, &actual, &expected);
}
static void
test_pend(TestState *s)
{
TestState expected, actual;
print("\tpending\n");
TestState actual, expected;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_pends);
// expected result
mk_sample_state(&expected);
expected.pending = 4;
expected.run = 7;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_pends);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): pending:",
&expected,
&actual
);
chk_TestState_eq(s, 0, &actual, &expected);
}
decl_test(null_state)
static TestResult
null_state(TestState *)
{
print("\tnull state\n");
// make sure it doesn't crash
run_test(0, always_passes);
return test_success;
}
@@ -135,37 +114,33 @@ static void
null_test(TestState *s)
{
TestState actual, expected;
print("\tnull test\n");
// actual value
mk_sample_state(&actual);
run_test(&actual, 0);
// expected value
mk_sample_state(&expected);
expected.pending = 4;
expected.run = 7;
// actual value
mk_sample_state(&actual);
run_test(&actual, 0);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): null_test:",
&actual,
&expected
);
chk_TestState_eq(s, 0, &actual, &expected);
}
decl_test(always_passes)
static TestResult
always_passes(TestState *)
{
return test_success;
}
decl_test(always_fails)
static TestResult
always_fails(TestState *)
{
return test_failure;
}
decl_test(always_pends)
static TestResult
always_pends(TestState *)
{
return test_pending;
}
+118 -33
View File
@@ -26,7 +26,7 @@
#include "util.h"
#include "test-context.h"
// Local Types
// Internal Types
typedef struct ContextData ContextData;
@@ -37,21 +37,21 @@ struct ContextData
const char *initial_full_context;
int initial_depth;
const char *new_context;
const char *expected_sub_full_context;
int expected_sub_depth;
const char *sub_context;
const char *sub_full_context;
int sub_depth;
const char *expected_sub_full_context;
int expected_sub_depth;
};
// Local Prototypes
// Internal Prototypes
static void no_prior_context(TestState *);
static void prior_context(TestState *);
static void test_context_common(TestState *, ContextData *);
static void init_ContextData(ContextData *);
static void run_test_context(ContextData *);
static void get_context(TestState *);
static void check_results(TestState *, ContextData *);
static void check_sub_context(TestState *);
static void clean_up(ContextData *);
// Public Functions
@@ -60,44 +60,85 @@ void
test_test_context(TestState *s)
{
test_context(s, "no prior context", no_prior_context);
test_context(s, "prior context", prior_context);
}
// Local Functions
// Internal Functions
static void
no_prior_context(TestState *s)
{
// set initial state
ContextData cd;
// set initial state
cd.new_context = "my test";
cd.initial_context = 0;
cd.initial_full_context = 0;
cd.initial_depth = 0;
init_ContextData(&cd);
run_test_context(&cd);
// set expectations
cd.expected_sub_full_context = "my test";
cd.expected_sub_depth = 1;
check_results(s, &cd);
clean_up(&cd);
test_context_common(s, &cd);
}
static void
prior_context(TestState *s)
{
ContextData cd;
// set initial state
cd.new_context = "my other test";
cd.initial_context = "bar";
cd.initial_full_context = "foo: bar";
cd.initial_depth = 2;
// set expectations
cd.expected_sub_full_context = "foo: bar: my other test";
cd.expected_sub_depth = 3;
test_context_common(s, &cd);
}
static void
test_context_common(
TestState *s,
ContextData *cd
)
{
init_ContextData(cd);
test_context(&cd->state, cd->new_context, get_context);
check_results(s, cd);
clean_up(cd);
}
static void
init_ContextData(ContextData *cd)
{
mk_sample_state(&cd->state);
cd->state.context = cd->initial_context;
cd->state.full_context = cd->initial_full_context;
cd->state.depth = cd->initial_depth;
}
static void
run_test_context(ContextData *cd)
{
void *old_ptr = cd->state.ptr;
cd->state.ptr = cd;
test_context(&cd->state, cd->new_context, get_context);
cd->state.ptr = old_ptr;
// initial context
if (cd->initial_context)
{
cd->state.context =
malloc(strlen(cd->initial_context) + 1);
strcpy(cd->state.context, cd->initial_context);
}
// initial full_context
if (cd->initial_full_context)
{
cd->state.full_context =
malloc(strlen(cd->initial_full_context) + 1);
strcpy(
cd->state.full_context,
cd->initial_full_context
);
}
cd->state.depth = cd->initial_depth;
}
static void
@@ -128,23 +169,67 @@ get_context(TestState *s)
}
static void
check_results(TestState *s, ContextData *cd)
check_results(
TestState *s,
ContextData *cd
)
{
void *old_ptr = s->ptr;
s->ptr = cd;
test_context(s, "sub context", check_sub_context);
s->ptr = old_ptr;
}
// sub context
chk_str_eq(
s,
"sub context",
cd->sub_context,
cd->new_context
);
static void
check_sub_context(TestState *s)
{
ContextData *cd = s->ptr;
// sub full_context
chk_str_eq(
s,
"sub full_context",
cd->sub_full_context,
cd->expected_sub_full_context
);
// sub depth
chk_int_eq(
s,
"sub depth",
cd->sub_depth,
cd->expected_sub_depth
);
// final context
chk_str_eq(
s,
"final context",
cd->state.context,
cd->initial_context
);
// final full_context
chk_str_eq(
s,
"final full_context",
cd->state.full_context,
cd->initial_full_context
);
// final depth
chk_int_eq(
s,
"final depth",
cd->state.depth,
cd->initial_depth
);
}
static void
clean_up(ContextData *cd)
{
free(cd->state.context);
free(cd->state.full_context);
free(cd->sub_context);
free(cd->sub_full_context);
}
//jl
+8 -5
View File
@@ -25,6 +25,8 @@
#include "../9unit.h"
#include "initial-state.h"
#include "run-test.h"
#include "run-test-with.h"
#include "run-test-compare.h"
#include "append-test-log.h"
#include "test-context.h"
@@ -48,14 +50,15 @@ tests(TestState *s)
{
if (!s) exits("ERROR: no TestState");
// Make a copy of the initial TestState and store it
// Make a copy of the initial TestState
TestState scpy;
memcpy(&scpy, s, sizeof(TestState));
s->ptr = &scpy;
test_context(s, "initial state", test_initial_state);
test_run_test(s);
test_append_test_log(s);
test_context_with(s, "initial state", test_initial_state, &scpy);
test_context(s, "run_test()", test_run_test);
test_context(s, "run_test_with()", test_run_test_with);
test_context(s, "run_test_compare()", test_run_test_compare);
test_context(s, "append_test_log()", test_append_test_log);
test_context(s, "test_context()", test_test_context);
}
+145 -359
View File
@@ -26,134 +26,17 @@
#include "../9unit.h"
#include "util.h"
// Local Types
// Internal Prototypes
typedef struct CompareInts CompareInts;
typedef struct ComparePtrs ComparePtrs;
struct CompareInts
{
const char *context;
int chk_val;
int ref_val;
};
struct ComparePtrs
{
const char *context;
const void *chk_val;
const void *ref_val;
};
// Local Prototypes
// compare the run value of two states
static void chk_TestState_run_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the passed value of two states
static void chk_TestState_passed_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the failed value of two states
static void chk_TestState_failed_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the pending value of two states
static void chk_TestState_pending_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the first_log value of two states
static void chk_TestState_first_log_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the last_log value of two states
static void chk_TestState_last_log_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the ptr value of two states
static void chk_TestState_ptr_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the context value of two states
static void chk_TestState_context_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the full_context value of two states
static void chk_TestState_full_context_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the depth value of two states
static void chk_TestState_depth_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the report pointer of two states
static void chk_TestState_report_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
static void chk_TestState_eq_test(TestState *, void *, void *);
static TestResult chk_int_eq_test(TestState *, void *, void *);
static TestResult chk_ptr_eq_test(TestState *, void *, void *);
static TestResult chk_ptr_ne_test(TestState *, void *, void *);
static TestResult chk_str_eq_test(TestState *, void *, void *);
// discard report data
static void report(const char *);
decl_test(chk_int_eq_test); // ensure ints are equal
decl_test(chk_ptr_eq_test); // ensure pointers are equal
decl_test(chk_ptr_ne_test); // ensure pointers are not equal
decl_test(chk_str_eq_test); // ensure strings are equal
// Public Functions
void
@@ -170,23 +53,18 @@ mk_sample_state(TestState *s)
void
chk_TestState_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
chk_TestState_run_eq(s, prefix, context, actual, expected);
chk_TestState_passed_eq(s, prefix, context, actual, expected);
chk_TestState_failed_eq(s, prefix, context, actual, expected);
chk_TestState_pending_eq(s, prefix, context, actual, expected);
chk_TestState_first_log_eq(s, prefix, context, actual, expected);
chk_TestState_last_log_eq(s, prefix, context, actual, expected);
chk_TestState_ptr_eq(s, prefix, context, actual, expected);
chk_TestState_context_eq(s, prefix, context, actual, expected);
chk_TestState_full_context_eq(s, prefix, context, actual, expected);
chk_TestState_depth_eq(s, prefix, context, actual, expected);
chk_TestState_report_eq(s, prefix, context, actual, expected);
test_context_compare(
s,
context,
chk_TestState_eq_test,
actual,
expected
);
}
void
@@ -197,32 +75,30 @@ chk_int_eq(
int expected
)
{
void *old_ptr = s->ptr;
CompareInts ci;
ci.context = context;
ci.chk_val = actual;
ci.ref_val = expected;
s->ptr = &ci;
run_test(s, chk_int_eq_test);
s->ptr = old_ptr;
single_test_context_compare(
s,
context,
chk_int_eq_test,
&actual,
&expected
);
}
void
chk_ptr_eq(
TestState *s,
const char *context,
void *actual,
void *expected
const void *actual,
const void *expected
)
{
void *old_ptr = s->ptr;
ComparePtrs cp;
cp.context = context;
cp.chk_val = actual;
cp.ref_val = expected;
s->ptr = &cp;
run_test(s, chk_ptr_eq_test);
s->ptr = old_ptr;
single_test_context_compare(
s,
context,
chk_ptr_eq_test,
actual,
expected
);
}
void
@@ -233,14 +109,13 @@ chk_ptr_ne(
const void *prohibited
)
{
void *old_ptr = s->ptr;
ComparePtrs cp;
cp.context = context;
cp.chk_val = actual;
cp.ref_val = prohibited;
s->ptr = &cp;
run_test(s, chk_ptr_ne_test);
s->ptr = old_ptr;
single_test_context_compare(
s,
context,
chk_ptr_ne_test,
actual,
prohibited
);
}
void
@@ -251,242 +126,153 @@ chk_str_eq(
const char *expected
)
{
void *old_ptr = s->ptr;
ComparePtrs cp;
cp.context = context;
cp.chk_val = actual;
cp.ref_val = expected;
s->ptr = &cp;
run_test(s, chk_str_eq_test);
s->ptr = old_ptr;
single_test_context_compare(
s,
context,
chk_str_eq_test,
actual,
expected
);
}
// Local Functions
static void
chk_TestState_run_eq(
chk_TestState_eq_test(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
void *aptr,
void *eptr
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("run\n");
snprintf(full_context, STR_BUF_SIZE, "%s run:", context);
chk_int_eq(s, full_context, actual->run, expected->run);
if (!eptr)
{
chk_ptr_eq(s, "is null", aptr, 0);
return;
}
TestState
*actual = aptr,
*expected = eptr;
chk_int_eq(s, "run", actual->run, expected->run);
chk_int_eq(s, "passed", actual->passed, expected->passed);
chk_int_eq(s, "failed", actual->failed, expected->failed);
chk_int_eq(s, "pending", actual->pending, expected->pending);
chk_ptr_eq(s, "first_log", actual->first_log, expected->first_log);
chk_ptr_eq(s, "last_log", actual->last_log, expected->last_log);
chk_ptr_eq(s, "ptr", actual->ptr, expected->ptr);
chk_str_eq(s, "context", actual->context, expected->context);
chk_str_eq(s, "full_context", actual->full_context, expected->full_context);
chk_int_eq(s, "depth", actual->depth, expected->depth);
chk_ptr_eq(s, "report()", actual->report, expected->report);
}
static void
chk_TestState_passed_eq(
static TestResult
chk_int_eq_test(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
void *aptr,
void *eptr
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("passed\n");
snprintf(full_context, STR_BUF_SIZE, "%s passed:", context);
chk_int_eq(s, full_context, actual->passed, expected->passed);
}
int
*actual = aptr,
*expected = eptr;
static void
chk_TestState_failed_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("failed\n");
snprintf(full_context, STR_BUF_SIZE, "%s failed:", context);
chk_int_eq(s, full_context, actual->failed, expected->failed);
}
if (*actual == *expected)
return test_success;
static void
chk_TestState_pending_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("pending\n");
snprintf(full_context, STR_BUF_SIZE, "%s pending:", context);
chk_int_eq(s, full_context, actual->pending, expected->pending);
}
static void
chk_TestState_first_log_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("first_log\n");
snprintf(full_context, STR_BUF_SIZE, "%s first_log:", context);
chk_ptr_eq(s, full_context, actual->first_log, expected->first_log);
}
static void
chk_TestState_last_log_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("last_log\n");
snprintf(full_context, STR_BUF_SIZE, "%s last_log:", context);
chk_ptr_eq(s, full_context, actual->last_log, expected->last_log);
}
static void
chk_TestState_ptr_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("ptr\n");
snprintf(full_context, STR_BUF_SIZE, "%s ptr:", context);
chk_ptr_eq(s, full_context, actual->ptr, expected->ptr);
}
static void
chk_TestState_context_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("context\n");
snprintf(full_context, STR_BUF_SIZE, "%s context:", context);
chk_str_eq(s, full_context, actual->context, expected->context);
}
static void
chk_TestState_full_context_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("full_context\n");
snprintf(full_context, STR_BUF_SIZE, "%s full_context:", context);
chk_str_eq(s, full_context, actual->full_context, expected->full_context);
}
static void
chk_TestState_depth_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("depth\n");
snprintf(full_context, STR_BUF_SIZE, "%s depth:", context);
chk_int_eq(s, full_context, actual->depth, expected->depth);
}
static void
chk_TestState_report_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("report()\n");
snprintf(full_context, STR_BUF_SIZE, "%s report():", context);
chk_ptr_eq(s, full_context, actual->report, expected->report);
}
def_test(chk_int_eq_test, s)
{
const CompareInts *ci = s->ptr;
if (ci->chk_val == ci->ref_val) return test_success;
// log the error
char str[STR_BUF_SIZE];
append_test_log(s, ci->context);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->ref_val);
log_test_context(s);
append_test_log(s, "ERROR:");
snprintf(str, STR_BUF_SIZE, "\texpected: %d", *expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->chk_val);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", *actual);
append_test_log(s, str);
return test_failure;
}
def_test(chk_ptr_eq_test, s)
static TestResult
chk_ptr_eq_test(
TestState *s,
void *actual,
void *expected
)
{
const ComparePtrs *cp = s->ptr;
if (cp->chk_val == cp->ref_val) return test_success;
if (actual == expected)
return test_success;
// log the error
char str[STR_BUF_SIZE];
append_test_log(s, cp->context);
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", cp->ref_val);
log_test_context(s);
append_test_log(s, "ERROR:");
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", cp->chk_val);
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", actual);
append_test_log(s, str);
return test_failure;
}
def_test(chk_ptr_ne_test, s)
static TestResult
chk_ptr_ne_test(
TestState *s,
void *actual,
void *prohibited
)
{
const ComparePtrs *cp = s->ptr;
if (cp->chk_val != cp->ref_val) return test_success;
if (actual != prohibited)
return test_success;
// log the error
char str[STR_BUF_SIZE];
append_test_log(s, cp->context);
snprintf(str, STR_BUF_SIZE, "\tcannot be: 0x%x", cp->ref_val);
log_test_context(s);
snprintf(
str,
STR_BUF_SIZE,
"ERROR: prohibited value: 0x%x",
actual
);
append_test_log(s, str);
return test_failure;
}
def_test(chk_str_eq_test, s)
static TestResult
chk_str_eq_test(
TestState *s,
void *aptr,
void *eptr
)
{
const ComparePtrs *cp = s->ptr;
if (!cp->chk_val && !cp->ref_val) return test_success;
if (!strcmp(cp->chk_val, cp->ref_val)) return test_success;
if (!eptr)
{
if (!aptr) return test_success;
char str[STR_BUF_SIZE];
log_test_context(s);
append_test_log(s, "ERROR:");
append_test_log(s, "\texpected: 0x0");
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", aptr);
append_test_log(s, str);
return test_failure;
}
const char
*actual = aptr,
*expected = eptr;
if (!strcmp(actual, expected)) return;
// log the error
char str[STR_BUF_SIZE];
append_test_log(s, cp->context);
snprintf(str, STR_BUF_SIZE, "\texpected: %s", cp->ref_val);
log_test_context(s);
append_test_log(s, "ERROR:");
snprintf(str, STR_BUF_SIZE, "\texpected: %s", expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %s", cp->ref_val);
snprintf(str, STR_BUF_SIZE, "\tactual: %s", actual);
append_test_log(s, str);
return test_failure;
}
+14 -18
View File
@@ -19,10 +19,7 @@
*/
#define STR_BUF_SIZE 256 // buffer size for constructing arbitrary strings
#define decl_test(n) static TestResult n(TestState *)
#define def_test(n, s) static TestResult n(TestState *s)
#define STR_BUF_SIZE 256 // maximum string buffer size
// initializes a sample TestState value
void mk_sample_state(TestState *);
@@ -30,40 +27,39 @@ void mk_sample_state(TestState *);
// ensures two TestState values are equal
extern void chk_TestState_eq(
TestState *, // the state we are *actually* updating ;)
const char *, // prefix for each status line
const char *, // context for errors
const char *, // the context
const TestState *, // actual state
const TestState * // expected state
);
// ensure two integers are equal
extern void chk_int_eq(
TestState *,
const char *, // the error context
TestState *, // the test state
const char *, // the context
int, // the actual value
int // the expected value
);
// ensure two pointers are equal
extern void chk_ptr_eq(
TestState *,
const char *, // the error context
const void *, // the actual value
const void * // the expected value
TestState *, // the test state
const char *, // the context
void *, // the actual value
void * // the expected value
);
// ensure two pointers are not equal
extern void chk_ptr_ne(
TestState *,
const char *, // the error context
const void *, // the actual value
const void * // the prohibited value
TestState *, // the test state
const char *, // the context
void *, // the actual value
void * // the prohibited value
);
// ensure two strings are equal
extern void chk_str_eq(
TestState *,
const char *, // the error context
TestState *, // the test state
const char *, // the context
const char *, // the actual value
const char * // the expected value
);