Skip to content

Commit e93b6c3

Browse files
committed
Reorganize test code a bit
A few guiding principles: * Each test source file includes ONLY "test.h" to make it easy to create new tests. * Each test suite has a "test.h" that includes "test_util/test_common.h" to get access to all the common testing utility functions. So "test_common.h" is then responsible for including any smaller headers that declare specific pieces of shared test functionality. I've also pulled some test filtering logic that was _only_ used in test_main.c into that file, and repurposed "test_utils.[ch]" for common utility code. (Eventually, a lot of the assertion helpers currently in "test_main.c" should probably be organized into one or more source files of their own.)
1 parent 3665c75 commit e93b6c3

11 files changed

Lines changed: 105 additions & 112 deletions

libarchive/test/test_read_data_large.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525
#include "test.h"
26-
#include "test_utils.h"
2726
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_data_large.c 201247 2009-12-30 05:59:21Z kientzle $");
2827

2928
/*

libarchive/test/test_read_extract.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525
#include "test.h"
26-
#include "test_utils.h"
2726
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_extract.c 201247 2009-12-30 05:59:21Z kientzle $");
2827

2928
#define BUFF_SIZE 1000000

libarchive/test/test_read_large.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525
#include "test.h"
26-
#include "test_utils.h"
2726
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_large.c 201247 2009-12-30 05:59:21Z kientzle $");
2827

2928
static unsigned char testdata[10 * 1024 * 1024];

libarchive/test/test_read_pax_truncated.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525
#include "test.h"
26-
#include "test_utils.h"
2726
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_pax_truncated.c 189483 2009-03-07 03:34:34Z kientzle $");
2827

2928
DEFINE_TEST(test_read_pax_truncated)

libarchive/test/test_read_truncated.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525
#include "test.h"
26-
#include "test_utils.h"
2726
__FBSDID("$FreeBSD: src/lib/libarchive/test/test_read_truncated.c,v 1.4 2008/09/01 05:38:33 kientzle Exp $");
2827

2928
static char buff[1000000];

libarchive/test/test_read_truncated_filter.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626

2727
#include "test.h"
28-
#include "test_utils.h"
2928
__FBSDID("$FreeBSD$");
3029

3130
/*

libarchive/test/test_write_format_7zip_large.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626

2727
#include "test.h"
28-
#include "test_utils.h"
2928
__FBSDID("$FreeBSD$");
3029

3130
#define LARGE_SIZE (16*1024*1024)

test_utils/test_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,4 +471,6 @@ void assertVersion(const char *prog, const char *base);
471471
#include <dmalloc.h>
472472
#endif
473473

474+
#include "test_utils.h"
475+
474476
#endif /* TEST_COMMON_H */

test_utils/test_main.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3462,6 +3462,12 @@ assertion_entry_compare_acls(const char *file, int line,
34623462
* DEFINE_TEST(test_function)
34633463
* for each test.
34643464
*/
3465+
struct test_list_t
3466+
{
3467+
void (*func)(void);
3468+
const char *name;
3469+
int failures;
3470+
};
34653471

34663472
/* Use "list.h" to declare all of the test functions. */
34673473
#undef DEFINE_TEST
@@ -3753,6 +3759,100 @@ get_refdir(const char *d)
37533759
return p;
37543760
}
37553761

