convert: use skip_iprefix() in validate_encoding()
[git/raj.git] / t / helper / test-progress.c
blob42b96cb103c6acee9ad2a9dfad8afbaa7ef854e9
1 /*
2 * A test helper to exercise the progress display.
4 * Reads instructions from standard input, one instruction per line:
6 * "progress <items>" - Call display_progress() with the given item count
7 * as parameter.
8 * "throughput <bytes> <millis> - Call display_throughput() with the given
9 * byte count as parameter. The 'millis'
10 * specify the time elapsed since the
11 * start_progress() call.
12 * "update" - Set the 'progress_update' flag.
14 * See 't0500-progress-display.sh' for examples.
16 #include "test-tool.h"
17 #include "gettext.h"
18 #include "parse-options.h"
19 #include "progress.h"
20 #include "strbuf.h"
23 * These are defined in 'progress.c', but are not exposed in 'progress.h',
24 * because they are exclusively for testing.
26 extern int progress_testing;
27 extern uint64_t progress_test_ns;
28 void progress_test_force_update(void);
30 int cmd__progress(int argc, const char **argv)
32 int total = 0;
33 const char *title;
34 struct strbuf line = STRBUF_INIT;
35 struct progress *progress;
37 const char *usage[] = {
38 "test-tool progress [--total=<n>] <progress-title>",
39 NULL
41 struct option options[] = {
42 OPT_INTEGER(0, "total", &total, "total number of items"),
43 OPT_END(),
46 argc = parse_options(argc, argv, NULL, options, usage, 0);
47 if (argc != 1)
48 die("need a title for the progress output");
49 title = argv[0];
51 progress_testing = 1;
52 progress = start_progress(title, total);
53 while (strbuf_getline(&line, stdin) != EOF) {
54 char *end;
56 if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
57 uint64_t item_count = strtoull(end, &end, 10);
58 if (*end != '\0')
59 die("invalid input: '%s'\n", line.buf);
60 display_progress(progress, item_count);
61 } else if (skip_prefix(line.buf, "throughput ",
62 (const char **) &end)) {
63 uint64_t byte_count, test_ms;
65 byte_count = strtoull(end, &end, 10);
66 if (*end != ' ')
67 die("invalid input: '%s'\n", line.buf);
68 test_ms = strtoull(end + 1, &end, 10);
69 if (*end != '\0')
70 die("invalid input: '%s'\n", line.buf);
71 progress_test_ns = test_ms * 1000 * 1000;
72 display_throughput(progress, byte_count);
73 } else if (!strcmp(line.buf, "update"))
74 progress_test_force_update();
75 else
76 die("invalid input: '%s'\n", line.buf);
78 stop_progress(&progress);
80 return 0;