Skip to content

Commit 0cecb75

Browse files
rscharfegitster
authored andcommitted
test-mergesort: add generate subcommand
Add a subcommand for printing test data. It can be used to generate special test cases and feed them into the sort subcommand or sort(1) for performance measurements. It may also be useful to illustrate the effect of distributions, modes and their parameters. It generates n integers with the specified distribution and its distribution-specific parameter m. E.g. m is the maximum value for the plateau distribution and the length and height of individual teeth of the sawtooth distribution. The generated values are printed as zero-padded eight-digit hexadecimal numbers to make sure alphabetic and numeric order are the same. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e031e97 commit 0cecb75

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

t/helper/test-mergesort.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ static struct dist {
9898
DIST(shuffle),
9999
};
100100

101+
static const struct dist *get_dist_by_name(const char *name)
102+
{
103+
int i;
104+
for (i = 0; i < ARRAY_SIZE(dist); i++) {
105+
if (!strcmp(dist[i].name, name))
106+
return &dist[i];
107+
}
108+
return NULL;
109+
}
110+
101111
static void mode_copy(int *arr, int n)
102112
{
103113
/* nothing */
@@ -154,6 +164,41 @@ static struct mode {
154164
MODE(dither),
155165
};
156166

167+
static const struct mode *get_mode_by_name(const char *name)
168+
{
169+
int i;
170+
for (i = 0; i < ARRAY_SIZE(mode); i++) {
171+
if (!strcmp(mode[i].name, name))
172+
return &mode[i];
173+
}
174+
return NULL;
175+
}
176+
177+
static int generate(int argc, const char **argv)
178+
{
179+
const struct dist *dist = NULL;
180+
const struct mode *mode = NULL;
181+
int i, n, m, *arr;
182+
183+
if (argc != 4)
184+
return 1;
185+
186+
dist = get_dist_by_name(argv[0]);
187+
mode = get_mode_by_name(argv[1]);
188+
n = strtol(argv[2], NULL, 10);
189+
m = strtol(argv[3], NULL, 10);
190+
if (!dist || !mode)
191+
return 1;
192+
193+
ALLOC_ARRAY(arr, n);
194+
dist->fn(arr, n, m);
195+
mode->fn(arr, n);
196+
for (i = 0; i < n; i++)
197+
printf("%08x\n", arr[i]);
198+
free(arr);
199+
return 0;
200+
}
201+
157202
static struct stats {
158203
int get_next, set_next, compare;
159204
} stats;
@@ -278,11 +323,24 @@ static int run_tests(int argc, const char **argv)
278323

279324
int cmd__mergesort(int argc, const char **argv)
280325
{
326+
int i;
327+
const char *sep;
328+
329+
if (argc == 6 && !strcmp(argv[1], "generate"))
330+
return generate(argc - 2, argv + 2);
281331
if (argc == 2 && !strcmp(argv[1], "sort"))
282332
return sort_stdin();
283333
if (argc > 1 && !strcmp(argv[1], "test"))
284334
return run_tests(argc - 2, argv + 2);
285-
fprintf(stderr, "usage: test-tool mergesort sort\n");
335+
fprintf(stderr, "usage: test-tool mergesort generate <distribution> <mode> <n> <m>\n");
336+
fprintf(stderr, " or: test-tool mergesort sort\n");
286337
fprintf(stderr, " or: test-tool mergesort test [<n>...]\n");
338+
fprintf(stderr, "\n");
339+
for (i = 0, sep = "distributions: "; i < ARRAY_SIZE(dist); i++, sep = ", ")
340+
fprintf(stderr, "%s%s", sep, dist[i].name);
341+
fprintf(stderr, "\n");
342+
for (i = 0, sep = "modes: "; i < ARRAY_SIZE(mode); i++, sep = ", ")
343+
fprintf(stderr, "%s%s", sep, mode[i].name);
344+
fprintf(stderr, "\n");
287345
return 129;
288346
}

0 commit comments

Comments
 (0)