3762+
/* Filter tests against a glob pattern. Returns non-zero if test matches
3763+
* pattern, zero otherwise. A '^' at the beginning of the pattern negates
3764+
* the return values (i.e. returns zero for a match, non-zero otherwise.
3765+
*/
3766+
static int
3767+
test_filter(const char *pattern, const char *test)
3768+
{
3769+
int retval = 0;
3770+
int negate = 0;
3771+
const char *p = pattern;
3772+
const char *t = test;
3773+
3774+
if (p[0] == '^')
3775+
{
3776+
negate = 1;
3777+
p++;
3778+
}
3779+
3780+
while (1)
3781+
{
3782+
if (p[0] == '\\')
3783+
p++;
3784+
else if (p[0] == '*')
3785+
{
3786+
while (p[0] == '*')
3787+
p++;
3788+
if (p[0] == '\\')
3789+
p++;
3790+
if ((t = strchr(t, p[0])) == 0)
3791+
break;
3792+
}
3793+
if (p[0] != t[0])
3794+
break;
3795+
if (p[0] == '\0') {
3796+
retval = 1;
3797+
break;
3798+
}
3799+
p++;
3800+
t++;
3801+
}
3802+
3803+
return (negate) ? !retval : retval;
3804+
}
3805+
3806+
static int
3807+
get_test_set(int *test_set, int limit, const char *test)
3808+
{
3809+
int start, end;
3810+
int idx = 0;
3811+
3812+
if (test == NULL) {
3813+
/* Default: Run all tests. */
3814+
for (;idx < limit; idx++)
3815+
test_set[idx] = idx;
3816+
return (limit);
3817+
}
3818+
if (*test >= '0' && *test <= '9') {
3819+
const char *vp = test;
3820+
start = 0;
3821+
while (*vp >= '0' && *vp <= '9') {
3822+
start *= 10;
3823+
start += *vp - '0';
3824+
++vp;
3825+
}
3826+
if (*vp == '\0') {
3827+
end = start;
3828+
} else if (*vp == '-') {
3829+
++vp;
3830+
if (*vp == '\0') {
3831+
end = limit - 1;
3832+
} else {
3833+
end = 0;
3834+
while (*vp >= '0' && *vp <= '9') {
3835+
end *= 10;
3836+
end += *vp - '0';
3837+
++vp;
3838+
}
3839+
}
3840+
} else
3841+
return (-1);
3842+
if (start < 0 || end >= limit || start > end)
3843+
return (-1);
3844+
while (start <= end)
3845+
test_set[idx++] = start++;
3846+
} else {
3847+
for (start = 0; start < limit; ++start) {
3848+
const char *name = tests[start].name;
3849+
if (test_filter(test, name))
3850+
test_set[idx++] = start;
3851+
}
3852+
}
3853+
return ((idx == 0)?-1:idx);
3854+
}
3855+
37563856
int
37573857
main(int argc, char **argv)
37583858
{
@@ -4049,7 +4149,7 @@ main(int argc, char **argv)
40494149
do {
40504150
int test_num;
40514151

4052-
test_num = get_test_set(test_set, limit, *argv, tests);
4152+
test_num = get_test_set(test_set, limit, *argv);
40534153
if (test_num < 0) {
40544154
printf("*** INVALID Test %s\n", *argv);
40554155
free(refdir_alloc);

test_utils/test_utils.c

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -32,100 +32,6 @@
3232
#include <string.h>
3333
#include <assert.h>
3434

35-
/* Filter tests against a glob pattern. Returns non-zero if test matches
36-
* pattern, zero otherwise. A '^' at the beginning of the pattern negates
37-
* the return values (i.e. returns zero for a match, non-zero otherwise.
38-
*/
39-
static int
40-
test_filter(const char *pattern, const char *test)
41-
{
42-
int retval = 0;
43-
int negate = 0;
44-
const char *p = pattern;
45-
const char *t = test;
46-
47-
if (p[0] == '^')
48-
{
49-
negate = 1;
50-
p++;
51-
}
52-
53-
while (1)
54-
{
55-
if (p[0] == '\\')
56-
p++;
57-
else if (p[0] == '*')
58-
{
59-
while (p[0] == '*')
60-
p++;
61-
if (p[0] == '\\')
62-
p++;
63-
if ((t = strchr(t, p[0])) == 0)
64-
break;
65-
}
66-
if (p[0] != t[0])
67-
break;
68-
if (p[0] == '\0') {
69-
retval = 1;
70-
break;
71-
}
72-
p++;
73-
t++;
74-
}
75-
76-
return (negate) ? !retval : retval;
77-
}
78-
79-
int get_test_set(int *test_set, int limit, const char *test,
80-
struct test_list_t *tests)
81-
{
82-
int start, end;
83-
int idx = 0;
84-
85-
if (test == NULL) {
86-
/* Default: Run all tests. */
87-
for (;idx < limit; idx++)
88-
test_set[idx] = idx;
89-
return (limit);
90-
}
91-
if (*test >= '0' && *test <= '9') {
92-
const char *vp = test;
93-
start = 0;
94-
while (*vp >= '0' && *vp <= '9') {
95-
start *= 10;
96-
start += *vp - '0';
97-
++vp;
98-
}
99-
if (*vp == '\0') {
100-
end = start;
101-
} else if (*vp == '-') {
102-
++vp;
103-
if (*vp == '\0') {
104-
end = limit - 1;
105-
} else {
106-
end = 0;
107-
while (*vp >= '0' && *vp <= '9') {
108-
end *= 10;
109-
end += *vp - '0';
110-
++vp;
111-
}
112-
}
113-
} else
114-
return (-1);
115-
if (start < 0 || end >= limit || start > end)
116-
return (-1);
117-
while (start <= end)
118-
test_set[idx++] = start++;
119-
} else {
120-
for (start = 0; start < limit; ++start) {
121-
const char *name = tests[start].name;
122-
if (test_filter(test, name))
123-
test_set[idx++] = start;
124-
}
125-
}
126-
return ((idx == 0)?-1:idx);
127-
}
128-
12935
static inline uint64_t
13036
xorshift64(uint64_t *state)
13137
{
@@ -146,7 +52,7 @@ xorshift64(uint64_t *state)
14652
* took ~22 seconds, whereas using a xorshift random number generator (that can
14753
* be inlined) reduces it to ~17 seconds on QEMU RISC-V.
14854
*/
149-
void
55+
static void
15056
fill_with_pseudorandom_data_seed(uint64_t seed, void *buffer, size_t size)
15157
{
15258
uint64_t *aligned_buffer;

0 commit comments

Comments
 (0)