vcs-svn: make test-line-buffer input format more flexible
Imitate the input format of test-obj-pool to support arbitrary sequences of commands rather than alternating read/copy. This should make it easier to add tests that exercise other line_buffer functions. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Jonathan Nieder committed
Jan 2, 2011 at 18:50 UTC
850c5ea44ce0b4aac3be7c4d14b38ec901e777d1
2 files changed
+45
-29
t/t0080-vcs-svn.sh
+9
-9
@@ -85,40 +85,40 @@ test_expect_success 'line buffer' '
85
printf "%s\n" "" foo >expected6 &&
86
87
test-line-buffer <<-\EOF >actual1 &&
88
- 5
88
+ read 5
89
HELLO
90
EOF
91
92
test-line-buffer <<-\EOF >actual2 &&
93
- 0
93
+ read 0
94
95
- 5
95
+ copy 5
96
HELLO
97
EOF
98
99
q_to_nul <<-\EOF |
100
- 1
100
+ read 1
101
Q
102
EOF
103
test-line-buffer >actual3 &&
104
105
q_to_nul <<-\EOF |
106
- 0
106
+ read 0
107
108
- 1
108
+ copy 1
109
Q
110
EOF
111
test-line-buffer >actual4 &&
112
113
test-line-buffer <<-\EOF >actual5 &&
114
- 5
114
+ read 5
115
foo
116
EOF
117
118
test-line-buffer <<-\EOF >actual6 &&
119
- 0
119
+ read 0
120
121
- 5
121
+ copy 5
122
foo
123
EOF
124
test-line-buffer.c
+36
-20
@@ -1,11 +1,5 @@
1
/*
2
* test-line-buffer.c: code to exercise the svn importer's input helper
3
- *
4
- * Input format:
5
- * number NL
6
- * (number bytes) NL
7
- * number NL
8
- * ...
3
*/
4
5
#include "git-compat-util.h"
@@ -20,28 +14,50 @@ static uint32_t strtouint32(const char *s)
14
return (uint32_t) n;
15
}
16
17
+static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
18
+{
19
+ switch (*command) {
20
+ case 'c':
21
+ if (!prefixcmp(command, "copy ")) {
22
+ buffer_copy_bytes(buf, strtouint32(arg) + 1);
23
+ return;
24
+ }
25
+ case 'r':
26
+ if (!prefixcmp(command, "read ")) {
27
+ const char *s = buffer_read_string(buf, strtouint32(arg));
28
+ printf("%s\n", s);
29
+ buffer_skip_bytes(buf, 1); /* consume newline */
30
+ return;
31
+ }
32
+ default:
33
+ die("unrecognized command: %s", command);
34
+ }
35
+}
36
+
37
+static void handle_line(const char *line, struct line_buffer *stdin_buf)
38
+{
39
+ const char *arg = strchr(line, ' ');
40
+ if (!arg)
41
+ die("no argument in line: %s", line);
42
+ handle_command(line, arg + 1, stdin_buf);
43
+}
44
+
45
int main(int argc, char *argv[])
46
{
25
- struct line_buffer buf = LINE_BUFFER_INIT;
47
+ struct line_buffer stdin_buf = LINE_BUFFER_INIT;
48
char *s;
49
50
if (argc != 1)
29
- usage("test-line-buffer < input.txt");
30
- if (buffer_init(&buf, NULL))
51
+ usage("test-line-buffer < script");
52
+
53
+ if (buffer_init(&stdin_buf, NULL))
54
die_errno("open error");
32
- while ((s = buffer_read_line(&buf))) {
33
- s = buffer_read_string(&buf, strtouint32(s));
34
- fputs(s, stdout);
35
- fputc('\n', stdout);
36
- buffer_skip_bytes(&buf, 1);
37
- if (!(s = buffer_read_line(&buf)))
38
- break;
39
- buffer_copy_bytes(&buf, strtouint32(s) + 1);
40
- }
41
- if (buffer_deinit(&buf))
55
+ while ((s = buffer_read_line(&stdin_buf)))
56
+ handle_line(s, &stdin_buf);
57
+ if (buffer_deinit(&stdin_buf))
58
die("input error");
59
if (ferror(stdout))
60
die("output error");
45
- buffer_reset(&buf);
61
+ buffer_reset(&stdin_buf);
62
return 0;
63
}